@@ -17,7 +17,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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,41 +156,41 @@ discard block |
||
| 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(...$args) { |
| 164 | - return join('', $args); |
|
| 164 | + return join('', $args); |
|
| 165 | 165 | } |
| 166 | 166 | |
| 167 | 167 | |
| 168 | 168 | // https://code.spip.net/@_sqlite_func_dayofmonth |
| 169 | 169 | function _sqlite_func_dayofmonth($d) { |
| 170 | - return _sqlite_func_date("d", $d); |
|
| 170 | + return _sqlite_func_date("d", $d); |
|
| 171 | 171 | } |
| 172 | 172 | |
| 173 | 173 | |
| 174 | 174 | // https://code.spip.net/@_sqlite_func_find_in_set |
| 175 | 175 | function _sqlite_func_find_in_set($num, $set) { |
| 176 | - $rank = 0; |
|
| 177 | - foreach (explode(",", $set) as $v) { |
|
| 178 | - if ($v == $num) { |
|
| 179 | - return (++$rank); |
|
| 180 | - } |
|
| 181 | - $rank++; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - return 0; |
|
| 176 | + $rank = 0; |
|
| 177 | + foreach (explode(",", $set) as $v) { |
|
| 178 | + if ($v == $num) { |
|
| 179 | + return (++$rank); |
|
| 180 | + } |
|
| 181 | + $rank++; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + return 0; |
|
| 185 | 185 | } |
| 186 | 186 | |
| 187 | 187 | function _sqlite_func_floor($a) { |
| 188 | - return floor($a); |
|
| 188 | + return floor($a); |
|
| 189 | 189 | } |
| 190 | 190 | |
| 191 | 191 | // https://code.spip.net/@_sqlite_func_if |
| 192 | 192 | function _sqlite_func_if($bool, $oui, $non) { |
| 193 | - return ($bool) ? $oui : $non; |
|
| 193 | + return ($bool) ? $oui : $non; |
|
| 194 | 194 | } |
| 195 | 195 | |
| 196 | 196 | |
@@ -201,59 +201,59 @@ discard block |
||
| 201 | 201 | */ |
| 202 | 202 | // https://code.spip.net/@_sqlite_func_insert |
| 203 | 203 | function _sqlite_func_insert($s, $index, $longueur, $chaine) { |
| 204 | - return |
|
| 205 | - substr($s, 0, $index) |
|
| 206 | - . $chaine |
|
| 207 | - . substr(substr($s, $index), $longueur); |
|
| 204 | + return |
|
| 205 | + substr($s, 0, $index) |
|
| 206 | + . $chaine |
|
| 207 | + . substr(substr($s, $index), $longueur); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | |
| 211 | 211 | // https://code.spip.net/@_sqlite_func_instr |
| 212 | 212 | function _sqlite_func_instr($s, $search) { |
| 213 | - return strpos($s, $search); |
|
| 213 | + return strpos($s, $search); |
|
| 214 | 214 | } |
| 215 | 215 | |
| 216 | 216 | |
| 217 | 217 | // https://code.spip.net/@_sqlite_func_least |
| 218 | 218 | function _sqlite_func_least() { |
| 219 | - $arg_list = func_get_args(); |
|
| 220 | - $least = min($arg_list); |
|
| 219 | + $arg_list = func_get_args(); |
|
| 220 | + $least = min($arg_list); |
|
| 221 | 221 | |
| 222 | - #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG); |
|
| 223 | - return $least; |
|
| 222 | + #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG); |
|
| 223 | + return $least; |
|
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | |
| 227 | 227 | // https://code.spip.net/@_sqlite_func_left |
| 228 | 228 | function _sqlite_func_left($s, $lenght) { |
| 229 | - return substr($s, $lenght); |
|
| 229 | + return substr($s, $lenght); |
|
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | |
| 233 | 233 | // https://code.spip.net/@_sqlite_func_now |
| 234 | 234 | function _sqlite_func_now() { |
| 235 | - static $now = null; |
|
| 236 | - if (is_null($now)) { |
|
| 237 | - $now = date("Y-m-d H:i:s"); |
|
| 238 | - } |
|
| 235 | + static $now = null; |
|
| 236 | + if (is_null($now)) { |
|
| 237 | + $now = date("Y-m-d H:i:s"); |
|
| 238 | + } |
|
| 239 | 239 | |
| 240 | - #spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG); |
|
| 241 | - return $now; |
|
| 240 | + #spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG); |
|
| 241 | + return $now; |
|
| 242 | 242 | } |
| 243 | 243 | |
| 244 | 244 | |
| 245 | 245 | // https://code.spip.net/@_sqlite_func_month |
| 246 | 246 | function _sqlite_func_month($d) { |
| 247 | - return _sqlite_func_date("m", $d); |
|
| 247 | + return _sqlite_func_date("m", $d); |
|
| 248 | 248 | } |
| 249 | 249 | |
| 250 | 250 | |
| 251 | 251 | // https://code.spip.net/@_sqlite_func_preg_replace |
| 252 | 252 | function _sqlite_func_preg_replace($quoi, $cherche, $remplace) { |
| 253 | - $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 253 | + $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 254 | 254 | |
| 255 | - #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 256 | - return $return; |
|
| 255 | + #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 256 | + return $return; |
|
| 257 | 257 | } |
| 258 | 258 | |
| 259 | 259 | /** |
@@ -264,54 +264,54 @@ discard block |
||
| 264 | 264 | * @return string, l'extrait trouve. |
| 265 | 265 | **/ |
| 266 | 266 | function _sqlite_func_extraire_multi($quoi, $lang) { |
| 267 | - if (!defined('_EXTRAIRE_MULTI')) { |
|
| 268 | - include_spip('inc/filtres'); |
|
| 269 | - } |
|
| 270 | - if (!function_exists('approcher_langue')) { |
|
| 271 | - include_spip('inc/lang'); |
|
| 272 | - } |
|
| 273 | - if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 274 | - foreach ($regs as $reg) { |
|
| 275 | - // chercher la version de la langue courante |
|
| 276 | - $trads = extraire_trads($reg[1]); |
|
| 277 | - if ($l = approcher_langue($trads, $lang)) { |
|
| 278 | - $trad = $trads[$l]; |
|
| 279 | - } else { |
|
| 280 | - $trad = reset($trads); |
|
| 281 | - } |
|
| 282 | - $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - return $quoi; |
|
| 267 | + if (!defined('_EXTRAIRE_MULTI')) { |
|
| 268 | + include_spip('inc/filtres'); |
|
| 269 | + } |
|
| 270 | + if (!function_exists('approcher_langue')) { |
|
| 271 | + include_spip('inc/lang'); |
|
| 272 | + } |
|
| 273 | + if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 274 | + foreach ($regs as $reg) { |
|
| 275 | + // chercher la version de la langue courante |
|
| 276 | + $trads = extraire_trads($reg[1]); |
|
| 277 | + if ($l = approcher_langue($trads, $lang)) { |
|
| 278 | + $trad = $trads[$l]; |
|
| 279 | + } else { |
|
| 280 | + $trad = reset($trads); |
|
| 281 | + } |
|
| 282 | + $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + return $quoi; |
|
| 287 | 287 | } |
| 288 | 288 | |
| 289 | 289 | |
| 290 | 290 | // https://code.spip.net/@_sqlite_func_rand |
| 291 | 291 | function _sqlite_func_rand() { |
| 292 | - return rand(); |
|
| 292 | + return rand(); |
|
| 293 | 293 | } |
| 294 | 294 | |
| 295 | 295 | |
| 296 | 296 | // https://code.spip.net/@_sqlite_func_right |
| 297 | 297 | function _sqlite_func_right($s, $length) { |
| 298 | - return substr($s, 0 - $length); |
|
| 298 | + return substr($s, 0 - $length); |
|
| 299 | 299 | } |
| 300 | 300 | |
| 301 | 301 | |
| 302 | 302 | // https://code.spip.net/@_sqlite_func_regexp_match |
| 303 | 303 | function _sqlite_func_regexp_match($cherche, $quoi) { |
| 304 | - // optimiser un cas tres courant avec les requetes en base |
|
| 305 | - if (!$quoi and !strlen($quoi)) { |
|
| 306 | - return false; |
|
| 307 | - } |
|
| 308 | - // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 309 | - $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 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; |
|
| 304 | + // optimiser un cas tres courant avec les requetes en base |
|
| 305 | + if (!$quoi and !strlen($quoi)) { |
|
| 306 | + return false; |
|
| 307 | + } |
|
| 308 | + // il faut enlever un niveau d'echappement pour être homogène à mysql |
|
| 309 | + $cherche = str_replace('\\\\', '\\', $cherche); |
|
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | } |