Completed
Push — master ( 0c3b57...9f4cc0 )
by cam
04:50 queued 40s
created
ecrire/req/sqlite_fonctions.php 3 patches
Doc Comments   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -33,7 +33,7 @@  discard block
 block discarded – undo
33 33
  * @link http://www.sqlite.org/lang_corefunc.html Liste des fonctions natives
34 34
  * @link http://sqlite.org/changes.html Liste des évolutions
35 35
  * 
36
- * @param PDO|resource $sqlite Représente la connexion Sqlite
36
+ * @param PDO $sqlite Représente la connexion Sqlite
37 37
  * @return false|void
38 38
  */
39 39
 function _sqlite_init_functions(&$sqlite) {
@@ -337,7 +337,7 @@  discard block
 block discarded – undo
337 337
  * soit inadapté.
338 338
  *
339 339
  * @param string $conv
340
- * @return void
340
+ * @return string
341 341
  */
342 342
 function _sqlite_func_strftime_format_converter(string $conv) : string {
343 343
 	// ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y
Please login to merge, or discard this patch.
Indentation   +228 added lines, -228 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
  */
18 18
  
19 19
 if (!defined('_ECRIRE_INC_VERSION')) {
20
-	return;
20
+    return;
21 21
 }
22 22
 
23 23
 /**
@@ -38,93 +38,93 @@  discard block
 block discarded – undo
38 38
  */
39 39
 function _sqlite_init_functions(&$sqlite) {
40 40
 
41
-	if (!$sqlite) {
42
-		return false;
43
-	}
41
+    if (!$sqlite) {
42
+        return false;
43
+    }
44 44
 
45 45
 
46
-	$fonctions = array(
47
-		// A
48
-		'ACOS'  => array('acos', 1),
49
-		'ASIN'  => array('asin', 1),
50
-		'ATAN'  => array('atan', 1), // mysql accepte 2 params comme atan2… hum ?
51
-		'ATAN2' => array('atan2', 2),
46
+    $fonctions = array(
47
+        // A
48
+        'ACOS'  => array('acos', 1),
49
+        'ASIN'  => array('asin', 1),
50
+        'ATAN'  => array('atan', 1), // mysql accepte 2 params comme atan2… hum ?
51
+        'ATAN2' => array('atan2', 2),
52 52
 
53
-		// C
54
-		'CEIL'   => array('_sqlite_func_ceil', 1),
55
-		'CONCAT' => array('_sqlite_func_concat', -1),
56
-		'COS'    => array('cos', 1),
53
+        // C
54
+        'CEIL'   => array('_sqlite_func_ceil', 1),
55
+        'CONCAT' => array('_sqlite_func_concat', -1),
56
+        'COS'    => array('cos', 1),
57 57
 
58
-		// D
59
-		'DATE_FORMAT' => array('_sqlite_func_strftime', 2),
60
-		'DAYOFMONTH'  => array('_sqlite_func_dayofmonth', 1),
61
-		'DEGREES'     => array('rad2deg', 1),
58
+        // D
59
+        'DATE_FORMAT' => array('_sqlite_func_strftime', 2),
60
+        'DAYOFMONTH'  => array('_sqlite_func_dayofmonth', 1),
61
+        'DEGREES'     => array('rad2deg', 1),
62 62
 
63
-		// E
64
-		'EXTRAIRE_MULTI' => array('_sqlite_func_extraire_multi', 2), // specifique a SPIP/sql_multi()
65
-		'EXP'            => array('exp', 1),
63
+        // E
64
+        'EXTRAIRE_MULTI' => array('_sqlite_func_extraire_multi', 2), // specifique a SPIP/sql_multi()
65
+        'EXP'            => array('exp', 1),
66 66
 
67
-		// F
68
-		'FIND_IN_SET' => array('_sqlite_func_find_in_set', 2),
69
-		'FLOOR'       => array('_sqlite_func_floor', 1),
67
+        // F
68
+        'FIND_IN_SET' => array('_sqlite_func_find_in_set', 2),
69
+        'FLOOR'       => array('_sqlite_func_floor', 1),
70 70
 
71
-		// I
72
-		'IF'     => array('_sqlite_func_if', 3),
73
-		'INSERT' => array('_sqlite_func_insert', 4),
74
-		'INSTR'  => array('_sqlite_func_instr', 2),
71
+        // I
72
+        'IF'     => array('_sqlite_func_if', 3),
73
+        'INSERT' => array('_sqlite_func_insert', 4),
74
+        'INSTR'  => array('_sqlite_func_instr', 2),
75 75
 
76
-		// L
77
-		'LEAST'  => array('_sqlite_func_least', 3),
78
-		'_LEFT'  => array('_sqlite_func_left', 2),
76
+        // L
77
+        'LEAST'  => array('_sqlite_func_least', 3),
78
+        '_LEFT'  => array('_sqlite_func_left', 2),
79 79
 #		'LENGTH' => array('strlen', 1), // present v1.0.4
80 80
 #		'LOWER'  => array('strtolower', 1), // present v2.4
81 81
 #		'LTRIM'  => array('ltrim', 1), // present
82 82
 
83
-		// N
84
-		'NOW' => array('_sqlite_func_now', 0),
83
+        // N
84
+        'NOW' => array('_sqlite_func_now', 0),
85 85
 
86
-		// M
87
-		'MD5'   => array('md5', 1),
88
-		'MONTH' => array('_sqlite_func_month', 1),
86
+        // M
87
+        'MD5'   => array('md5', 1),
88
+        'MONTH' => array('_sqlite_func_month', 1),
89 89
 
90
-		// P
91
-		'PREG_REPLACE' => array('_sqlite_func_preg_replace', 3),
90
+        // P
91
+        'PREG_REPLACE' => array('_sqlite_func_preg_replace', 3),
92 92
 
93
-		// R
94
-		'RADIANS' => array('deg2rad', 1),
95
-		'RAND'    => array('_sqlite_func_rand', 0), // sinon random() v2.4
96
-		'REGEXP'  => array('_sqlite_func_regexp_match', 2), // critere REGEXP supporte a partir de v3.3.2
97
-		'RIGHT'   => array('_sqlite_func_right', 2),
93
+        // R
94
+        'RADIANS' => array('deg2rad', 1),
95
+        'RAND'    => array('_sqlite_func_rand', 0), // sinon random() v2.4
96
+        'REGEXP'  => array('_sqlite_func_regexp_match', 2), // critere REGEXP supporte a partir de v3.3.2
97
+        'RIGHT'   => array('_sqlite_func_right', 2),
98 98
 #		'RTRIM'   => array('rtrim', 1), // present
99 99
 
100
-		// S
101
-		'SETTYPE'   => array('settype', 2), // CAST present en v3.2.3
102
-		'SIN'       => array('sin', 1),
103
-		'SQRT'      => array('sqrt', 1),
104
-		'SUBSTRING' => array('_sqlite_func_substring' /*, 3*/), // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0
100
+        // S
101
+        'SETTYPE'   => array('settype', 2), // CAST present en v3.2.3
102
+        'SIN'       => array('sin', 1),
103
+        'SQRT'      => array('sqrt', 1),
104
+        'SUBSTRING' => array('_sqlite_func_substring' /*, 3*/), // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0
105 105
 
106
-		// T
107
-		'TAN'           => array('tan', 1),
108
-		'TIMESTAMPDIFF' => array('_sqlite_timestampdiff'    /*, 3*/),
109
-		'TO_DAYS'       => array('_sqlite_func_to_days', 1),
106
+        // T
107
+        'TAN'           => array('tan', 1),
108
+        'TIMESTAMPDIFF' => array('_sqlite_timestampdiff'    /*, 3*/),
109
+        'TO_DAYS'       => array('_sqlite_func_to_days', 1),
110 110
 #		'TRIM'          => array('trim', 1), // present
111 111
 
112
-		// U
113
-		'UNIX_TIMESTAMP' => array('_sqlite_func_unix_timestamp', 1),
112
+        // U
113
+        'UNIX_TIMESTAMP' => array('_sqlite_func_unix_timestamp', 1),
114 114
 #		'UPPER'          => array('strtoupper', 1), // present v2.4
115 115
 
116
-		// V
117
-		'VIDE' => array('_sqlite_func_vide', 0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
116
+        // V
117
+        'VIDE' => array('_sqlite_func_vide', 0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
118 118
 
119
-		// Y
120
-		'YEAR' => array('_sqlite_func_year', 1)
121
-	);
119
+        // Y
120
+        'YEAR' => array('_sqlite_func_year', 1)
121
+    );
122 122
 
123
-	foreach ($fonctions as $f => $r) {
124
-		_sqlite_add_function($sqlite, $f, $r);
125
-	}
123
+    foreach ($fonctions as $f => $r) {
124
+        _sqlite_add_function($sqlite, $f, $r);
125
+    }
126 126
 
127
-	#spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG);
127
+    #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG);
128 128
 }
129 129
 
130 130
 
@@ -145,9 +145,9 @@  discard block
 block discarded – undo
145 145
  * 
146 146
 **/
147 147
 function _sqlite_add_function(&$sqlite, &$f, &$r) {
148
-	isset($r[1])
149
-		? $sqlite->sqliteCreateFunction($f, $r[0], $r[1])
150
-		: $sqlite->sqliteCreateFunction($f, $r[0]);
148
+    isset($r[1])
149
+        ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1])
150
+        : $sqlite->sqliteCreateFunction($f, $r[0]);
151 151
 }
152 152
 
153 153
 //
@@ -156,43 +156,43 @@  discard block
 block discarded – undo
156 156
 //
157 157
 
158 158
 function _sqlite_func_ceil($a) {
159
-	return ceil($a);
159
+    return ceil($a);
160 160
 }
161 161
 
162 162
 // https://code.spip.net/@_sqlite_func_concat
163 163
 function _sqlite_func_concat() {
164
-	$args = func_get_args();
164
+    $args = func_get_args();
165 165
 
166
-	return join('', $args);
166
+    return join('', $args);
167 167
 }
168 168
 
169 169
 
170 170
 // https://code.spip.net/@_sqlite_func_dayofmonth
171 171
 function _sqlite_func_dayofmonth($d) {
172
-	return _sqlite_func_date("d", $d);
172
+    return _sqlite_func_date("d", $d);
173 173
 }
174 174
 
175 175
 
176 176
 // https://code.spip.net/@_sqlite_func_find_in_set
177 177
 function _sqlite_func_find_in_set($num, $set) {
178
-	$rank = 0;
179
-	foreach (explode(",", $set) as $v) {
180
-		if ($v == $num) {
181
-			return (++$rank);
182
-		}
183
-		$rank++;
184
-	}
185
-
186
-	return 0;
178
+    $rank = 0;
179
+    foreach (explode(",", $set) as $v) {
180
+        if ($v == $num) {
181
+            return (++$rank);
182
+        }
183
+        $rank++;
184
+    }
185
+
186
+    return 0;
187 187
 }
188 188
 
189 189
 function _sqlite_func_floor($a) {
190
-	return floor($a);
190
+    return floor($a);
191 191
 }
192 192
 
193 193
 // https://code.spip.net/@_sqlite_func_if
194 194
 function _sqlite_func_if($bool, $oui, $non) {
195
-	return ($bool) ? $oui : $non;
195
+    return ($bool) ? $oui : $non;
196 196
 }
197 197
 
198 198
 
@@ -203,59 +203,59 @@  discard block
 block discarded – undo
203 203
  */
204 204
 // https://code.spip.net/@_sqlite_func_insert
205 205
 function _sqlite_func_insert($s, $index, $longueur, $chaine) {
206
-	return
207
-		substr($s, 0, $index)
208
-		. $chaine
209
-		. substr(substr($s, $index), $longueur);
206
+    return
207
+        substr($s, 0, $index)
208
+        . $chaine
209
+        . substr(substr($s, $index), $longueur);
210 210
 }
211 211
 
212 212
 
213 213
 // https://code.spip.net/@_sqlite_func_instr
214 214
 function _sqlite_func_instr($s, $search) {
215
-	return strpos($s, $search);
215
+    return strpos($s, $search);
216 216
 }
217 217
 
218 218
 
219 219
 // https://code.spip.net/@_sqlite_func_least
220 220
 function _sqlite_func_least() {
221
-	$arg_list = func_get_args();
222
-	$least = min($arg_list);
221
+    $arg_list = func_get_args();
222
+    $least = min($arg_list);
223 223
 
224
-	#spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG);
225
-	return $least;
224
+    #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG);
225
+    return $least;
226 226
 }
227 227
 
228 228
 
229 229
 // https://code.spip.net/@_sqlite_func_left
230 230
 function _sqlite_func_left($s, $lenght) {
231
-	return substr($s, $lenght);
231
+    return substr($s, $lenght);
232 232
 }
233 233
 
234 234
 
235 235
 // https://code.spip.net/@_sqlite_func_now
236 236
 function _sqlite_func_now() {
237
-	static $now = null;
238
-	if (is_null($now)) {
239
-		$now = date("Y-m-d H:i:s");
240
-	}
237
+    static $now = null;
238
+    if (is_null($now)) {
239
+        $now = date("Y-m-d H:i:s");
240
+    }
241 241
 
242
-	#spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG);
243
-	return $now;
242
+    #spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG);
243
+    return $now;
244 244
 }
245 245
 
246 246
 
247 247
 // https://code.spip.net/@_sqlite_func_month
248 248
 function _sqlite_func_month($d) {
249
-	return _sqlite_func_date("m", $d);
249
+    return _sqlite_func_date("m", $d);
250 250
 }
251 251
 
252 252
 
253 253
 // https://code.spip.net/@_sqlite_func_preg_replace
254 254
 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
255
-	$return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
255
+    $return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
256 256
 
257
-	#spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
258
-	return $return;
257
+    #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
258
+    return $return;
259 259
 }
260 260
 
261 261
 /**
@@ -266,52 +266,52 @@  discard block
 block discarded – undo
266 266
  * @return string, l'extrait trouve.
267 267
  **/
268 268
 function _sqlite_func_extraire_multi($quoi, $lang) {
269
-	if (!defined('_EXTRAIRE_MULTI')) {
270
-		include_spip('inc/filtres');
271
-	}
272
-	if (!function_exists('approcher_langue')) {
273
-		include_spip('inc/lang');
274
-	}
275
-	if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) {
276
-		foreach ($regs as $reg) {
277
-			// chercher la version de la langue courante
278
-			$trads = extraire_trads($reg[1]);
279
-			if ($l = approcher_langue($trads, $lang)) {
280
-				$trad = $trads[$l];
281
-			} else {
282
-				$trad = reset($trads);
283
-			}
284
-			$quoi = str_replace($reg[0], $trad, $quoi);
285
-		}
286
-	}
287
-
288
-	return $quoi;
269
+    if (!defined('_EXTRAIRE_MULTI')) {
270
+        include_spip('inc/filtres');
271
+    }
272
+    if (!function_exists('approcher_langue')) {
273
+        include_spip('inc/lang');
274
+    }
275
+    if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) {
276
+        foreach ($regs as $reg) {
277
+            // chercher la version de la langue courante
278
+            $trads = extraire_trads($reg[1]);
279
+            if ($l = approcher_langue($trads, $lang)) {
280
+                $trad = $trads[$l];
281
+            } else {
282
+                $trad = reset($trads);
283
+            }
284
+            $quoi = str_replace($reg[0], $trad, $quoi);
285
+        }
286
+    }
287
+
288
+    return $quoi;
289 289
 }
290 290
 
291 291
 
292 292
 // https://code.spip.net/@_sqlite_func_rand
293 293
 function _sqlite_func_rand() {
294
-	return rand();
294
+    return rand();
295 295
 }
296 296
 
297 297
 
298 298
 // https://code.spip.net/@_sqlite_func_right
299 299
 function _sqlite_func_right($s, $length) {
300
-	return substr($s, 0 - $length);
300
+    return substr($s, 0 - $length);
301 301
 }
302 302
 
303 303
 
304 304
 // https://code.spip.net/@_sqlite_func_regexp_match
305 305
 function _sqlite_func_regexp_match($cherche, $quoi) {
306
-	// optimiser un cas tres courant avec les requetes en base
307
-	if (!$quoi and !strlen($quoi)) {
308
-		return false;
309
-	}
310
-	$u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u';
311
-	$return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
312
-
313
-	#spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
314
-	return $return;
306
+    // optimiser un cas tres courant avec les requetes en base
307
+    if (!$quoi and !strlen($quoi)) {
308
+        return false;
309
+    }
310
+    $u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u';
311
+    $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
312
+
313
+    #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
314
+    return $return;
315 315
 }
316 316
 
317 317
 
@@ -323,8 +323,8 @@  discard block
 block discarded – undo
323 323
  * @return string
324 324
  */
325 325
 function _sqlite_func_strftime($date, $conv) {
326
-	$conv = _sqlite_func_strftime_format_converter($conv);
327
-	return strftime($conv, is_int($date) ? $date : strtotime($date));
326
+    $conv = _sqlite_func_strftime_format_converter($conv);
327
+    return strftime($conv, is_int($date) ? $date : strtotime($date));
328 328
 }
329 329
 
330 330
 /**
@@ -340,28 +340,28 @@  discard block
 block discarded – undo
340 340
  * @return void
341 341
  */
342 342
 function _sqlite_func_strftime_format_converter(string $conv) : string {
343
-	// ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y
344
-	// on ne sait pas en gérer certains...
345
-	static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X'];
346
-	static $mysql_to_strftime = [
347
-		'%h' => '%I',
348
-		'%i' => '%M',
349
-		'%M' => '%B',
350
-		'%s' => '%S',
351
-		'%u' => '%U',
352
-		'%v' => '%V',
353
-		'%x' => '%G',
354
-	];
355
-	static $to_strftime = [];
356
-	if (!isset($to_strftime[$conv])) {
357
-		$count = 0;
358
-		str_replace($mysql_to_strftime_not_ok, '', $conv, $count);
359
-		if ($count > 0) {
360
-			spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR);
361
-		}
362
-		$to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv);
363
-	}
364
-	return $to_strftime[$conv];
343
+    // ok : %a %b %d %e %H %I %l %j %k %m %p %r %S %T %w %y %Y
344
+    // on ne sait pas en gérer certains...
345
+    static $mysql_to_strftime_not_ok = ['%c', '%D', '%f', '%U', '%V', '%W', '%X'];
346
+    static $mysql_to_strftime = [
347
+        '%h' => '%I',
348
+        '%i' => '%M',
349
+        '%M' => '%B',
350
+        '%s' => '%S',
351
+        '%u' => '%U',
352
+        '%v' => '%V',
353
+        '%x' => '%G',
354
+    ];
355
+    static $to_strftime = [];
356
+    if (!isset($to_strftime[$conv])) {
357
+        $count = 0;
358
+        str_replace($mysql_to_strftime_not_ok, '', $conv, $count);
359
+        if ($count > 0) {
360
+            spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR);
361
+        }
362
+        $to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv);
363
+    }
364
+    return $to_strftime[$conv];
365 365
 }
366 366
 
367 367
 /**
@@ -373,21 +373,21 @@  discard block
 block discarded – undo
373 373
  * @return int
374 374
  */
375 375
 function _sqlite_func_to_days($d) {
376
-	static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
377
-	$result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
376
+    static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
377
+    $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
378 378
 
379
-	#spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
380
-	return $result;
379
+    #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
380
+    return $result;
381 381
 }
382 382
 
383 383
 function _sqlite_func_substring($string, $start, $len = null) {
384
-	// SQL compte a partir de 1, php a partir de 0
385
-	$start = ($start > 0) ? $start - 1 : $start;
386
-	if (is_null($len)) {
387
-		return substr($string, $start);
388
-	} else {
389
-		return substr($string, $start, $len);
390
-	}
384
+    // SQL compte a partir de 1, php a partir de 0
385
+    $start = ($start > 0) ? $start - 1 : $start;
386
+    if (is_null($len)) {
387
+        return substr($string, $start);
388
+    } else {
389
+        return substr($string, $start, $len);
390
+    }
391 391
 }
392 392
 
393 393
 /**
@@ -401,61 +401,61 @@  discard block
 block discarded – undo
401 401
  * @return int
402 402
  */
403 403
 function _sqlite_timestampdiff($unit, $date1, $date2) {
404
-	$d1 = date_create($date1);
405
-	$d2 = date_create($date2);
406
-	$diff = date_diff($d1, $d2);
407
-	$inv = $diff->invert ? -1 : 1;
408
-	switch ($unit) {
409
-		case "YEAR":
410
-			return $inv * $diff->y;
411
-		case "QUARTER":
412
-			return $inv * (4 * $diff->y + intval(floor($diff->m / 3)));
413
-		case "MONTH":
414
-			return $inv * (12 * $diff->y + $diff->m);
415
-		case "WEEK":
416
-			return $inv * intval(floor($diff->days / 7));
417
-		case "DAY":
418
-			#var_dump($inv*$diff->days);
419
-			return $inv * $diff->days;
420
-		case "HOUR":
421
-			return $inv * (24 * $diff->days + $diff->h);
422
-		case "MINUTE":
423
-			return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i);
424
-		case "SECOND":
425
-			return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s);
426
-		case "MICROSECOND":
427
-			return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1000000;
428
-	}
429
-
430
-	return 0;
404
+    $d1 = date_create($date1);
405
+    $d2 = date_create($date2);
406
+    $diff = date_diff($d1, $d2);
407
+    $inv = $diff->invert ? -1 : 1;
408
+    switch ($unit) {
409
+        case "YEAR":
410
+            return $inv * $diff->y;
411
+        case "QUARTER":
412
+            return $inv * (4 * $diff->y + intval(floor($diff->m / 3)));
413
+        case "MONTH":
414
+            return $inv * (12 * $diff->y + $diff->m);
415
+        case "WEEK":
416
+            return $inv * intval(floor($diff->days / 7));
417
+        case "DAY":
418
+            #var_dump($inv*$diff->days);
419
+            return $inv * $diff->days;
420
+        case "HOUR":
421
+            return $inv * (24 * $diff->days + $diff->h);
422
+        case "MINUTE":
423
+            return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i);
424
+        case "SECOND":
425
+            return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s);
426
+        case "MICROSECOND":
427
+            return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1000000;
428
+    }
429
+
430
+    return 0;
431 431
 }
432 432
 
433 433
 // https://code.spip.net/@_sqlite_func_unix_timestamp
434 434
 function _sqlite_func_unix_timestamp($d) {
435
-	static $mem = array();
436
-	static $n = 0;
437
-	if (isset($mem[$d])) {
438
-		return $mem[$d];
439
-	}
440
-	if ($n++ > 100) {
441
-		$mem = array();
442
-		$n = 0;
443
-	}
444
-
445
-	//2005-12-02 20:53:53
446
-	#spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG);
447
-	if (!$d) {
448
-		return $mem[$d] = mktime();
449
-	}
450
-
451
-	// une pile plus grosse n'accelere pas le calcul
452
-	return $mem[$d] = strtotime($d);
435
+    static $mem = array();
436
+    static $n = 0;
437
+    if (isset($mem[$d])) {
438
+        return $mem[$d];
439
+    }
440
+    if ($n++ > 100) {
441
+        $mem = array();
442
+        $n = 0;
443
+    }
444
+
445
+    //2005-12-02 20:53:53
446
+    #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG);
447
+    if (!$d) {
448
+        return $mem[$d] = mktime();
449
+    }
450
+
451
+    // une pile plus grosse n'accelere pas le calcul
452
+    return $mem[$d] = strtotime($d);
453 453
 }
454 454
 
455 455
 
456 456
 // https://code.spip.net/@_sqlite_func_year
457 457
 function _sqlite_func_year($d) {
458
-	return _sqlite_func_date("Y", $d);
458
+    return _sqlite_func_date("Y", $d);
459 459
 }
460 460
 
461 461
 /**
@@ -469,23 +469,23 @@  discard block
 block discarded – undo
469 469
  * @return int
470 470
  */
471 471
 function _sqlite_func_date($quoi, $d) {
472
-	static $mem = array();
473
-	static $n = 0;
474
-	if (isset($mem[$d])) {
475
-		return $mem[$d][$quoi];
476
-	}
477
-	if ($n++ > 100) {
478
-		$mem = array();
479
-		$n = 0;
480
-	}
481
-
482
-	$dec = date("Y-m-d", _sqlite_func_unix_timestamp($d));
483
-	$mem[$d] = array("Y" => substr($dec, 0, 4), "m" => substr($dec, 5, 2), "d" => substr($dec, 8, 2));
484
-
485
-	return $mem[$d][$quoi];
472
+    static $mem = array();
473
+    static $n = 0;
474
+    if (isset($mem[$d])) {
475
+        return $mem[$d][$quoi];
476
+    }
477
+    if ($n++ > 100) {
478
+        $mem = array();
479
+        $n = 0;
480
+    }
481
+
482
+    $dec = date("Y-m-d", _sqlite_func_unix_timestamp($d));
483
+    $mem[$d] = array("Y" => substr($dec, 0, 4), "m" => substr($dec, 5, 2), "d" => substr($dec, 8, 2));
484
+
485
+    return $mem[$d][$quoi];
486 486
 }
487 487
 
488 488
 // https://code.spip.net/@_sqlite_func_vide
489 489
 function _sqlite_func_vide() {
490
-	return;
490
+    return;
491 491
 }
Please login to merge, or discard this patch.
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -252,7 +252,7 @@  discard block
 block discarded – undo
252 252
 
253 253
 // https://code.spip.net/@_sqlite_func_preg_replace
254 254
 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
255
-	$return = preg_replace('%' . $cherche . '%', $remplace, $quoi);
255
+	$return = preg_replace('%'.$cherche.'%', $remplace, $quoi);
256 256
 
257 257
 	#spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
258 258
 	return $return;
@@ -308,7 +308,7 @@  discard block
 block discarded – undo
308 308
 		return false;
309 309
 	}
310 310
 	$u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u';
311
-	$return = preg_match('%' . $cherche . '%imsS' . $u, $quoi);
311
+	$return = preg_match('%'.$cherche.'%imsS'.$u, $quoi);
312 312
 
313 313
 	#spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
314 314
 	return $return;
@@ -357,7 +357,7 @@  discard block
 block discarded – undo
357 357
 		$count = 0;
358 358
 		str_replace($mysql_to_strftime_not_ok, '', $conv, $count);
359 359
 		if ($count > 0) {
360
-			spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.' . _LOG_ERREUR);
360
+			spip_log("DATE_FORMAT : At least one parameter can't be parsed by strftime with format '$conv'", 'sqlite.'._LOG_ERREUR);
361 361
 		}
362 362
 		$to_strftime[$conv] = str_replace(array_keys($mysql_to_strftime), $mysql_to_strftime, $conv);
363 363
 	}
@@ -374,7 +374,7 @@  discard block
 block discarded – undo
374 374
  */
375 375
 function _sqlite_func_to_days($d) {
376 376
 	static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
377
-	$result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
377
+	$result = $offset + (int) ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600));
378 378
 
379 379
 	#spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
380 380
 	return $result;
Please login to merge, or discard this patch.