@@ -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 | /** |
@@ -37,94 +37,94 @@ discard block |
||
| 37 | 37 | */ |
| 38 | 38 | function _sqlite_init_functions(&$sqlite) { |
| 39 | 39 | |
| 40 | - if (!$sqlite) { |
|
| 41 | - return false; |
|
| 42 | - } |
|
| 40 | + if (!$sqlite) { |
|
| 41 | + return false; |
|
| 42 | + } |
|
| 43 | 43 | |
| 44 | 44 | |
| 45 | - $fonctions = array( |
|
| 46 | - // A |
|
| 47 | - 'ACOS' => array('acos', 1), |
|
| 48 | - 'ASIN' => array('asin', 1), |
|
| 49 | - 'ATAN' => array('atan', 1), // mysql accepte 2 params comme atan2… hum ? |
|
| 50 | - 'ATAN2' => array('atan2', 2), |
|
| 45 | + $fonctions = array( |
|
| 46 | + // A |
|
| 47 | + 'ACOS' => array('acos', 1), |
|
| 48 | + 'ASIN' => array('asin', 1), |
|
| 49 | + 'ATAN' => array('atan', 1), // mysql accepte 2 params comme atan2… hum ? |
|
| 50 | + 'ATAN2' => array('atan2', 2), |
|
| 51 | 51 | |
| 52 | - // C |
|
| 53 | - 'CEIL' => array('_sqlite_func_ceil', 1), |
|
| 54 | - 'CONCAT' => array('_sqlite_func_concat', -1), |
|
| 55 | - 'COS' => array('cos', 1), |
|
| 52 | + // C |
|
| 53 | + 'CEIL' => array('_sqlite_func_ceil', 1), |
|
| 54 | + 'CONCAT' => array('_sqlite_func_concat', -1), |
|
| 55 | + 'COS' => array('cos', 1), |
|
| 56 | 56 | |
| 57 | - // D |
|
| 58 | - 'DATE_FORMAT' => array('_sqlite_func_strftime', 2), |
|
| 59 | - 'DAYOFMONTH' => array('_sqlite_func_dayofmonth', 1), |
|
| 60 | - 'DEGREES' => array('rad2deg', 1), |
|
| 57 | + // D |
|
| 58 | + 'DATE_FORMAT' => array('_sqlite_func_strftime', 2), |
|
| 59 | + 'DAYOFMONTH' => array('_sqlite_func_dayofmonth', 1), |
|
| 60 | + 'DEGREES' => array('rad2deg', 1), |
|
| 61 | 61 | |
| 62 | - // E |
|
| 63 | - 'EXTRAIRE_MULTI' => array('_sqlite_func_extraire_multi', 2), // specifique a SPIP/sql_multi() |
|
| 64 | - 'EXP' => array('exp', 1), |
|
| 62 | + // E |
|
| 63 | + 'EXTRAIRE_MULTI' => array('_sqlite_func_extraire_multi', 2), // specifique a SPIP/sql_multi() |
|
| 64 | + 'EXP' => array('exp', 1), |
|
| 65 | 65 | |
| 66 | - // F |
|
| 67 | - 'FIND_IN_SET' => array('_sqlite_func_find_in_set', 2), |
|
| 68 | - 'FLOOR' => array('_sqlite_func_floor', 1), |
|
| 66 | + // F |
|
| 67 | + 'FIND_IN_SET' => array('_sqlite_func_find_in_set', 2), |
|
| 68 | + 'FLOOR' => array('_sqlite_func_floor', 1), |
|
| 69 | 69 | |
| 70 | - // I |
|
| 71 | - 'IF' => array('_sqlite_func_if', 3), |
|
| 72 | - 'INSERT' => array('_sqlite_func_insert', 4), |
|
| 73 | - 'INSTR' => array('_sqlite_func_instr', 2), |
|
| 70 | + // I |
|
| 71 | + 'IF' => array('_sqlite_func_if', 3), |
|
| 72 | + 'INSERT' => array('_sqlite_func_insert', 4), |
|
| 73 | + 'INSTR' => array('_sqlite_func_instr', 2), |
|
| 74 | 74 | |
| 75 | - // L |
|
| 76 | - 'LEAST' => array('_sqlite_func_least', 3), |
|
| 77 | - '_LEFT' => array('_sqlite_func_left', 2), |
|
| 75 | + // L |
|
| 76 | + 'LEAST' => array('_sqlite_func_least', 3), |
|
| 77 | + '_LEFT' => array('_sqlite_func_left', 2), |
|
| 78 | 78 | # 'LENGTH' => array('strlen', 1), // present v1.0.4 |
| 79 | 79 | # 'LOWER' => array('strtolower', 1), // present v2.4 |
| 80 | 80 | # 'LTRIM' => array('ltrim', 1), // present |
| 81 | 81 | |
| 82 | - // N |
|
| 83 | - 'NOW' => array('_sqlite_func_now', 0), |
|
| 82 | + // N |
|
| 83 | + 'NOW' => array('_sqlite_func_now', 0), |
|
| 84 | 84 | |
| 85 | - // M |
|
| 86 | - 'MD5' => array('md5', 1), |
|
| 87 | - 'MONTH' => array('_sqlite_func_month', 1), |
|
| 85 | + // M |
|
| 86 | + 'MD5' => array('md5', 1), |
|
| 87 | + 'MONTH' => array('_sqlite_func_month', 1), |
|
| 88 | 88 | |
| 89 | - // P |
|
| 90 | - 'PREG_REPLACE' => array('_sqlite_func_preg_replace', 3), |
|
| 89 | + // P |
|
| 90 | + 'PREG_REPLACE' => array('_sqlite_func_preg_replace', 3), |
|
| 91 | 91 | |
| 92 | - // R |
|
| 93 | - 'RADIANS' => array('deg2rad', 1), |
|
| 94 | - 'RAND' => array('_sqlite_func_rand', 0), // sinon random() v2.4 |
|
| 95 | - 'REGEXP' => array('_sqlite_func_regexp_match', 2), // critere REGEXP supporte a partir de v3.3.2 |
|
| 96 | - 'RIGHT' => array('_sqlite_func_right', 2), |
|
| 92 | + // R |
|
| 93 | + 'RADIANS' => array('deg2rad', 1), |
|
| 94 | + 'RAND' => array('_sqlite_func_rand', 0), // sinon random() v2.4 |
|
| 95 | + 'REGEXP' => array('_sqlite_func_regexp_match', 2), // critere REGEXP supporte a partir de v3.3.2 |
|
| 96 | + 'RIGHT' => array('_sqlite_func_right', 2), |
|
| 97 | 97 | # 'RTRIM' => array('rtrim', 1), // present |
| 98 | 98 | |
| 99 | - // S |
|
| 100 | - 'SETTYPE' => array('settype', 2), // CAST present en v3.2.3 |
|
| 101 | - 'SIN' => array('sin', 1), |
|
| 102 | - 'SQRT' => array('sqrt', 1), |
|
| 103 | - 'SUBSTRING' => array('_sqlite_func_substring' /*, 3*/), // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 99 | + // S |
|
| 100 | + 'SETTYPE' => array('settype', 2), // CAST present en v3.2.3 |
|
| 101 | + 'SIN' => array('sin', 1), |
|
| 102 | + 'SQRT' => array('sqrt', 1), |
|
| 103 | + 'SUBSTRING' => array('_sqlite_func_substring' /*, 3*/), // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0 |
|
| 104 | 104 | |
| 105 | - // T |
|
| 106 | - 'TAN' => array('tan', 1), |
|
| 107 | - 'TIMESTAMPDIFF' => array('_sqlite_timestampdiff' /*, 3*/), |
|
| 108 | - 'TO_DAYS' => array('_sqlite_func_to_days', 1), |
|
| 105 | + // T |
|
| 106 | + 'TAN' => array('tan', 1), |
|
| 107 | + 'TIMESTAMPDIFF' => array('_sqlite_timestampdiff' /*, 3*/), |
|
| 108 | + 'TO_DAYS' => array('_sqlite_func_to_days', 1), |
|
| 109 | 109 | # 'TRIM' => array('trim', 1), // present |
| 110 | 110 | |
| 111 | - // U |
|
| 112 | - 'UNIX_TIMESTAMP' => array('_sqlite_func_unix_timestamp', 1), |
|
| 111 | + // U |
|
| 112 | + 'UNIX_TIMESTAMP' => array('_sqlite_func_unix_timestamp', 1), |
|
| 113 | 113 | # 'UPPER' => array('strtoupper', 1), // present v2.4 |
| 114 | 114 | |
| 115 | - // V |
|
| 116 | - 'VIDE' => array('_sqlite_func_vide', 0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 115 | + // V |
|
| 116 | + 'VIDE' => array('_sqlite_func_vide', 0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide() |
|
| 117 | 117 | |
| 118 | - // Y |
|
| 119 | - 'YEAR' => array('_sqlite_func_year', 1) |
|
| 120 | - ); |
|
| 118 | + // Y |
|
| 119 | + 'YEAR' => array('_sqlite_func_year', 1) |
|
| 120 | + ); |
|
| 121 | 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,15 +145,15 @@ discard block |
||
| 145 | 145 | * |
| 146 | 146 | **/ |
| 147 | 147 | function _sqlite_add_function(&$sqlite, &$f, &$r) { |
| 148 | - if (_sqlite_is_version(3, $sqlite)) { |
|
| 149 | - isset($r[1]) |
|
| 150 | - ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 151 | - : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 152 | - } else { |
|
| 153 | - isset($r[1]) |
|
| 154 | - ? sqlite_create_function($sqlite, $f, $r[0], $r[1]) |
|
| 155 | - : sqlite_create_function($sqlite, $f, $r[0]); |
|
| 156 | - } |
|
| 148 | + if (_sqlite_is_version(3, $sqlite)) { |
|
| 149 | + isset($r[1]) |
|
| 150 | + ? $sqlite->sqliteCreateFunction($f, $r[0], $r[1]) |
|
| 151 | + : $sqlite->sqliteCreateFunction($f, $r[0]); |
|
| 152 | + } else { |
|
| 153 | + isset($r[1]) |
|
| 154 | + ? sqlite_create_function($sqlite, $f, $r[0], $r[1]) |
|
| 155 | + : sqlite_create_function($sqlite, $f, $r[0]); |
|
| 156 | + } |
|
| 157 | 157 | } |
| 158 | 158 | |
| 159 | 159 | // |
@@ -162,43 +162,43 @@ discard block |
||
| 162 | 162 | // |
| 163 | 163 | |
| 164 | 164 | function _sqlite_func_ceil($a) { |
| 165 | - return ceil($a); |
|
| 165 | + return ceil($a); |
|
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | // http://code.spip.net/@_sqlite_func_concat |
| 169 | 169 | function _sqlite_func_concat() { |
| 170 | - $args = func_get_args(); |
|
| 170 | + $args = func_get_args(); |
|
| 171 | 171 | |
| 172 | - return join('', $args); |
|
| 172 | + return join('', $args); |
|
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | |
| 176 | 176 | // http://code.spip.net/@_sqlite_func_dayofmonth |
| 177 | 177 | function _sqlite_func_dayofmonth($d) { |
| 178 | - return _sqlite_func_date("d", $d); |
|
| 178 | + return _sqlite_func_date("d", $d); |
|
| 179 | 179 | } |
| 180 | 180 | |
| 181 | 181 | |
| 182 | 182 | // http://code.spip.net/@_sqlite_func_find_in_set |
| 183 | 183 | function _sqlite_func_find_in_set($num, $set) { |
| 184 | - $rank = 0; |
|
| 185 | - foreach (explode(",", $set) as $v) { |
|
| 186 | - if ($v == $num) { |
|
| 187 | - return (++$rank); |
|
| 188 | - } |
|
| 189 | - $rank++; |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return 0; |
|
| 184 | + $rank = 0; |
|
| 185 | + foreach (explode(",", $set) as $v) { |
|
| 186 | + if ($v == $num) { |
|
| 187 | + return (++$rank); |
|
| 188 | + } |
|
| 189 | + $rank++; |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return 0; |
|
| 193 | 193 | } |
| 194 | 194 | |
| 195 | 195 | function _sqlite_func_floor($a) { |
| 196 | - return floor($a); |
|
| 196 | + return floor($a); |
|
| 197 | 197 | } |
| 198 | 198 | |
| 199 | 199 | // http://code.spip.net/@_sqlite_func_if |
| 200 | 200 | function _sqlite_func_if($bool, $oui, $non) { |
| 201 | - return ($bool) ? $oui : $non; |
|
| 201 | + return ($bool) ? $oui : $non; |
|
| 202 | 202 | } |
| 203 | 203 | |
| 204 | 204 | |
@@ -209,59 +209,59 @@ discard block |
||
| 209 | 209 | */ |
| 210 | 210 | // http://code.spip.net/@_sqlite_func_insert |
| 211 | 211 | function _sqlite_func_insert($s, $index, $longueur, $chaine) { |
| 212 | - return |
|
| 213 | - substr($s, 0, $index) |
|
| 214 | - . $chaine |
|
| 215 | - . substr(substr($s, $index), $longueur); |
|
| 212 | + return |
|
| 213 | + substr($s, 0, $index) |
|
| 214 | + . $chaine |
|
| 215 | + . substr(substr($s, $index), $longueur); |
|
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | |
| 219 | 219 | // http://code.spip.net/@_sqlite_func_instr |
| 220 | 220 | function _sqlite_func_instr($s, $search) { |
| 221 | - return strpos($s, $search); |
|
| 221 | + return strpos($s, $search); |
|
| 222 | 222 | } |
| 223 | 223 | |
| 224 | 224 | |
| 225 | 225 | // http://code.spip.net/@_sqlite_func_least |
| 226 | 226 | function _sqlite_func_least() { |
| 227 | - $arg_list = func_get_args(); |
|
| 228 | - $least = min($arg_list); |
|
| 227 | + $arg_list = func_get_args(); |
|
| 228 | + $least = min($arg_list); |
|
| 229 | 229 | |
| 230 | - #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG); |
|
| 231 | - return $least; |
|
| 230 | + #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG); |
|
| 231 | + return $least; |
|
| 232 | 232 | } |
| 233 | 233 | |
| 234 | 234 | |
| 235 | 235 | // http://code.spip.net/@_sqlite_func_left |
| 236 | 236 | function _sqlite_func_left($s, $lenght) { |
| 237 | - return substr($s, $lenght); |
|
| 237 | + return substr($s, $lenght); |
|
| 238 | 238 | } |
| 239 | 239 | |
| 240 | 240 | |
| 241 | 241 | // http://code.spip.net/@_sqlite_func_now |
| 242 | 242 | function _sqlite_func_now() { |
| 243 | - static $now = null; |
|
| 244 | - if (is_null($now)) { |
|
| 245 | - $now = date("Y-m-d H:i:s"); |
|
| 246 | - } |
|
| 243 | + static $now = null; |
|
| 244 | + if (is_null($now)) { |
|
| 245 | + $now = date("Y-m-d H:i:s"); |
|
| 246 | + } |
|
| 247 | 247 | |
| 248 | - #spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG); |
|
| 249 | - return $now; |
|
| 248 | + #spip_log("Passage avec NOW : $now",'sqlite.'._LOG_DEBUG); |
|
| 249 | + return $now; |
|
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | |
| 253 | 253 | // http://code.spip.net/@_sqlite_func_month |
| 254 | 254 | function _sqlite_func_month($d) { |
| 255 | - return _sqlite_func_date("m", $d); |
|
| 255 | + return _sqlite_func_date("m", $d); |
|
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | |
| 259 | 259 | // http://code.spip.net/@_sqlite_func_preg_replace |
| 260 | 260 | function _sqlite_func_preg_replace($quoi, $cherche, $remplace) { |
| 261 | - $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 261 | + $return = preg_replace('%' . $cherche . '%', $remplace, $quoi); |
|
| 262 | 262 | |
| 263 | - #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 264 | - return $return; |
|
| 263 | + #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 264 | + return $return; |
|
| 265 | 265 | } |
| 266 | 266 | |
| 267 | 267 | /** |
@@ -272,57 +272,57 @@ discard block |
||
| 272 | 272 | * @return string, l'extrait trouve. |
| 273 | 273 | **/ |
| 274 | 274 | function _sqlite_func_extraire_multi($quoi, $lang) { |
| 275 | - if (!defined('_EXTRAIRE_MULTI')) { |
|
| 276 | - include_spip('inc/filtres'); |
|
| 277 | - } |
|
| 278 | - if (!function_exists('approcher_langue')) { |
|
| 279 | - include_spip('inc/lang'); |
|
| 280 | - } |
|
| 281 | - if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 282 | - foreach ($regs as $reg) { |
|
| 283 | - // chercher la version de la langue courante |
|
| 284 | - $trads = extraire_trads($reg[1]); |
|
| 285 | - if ($l = approcher_langue($trads, $lang)) { |
|
| 286 | - $trad = $trads[$l]; |
|
| 287 | - } else { |
|
| 288 | - $trad = reset($trads); |
|
| 289 | - } |
|
| 290 | - $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 291 | - } |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - return $quoi; |
|
| 275 | + if (!defined('_EXTRAIRE_MULTI')) { |
|
| 276 | + include_spip('inc/filtres'); |
|
| 277 | + } |
|
| 278 | + if (!function_exists('approcher_langue')) { |
|
| 279 | + include_spip('inc/lang'); |
|
| 280 | + } |
|
| 281 | + if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) { |
|
| 282 | + foreach ($regs as $reg) { |
|
| 283 | + // chercher la version de la langue courante |
|
| 284 | + $trads = extraire_trads($reg[1]); |
|
| 285 | + if ($l = approcher_langue($trads, $lang)) { |
|
| 286 | + $trad = $trads[$l]; |
|
| 287 | + } else { |
|
| 288 | + $trad = reset($trads); |
|
| 289 | + } |
|
| 290 | + $quoi = str_replace($reg[0], $trad, $quoi); |
|
| 291 | + } |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + return $quoi; |
|
| 295 | 295 | } |
| 296 | 296 | |
| 297 | 297 | |
| 298 | 298 | // http://code.spip.net/@_sqlite_func_rand |
| 299 | 299 | function _sqlite_func_rand() { |
| 300 | - return rand(); |
|
| 300 | + return rand(); |
|
| 301 | 301 | } |
| 302 | 302 | |
| 303 | 303 | |
| 304 | 304 | // http://code.spip.net/@_sqlite_func_right |
| 305 | 305 | function _sqlite_func_right($s, $length) { |
| 306 | - return substr($s, 0 - $length); |
|
| 306 | + return substr($s, 0 - $length); |
|
| 307 | 307 | } |
| 308 | 308 | |
| 309 | 309 | |
| 310 | 310 | // http://code.spip.net/@_sqlite_func_regexp_match |
| 311 | 311 | function _sqlite_func_regexp_match($cherche, $quoi) { |
| 312 | - // optimiser un cas tres courant avec les requetes en base |
|
| 313 | - if (!$quoi and !strlen($quoi)) { |
|
| 314 | - return false; |
|
| 315 | - } |
|
| 316 | - $u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u'; |
|
| 317 | - $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 318 | - |
|
| 319 | - #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 320 | - return $return; |
|
| 312 | + // optimiser un cas tres courant avec les requetes en base |
|
| 313 | + if (!$quoi and !strlen($quoi)) { |
|
| 314 | + return false; |
|
| 315 | + } |
|
| 316 | + $u = isset($GLOBALS['meta']['pcre_u']) ? $GLOBALS['meta']['pcre_u'] : 'u'; |
|
| 317 | + $return = preg_match('%' . $cherche . '%imsS' . $u, $quoi); |
|
| 318 | + |
|
| 319 | + #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG); |
|
| 320 | + return $return; |
|
| 321 | 321 | } |
| 322 | 322 | |
| 323 | 323 | // http://code.spip.net/@_sqlite_func_strftime |
| 324 | 324 | function _sqlite_func_strftime($date, $conv) { |
| 325 | - return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 325 | + return strftime($conv, is_int($date) ? $date : strtotime($date)); |
|
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | /** |
@@ -334,21 +334,21 @@ discard block |
||
| 334 | 334 | * @return int |
| 335 | 335 | */ |
| 336 | 336 | function _sqlite_func_to_days($d) { |
| 337 | - static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 338 | - $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 337 | + static $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01 |
|
| 338 | + $result = $offset + (int)ceil(_sqlite_func_unix_timestamp($d) / (24 * 3600)); |
|
| 339 | 339 | |
| 340 | - #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 341 | - return $result; |
|
| 340 | + #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG); |
|
| 341 | + return $result; |
|
| 342 | 342 | } |
| 343 | 343 | |
| 344 | 344 | function _sqlite_func_substring($string, $start, $len = null) { |
| 345 | - // SQL compte a partir de 1, php a partir de 0 |
|
| 346 | - $start = ($start > 0) ? $start - 1 : $start; |
|
| 347 | - if (is_null($len)) { |
|
| 348 | - return substr($string, $start); |
|
| 349 | - } else { |
|
| 350 | - return substr($string, $start, $len); |
|
| 351 | - } |
|
| 345 | + // SQL compte a partir de 1, php a partir de 0 |
|
| 346 | + $start = ($start > 0) ? $start - 1 : $start; |
|
| 347 | + if (is_null($len)) { |
|
| 348 | + return substr($string, $start); |
|
| 349 | + } else { |
|
| 350 | + return substr($string, $start, $len); |
|
| 351 | + } |
|
| 352 | 352 | } |
| 353 | 353 | |
| 354 | 354 | /** |
@@ -362,61 +362,61 @@ discard block |
||
| 362 | 362 | * @return int |
| 363 | 363 | */ |
| 364 | 364 | function _sqlite_timestampdiff($unit, $date1, $date2) { |
| 365 | - $d1 = date_create($date1); |
|
| 366 | - $d2 = date_create($date2); |
|
| 367 | - $diff = date_diff($d1, $d2); |
|
| 368 | - $inv = $diff->invert ? -1 : 1; |
|
| 369 | - switch ($unit) { |
|
| 370 | - case "YEAR": |
|
| 371 | - return $inv * $diff->y; |
|
| 372 | - case "QUARTER": |
|
| 373 | - return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 374 | - case "MONTH": |
|
| 375 | - return $inv * (12 * $diff->y + $diff->m); |
|
| 376 | - case "WEEK": |
|
| 377 | - return $inv * intval(floor($diff->days / 7)); |
|
| 378 | - case "DAY": |
|
| 379 | - #var_dump($inv*$diff->days); |
|
| 380 | - return $inv * $diff->days; |
|
| 381 | - case "HOUR": |
|
| 382 | - return $inv * (24 * $diff->days + $diff->h); |
|
| 383 | - case "MINUTE": |
|
| 384 | - return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 385 | - case "SECOND": |
|
| 386 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 387 | - case "MICROSECOND": |
|
| 388 | - return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1000000; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - return 0; |
|
| 365 | + $d1 = date_create($date1); |
|
| 366 | + $d2 = date_create($date2); |
|
| 367 | + $diff = date_diff($d1, $d2); |
|
| 368 | + $inv = $diff->invert ? -1 : 1; |
|
| 369 | + switch ($unit) { |
|
| 370 | + case "YEAR": |
|
| 371 | + return $inv * $diff->y; |
|
| 372 | + case "QUARTER": |
|
| 373 | + return $inv * (4 * $diff->y + intval(floor($diff->m / 3))); |
|
| 374 | + case "MONTH": |
|
| 375 | + return $inv * (12 * $diff->y + $diff->m); |
|
| 376 | + case "WEEK": |
|
| 377 | + return $inv * intval(floor($diff->days / 7)); |
|
| 378 | + case "DAY": |
|
| 379 | + #var_dump($inv*$diff->days); |
|
| 380 | + return $inv * $diff->days; |
|
| 381 | + case "HOUR": |
|
| 382 | + return $inv * (24 * $diff->days + $diff->h); |
|
| 383 | + case "MINUTE": |
|
| 384 | + return $inv * ((24 * $diff->days + $diff->h) * 60 + $diff->i); |
|
| 385 | + case "SECOND": |
|
| 386 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s); |
|
| 387 | + case "MICROSECOND": |
|
| 388 | + return $inv * (((24 * $diff->days + $diff->h) * 60 + $diff->i) * 60 + $diff->s) * 1000000; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + return 0; |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | // http://code.spip.net/@_sqlite_func_unix_timestamp |
| 395 | 395 | function _sqlite_func_unix_timestamp($d) { |
| 396 | - static $mem = array(); |
|
| 397 | - static $n = 0; |
|
| 398 | - if (isset($mem[$d])) { |
|
| 399 | - return $mem[$d]; |
|
| 400 | - } |
|
| 401 | - if ($n++ > 100) { |
|
| 402 | - $mem = array(); |
|
| 403 | - $n = 0; |
|
| 404 | - } |
|
| 405 | - |
|
| 406 | - //2005-12-02 20:53:53 |
|
| 407 | - #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 408 | - if (!$d) { |
|
| 409 | - return $mem[$d] = mktime(); |
|
| 410 | - } |
|
| 411 | - |
|
| 412 | - // une pile plus grosse n'accelere pas le calcul |
|
| 413 | - return $mem[$d] = strtotime($d); |
|
| 396 | + static $mem = array(); |
|
| 397 | + static $n = 0; |
|
| 398 | + if (isset($mem[$d])) { |
|
| 399 | + return $mem[$d]; |
|
| 400 | + } |
|
| 401 | + if ($n++ > 100) { |
|
| 402 | + $mem = array(); |
|
| 403 | + $n = 0; |
|
| 404 | + } |
|
| 405 | + |
|
| 406 | + //2005-12-02 20:53:53 |
|
| 407 | + #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG); |
|
| 408 | + if (!$d) { |
|
| 409 | + return $mem[$d] = mktime(); |
|
| 410 | + } |
|
| 411 | + |
|
| 412 | + // une pile plus grosse n'accelere pas le calcul |
|
| 413 | + return $mem[$d] = strtotime($d); |
|
| 414 | 414 | } |
| 415 | 415 | |
| 416 | 416 | |
| 417 | 417 | // http://code.spip.net/@_sqlite_func_year |
| 418 | 418 | function _sqlite_func_year($d) { |
| 419 | - return _sqlite_func_date("Y", $d); |
|
| 419 | + return _sqlite_func_date("Y", $d); |
|
| 420 | 420 | } |
| 421 | 421 | |
| 422 | 422 | /** |
@@ -430,23 +430,23 @@ discard block |
||
| 430 | 430 | * @return int |
| 431 | 431 | */ |
| 432 | 432 | function _sqlite_func_date($quoi, $d) { |
| 433 | - static $mem = array(); |
|
| 434 | - static $n = 0; |
|
| 435 | - if (isset($mem[$d])) { |
|
| 436 | - return $mem[$d][$quoi]; |
|
| 437 | - } |
|
| 438 | - if ($n++ > 100) { |
|
| 439 | - $mem = array(); |
|
| 440 | - $n = 0; |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - $dec = date("Y-m-d", _sqlite_func_unix_timestamp($d)); |
|
| 444 | - $mem[$d] = array("Y" => substr($dec, 0, 4), "m" => substr($dec, 5, 2), "d" => substr($dec, 8, 2)); |
|
| 445 | - |
|
| 446 | - return $mem[$d][$quoi]; |
|
| 433 | + static $mem = array(); |
|
| 434 | + static $n = 0; |
|
| 435 | + if (isset($mem[$d])) { |
|
| 436 | + return $mem[$d][$quoi]; |
|
| 437 | + } |
|
| 438 | + if ($n++ > 100) { |
|
| 439 | + $mem = array(); |
|
| 440 | + $n = 0; |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + $dec = date("Y-m-d", _sqlite_func_unix_timestamp($d)); |
|
| 444 | + $mem[$d] = array("Y" => substr($dec, 0, 4), "m" => substr($dec, 5, 2), "d" => substr($dec, 8, 2)); |
|
| 445 | + |
|
| 446 | + return $mem[$d][$quoi]; |
|
| 447 | 447 | } |
| 448 | 448 | |
| 449 | 449 | // http://code.spip.net/@_sqlite_func_vide |
| 450 | 450 | function _sqlite_func_vide() { |
| 451 | - return; |
|
| 451 | + return; |
|
| 452 | 452 | } |