PageRenderTime 69ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/spip/ecrire/req/sqlite_fonctions.php

https://github.com/eyeswebcrea/espace-couture-sittler.fr
PHP | 277 lines | 145 code | 65 blank | 67 comment | 14 complexity | dbcbf6f7004a078621e675680dd1ba8a MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. <?php
  2. /***************************************************************************\
  3. * SPIP, Systeme de publication pour l'internet *
  4. * *
  5. * Copyright (c) 2001-2011 *
  6. * Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James *
  7. * *
  8. * Ce programme est un logiciel libre distribue sous licence GNU/GPL. *
  9. * Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne. *
  10. \***************************************************************************/
  11. if (!defined('_ECRIRE_INC_VERSION')) return;
  12. /*
  13. * Des fonctions pour les requetes SQL
  14. *
  15. * Voir la liste des fonctions natives : http://www.sqlite.org/lang_corefunc.html
  16. * Et la liste des evolutions pour : http://sqlite.org/changes.html
  17. *
  18. */
  19. // http://doc.spip.org/@_sqlite_init_functions
  20. function _sqlite_init_functions(&$sqlite){
  21. if (!$sqlite) return false;
  22. $fonctions = array(
  23. 'CONCAT' => array( '_sqlite_func_concat' ,2),
  24. 'CEIL' => array( '_sqlite_func_ceil', 1), // absent de sqlite2
  25. 'DATE_FORMAT' => array( '_sqlite_func_strftime' ,2),
  26. 'DAYOFMONTH' => array( '_sqlite_func_dayofmonth' ,1),
  27. 'EXP' => array( 'exp' ,1),//exponentielle
  28. 'FIND_IN_SET' => array( '_sqlite_func_find_in_set' ,2),
  29. 'FLOOR' => array( '_sqlite_func_floor', 1), // absent de sqlite2
  30. 'IF' => array( '_sqlite_func_if' ,3),
  31. 'INSERT' => array( '_sqlite_func_insert' ,4),
  32. 'INSTR' => array( '_sqlite_func_instr' ,2),
  33. 'LEAST' => array( '_sqlite_func_least' ,3),
  34. 'LEFT' => array( '_sqlite_func_left' ,2),
  35. # 'LENGTH' => array( 'strlen' ,1), // present v1.0.4
  36. # 'LOWER' => array( 'strtolower' ,1), // present v2.4
  37. # 'LTRIM' => array( 'ltrim' ,1), // present en theorie
  38. 'NOW' => array( '_sqlite_func_now' ,0),
  39. 'MD5' => array( 'md5' ,1),
  40. 'MONTH' => array( '_sqlite_func_month' ,1),
  41. 'PREG_REPLACE' => array( '_sqlite_func_preg_replace' ,3),
  42. 'RAND' => array( '_sqlite_func_rand' ,0), // sinon random() v2.4
  43. 'REGEXP' => array( '_sqlite_func_regexp_match' ,2), // critere REGEXP supporte a partir de v3.3.2
  44. //'REGEXP_MATCH' => array( '_sqlite_func_regexp_match' ,2), // critere REGEXP supporte a partir de v3.3.2
  45. 'RIGHT' => array( '_sqlite_func_right' ,2),
  46. # 'RTRIM' => array( 'rtrim' ,1), // present en theorie
  47. 'SETTYPE' => array( 'settype' ,2), // CAST present en v3.2.3
  48. 'SQRT' => array( 'sqrt' ,1),
  49. 'SUBSTRING' => array( 'substr' ,3),
  50. 'TO_DAYS' => array( '_sqlite_func_to_days' ,1),
  51. # 'TRIM' => array( 'trim' ,1), // present en theorie
  52. 'UNIX_TIMESTAMP'=> array( '_sqlite_func_unix_timestamp' ,1),
  53. # 'UPPER' => array( 'strtoupper' ,1), // present v2.4
  54. 'VIDE' => array( '_sqlite_func_vide' ,0), // du vide pour SELECT 0 as x ... ORDER BY x -> ORDER BY vide()
  55. 'YEAR' => array( '_sqlite_func_year' ,1)
  56. );
  57. foreach ($fonctions as $f=>$r){
  58. _sqlite_add_function($sqlite, $f, $r);
  59. }
  60. #spip_log('functions sqlite chargees ');
  61. }
  62. // permet au besoin de charger des fonctions ailleurs par _sqlite_init_functions();
  63. // http://doc.spip.org/@_sqlite_add_function
  64. function _sqlite_add_function(&$sqlite, &$f, &$r){
  65. if (_sqlite_is_version(3, $sqlite)){
  66. isset($r[1])
  67. ?$sqlite->sqliteCreateFunction($f, $r[0], $r[1])
  68. :$sqlite->sqliteCreateFunction($f, $r[0]);
  69. } else {
  70. isset($r[1])
  71. ?sqlite_create_function($sqlite, $f, $r[0], $r[1])
  72. :sqlite_create_function($sqlite, $f, $r[0]);
  73. }
  74. }
  75. //
  76. // SQLite : fonctions sqlite -> php
  77. // entre autre auteurs : mlebas
  78. //
  79. function _sqlite_func_ceil($a) {
  80. return ceil($a);
  81. }
  82. // http://doc.spip.org/@_sqlite_func_concat
  83. function _sqlite_func_concat ($a, $b) {
  84. return $a.$b;
  85. }
  86. // http://doc.spip.org/@_sqlite_func_dayofmonth
  87. function _sqlite_func_dayofmonth ($d) {
  88. if (!$d){
  89. $result = date("j");
  90. } else {
  91. preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+) .*$;", $d, $f);
  92. $result = $f[3];
  93. }
  94. #spip_log("Passage avec DAYOFMONTH : $d, $result",'debug');
  95. return $result;
  96. }
  97. // http://doc.spip.org/@_sqlite_func_find_in_set
  98. function _sqlite_func_find_in_set($num, $set) {
  99. $rank=0;
  100. foreach (explode(",",$set) as $v) {
  101. if ($v == $num) return (++$rank);
  102. $rank++;
  103. }
  104. return 0;
  105. }
  106. function _sqlite_func_floor($a) {
  107. return floor($a);
  108. }
  109. // http://doc.spip.org/@_sqlite_func_if
  110. function _sqlite_func_if ($bool, $oui, $non) {
  111. return ($bool)?$oui:$non;
  112. }
  113. /*
  114. * INSERT(chaine, index, longueur, chaine) MySQL
  115. * Retourne une chaine de caracteres a partir d'une chaine dans laquelle "sschaine"
  116. * a ete inseree a la position "index" en remplacant "longueur" caracteres.
  117. */
  118. // http://doc.spip.org/@_sqlite_func_insert
  119. function _sqlite_func_insert ($s, $index, $longueur, $chaine) {
  120. return
  121. substr($s,0, $index)
  122. . $chaine
  123. . substr(substr($s, $index), $longueur);
  124. }
  125. // http://doc.spip.org/@_sqlite_func_instr
  126. function _sqlite_func_instr ($s, $search) {
  127. return strpos($s,$search);
  128. }
  129. // http://doc.spip.org/@_sqlite_func_least
  130. function _sqlite_func_least () {
  131. $numargs = func_num_args();
  132. $arg_list = func_get_args();
  133. $least=$arg_list[0];
  134. for ($i = 0; $i < $numargs; $i++) {
  135. if ($arg_list[$i] < $least) $least=$arg_list[$i];
  136. }
  137. #spip_log("Passage avec LEAST : $least",'debug');
  138. return $least;
  139. }
  140. // http://doc.spip.org/@_sqlite_func_left
  141. function _sqlite_func_left ($s, $lenght) {
  142. return substr($s,$lenght);
  143. }
  144. // http://doc.spip.org/@_sqlite_func_now
  145. function _sqlite_func_now(){
  146. $result = date("Y-m-d H:i:s", strtotime("now"));
  147. #spip_log("Passage avec NOW : $result",'debug');
  148. return $result;
  149. }
  150. // http://doc.spip.org/@_sqlite_func_month
  151. function _sqlite_func_month ($d) {
  152. #spip_log("Passage avec MONTH : $d",'debug');
  153. if (!$d) return date("n");
  154. preg_match(";^([0-9]{4})-([0-9]+).*$;", $d, $f);
  155. return $f[2];
  156. }
  157. // http://doc.spip.org/@_sqlite_func_preg_replace
  158. function _sqlite_func_preg_replace($quoi, $cherche, $remplace) {
  159. $return = preg_replace('%'.$cherche.'%', $remplace, $quoi);
  160. #spip_log("preg_replace : $quoi, $cherche, $remplace, $return",'debug');
  161. return $return;
  162. }
  163. // http://doc.spip.org/@_sqlite_func_rand
  164. function _sqlite_func_rand() {
  165. return rand();
  166. }
  167. // http://doc.spip.org/@_sqlite_func_right
  168. function _sqlite_func_right ($s, $lenght) {
  169. return substr($s,0 - $lenght);
  170. }
  171. // http://doc.spip.org/@_sqlite_func_regexp_match
  172. function _sqlite_func_regexp_match($cherche, $quoi) {
  173. $return = preg_match('%'.$cherche.'%', $quoi);
  174. #spip_log("regexp_replace : $quoi, $cherche, $remplace, $return",'debug');
  175. return $return;
  176. }
  177. // http://doc.spip.org/@_sqlite_func_strftime
  178. function _sqlite_func_strftime($date, $conv){
  179. return strftime($conv, $date);
  180. }
  181. // http://doc.spip.org/@_sqlite_func_to_days
  182. function _sqlite_func_to_days ($d) {
  183. $result = date("z", _sqlite_func_unix_timestamp($d));
  184. #spip_log("Passage avec TO_DAYS : $d, $result",'debug');
  185. return $result;
  186. }
  187. // http://doc.spip.org/@_sqlite_func_unix_timestamp
  188. function _sqlite_func_unix_timestamp($d) {
  189. //2005-12-02 20:53:53
  190. #spip_log("Passage avec UNIX_TIMESTAMP : $d",'debug');
  191. // mktime ( [int hour [, int minute [, int second [, int month [, int day [, int year [, int is_dst]]]]]]] )
  192. if (!$d) return mktime();
  193. return strtotime($d);
  194. #preg_match(";^([0-9]{4})-([0-9]+)-([0-9]+)\s*(?:([0-9]+)(?::([0-9]+)(?::([0-9]+))?)?)?;", $d, $f);
  195. #return mktime($f[4],$f[5],$f[6],$f[2],$f[3],$f[1]);
  196. }
  197. // http://doc.spip.org/@_sqlite_func_year
  198. function _sqlite_func_year ($d) {
  199. if (!$d){
  200. $result = date("Y");
  201. } else {
  202. preg_match(";^([0-9]{4}).*$;", $d, $f);
  203. $result = $f[1];
  204. }
  205. spip_log("Passage avec YEAR : $d, $result",'debug');
  206. return $result;
  207. }
  208. // http://doc.spip.org/@_sqlite_func_vide
  209. function _sqlite_func_vide(){
  210. return;
  211. }
  212. ?>