xref: /plugin/sqlite/helper.php (revision ddcdcb90690feefe0a7de708417362849bbc1df6)
1<?php
2/**
3 * DokuWiki Plugin sqlite (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
15
16if (!defined('DOKU_EXT_SQLITE')) define('DOKU_EXT_SQLITE', 'sqlite');
17if (!defined('DOKU_EXT_PDO')) define('DOKU_EXT_PDO', 'pdo');
18
19
20class helper_plugin_sqlite extends DokuWiki_Plugin {
21    var $db     = null;
22    var $dbname = '';
23    var $extension = null;
24    function getInfo() {
25        return confToHash(dirname(__FILE__).'plugin.info.txt');
26    }
27
28    /**
29     * constructor
30     */
31    function helper_plugin_sqlite(){
32
33      if(!$this->extension)
34      {
35        if (!extension_loaded('pdo_sqlite')) {
36            $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
37            if(function_exists('dl')) @dl($prefix . 'pdo_sqlite.' . PHP_SHLIB_SUFFIX);
38        }
39
40        if(class_exists('pdo')){
41            $this->extension = DOKU_EXT_PDO;
42        }
43      }
44
45      if(!$this->extension)
46      {
47        if (!extension_loaded('sqlite')) {
48            $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' : '';
49            if(function_exists('dl')) @dl($prefix . 'sqlite.' . PHP_SHLIB_SUFFIX);
50        }
51
52        if(function_exists('sqlite_open')){
53           $this->extension = DOKU_EXT_SQLITE;
54        }
55      }
56
57      if(!$this->extension)
58
59      {
60        msg('SQLite & PDO SQLite support missing in this PHP install - plugin will not work',-1);
61      }
62    }
63
64    /**
65     * Initializes and opens the database
66     *
67     * Needs to be called right after loading this helper plugin
68     */
69    function init($dbname,$updatedir){
70        global $conf;
71
72        // check for already open DB
73        if($this->db){
74            if($this->dbname == $dbname){
75                // db already open
76                return true;
77            }
78            // close other db
79            if($this->extension == DOKU_EXT_SQLITE)
80            {
81              sqlite_close($this->db);
82            }
83            else
84            {
85              $this->db->close();
86            }
87            $this->db     = null;
88            $this->dbname = '';
89        }
90
91        $this->dbname = $dbname;
92
93        $fileextension = '.sqlite';
94
95        $this->dbfile = $conf['metadir'].'/'.$dbname.$fileextension;
96
97        $init   = (!@file_exists($this->dbfile) || ((int) @filesize($this->dbfile)) < 3);
98
99        //first line tell the format of db file http://marc.info/?l=sqlite-users&m=109383875408202
100        $firstline=file_get_contents($this->dbfile,false,null,0,15);
101
102        if($this->extension == DOKU_EXT_SQLITE)
103        {
104          if($firstline=='SQLite format 3'){
105              msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has a sqlite3 format instead of sqlite2 format.)",-1);
106              return false;
107          }
108
109          $error='';
110          $this->db = sqlite_open($this->dbfile, 0666, $error);
111          if(!$this->db){
112              msg("SQLite: failed to open SQLite '".$this->dbname."' database ($error)",-1);
113              return false;
114          }
115
116          // register our custom aggregate function
117          sqlite_create_aggregate($this->db,'group_concat',
118                                  array($this,'_sqlite_group_concat_step'),
119                                  array($this,'_sqlite_group_concat_finalize'), 2);
120        }
121        else
122        {
123          if($firstline!='SQLite format 3'){
124              msg("SQLite: failed to open SQLite '".$this->dbname."' database (DB has not a sqlite3 format.)",-1);
125              return false;
126          }
127
128          $dsn = 'sqlite:'.$this->dbfile;
129
130          try {
131              $this->db = new PDO($dsn);
132          } catch (PDOException $e) {
133            msg("SQLite: failed to open SQLite '".$this->dbname."' database (".$e->getMessage().")",-1);
134              return false;
135          }
136          $this->db->sqliteCreateAggregate('group_concat',
137                                  array($this,'_pdo_group_concat_step'),
138                                  array($this,'_pdo_group_concat_finalize'));
139        }
140
141        $this->_updatedb($init,$updatedir);
142        return true;
143    }
144
145    /**
146     * Return the current Database Version
147     */
148    function _currentDBversion(){
149        $sql = "SELECT val FROM opts WHERE opt = 'dbversion';";
150        $res = $this->query($sql);
151        if(!$res) return false;
152        $row = $this->res2row($res,0);
153        return (int) $row['val'];
154    }
155    /**
156     * Update the database if needed
157     *
158     * @param bool   $init      - true if this is a new database to initialize
159     * @param string $updatedir - Database update infos
160     */
161    function _updatedb($init,$updatedir)
162    {
163        if($init){
164
165            $current = 0;
166        }else{
167            $current = $this->_currentDBversion();
168            if(!$current){
169                msg("SQLite: no DB version found. '".$this->dbname."' DB probably broken.",-1);
170                return false;
171            }
172        }
173
174        // in case of init, add versioning table
175        if($init){
176            if(!$this->_runupdatefile(dirname(__FILE__).'/db.sql',0)){
177                  msg("SQLite: '".$this->dbname."' database upgrade failed for version ", -1);
178                return false;
179            }
180        }
181
182        $latest  = (int) trim(io_readFile($updatedir.'/latest.version'));
183
184        // all up to date?
185        if($current >= $latest) return true;
186        for($i=$current+1; $i<=$latest; $i++){
187            $file = sprintf($updatedir.'/update%04d.sql',$i);
188            if(file_exists($file)){
189                if(!$this->_runupdatefile($file,$i)){
190                    msg("SQLite: '".$this->dbname."' database upgrade failed for version ".$i, -1);
191                    return false;
192                }
193            }
194        }
195        return true;
196    }
197
198    /**
199     * Updates the database structure using the given file to
200     * the given version.
201     */
202    function _runupdatefile($file,$version){
203        $sql  = io_readFile($file,false);
204
205        $sql = explode(";",$sql);
206        array_unshift($sql,'BEGIN TRANSACTION');
207        array_push($sql,"INSERT OR REPLACE INTO opts (val,opt) VALUES ($version,'dbversion')");
208        array_push($sql,"COMMIT TRANSACTION");
209
210        foreach($sql as $s){
211            $s = preg_replace('!^\s*--.*$!m', '', $s);
212            $s = trim($s);
213            if(!$s) continue;
214
215
216            $res = $this->query("$s;");
217            if ($res === false) {
218              if($this->extension == DOKU_EXT_SQLITE)
219              {
220                sqlite_query($this->db, 'ROLLBACK TRANSACTION');
221                return false;
222              }
223              else
224              {
225                return false;
226              }
227            }
228        }
229
230        return ($version == $this->_currentDBversion());
231    }
232
233    /**
234     * Emulate ALTER TABLE
235     *
236     * The ALTER TABLE syntax is parsed and then emulated using a
237     * temporary table
238     *
239     * @author <jon@jenseng.com>
240     * @link   http://code.jenseng.com/db/
241     * @author Andreas Gohr <gohr@cosmocode.de>
242     */
243    function _altertable($table,$alterdefs){
244
245        // load original table definition SQL
246        $result = $this->query("SELECT sql,name,type
247                                  FROM sqlite_master
248                                 WHERE tbl_name = '$table'
249                                   AND type = 'table'");
250
251        if(($result === false) || ($this->extension == DOKU_EXT_SQLITE && sqlite_num_rows($result)<=0)){
252            msg("ALTER TABLE failed, no such table '".hsc($table)."'",-1);
253            return false;
254        }
255
256        if($this->extension == DOKU_EXT_SQLITE )
257        {
258          $row = sqlite_fetch_array($result);
259        }
260        else
261        {
262           $row = $result->fetch(PDO::FETCH_ASSOC);
263        }
264
265        if($row === false){
266            msg("ALTER TABLE failed, table '".hsc($table)."' had no master data",-1);
267            return false;
268        }
269
270
271        // prepare temporary table SQL
272        $tmpname = 't'.time();
273        $origsql = trim(preg_replace("/[\s]+/"," ",
274                        str_replace(",",", ",
275                        preg_replace('/\)$/',' )',
276                        preg_replace("/[\(]/","( ",$row['sql'],1)))));
277        $createtemptableSQL = 'CREATE TEMPORARY '.substr(trim(preg_replace("'".$table."'",$tmpname,$origsql,1)),6);
278        $createindexsql = array();
279
280        // load indexes to reapply later
281        $result = $this->query("SELECT sql,name,type
282                                  FROM sqlite_master
283                                 WHERE tbl_name = '$table'
284                                   AND type = 'index'");
285        if(!$result){
286            $indexes = array();
287        }else{
288            $indexes = $this->res2arr($result);
289        }
290
291
292        $i = 0;
293        $defs = preg_split("/[,]+/",$alterdefs,-1,PREG_SPLIT_NO_EMPTY);
294        $prevword = $table;
295        $oldcols = preg_split("/[,]+/",substr(trim($createtemptableSQL),strpos(trim($createtemptableSQL),'(')+1),-1,PREG_SPLIT_NO_EMPTY);
296        $newcols = array();
297
298        for($i=0;$i<count($oldcols);$i++){
299            $colparts = preg_split("/[\s]+/",$oldcols[$i],-1,PREG_SPLIT_NO_EMPTY);
300            $oldcols[$i] = $colparts[0];
301            $newcols[$colparts[0]] = $colparts[0];
302        }
303        $newcolumns = '';
304        $oldcolumns = '';
305        reset($newcols);
306        while(list($key,$val) = each($newcols)){
307            $newcolumns .= ($newcolumns?', ':'').$val;
308            $oldcolumns .= ($oldcolumns?', ':'').$key;
309        }
310        $copytotempsql = 'INSERT INTO '.$tmpname.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$table;
311        $dropoldsql = 'DROP TABLE '.$table;
312        $createtesttableSQL = $createtemptableSQL;
313
314        foreach($defs as $def){
315            $defparts = preg_split("/[\s]+/",$def,-1,PREG_SPLIT_NO_EMPTY);
316            $action = strtolower($defparts[0]);
317            switch($action){
318                case 'add':
319                    if(count($defparts) < 2){
320                        msg('ALTER TABLE: not enough arguments for ADD statement',-1);
321                        return false;
322                    }
323                    $createtesttableSQL = substr($createtesttableSQL,0,strlen($createtesttableSQL)-1).',';
324                    for($i=1;$i<count($defparts);$i++)
325                        $createtesttableSQL.=' '.$defparts[$i];
326                    $createtesttableSQL.=')';
327                    break;
328
329                case 'change':
330                    if(count($defparts) <= 3){
331                        msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').($defparts[2]?' '.$defparts[2]:'').'": syntax error',-1);
332                        return false;
333                    }
334
335                    if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){
336                        if($newcols[$defparts[1]] != $defparts[1]){
337                            msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1);
338                            return false;
339                        }
340                        $newcols[$defparts[1]] = $defparts[2];
341                        $nextcommapos = strpos($createtesttableSQL,',',$severpos);
342                        $insertval = '';
343                        for($i=2;$i<count($defparts);$i++)
344                            $insertval.=' '.$defparts[$i];
345                        if($nextcommapos)
346                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos).$insertval.substr($createtesttableSQL,$nextcommapos);
347                        else
348                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1)).$insertval.')';
349                    } else {
350                        msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1);
351                        return false;
352                    }
353                    break;
354                case 'drop':
355                    if(count($defparts) < 2){
356                        msg('ALTER TABLE: near "'.$defparts[0].($defparts[1]?' '.$defparts[1]:'').'": syntax error',-1);
357                        return false;
358                    }
359                    if($severpos = strpos($createtesttableSQL,' '.$defparts[1].' ')){
360                        $nextcommapos = strpos($createtesttableSQL,',',$severpos);
361                        if($nextcommapos)
362                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos).substr($createtesttableSQL,$nextcommapos + 1);
363                        else
364                            $createtesttableSQL = substr($createtesttableSQL,0,$severpos-(strpos($createtesttableSQL,',')?0:1) - 1).')';
365                        unset($newcols[$defparts[1]]);
366                    }else{
367                        msg('ALTER TABLE: unknown column "'.$defparts[1].'" in "'.$table.'"',-1);
368                        return false;
369                    }
370                    break;
371                default:
372                    msg('ALTER TABLE: near "'.$prevword.'": syntax error',-1);
373                    return false;
374            }
375            $prevword = $defparts[count($defparts)-1];
376        }
377
378        // this block of code generates a test table simply to verify that the
379        // columns specifed are valid in an sql statement
380        // this ensures that no reserved words are used as columns, for example
381        $res = $this->query($createtesttableSQL);
382        if($res === false) return false;
383
384        $droptempsql = 'DROP TABLE '.$tmpname;
385        $res = $this->query($droptempsql);
386        if($res === false) return false;
387
388
389        $createnewtableSQL = 'CREATE '.substr(trim(preg_replace("'".$tmpname."'",$table,$createtesttableSQL,1)),17);
390        $newcolumns = '';
391        $oldcolumns = '';
392        reset($newcols);
393        while(list($key,$val) = each($newcols)){
394            $newcolumns .= ($newcolumns?', ':'').$val;
395            $oldcolumns .= ($oldcolumns?', ':'').$key;
396        }
397
398        $copytonewsql = 'INSERT INTO '.$table.'('.$newcolumns.') SELECT '.$oldcolumns.' FROM '.$tmpname;
399
400        $res = $this->query($createtemptableSQL); //create temp table
401        if($res === false) return false;
402        $res = $this->query($copytotempsql); //copy to table
403        if($res === false) return false;
404        $res = $this->query($dropoldsql); //drop old table
405        if($res === false) return false;
406
407        $res = $this->query($createnewtableSQL); //recreate original table
408        if($res === false) return false;
409        $res = $this->query($copytonewsql); //copy back to original table
410        if($res === false) return false;
411
412        foreach($indexes as $index){ // readd indexes
413            $res = $this->query($index['sql']);
414            if($res === false) return false;
415        }
416
417        $res = $this->query($droptempsql); //drop temp table
418        if($res === false) return false;
419
420        return $res; // return a valid resource
421    }
422
423    /**
424     * Registers a User Defined Function for use in SQL statements
425     */
426    function create_function($function_name,$callback,$num_args){
427        if($this->extension == DOKU_EXT_SQLITE )
428        {
429          sqlite_create_function($this->db,$function_name,$callback,$num_args);
430        }
431        else
432        {
433          $this->db->sqliteCreateFunction($function_name,$callback,$num_args);
434        }
435    }
436
437    /**
438     * Execute a query with the given parameters.
439     *
440     * Takes care of escaping
441     *
442     * @param string $sql - the statement
443     * @param arguments...
444     */
445    function query(){
446        if(!$this->db) return false;
447
448        // get function arguments
449        $args = func_get_args();
450        $sql  = trim(array_shift($args));
451        $sql  = rtrim($sql,';');
452
453        if(!$sql){
454            msg('No SQL statement given',-1);
455            return false;
456        }
457
458        $argc = count($args);
459        if($argc > 0 && is_array($args[0])) {
460            $args = $args[0];
461            $argc = count($args);
462        }
463
464        // check number of arguments
465        if($argc < substr_count($sql,'?')){
466            msg('Not enough arguments passed for statement. '.
467                'Expected '.substr_count($sql,'?').' got '.
468                $argc.' - '.hsc($sql),-1);
469            return false;
470        }
471
472        // explode at wildcard, then join again
473        $parts = explode('?',$sql,$argc+1);
474        $args  = array_map(array($this,'quote_string'),$args);
475        $sql   = '';
476
477        while( ($part = array_shift($parts)) !== null ){
478            $sql .= $part;
479            $sql .= array_shift($args);
480        }
481
482        // intercept ALTER TABLE statements
483        $match = null;
484        if(preg_match('/^ALTER\s+TABLE\s+([\w\.]+)\s+(.*)/i',$sql,$match)){
485            return $this->_altertable($match[1],$match[2]);
486        }
487
488        // execute query
489        $err = '';
490        if($this->extension == DOKU_EXT_SQLITE )
491        {
492          $res = @sqlite_query($this->db,$sql,SQLITE_ASSOC,$err);
493          if($err){
494              msg($err.':<br /><pre>'.hsc($sql).'</pre>',-1);
495              return false;
496          }elseif(!$res){
497              msg(sqlite_error_string(sqlite_last_error($this->db)).
498                  ':<br /><pre>'.hsc($sql).'</pre>',-1);
499              return false;
500          }
501        }
502        else
503        {
504          $res = $this->db->query($sql);
505
506          if(!$res){
507            $err = $this->db->errorInfo();
508              msg($err[2].':<br /><pre>'.hsc($sql).'</pre>',-1);
509              return false;
510          }
511        }
512        return $res;
513    }
514
515    /**
516     * Returns a complete result set as array
517     */
518    function res2arr($res){
519        $data = array();
520        if($this->extension == DOKU_EXT_SQLITE )
521        {
522          if(!sqlite_num_rows($res)) return $data;
523          sqlite_rewind($res);
524          while(($row = sqlite_fetch_array($res, SQLITE_ASSOC)) !== false){
525              $data[] = $row;
526          }
527        }
528        else
529        {
530          if(!$res) return $data;
531          $data = $res->fetchAll(PDO::FETCH_ASSOC);
532          if(!count(data))
533          {
534            return false;
535          }
536        }
537        return $data;
538    }
539
540    /**
541     * Return the wanted row from a given result set as
542     * associative array
543     */
544    function res2row($res,$rownum=0){
545        if($this->extension == DOKU_EXT_SQLITE )
546        {
547          if(!@sqlite_seek($res,$rownum)){
548              return false;
549          }
550          return sqlite_fetch_array($res, SQLITE_ASSOC);
551        }
552        else
553        {
554          if(!$res)
555          {
556            return false;
557          }
558          return $res->fetch(PDO::FETCH_ASSOC,PDO::FETCH_ORI_ABS,$rownum);
559        }
560    }
561
562    /**
563     * Return the first value from the first row.
564     */
565    function res2single($res)
566    {
567      if($this->extension == DOKU_EXT_SQLITE )
568      {
569        return sqlite_fetch_single($res);
570      }
571      else
572      {
573        if(!$res)
574        {
575          return false;
576        }
577        $data = $res->fetch(PDO::FETCH_NUM,PDO::FETCH_ORI_ABS,0);
578        if(!count(data))
579        {
580          return false;
581        }
582        return $data[0];
583      }
584    }
585
586    /**
587     * Join the given values and quote them for SQL insertion
588     */
589    function quote_and_join($vals,$sep=',') {
590        $vals = array_map(array('helper_plugin_sqlite','quote_string'),$vals);
591        return join($sep,$vals);
592    }
593
594    /**
595     * Run sqlite_escape_string() on the given string and surround it
596     * with quotes
597     */
598    function quote_string($string){
599      if($this->extension == DOKU_EXT_SQLITE )
600      {
601        return "'".sqlite_escape_string($string)."'";
602      }
603      else
604      {
605        return $this->db->quote($string);
606      }
607    }
608
609
610    /**
611     * Escape string for sql
612     */
613    function escape_string($str)
614    {
615      if($this->extension == DOKU_EXT_SQLITE )
616      {
617        return sqlite_escape_string($str);
618      }
619      else
620      {
621        return trim($this->db->quote($str), "'");
622      }
623    }
624
625
626
627    /**
628    * Aggregation function for SQLite
629    *
630    * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
631    */
632    function _sqlite_group_concat_step(&$context, $string, $separator = ',') {
633         $context['sep'] = $separator;
634         $context['data'][] = $string;
635    }
636
637    /**
638    * Aggregation function for SQLite
639    *
640    * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
641    */
642    function _sqlite_group_concat_finalize(&$context) {
643         $context['data'] = array_unique($context['data']);
644         return join($context['sep'],$context['data']);
645    }
646
647
648    /**
649     * Aggregation function for SQLite via PDO
650     *
651     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
652     */
653    function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') {
654         if(is_null($context))
655         {
656           $context = array(
657             'sep'  => $separator,
658             'data' => array()
659             );
660         }
661
662         $context['data'][] = $string;
663         return $context;
664    }
665
666     /**
667     * Aggregation function for SQLite via PDO
668     *
669     * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine
670     */
671    function _pdo_group_concat_finalize(&$context, $rownumber)
672    {
673        if(!is_array($context))
674        {
675          return null;
676        }
677        $context['data'] = array_unique($context['data']);
678        return join($context['sep'],$context['data']);
679    }
680
681    /**
682     * Keep separate instances for every call to keep database connections
683     */
684    function isSingleton() {
685         return false;
686    }
687
688     /**
689     * fetch the next row as zero indexed array
690     */
691    function res_fetch_array($res)
692    {
693      if($this->extension == DOKU_EXT_SQLITE )
694      {
695        return sqlite_fetch_array($res, SQLITE_NUM);
696      }
697      else
698      {
699        return $res->fetch(PDO::FETCH_NUM);
700      }
701    }
702
703
704    /**
705     * fetch the next row as assocative array
706     */
707    function res_fetch_assoc($res)
708    {
709      if($this->extension == DOKU_EXT_SQLITE )
710      {
711        return sqlite_fetch_array($res, SQLITE_ASSOC);
712      }
713      else
714      {
715       return $res->fetch(PDO::FETCH_ASSOC);
716      }
717    }
718
719
720    /**
721    * Count the number of records in rsult
722    */
723    function res2count($res) {
724      if($this->extension == DOKU_EXT_SQLITE )
725      {
726        return sqlite_num_rows($res);
727      }
728      else
729      {
730        $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i';
731        if (preg_match($regex, $res->queryString, $output) > 0) {
732            $stmt = $this->db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM);
733
734            return $stmt->fetchColumn();
735        }
736
737        return false;
738      }
739    }
740
741
742    /**
743    * Count the number of records changed last time
744    */
745    function countChanges($db, $res)
746    {
747      if($this->extension == DOKU_EXT_SQLITE )
748      {
749        return sqlite_changes($db);
750      }
751      else
752      {
753        return $res->rowCount();
754      }
755    }
756}
757
758// vim:ts=4:sw=4:et:enc=utf-8:
759