ea088275d26cb815ed02e990d43d3309a8441f23
[ptitvelo/web/www.git] / www / ecrire / req / sqlite_fonctions.php
1 <?php
2
3 /***************************************************************************\
4 * SPIP, Systeme de publication pour l'internet *
5 * *
6 * Copyright (c) 2001-2012 *
7 * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
8 * *
9 * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
10 * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
11 \***************************************************************************/
12
13
14 if (!defined('_ECRIRE_INC_VERSION')) return;
15
16 /*
17 * Des fonctions pour les requetes SQL
18 *
19 * Voir la liste des fonctions natives : http://www.sqlite.org/lang_corefunc.html
20 * Et la liste des evolutions pour : http://sqlite.org/changes.html
21 *
22 */
23 // http://doc.spip.org/@_sqlite_init_functions
24 function _sqlite_init_functions(&$sqlite){
25
26 if (!$sqlite) return false;
27
28
29 $fonctions = array(
30 'CONCAT' => array( '_sqlite_func_concat' ,2),
31 'CEIL' => array( '_sqlite_func_ceil', 1), // absent de sqlite2
32
33 'DATE_FORMAT' => array( '_sqlite_func_strftime' ,2),
34 'DAYOFMONTH' => array( '_sqlite_func_dayofmonth' ,1),
35
36 'EXTRAIRE_MULTI' => array( '_sqlite_func_extraire_multi', 2), // specifique a SPIP/sql_multi()
37 'EXP' => array( 'exp' ,1),//exponentielle
38 'FIND_IN_SET' => array( '_sqlite_func_find_in_set' ,2),
39 'FLOOR' => array( '_sqlite_func_floor', 1), // absent de sqlite2
40
41 'IF' => array( '_sqlite_func_if' ,3),
42 'INSERT' => array( '_sqlite_func_insert' ,4),
43 'INSTR' => array( '_sqlite_func_instr' ,2),
44
45 'LEAST' => array( '_sqlite_func_least' ,3),
46 '_LEFT' => array( '_sqlite_func_left' ,2),
47 # 'LENGTH' => array( 'strlen' ,1), // present v1.0.4
48 # 'LOWER' => array( 'strtolower' ,1), // present v2.4
49 # 'LTRIM' => array( 'ltrim' ,1), // present en theorie
50
51 'NOW' => array( '_sqlite_func_now' ,0),
52
53 'MD5' => array( 'md5' ,1),
54 'MONTH' => array( '_sqlite_func_month' ,1),
55
56 'PREG_REPLACE' => array( '_sqlite_func_preg_replace' ,3),
57
58 'RAND' => array( '_sqlite_func_rand' ,0), // sinon random() v2.4
59 'REGEXP' => array( '_sqlite_func_regexp_match' ,2), // critere REGEXP supporte a partir de v3.3.2
60 //'REGEXP_MATCH' => array( '_sqlite_func_regexp_match' ,2), // critere REGEXP supporte a partir de v3.3.2
61
62 'RIGHT' => array( '_sqlite_func_right' ,2),
63 # 'RTRIM' => array( 'rtrim' ,1), // present en theorie
64
65 'SETTYPE' => array( 'settype' ,2), // CAST present en v3.2.3
66 'SQRT' => array( 'sqrt' ,1),
67 'SUBSTRING' => array( '_sqlite_func_substring' /*,3*/), // peut etre appelee avec 2 ou 3 arguments, index base 1 et non 0
68
69 'TO_DAYS' => array( '_sqlite_func_to_days' ,1),
70 # 'TRIM' => array( 'trim' ,1), // present en theorie
71
72 'UNIX_TIMESTAMP'=> array( '_sqlite_func_unix_timestamp' ,1),
73 # 'UPPER' => array( 'strtoupper' ,1), // present v2.4
74
75 'VIDE' => array( '_sqlite_func_vide' ,0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
76
77 'YEAR' => array( '_sqlite_func_year' ,1)
78 );
79
80
81 foreach ($fonctions as $f=>$r){
82 _sqlite_add_function($sqlite, $f, $r);
83 }
84
85 #spip_log('functions sqlite chargees ','sqlite.'._LOG_DEBUG);
86 }
87
88 // permet au besoin de charger des fonctions ailleurs par _sqlite_init_functions();
89 // http://doc.spip.org/@_sqlite_add_function
90 function _sqlite_add_function(&$sqlite, &$f, &$r){
91 if (_sqlite_is_version(3, $sqlite)){
92 isset($r[1])
93 ?$sqlite->sqliteCreateFunction($f, $r[0], $r[1])
94 :$sqlite->sqliteCreateFunction($f, $r[0]);
95 } else {
96 isset($r[1])
97 ?sqlite_create_function($sqlite, $f, $r[0], $r[1])
98 :sqlite_create_function($sqlite, $f, $r[0]);
99 }
100 }
101
102 //
103 // SQLite : fonctions sqlite -> php
104 // entre autre auteurs : mlebas
105 //
106
107 function _sqlite_func_ceil($a) {
108 return ceil($a);
109 }
110
111 // http://doc.spip.org/@_sqlite_func_concat
112 function _sqlite_func_concat () {
113 $args = func_get_args();
114 return join('',$args);
115 }
116
117
118 // http://doc.spip.org/@_sqlite_func_dayofmonth
119 function _sqlite_func_dayofmonth ($d) {
120 return date("d",_sqlite_func_unix_timestamp($d));
121 }
122
123
124 // http://doc.spip.org/@_sqlite_func_find_in_set
125 function _sqlite_func_find_in_set($num, $set) {
126 $rank=0;
127 foreach (explode(",",$set) as $v) {
128 if ($v == $num) return (++$rank);
129 $rank++;
130 }
131 return 0;
132 }
133
134 function _sqlite_func_floor($a) {
135 return floor($a);
136 }
137
138 // http://doc.spip.org/@_sqlite_func_if
139 function _sqlite_func_if ($bool, $oui, $non) {
140 return ($bool)?$oui:$non;
141 }
142
143
144 /*
145 * INSERT(chaine, index, longueur, chaine) MySQL
146 * Retourne une chaine de caracteres a partir d'une chaine dans laquelle "sschaine"
147 * a ete inseree a la position "index" en remplacant "longueur" caracteres.
148 */
149 // http://doc.spip.org/@_sqlite_func_insert
150 function _sqlite_func_insert ($s, $index, $longueur, $chaine) {
151 return
152 substr($s,0, $index)
153 . $chaine
154 . substr(substr($s, $index), $longueur);
155 }
156
157
158 // http://doc.spip.org/@_sqlite_func_instr
159 function _sqlite_func_instr ($s, $search) {
160 return strpos($s,$search);
161 }
162
163
164 // http://doc.spip.org/@_sqlite_func_least
165 function _sqlite_func_least () {
166 $arg_list = func_get_args();
167 $least = min($arg_list);
168 #spip_log("Passage avec LEAST : $least",'sqlite.'._LOG_DEBUG);
169 return $least;
170 }
171
172
173 // http://doc.spip.org/@_sqlite_func_left
174 function _sqlite_func_left ($s, $lenght) {
175 return substr($s,0,$lenght);
176 }
177
178
179 // http://doc.spip.org/@_sqlite_func_now
180 function _sqlite_func_now(){
181 $result = date("Y-m-d H:i:s");
182 #spip_log("Passage avec NOW : $result",'sqlite.'._LOG_DEBUG);
183 return $result;
184 }
185
186
187 // http://doc.spip.org/@_sqlite_func_month
188 function _sqlite_func_month ($d) {
189 return date("m",_sqlite_func_unix_timestamp($d));
190 }
191
192
193
194 // http://doc.spip.org/@_sqlite_func_preg_replace
195 function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
196 $return = preg_replace('%'.$cherche.'%', $remplace, $quoi);
197 #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
198 return $return;
199 }
200
201 /**
202 * Extrait une langue d'un texte <multi>[fr] xxx [en] yyy</multi>
203 *
204 * @param string $quoi le texte contenant ou non un multi
205 * @param string $lang la langue a extraire
206 * @return string, l'extrait trouve.
207 **/
208 function _sqlite_func_extraire_multi($quoi, $lang) {
209 if (!defined('_EXTRAIRE_MULTI'))
210 include_spip('inc/filtres');
211 if (!function_exists('approcher_langue'))
212 include_spip('inc/lang');
213 if (preg_match_all(_EXTRAIRE_MULTI, $quoi, $regs, PREG_SET_ORDER)) {
214 foreach ($regs as $reg) {
215 // chercher la version de la langue courante
216 $trads = extraire_trads($reg[1]);
217 if ($l = approcher_langue($trads, $lang)) {
218 $trad = $trads[$l];
219 } else {
220 $trad = reset($trads);
221 }
222 $quoi = str_replace($reg[0], $trad, $quoi);
223 }
224 }
225 return $quoi;
226 }
227
228
229 // http://doc.spip.org/@_sqlite_func_rand
230 function _sqlite_func_rand() {
231 return rand();
232 }
233
234
235 // http://doc.spip.org/@_sqlite_func_right
236 function _sqlite_func_right ($s, $length) {
237 return substr($s,0 - $length);
238 }
239
240
241 // http://doc.spip.org/@_sqlite_func_regexp_match
242 function _sqlite_func_regexp_match($cherche, $quoi) {
243 $return = preg_match('%'.$cherche.'%', $quoi);
244 #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'sqlite.'._LOG_DEBUG);
245 return $return;
246 }
247
248 // http://doc.spip.org/@_sqlite_func_strftime
249 function _sqlite_func_strftime($date, $conv){
250 return strftime($conv, is_int($date)?$date:strtotime($date));
251 }
252
253 /**
254 * Nombre de jour entre 0000-00-00 et $d
255 * http://doc.spip.org/@_sqlite_func_to_days
256 * cf http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_to-days
257 * @param string $d
258 * @return int
259 */
260 function _sqlite_func_to_days ($d) {
261 $offset = 719528; // nb de jour entre 0000-00-00 et timestamp 0=1970-01-01
262 $result = $offset+(int)ceil(_sqlite_func_unix_timestamp($d)/(24*3600));
263 #spip_log("Passage avec TO_DAYS : $d, $result",'sqlite.'._LOG_DEBUG);
264 return $result;
265 }
266
267 function _sqlite_func_substring($string,$start,$len=null){
268 // SQL compte a partir de 1, php a partir de 0
269 $start = ($start>0)?$start-1:$start;
270 if (is_null($len))
271 return substr($string,$start);
272 else
273 return substr($string,$start,$len);
274 }
275
276 // http://doc.spip.org/@_sqlite_func_unix_timestamp
277 function _sqlite_func_unix_timestamp($d) {
278 //2005-12-02 20:53:53
279 #spip_log("Passage avec UNIX_TIMESTAMP : $d",'sqlite.'._LOG_DEBUG);
280 // mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
281 if (!$d) return mktime();
282 return strtotime($d);
283 #preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+)\s*(?:([0-9]+)(?::([0-9]+)(?::([0-9]+))?)?)?;", $d, $f);
284 #return mktime($f[4],$f[5],$f[6],$f[2],$f[3],$f[1]);
285 }
286
287
288 // http://doc.spip.org/@_sqlite_func_year
289 function _sqlite_func_year ($d) {
290 return date("Y",_sqlite_func_unix_timestamp($d));
291 }
292
293
294 // http://doc.spip.org/@_sqlite_func_vide
295 function _sqlite_func_vide(){
296 return;
297 }
298
299
300
301 ?>