Completed
Push — master ( ec6233...9f2fe8 )
by cam
01:28
created
ecrire/req/sqlite_fonctions.php 2 patches
Indentation   +229 added lines, -229 removed lines patch added patch discarded remove patch
@@ -22,7 +22,7 @@  discard block
 block discarded – undo
22 22
  */
23 23
 
24 24
 if (!defined('_ECRIRE_INC_VERSION')) {
25
-	return;
25
+    return;
26 26
 }
27 27
 
28 28
 /**
@@ -43,90 +43,90 @@  discard block
 block discarded – undo
43 43
  */
44 44
 function _sqlite_init_functions(&$sqlite) {
45 45
 
46
-	if (!$sqlite) {
47
-		return false;
48
-	}
46
+    if (!$sqlite) {
47
+        return false;
48
+    }
49 49
 
50 50
 
51
-	$fonctions = [
52
-		// A
53
-		'ACOS'  => ['acos', 1],
54
-		'ASIN'  => ['asin', 1],
55
-		'ATAN'  => ['atan', 1], // mysql accepte 2 params comme atan2… hum ?
56
-		'ATAN2' => ['atan2', 2],
51
+    $fonctions = [
52
+        // A
53
+        'ACOS'  => ['acos', 1],
54
+        'ASIN'  => ['asin', 1],
55
+        'ATAN'  => ['atan', 1], // mysql accepte 2 params comme atan2… hum ?
56
+        'ATAN2' => ['atan2', 2],
57 57
 
58
-		// C
59
-		'CEIL'   => ['_sqlite_func_ceil', 1],
60
-		'CONCAT' => ['_sqlite_func_concat', -1],
61
-		'COS'    => ['cos', 1],
58
+        // C
59
+        'CEIL'   => ['_sqlite_func_ceil', 1],
60
+        'CONCAT' => ['_sqlite_func_concat', -1],
61
+        'COS'    => ['cos', 1],
62 62
 
63
-		// D
64
-		'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés
65
-		'DAYOFMONTH'  => ['_sqlite_func_dayofmonth', 1],
66
-		'DEGREES'     => ['rad2deg', 1],
63
+        // D
64
+        'DATE_FORMAT' => ['_sqlite_func_date_format', 2], // équivalent a strftime avec args inversés
65
+        'DAYOFMONTH'  => ['_sqlite_func_dayofmonth', 1],
66
+        'DEGREES'     => ['rad2deg', 1],
67 67
 
68
-		// E
69
-		'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi()
70
-		'EXP'            => ['exp', 1],
68
+        // E
69
+        'EXTRAIRE_MULTI' => ['_sqlite_func_extraire_multi', 2], // specifique a SPIP/sql_multi()
70
+        'EXP'            => ['exp', 1],
71 71
 
72
-		// F
73
-		'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2],
74
-		'FLOOR'       => ['_sqlite_func_floor', 1],
72
+        // F
73
+        'FIND_IN_SET' => ['_sqlite_func_find_in_set', 2],
74
+        'FLOOR'       => ['_sqlite_func_floor', 1],
75 75
 
76
-		// G
77
-		'GREATEST' => ['_sqlite_func_greatest', -1],
76
+        // G
77
+        'GREATEST' => ['_sqlite_func_greatest', -1],
78 78
 
79
-		// I
80
-		'IF'     => ['_sqlite_func_if', 3],
81
-		'INSERT' => ['_sqlite_func_insert', 4],
82
-		'INSTR'  => ['_sqlite_func_instr', 2],
79
+        // I
80
+        'IF'     => ['_sqlite_func_if', 3],
81
+        'INSERT' => ['_sqlite_func_insert', 4],
82
+        'INSTR'  => ['_sqlite_func_instr', 2],
83 83
 
84
-		// L
85
-		'LEAST'  => ['_sqlite_func_least', -1],
86
-		'_LEFT'  => ['_sqlite_func_left', 2],
84
+        // L
85
+        'LEAST'  => ['_sqlite_func_least', -1],
86
+        '_LEFT'  => ['_sqlite_func_left', 2],
87 87
 
88
-		// N
89
-		'NOW' => ['_sqlite_func_now', 0],
88
+        // N
89
+        'NOW' => ['_sqlite_func_now', 0],
90 90
 
91
-		// M
92
-		'MD5'   => ['md5', 1],
93
-		'MONTH' => ['_sqlite_func_month', 1],
91
+        // M
92
+        'MD5'   => ['md5', 1],
93
+        'MONTH' => ['_sqlite_func_month', 1],
94 94
 
95
-		// P
96
-		'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3],
95
+        // P
96
+        'PREG_REPLACE' => ['_sqlite_func_preg_replace', 3],
97 97
 
98
-		// R
99
-		'RADIANS' => ['deg2rad', 1],
100
-		'RAND'    => ['_sqlite_func_rand', 0], // sinon random() v2.4
101
-		'REGEXP'  => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2
102
-		'RIGHT'   => ['_sqlite_func_right', 2],
98
+        // R
99
+        'RADIANS' => ['deg2rad', 1],
100
+        'RAND'    => ['_sqlite_func_rand', 0], // sinon random() v2.4
101
+        'REGEXP'  => ['_sqlite_func_regexp_match', 2], // critere REGEXP supporte a partir de v3.3.2
102
+        'RIGHT'   => ['_sqlite_func_right', 2],
103 103
 
104
-		// S
105
-		'SETTYPE'   => ['settype', 2], // CAST present en v3.2.3
106
-		'SIN'       => ['sin', 1],
107
-		'SQRT'      => ['sqrt', 1],
108
-		'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0
104
+        // S
105
+        'SETTYPE'   => ['settype', 2], // CAST present en v3.2.3
106
+        'SIN'       => ['sin', 1],
107
+        'SQRT'      => ['sqrt', 1],
108
+        'SUBSTRING' => ['_sqlite_func_substring' /*, 3*/], // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0
109 109
 
110
-		// T
111
-		'TAN'           => ['tan', 1],
112
-		'TIMESTAMPDIFF' => ['_sqlite_timestampdiff'    /*, 3*/],
113
-		'TO_DAYS'       => ['_sqlite_func_to_days', 1],
110
+        // T
111
+        'TAN'           => ['tan', 1],
112
+        'TIMESTAMPDIFF' => ['_sqlite_timestampdiff'    /*, 3*/],
113
+        'TO_DAYS'       => ['_sqlite_func_to_days', 1],
114 114
 
115
-		// U
116
-		'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1],
115
+        // U
116
+        'UNIX_TIMESTAMP' => ['_sqlite_func_unix_timestamp', 1],
117 117
 
118
-		// V
119
-		'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
118
+        // V
119
+        'VIDE' => ['_sqlite_func_vide', 0], // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
120 120
 
121
-		// Y
122
-		'YEAR' => ['_sqlite_func_year', 1]
123
-	];
121
+        // Y
122
+        'YEAR' => ['_sqlite_func_year', 1]
123
+    ];
124 124
 
125
-	foreach ($fonctions as $f => $r) {
126
-		_sqlite_add_function($sqlite, $f, $r);
127
-	}
125
+    foreach ($fonctions as $f => $r) {
126
+        _sqlite_add_function($sqlite, $f, $r);
127
+    }
128 128
 
129
-	#spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG);
129
+    #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG);
130 130
 }
131 131
 
132 132
 
@@ -147,9 +147,9 @@  discard block
 block discarded – undo
147 147
  *
148 148
 **/
149 149
 function _sqlite_add_function(&$sqlite, &$f, &$r) {
150
-	isset($r[1])
151
-		? $sqlite->sqliteCreateFunction($f, $r[0], $r[1])
152
-		: $sqlite->sqliteCreateFunction($f, $r[0]);
150
+    isset($r[1])
151
+        ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1])
152
+        : $sqlite->sqliteCreateFunction($f, $r[0]);
153 153
 }
154 154
 
155 155
 /**
@@ -159,7 +159,7 @@  discard block
 block discarded – undo
159 159
  * @return int
160 160
  */
161 161
 function _sqlite_func_ceil($a) {
162
-	return ceil($a);
162
+    return ceil($a);
163 163
 }
164 164
 
165 165
 /**
@@ -169,7 +169,7 @@  discard block
 block discarded – undo
169 169
  * @return string
170 170
  */
171 171
 function _sqlite_func_concat(...$args) {
172
-	return join('', $args);
172
+    return join('', $args);
173 173
 }
174 174
 
175 175
 
@@ -182,7 +182,7 @@  discard block
 block discarded – undo
182 182
  * @return int
183 183
  */
184 184
 function _sqlite_func_dayofmonth($d) {
185
-	return _sqlite_func_date('d', $d);
185
+    return _sqlite_func_date('d', $d);
186 186
 }
187 187
 
188 188
 
@@ -194,15 +194,15 @@  discard block
 block discarded – undo
194 194
  * @return int
195 195
  */
196 196
 function _sqlite_func_find_in_set($num, $set) {
197
-	$rank = 0;
198
-	foreach (explode(',', $set) as $v) {
199
-		if ($v == $num) {
200
-			return (++$rank);
201
-		}
202
-		$rank++;
203
-	}
204
-
205
-	return 0;
197
+    $rank = 0;
198
+    foreach (explode(',', $set) as $v) {
199
+        if ($v == $num) {
200
+            return (++$rank);
201
+        }
202
+        $rank++;
203
+    }
204
+
205
+    return 0;
206 206
 }
207 207
 
208 208
 /**
@@ -212,7 +212,7 @@  discard block
 block discarded – undo
212 212
  * @return int
213 213
  */
214 214
 function _sqlite_func_floor($a) {
215
-	return floor($a);
215
+    return floor($a);
216 216
 }
217 217
 
218 218
 
@@ -225,7 +225,7 @@  discard block
 block discarded – undo
225 225
  * @return mixed
226 226
  */
227 227
 function _sqlite_func_if($bool, $oui, $non) {
228
-	return ($bool) ? $oui : $non;
228
+    return ($bool) ? $oui : $non;
229 229
 }
230 230
 
231 231
 
@@ -242,10 +242,10 @@  discard block
 block discarded – undo
242 242
  * @return string
243 243
  */
244 244
 function _sqlite_func_insert($s, $index, $longueur, $chaine) {
245
-	return
246
-		substr($s, 0, $index)
247
-		. $chaine
248
-		. substr(substr($s, $index), $longueur);
245
+    return
246
+        substr($s, 0, $index)
247
+        . $chaine
248
+        . substr(substr($s, $index), $longueur);
249 249
 }
250 250
 
251 251
 
@@ -257,7 +257,7 @@  discard block
 block discarded – undo
257 257
  * @return int
258 258
  */
259 259
 function _sqlite_func_instr($s, $search) {
260
-	return strpos($s, $search);
260
+    return strpos($s, $search);
261 261
 }
262 262
 
263 263
 
@@ -268,7 +268,7 @@  discard block
 block discarded – undo
268 268
  * @return int
269 269
  */
270 270
 function _sqlite_func_least(...$args) {
271
-	return min($args);
271
+    return min($args);
272 272
 }
273 273
 
274 274
 
@@ -279,7 +279,7 @@  discard block
 block discarded – undo
279 279
  * @return int
280 280
  */
281 281
 function _sqlite_func_greatest(...$args) {
282
-	return max($args);
282
+    return max($args);
283 283
 }
284 284
 
285 285
 
@@ -291,7 +291,7 @@  discard block
 block discarded – undo
291 291
  * @return string
292 292
  */
293 293
 function _sqlite_func_left($s, $lenght) {
294
-	return substr($s, $lenght);
294
+    return substr($s, $lenght);
295 295
 }
296 296
 
297 297
 /**
@@ -301,13 +301,13 @@  discard block
 block discarded – undo
301 301
  * @return string
302 302
  */
303 303
 function _sqlite_func_now($force_refresh = false) {
304
-	static $now = null;
305
-	if (is_null($now) or $force_refresh) {
306
-		$now = date('Y-m-d H:i:s');
307
-	}
304
+    static $now = null;
305
+    if (is_null($now) or $force_refresh) {
306
+        $now = date('Y-m-d H:i:s');
307
+    }
308 308
 
309
-	#spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG);
310
-	return $now;
309
+    #spip_log("Passage avec NOW : $now | ".time(),'sqlite.'._LOG_DEBUG);
310
+    return $now;
311 311
 }
312 312
 
313 313
 
@@ -320,7 +320,7 @@  discard block
 block discarded – undo
320 320
  * @return int
321 321
  */
322 322
 function _sqlite_func_month($d) {
323
-	return _sqlite_func_date('m', $d);
323
+    return _sqlite_func_date('m', $d);
324 324
 }
325 325
 
326 326
 
@@ -333,10 +333,10 @@  discard block
 block discarded – undo
333 333
  * @return string
334 334
  */
335 335
 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
336
-	$return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
336
+    $return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
337 337
 
338
-	#spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
339
-	return $return;
338
+    #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
339
+    return $return;
340 340
 }
341 341
 
342 342
 /**
@@ -349,26 +349,26 @@  discard block
 block discarded – undo
349 349
  * @return string, l'extrait trouve.
350 350
  **/
351 351
 function _sqlite_func_extraire_multi($quoi, $lang) {
352
-	if (!defined('_EXTRAIRE_MULTI')) {
353
-		include_spip('inc/filtres');
354
-	}
355
-	if (!function_exists('approcher_langue')) {
356
-		include_spip('inc/lang');
357
-	}
358
-	if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) {
359
-		foreach ($regs as $reg) {
360
-			// chercher la version de la langue courante
361
-			$trads = extraire_trads($reg[1]);
362
-			if ($l = approcher_langue($trads, $lang)) {
363
-				$trad = $trads[$l];
364
-			} else {
365
-				$trad = reset($trads);
366
-			}
367
-			$quoi = str_replace($reg[0], $trad, $quoi);
368
-		}
369
-	}
370
-
371
-	return $quoi;
352
+    if (!defined('_EXTRAIRE_MULTI')) {
353
+        include_spip('inc/filtres');
354
+    }
355
+    if (!function_exists('approcher_langue')) {
356
+        include_spip('inc/lang');
357
+    }
358
+    if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) {
359
+        foreach ($regs as $reg) {
360
+            // chercher la version de la langue courante
361
+            $trads = extraire_trads($reg[1]);
362
+            if ($l = approcher_langue($trads, $lang)) {
363
+                $trad = $trads[$l];
364
+            } else {
365
+                $trad = reset($trads);
366
+            }
367
+            $quoi = str_replace($reg[0], $trad, $quoi);
368
+        }
369
+    }
370
+
371
+    return $quoi;
372 372
 }
373 373
 
374 374
 
@@ -378,7 +378,7 @@  discard block
 block discarded – undo
378 378
  * @return float
379 379
  */
380 380
 function _sqlite_func_rand() {
381
-	return rand();
381
+    return rand();
382 382
 }
383 383
 
384 384
 
@@ -390,7 +390,7 @@  discard block
 block discarded – undo
390 390
  * @return string
391 391
  */
392 392
 function _sqlite_func_right($s, $length) {
393
-	return substr($s, 0 - $length);
393
+    return substr($s, 0 - $length);
394 394
 }
395 395
 
396 396
 
@@ -402,17 +402,17 @@  discard block
 block discarded – undo
402 402
  * @return bool
403 403
  */
404 404
 function _sqlite_func_regexp_match($cherche, $quoi) {
405
-	// optimiser un cas tres courant avec les requetes en base
406
-	if (!$quoi and !strlen($quoi)) {
407
-		return false;
408
-	}
409
-	// il faut enlever un niveau d'echappement pour être homogène à mysql
410
-	$cherche = str_replace('\\\\', '\\', $cherche);
411
-	$u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u';
412
-	$return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
413
-
414
-	#spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
415
-	return $return;
405
+    // optimiser un cas tres courant avec les requetes en base
406
+    if (!$quoi and !strlen($quoi)) {
407
+        return false;
408
+    }
409
+    // il faut enlever un niveau d'echappement pour être homogène à mysql
410
+    $cherche = str_replace('\\\\', '\\', $cherche);
411
+    $u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u';
412
+    $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
413
+
414
+    #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
415
+    return $return;
416 416
 }
417 417
 
418 418
 
@@ -427,8 +427,8 @@  discard block
 block discarded – undo
427 427
  * @return string
428 428
  */
429 429
 function _sqlite_func_date_format($date, $conv) {
430
-	$conv = _sqlite_func_strftime_format_converter($conv);
431
-	return strftime($conv, is_int($date) ? $date : strtotime($date));
430
+    $conv = _sqlite_func_strftime_format_converter($conv);
431
+    return strftime($conv, is_int($date) ? $date : strtotime($date));
432 432
 }
433 433
 
434 434
 /**
@@ -444,28 +444,28 @@  discard block
 block discarded – undo
444 444
  * @return void
445 445
  */
446 446
 function _sqlite_func_strftime_format_converter(string $conv): string {
447
-	// ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y
448
-	// on ne sait pas en gérer certains...
449
-	static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X'];
450
-	static $mysql_to_strftime = [
451
-		'%h' => '%I',
452
-		'%i' => '%M',
453
-		'%M' => '%B',
454
-		'%s' => '%S',
455
-		'%u' => '%U',
456
-		'%v' => '%V',
457
-		'%x' => '%G',
458
-	];
459
-	static $to_strftime = [];
460
-	if (!isset($to_strftime[$conv])) {
461
-		$count = 0;
462
-		str_replace($mysql_to_strftime_not_ok, '', $conv, $count);
463
-		if ($count > 0) {
464
-			spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR);
465
-		}
466
-		$to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv);
467
-	}
468
-	return $to_strftime[$conv];
447
+    // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y
448
+    // on ne sait pas en gérer certains...
449
+    static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X'];
450
+    static $mysql_to_strftime = [
451
+        '%h' => '%I',
452
+        '%i' => '%M',
453
+        '%M' => '%B',
454
+        '%s' => '%S',
455
+        '%u' => '%U',
456
+        '%v' => '%V',
457
+        '%x' => '%G',
458
+    ];
459
+    static $to_strftime = [];
460
+    if (!isset($to_strftime[$conv])) {
461
+        $count = 0;
462
+        str_replace($mysql_to_strftime_not_ok, '', $conv, $count);
463
+        if ($count > 0) {
464
+            spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR);
465
+        }
466
+        $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv);
467
+    }
468
+    return $to_strftime[$conv];
469 469
 }
470 470
 
471 471
 /**
@@ -479,11 +479,11 @@  discard block
 block discarded – undo
479 479
  * @return int
480 480
  */
481 481
 function _sqlite_func_to_days($d) {
482
-	static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
483
-	$result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
482
+    static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
483
+    $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
484 484
 
485
-	#spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
486
-	return $result;
485
+    #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
486
+    return $result;
487 487
 }
488 488
 
489 489
 /**
@@ -495,13 +495,13 @@  discard block
 block discarded – undo
495 495
  * @return string
496 496
  */
497 497
 function _sqlite_func_substring($string, $start, $len = null) {
498
-	// SQL compte a partir de 1, php a partir de 0
499
-	$start = ($start > 0) ? $start - 1 : $start;
500
-	if (is_null($len)) {
501
-		return substr($string, $start);
502
-	} else {
503
-		return substr($string, $start, $len);
504
-	}
498
+    // SQL compte a partir de 1, php a partir de 0
499
+    $start = ($start > 0) ? $start - 1 : $start;
500
+    if (is_null($len)) {
501
+        return substr($string, $start);
502
+    } else {
503
+        return substr($string, $start, $len);
504
+    }
505 505
 }
506 506
 
507 507
 /**
@@ -517,33 +517,33 @@  discard block
 block discarded – undo
517 517
  * @return int
518 518
  */
519 519
 function _sqlite_timestampdiff($unit, $date1, $date2) {
520
-	$d1 = date_create($date1);
521
-	$d2 = date_create($date2);
522
-	$diff = date_diff($d1, $d2);
523
-	$inv = $diff->invert ? -1 : 1;
524
-	switch ($unit) {
525
-		case 'YEAR':
526
-			return $inv * $diff->y;
527
-		case 'QUARTER':
528
-			return $inv * (4 * $diff->y + intval(floor($diff->m / 3)));
529
-		case 'MONTH':
530
-			return $inv * (12 * $diff->y + $diff->m);
531
-		case 'WEEK':
532
-			return $inv * intval(floor($diff->days / 7));
533
-		case 'DAY':
534
-			#var_dump($inv*$diff->days);
535
-			return $inv * $diff->days;
536
-		case 'HOUR':
537
-			return $inv * (24 * $diff->days + $diff->h);
538
-		case 'MINUTE':
539
-			return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i);
540
-		case 'SECOND':
541
-			return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s);
542
-		case 'MICROSECOND':
543
-			return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1000000;
544
-	}
545
-
546
-	return 0;
520
+    $d1 = date_create($date1);
521
+    $d2 = date_create($date2);
522
+    $diff = date_diff($d1, $d2);
523
+    $inv = $diff->invert ? -1 : 1;
524
+    switch ($unit) {
525
+        case 'YEAR':
526
+            return $inv * $diff->y;
527
+        case 'QUARTER':
528
+            return $inv * (4 * $diff->y + intval(floor($diff->m / 3)));
529
+        case 'MONTH':
530
+            return $inv * (12 * $diff->y + $diff->m);
531
+        case 'WEEK':
532
+            return $inv * intval(floor($diff->days / 7));
533
+        case 'DAY':
534
+            #var_dump($inv*$diff->days);
535
+            return $inv * $diff->days;
536
+        case 'HOUR':
537
+            return $inv * (24 * $diff->days + $diff->h);
538
+        case 'MINUTE':
539
+            return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i);
540
+        case 'SECOND':
541
+            return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s);
542
+        case 'MICROSECOND':
543
+            return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1000000;
544
+    }
545
+
546
+    return 0;
547 547
 }
548 548
 
549 549
 /**
@@ -553,24 +553,24 @@  discard block
 block discarded – undo
553 553
  * @return int
554 554
  */
555 555
 function _sqlite_func_unix_timestamp($d) {
556
-	static $mem = [];
557
-	static $n = 0;
558
-	if (isset($mem[$d])) {
559
-		return $mem[$d];
560
-	}
561
-	if ($n++ > 100) {
562
-		$mem = [];
563
-		$n = 0;
564
-	}
565
-
566
-	//2005-12-02 20:53:53
567
-	#spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG);
568
-	if (!$d) {
569
-		return $mem[$d] = mktime();
570
-	}
571
-
572
-	// une pile plus grosse n'accelere pas le calcul
573
-	return $mem[$d] = strtotime($d);
556
+    static $mem = [];
557
+    static $n = 0;
558
+    if (isset($mem[$d])) {
559
+        return $mem[$d];
560
+    }
561
+    if ($n++ > 100) {
562
+        $mem = [];
563
+        $n = 0;
564
+    }
565
+
566
+    //2005-12-02 20:53:53
567
+    #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG);
568
+    if (!$d) {
569
+        return $mem[$d] = mktime();
570
+    }
571
+
572
+    // une pile plus grosse n'accelere pas le calcul
573
+    return $mem[$d] = strtotime($d);
574 574
 }
575 575
 
576 576
 
@@ -583,7 +583,7 @@  discard block
 block discarded – undo
583 583
  * @return int
584 584
  */
585 585
 function _sqlite_func_year($d) {
586
-	return _sqlite_func_date('Y', $d);
586
+    return _sqlite_func_date('Y', $d);
587 587
 }
588 588
 
589 589
 /**
@@ -596,20 +596,20 @@  discard block
 block discarded – undo
596 596
  * @return int
597 597
  */
598 598
 function _sqlite_func_date($quoi, $d) {
599
-	static $mem = [];
600
-	static $n = 0;
601
-	if (isset($mem[$d])) {
602
-		return $mem[$d][$quoi];
603
-	}
604
-	if ($n++ > 100) {
605
-		$mem = [];
606
-		$n = 0;
607
-	}
608
-
609
-	$dec = date('Y-m-d', _sqlite_func_unix_timestamp($d));
610
-	$mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)];
611
-
612
-	return $mem[$d][$quoi];
599
+    static $mem = [];
600
+    static $n = 0;
601
+    if (isset($mem[$d])) {
602
+        return $mem[$d][$quoi];
603
+    }
604
+    if ($n++ > 100) {
605
+        $mem = [];
606
+        $n = 0;
607
+    }
608
+
609
+    $dec = date('Y-m-d', _sqlite_func_unix_timestamp($d));
610
+    $mem[$d] = ['Y' => substr($dec, 0, 4), 'm' => substr($dec, 5, 2), 'd' => substr($dec, 8, 2)];
611
+
612
+    return $mem[$d][$quoi];
613 613
 }
614 614
 
615 615
 /**
@@ -618,5 +618,5 @@  discard block
 block discarded – undo
618 618
  * @return void
619 619
  */
620 620
 function _sqlite_func_vide() {
621
-	return;
621
+    return;
622 622
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -333,7 +333,7 @@  discard block
 block discarded – undo
333 333
  * @return string
334 334
  */
335 335
 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
336
-	$return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
336
+	$return = preg_replace('%'.$cherche.'%', $remplace, $quoi);
337 337
 
338 338
 	#spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
339 339
 	return $return;
@@ -409,7 +409,7 @@  discard block
 block discarded – undo
409 409
 	// il faut enlever un niveau d'echappement pour être homogène à mysql
410 410
 	$cherche = str_replace('\\\\', '\\', $cherche);
411 411
 	$u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u';
412
-	$return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
412
+	$return = preg_match('%'.$cherche.'%imsS'.$u, $quoi);
413 413
 
414 414
 	#spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
415 415
 	return $return;
@@ -461,7 +461,7 @@  discard block
 block discarded – undo
461 461
 		$count = 0;
462 462
 		str_replace($mysql_to_strftime_not_ok, '', $conv, $count);
463 463
 		if ($count > 0) {
464
-			spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR);
464
+			spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.'._LOG_ERREUR);
465 465
 		}
466 466
 		$to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv);
467 467
 	}
@@ -480,7 +480,7 @@  discard block
 block discarded – undo
480 480
  */
481 481
 function _sqlite_func_to_days($d) {
482 482
 	static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
483
-	$result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
483
+	$result = $offset + (int) ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
484 484
 
485 485
 	#spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
486 486
 	return $result;
Please login to merge, or discard this patch.