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 $data = $res->fetchAll(PDO::FETCH_ASSOC); 531 if(!count(data)) 532 { 533 return false; 534 } 535 } 536 return $data; 537 } 538 539 /** 540 * Return the wanted row from a given result set as 541 * associative array 542 */ 543 function res2row($res,$rownum=0){ 544 if($this->extension == DOKU_EXT_SQLITE ) 545 { 546 if(!@sqlite_seek($res,$rownum)){ 547 return false; 548 } 549 return sqlite_fetch_array($res, SQLITE_ASSOC); 550 } 551 else 552 { 553 //very dirty replication of the same functionality (really must look at cursors) 554 $data = array(); 555 //do we need to rewind? 556 $data = $res->fetchAll(PDO::FETCH_ASSOC); 557 if(!count(data)) 558 { 559 return false; 560 } 561 562 if(!isset($data[$rownum])) 563 { 564 return false; 565 } 566 else 567 { 568 return $data[$rownum]; 569 } 570 } 571 } 572 573 /** 574 * Return the first value from the first row. 575 */ 576 function res2single($res) 577 { 578 if($this->extension == DOKU_EXT_SQLITE ) 579 { 580 return sqlite_fetch_single($res); 581 } 582 else 583 { 584 $data = $res->fetchAll(PDO::FETCH_NUM); 585 if(!count(data)) 586 { 587 return false; 588 } 589 return $data[0][0]; 590 } 591 } 592 593 /** 594 * Join the given values and quote them for SQL insertion 595 */ 596 function quote_and_join($vals,$sep=',') { 597 $vals = array_map(array('helper_plugin_sqlite','quote_string'),$vals); 598 return join($sep,$vals); 599 } 600 601 /** 602 * Run sqlite_escape_string() on the given string and surround it 603 * with quotes 604 */ 605 function quote_string($string){ 606 if($this->extension == DOKU_EXT_SQLITE ) 607 { 608 return "'".sqlite_escape_string($string)."'"; 609 } 610 else 611 { 612 return $this->db->quote($string); 613 } 614 } 615 616 617 /** 618 * Escape string for sql 619 */ 620 function escape_string($str) 621 { 622 if($this->extension == DOKU_EXT_SQLITE ) 623 { 624 return sqlite_escape_string($str); 625 } 626 else 627 { 628 return trim($this->db->quote($str), "'"); 629 } 630 } 631 632 633 634 /** 635 * Aggregation function for SQLite 636 * 637 * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 638 */ 639 function _sqlite_group_concat_step(&$context, $string, $separator = ',') { 640 $context['sep'] = $separator; 641 $context['data'][] = $string; 642 } 643 644 /** 645 * Aggregation function for SQLite 646 * 647 * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 648 */ 649 function _sqlite_group_concat_finalize(&$context) { 650 $context['data'] = array_unique($context['data']); 651 return join($context['sep'],$context['data']); 652 } 653 654 655 /** 656 * Aggregation function for SQLite via PDO 657 * 658 * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 659 */ 660 function _pdo_group_concat_step(&$context, $rownumber, $string, $separator = ',') { 661 if(is_null($context)) 662 { 663 $context = array( 664 'sep' => $separator, 665 'data' => array() 666 ); 667 } 668 669 $context['data'][] = $string; 670 return $context; 671 } 672 673 /** 674 * Aggregation function for SQLite via PDO 675 * 676 * @link http://devzone.zend.com/article/863-SQLite-Lean-Mean-DB-Machine 677 */ 678 function _pdo_group_concat_finalize(&$context, $rownumber) 679 { 680 if(!is_array($context)) 681 { 682 return null; 683 } 684 $context['data'] = array_unique($context['data']); 685 return join($context['sep'],$context['data']); 686 } 687 688 /** 689 * Keep separate instances for every call to keep database connections 690 */ 691 function isSingleton() { 692 return false; 693 } 694 695 /** 696 * fetch the next row as zero indexed array 697 */ 698 function res_fetch_array($res) 699 { 700 if($this->extension == DOKU_EXT_SQLITE ) 701 { 702 return sqlite_fetch_array($res, SQLITE_NUM); 703 } 704 else 705 { 706 return $res->fetch(PDO::FETCH_NUM); 707 } 708 } 709 710 711 /** 712 * fetch the next row as assocative array 713 */ 714 function res_fetch_assoc($res) 715 { 716 if($this->extension == DOKU_EXT_SQLITE ) 717 { 718 return sqlite_fetch_array($res, SQLITE_ASSOC); 719 } 720 else 721 { 722 return $res->fetch(PDO::FETCH_ASSOC); 723 } 724 } 725 726 727 /** 728 * Count the number of records in rsult 729 */ 730 function res2count($res) { 731 if($this->extension == DOKU_EXT_SQLITE ) 732 { 733 return sqlite_num_rows($res); 734 } 735 else 736 { 737 $regex = '/^SELECT\s+(?:ALL\s+|DISTINCT\s+)?(?:.*?)\s+FROM\s+(.*)$/i'; 738 if (preg_match($regex, $res->queryString, $output) > 0) { 739 $stmt = $this->db->query("SELECT COUNT(*) FROM {$output[1]}", PDO::FETCH_NUM); 740 741 return $stmt->fetchColumn(); 742 } 743 744 return false; 745 } 746 } 747 748 749 /** 750 * Count the number of records changed last time 751 */ 752 function countChanges($db, $res) 753 { 754 if($this->extension == DOKU_EXT_SQLITE ) 755 { 756 return sqlite_changes($db); 757 } 758 else 759 { 760 return $res->rowCount(); 761 } 762 } 763} 764 765// vim:ts=4:sw=4:et:enc=utf-8: 766