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