2 rcs_id('$Id: PearDB.php,v 1.60 2004-07-09 10:06:50 rurban Exp $');
4 require_once('lib/WikiDB/backend.php');
5 //require_once('lib/FileFinder.php');
6 require_once('lib/ErrorManager.php');
7 require_once('DB.php'); // Always favor use our local pear copy
9 class WikiDB_backend_PearDB
10 extends WikiDB_backend
14 function WikiDB_backend_PearDB ($dbparams) {
15 // Find and include PEAR's DB.php.
16 //$pearFinder = new PearFileFinder;
17 //$pearFinder->includeOnce('DB.php');
19 // Install filter to handle bogus error notices from buggy DB.php's.
21 $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_pear_notice_filter'));
23 // Open connection to database
24 $this->_dsn = $dbparams['dsn'];
25 $this->_dbparams = $dbparams;
26 $dboptions = array('persistent' => true,
28 if (preg_match('/^pgsql/',$this->_dsn))
29 $dboptions['persistent'] = false;
30 $this->_dbh = DB::connect($this->_dsn, $dboptions);
32 if (DB::isError($dbh)) {
33 trigger_error(sprintf("Can't connect to database: %s",
34 $this->_pear_error_message($dbh)),
37 $dbh->setErrorHandling(PEAR_ERROR_CALLBACK,
38 array(&$this, '_pear_error_callback'));
39 $dbh->setFetchMode(DB_FETCHMODE_ASSOC);
41 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
44 = array('page_tbl' => $prefix . 'page',
45 'version_tbl' => $prefix . 'version',
46 'link_tbl' => $prefix . 'link',
47 'recent_tbl' => $prefix . 'recent',
48 'nonempty_tbl' => $prefix . 'nonempty');
49 $page_tbl = $this->_table_names['page_tbl'];
50 $version_tbl = $this->_table_names['version_tbl'];
51 $this->page_tbl_fields = "$page_tbl.id AS id, $page_tbl.pagename AS pagename, $page_tbl.hits AS hits";
52 $this->version_tbl_fields = "$version_tbl.version AS version, $version_tbl.mtime AS mtime, ".
53 "$version_tbl.minor_edit AS minor_edit, $version_tbl.content AS content, $version_tbl.versiondata AS versiondata";
56 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
57 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
58 'maxversion' => "MAX(version)",
60 'iscontent' => "content<>''");
62 $this->_lock_count = 0;
66 * Close database connection.
71 if ($this->_lock_count) {
72 trigger_error( "WARNING: database still locked " . '(lock_count = $this->_lock_count)' . "\n<br />",
75 $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
76 $this->unlock('force');
78 $this->_dbh->disconnect();
85 function is_wiki_page($pagename) {
87 extract($this->_table_names);
88 return $dbh->getOne(sprintf("SELECT $page_tbl.id as id"
89 . " FROM $nonempty_tbl, $page_tbl"
90 . " WHERE $nonempty_tbl.id=$page_tbl.id"
91 . " AND pagename='%s'",
92 $dbh->quoteString($pagename)));
95 function get_all_pagenames() {
97 extract($this->_table_names);
98 return $dbh->getCol("SELECT pagename"
99 . " FROM $nonempty_tbl, $page_tbl"
100 . " WHERE $nonempty_tbl.id=$page_tbl.id");
103 function numPages($filter=false, $exclude='') {
105 extract($this->_table_names);
106 return $dbh->getOne("SELECT count(*)"
107 . " FROM $nonempty_tbl, $page_tbl"
108 . " WHERE $nonempty_tbl.id=$page_tbl.id");
112 * Read page information from database.
114 function get_pagedata($pagename) {
116 $page_tbl = $this->_table_names['page_tbl'];
118 //trigger_error("GET_PAGEDATA $pagename", E_USER_NOTICE);
120 $result = $dbh->getRow(sprintf("SELECT %s FROM $page_tbl WHERE pagename='%s'",
121 $this->page_tbl_fields.",pagedata",
122 $dbh->quoteString($pagename)),
126 return $this->_extract_page_data($result);
129 function _extract_page_data(&$query_result) {
130 extract($query_result);
131 if (isset($query_result['pagedata'])) {
132 $data = $this->_unserialize($query_result['pagedata']);
133 // Memory optimization:
134 // Only store the cached_html for the current pagename
135 // Do it here or unset it in the Cache?
137 $data['hits'] = $query_result['hits'];
141 function update_pagedata($pagename, $newdata) {
143 $page_tbl = $this->_table_names['page_tbl'];
145 // Hits is the only thing we can update in a fast manner.
146 if (count($newdata) == 1 && isset($newdata['hits'])) {
147 // Note that this will fail silently if the page does not
148 // have a record in the page table. Since it's just the
149 // hit count, who cares?
150 $dbh->query(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename='%s'",
151 $newdata['hits'], $dbh->quoteString($pagename)));
156 $data = $this->get_pagedata($pagename);
159 $this->_get_pageid($pagename, true); // Creates page record
162 @$hits = (int)$data['hits'];
163 unset($data['hits']);
165 foreach ($newdata as $key => $val) {
168 else if (empty($val))
174 /* Portability issue -- not all DBMS supports huge strings
175 * so we need to 'bind' instead of building a SQL statment.
176 * Note that we do not need to quoteString when we bind
177 $dbh->query(sprintf("UPDATE $page_tbl"
178 . " SET hits=%d, pagedata='%s'"
179 . " WHERE pagename='%s'",
181 $dbh->quoteString($this->_serialize($data)),
182 $dbh->quoteString($pagename)));
184 $sth = $dbh->query("UPDATE $page_tbl"
185 . " SET hits=?, pagedata=?"
186 . " WHERE pagename=?",
187 array($hits, $this->_serialize($data), $pagename));
191 function _get_pageid($pagename, $create_if_missing = false) {
194 $page_tbl = $this->_table_names['page_tbl'];
196 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
197 $dbh->quoteString($pagename));
199 if (!$create_if_missing)
200 return $dbh->getOne($query);
203 $id = $dbh->getOne($query);
205 $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
207 $dbh->query(sprintf("INSERT INTO $page_tbl"
208 . " (id,pagename,hits)"
209 . " VALUES (%d,'%s',0)",
210 $id, $dbh->quoteString($pagename)));
216 function get_latest_version($pagename) {
218 extract($this->_table_names);
220 (int)$dbh->getOne(sprintf("SELECT latestversion"
221 . " FROM $page_tbl, $recent_tbl"
222 . " WHERE $page_tbl.id=$recent_tbl.id"
223 . " AND pagename='%s'",
224 $dbh->quoteString($pagename)));
227 function get_previous_version($pagename, $version) {
229 extract($this->_table_names);
232 (int)$dbh->getOne(sprintf("SELECT version"
233 . " FROM $version_tbl, $page_tbl"
234 . " WHERE $version_tbl.id=$page_tbl.id"
235 . " AND pagename='%s'"
236 . " AND version < %d"
237 . " ORDER BY version DESC",
238 /* Non portable and useless anyway with getOne
241 $dbh->quoteString($pagename),
248 * @param $version int Which version to get.
250 * @return hash The version data, or false if specified version does not
253 function get_versiondata($pagename, $version, $want_content = false) {
255 extract($this->_table_names);
256 extract($this->_expressions);
258 assert(is_string($pagename) and $pagename != "");
259 assert($version > 0);
261 //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
262 // FIXME: optimization: sometimes don't get page data?
264 $fields = $this->page_tbl_fields . ",$page_tbl.pagedata as pagedata," . $this->version_tbl_fields;
267 $fields = $this->page_tbl_fields . ","
268 . "mtime, minor_edit, versiondata,"
269 . "$iscontent AS have_content";
272 $result = $dbh->getRow(sprintf("SELECT $fields"
273 . " FROM $page_tbl, $version_tbl"
274 . " WHERE $page_tbl.id=$version_tbl.id"
275 . " AND pagename='%s'"
277 $dbh->quoteString($pagename), $version),
280 return $this->_extract_version_data($result);
283 function _extract_version_data(&$query_result) {
287 extract($query_result);
288 $data = $this->_unserialize($versiondata);
290 $data['mtime'] = $mtime;
291 $data['is_minor_edit'] = !empty($minor_edit);
294 $data['%content'] = $content;
295 elseif ($have_content)
296 $data['%content'] = true;
298 $data['%content'] = '';
300 // FIXME: this is ugly.
301 if (isset($pagename)) {
302 // Query also includes page data.
303 // We might as well send that back too...
304 $data['%pagedata'] = $this->_extract_page_data($query_result);
312 * Create a new revision of a page.
314 function set_versiondata($pagename, $version, $data) {
316 $version_tbl = $this->_table_names['version_tbl'];
318 $minor_edit = (int) !empty($data['is_minor_edit']);
319 unset($data['is_minor_edit']);
321 $mtime = (int)$data['mtime'];
322 unset($data['mtime']);
323 assert(!empty($mtime));
325 @$content = (string) $data['%content'];
326 unset($data['%content']);
328 unset($data['%pagedata']);
331 $id = $this->_get_pageid($pagename, true);
333 // FIXME: optimize: mysql can do this with one REPLACE INTO (I think).
334 $dbh->query(sprintf("DELETE FROM $version_tbl"
335 . " WHERE id=%d AND version=%d",
338 /* Same as above -- bind!
339 $dbh->query(sprintf("INSERT INTO $version_tbl"
340 . " (id,version,mtime,minor_edit,content,versiondata)"
341 . " VALUES(%d,%d,%d,%d,'%s','%s')",
342 $id, $version, $mtime, $minor_edit,
343 $dbh->quoteString($content),
344 $dbh->quoteString($this->_serialize($data))));
346 $dbh->query("INSERT INTO $version_tbl"
347 . " (id,version,mtime,minor_edit,content,versiondata)"
348 . " VALUES(?, ?, ?, ?, ?, ?)",
349 array($id, $version, $mtime, $minor_edit, $content,
350 $this->_serialize($data)));
352 $this->_update_recent_table($id);
353 $this->_update_nonempty_table($id);
359 * Delete an old revision of a page.
361 function delete_versiondata($pagename, $version) {
363 extract($this->_table_names);
366 if ( ($id = $this->_get_pageid($pagename)) ) {
367 $dbh->query("DELETE FROM $version_tbl"
368 . " WHERE id=$id AND version=$version");
369 $this->_update_recent_table($id);
370 // This shouldn't be needed (as long as the latestversion
371 // never gets deleted.) But, let's be safe.
372 $this->_update_nonempty_table($id);
378 * Delete page completely from the database.
379 * I'm not sure if this is what we want. Maybe just delete the revisions
381 function delete_page($pagename) {
383 extract($this->_table_names);
386 if ( ($id = $this->_get_pageid($pagename, false)) ) {
387 $dbh->query("DELETE FROM $version_tbl WHERE id=$id");
388 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id");
389 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
390 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$id");
391 $nlinks = $dbh->getOne("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
393 // We're still in the link table (dangling link) so we can't delete this
395 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
398 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
400 $this->_update_recent_table();
401 $this->_update_nonempty_table();
407 // The only thing we might be interested in updating which we can
408 // do fast in the flags (minor_edit). I think the default
409 // update_versiondata will work fine...
410 //function update_versiondata($pagename, $version, $data) {
413 function set_links($pagename, $links) {
414 // Update link table.
415 // FIXME: optimize: mysql can do this all in one big INSERT.
418 extract($this->_table_names);
421 $pageid = $this->_get_pageid($pagename, true);
423 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
426 foreach($links as $link) {
427 if (isset($linkseen[$link]))
429 $linkseen[$link] = true;
430 $linkid = $this->_get_pageid($link, true);
431 $dbh->query("INSERT INTO $link_tbl (linkfrom, linkto)"
432 . " VALUES ($pageid, $linkid)");
439 * Find pages which link to or are linked from a page.
441 function get_links($pagename, $reversed = true) {
443 extract($this->_table_names);
446 list($have,$want) = array('linkee', 'linker');
448 list($have,$want) = array('linker', 'linkee');
450 $qpagename = $dbh->quoteString($pagename);
452 $result = $dbh->query("SELECT $want.id as id, $want.pagename as pagename, $want.hits as hits"
453 // Looks like 'AS' in column alias is a MySQL thing, Oracle does not like it
454 // and the PostgresSQL manual does not have it either
455 // Since it is optional in mySQL, just remove it...
456 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee"
457 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
458 . " AND $have.pagename='$qpagename'"
459 //. " GROUP BY $want.id"
460 . " ORDER BY $want.pagename");
462 return new WikiDB_backend_PearDB_iter($this, $result);
465 function get_all_pages($include_deleted=false, $sortby=false, $limit=false) {
467 extract($this->_table_names);
468 // Limit clause is NOT portable!
469 // if ($limit) $limit = "LIMIT $limit";
471 $orderby = $this->sortby($sortby, 'db');
472 if ($orderby) $orderby = 'ORDER BY ' . $orderby;
473 if (strstr($orderby, 'mtime ')) { // multiple columns possible
474 if ($include_deleted) {
476 . $this->page_tbl_fields
477 . " FROM $page_tbl, $recent_tbl, $version_tbl"
478 . " WHERE $page_tbl.id=$recent_tbl.id"
479 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
484 . $this->page_tbl_fields
485 . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
486 . " WHERE $nonempty_tbl.id=$page_tbl.id"
487 . " AND $page_tbl.id=$recent_tbl.id"
488 . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
492 if ($include_deleted) {
494 . $this->page_tbl_fields
495 ." FROM $page_tbl $orderby";
499 . $this->page_tbl_fields
500 . " FROM $nonempty_tbl, $page_tbl"
501 . " WHERE $nonempty_tbl.id=$page_tbl.id"
506 // extract from,count from limit
507 list($from,$count) = $this->limit($limit);
508 $result = $dbh->limitQuery($sql, $from, $count);
510 $result = $dbh->query($sql);
512 return new WikiDB_backend_PearDB_iter($this, $result);
518 function text_search($search = '', $fullsearch = false) {
520 extract($this->_table_names);
522 $table = "$nonempty_tbl, $page_tbl";
523 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
524 $fields = $this->page_tbl_fields;
525 $callback = new WikiMethodCb($this, '_sql_match_clause');
528 $table .= ", $recent_tbl";
529 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
531 $table .= ", $version_tbl";
532 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
534 $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
535 $callback = new WikiMethodCb($this, '_fullsearch_sql_match_clause');
538 $search_clause = $search->makeSqlClause($callback);
540 $result = $dbh->query("SELECT $fields FROM $table"
541 . " WHERE $join_clause"
542 . " AND ($search_clause)"
543 . " ORDER BY pagename");
545 return new WikiDB_backend_PearDB_iter($this, $result);
548 //Todo: check if the better Mysql MATCH operator is supported,
549 // (ranked search) and also google like expressions.
550 function _sql_match_clause($word) {
551 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
552 $word = $this->_dbh->quoteString($word);
553 //$page_tbl = $this->_table_names['page_tbl'];
554 //Note: Mysql 4.1.0 has a bug which fails with binary fields.
555 // e.g. if word is lowercased.
556 // http://bugs.mysql.com/bug.php?id=1491
557 return "LOWER(pagename) LIKE '%$word%'";
560 function _fullsearch_sql_match_clause($word) {
561 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
562 $word = $this->_dbh->quoteString($word);
563 //$page_tbl = $this->_table_names['page_tbl'];
564 //Mysql 4.1.1 has a bug which fails here if word is lowercased.
565 return "LOWER(pagename) LIKE '%$word%' OR content LIKE '%$word%'";
569 * Find highest or lowest hit counts.
571 function most_popular($limit=false) {
573 extract($this->_table_names);
580 $where = " AND hits > 0";
582 $orderby = " ORDER BY hits $order";
583 //$limitclause = $limit ? " LIMIT $limit" : '';
585 . $this->page_tbl_fields
586 . " FROM $nonempty_tbl, $page_tbl"
587 . " WHERE $nonempty_tbl.id=$page_tbl.id"
591 list($from,$count) = $this->limit($limit);
592 $result = $dbh->limitQuery($sql, $from, $count);
594 $result = $dbh->query($sql);
597 return new WikiDB_backend_PearDB_iter($this, $result);
601 * Find recent changes.
603 function most_recent($params) {
606 $include_minor_revisions = false;
607 $exclude_major_revisions = false;
608 $include_all_revisions = false;
612 extract($this->_table_names);
616 $pick[] = "mtime >= $since";
619 if ($include_all_revisions) {
620 // Include all revisions of each page.
621 $table = "$page_tbl, $version_tbl";
622 $join_clause = "$page_tbl.id=$version_tbl.id";
624 if ($exclude_major_revisions) {
625 // Include only minor revisions
626 $pick[] = "minor_edit <> 0";
628 elseif (!$include_minor_revisions) {
629 // Include only major revisions
630 $pick[] = "minor_edit = 0";
634 $table = "$page_tbl, $recent_tbl";
635 $join_clause = "$page_tbl.id=$recent_tbl.id";
636 $table .= ", $version_tbl";
637 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
639 if ($exclude_major_revisions) {
640 // Include only most recent minor revision
641 $pick[] = 'version=latestminor';
643 elseif (!$include_minor_revisions) {
644 // Include only most recent major revision
645 $pick[] = 'version=latestmajor';
648 // Include only the latest revision (whether major or minor).
649 $pick[] ='version=latestversion';
657 // $limitclause = $limit ? " LIMIT $limit" : '';
658 $where_clause = $join_clause;
660 $where_clause .= " AND " . join(" AND ", $pick);
662 // FIXME: use SQL_BUFFER_RESULT for mysql?
664 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
666 . " WHERE $where_clause"
667 . " ORDER BY mtime $order";
670 $result = $dbh->limitQuery($sql, 0, $limit);
672 $result = $dbh->query($sql);
675 return new WikiDB_backend_PearDB_iter($this, $result);
679 * Rename page in the database.
681 function rename_page($pagename, $to) {
683 extract($this->_table_names);
686 if ( ($id = $this->_get_pageid($pagename, false)) ) {
687 if ($new = $this->_get_pageid($to, false)) {
689 //this page does not exist (already verified before), but exists in the page table.
690 //so we delete this page.
691 $dbh->query(sprintf("DELETE FROM $page_tbl WHERE id=$id",
692 $dbh->quoteString($to)));
694 $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
695 $dbh->quoteString($to)));
701 function _update_recent_table($pageid = false) {
703 extract($this->_table_names);
704 extract($this->_expressions);
706 $pageid = (int)$pageid;
708 // optimize: mysql can do this with one REPLACE INTO.
709 $backend_type = $this->backendType();
710 if (substr($backend_type,0,5) == 'mysql') {
711 $dbh->query("REPLACE INTO $recent_tbl"
712 . " (id, latestversion, latestmajor, latestminor)"
713 . " SELECT id, $maxversion, $maxmajor, $maxminor"
714 . " FROM $version_tbl"
715 . ( $pageid ? " WHERE id=$pageid" : "")
719 $dbh->query("DELETE FROM $recent_tbl"
720 . ( $pageid ? " WHERE id=$pageid" : ""));
721 $dbh->query( "INSERT INTO $recent_tbl"
722 . " (id, latestversion, latestmajor, latestminor)"
723 . " SELECT id, $maxversion, $maxmajor, $maxminor"
724 . " FROM $version_tbl"
725 . ( $pageid ? " WHERE id=$pageid" : "")
731 function _update_nonempty_table($pageid = false) {
733 extract($this->_table_names);
735 $pageid = (int)$pageid;
737 // Optimize: mysql can do this with one REPLACE INTO.
738 // FIXME: This treally should be moved into ADODB_mysql.php but
739 // then it must be duplicated for mysqli and mysqlt also.
740 $backend_type = $this->backendType();
741 if (substr($backend_type,0,5) == 'mysql') {
742 $dbh->query("REPLACE INTO $nonempty_tbl (id)"
743 . " SELECT $recent_tbl.id"
744 . " FROM $recent_tbl, $version_tbl"
745 . " WHERE $recent_tbl.id=$version_tbl.id"
746 . " AND version=latestversion"
748 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
750 extract($this->_expressions);
752 $dbh->query("DELETE FROM $nonempty_tbl"
753 . ( $pageid ? " WHERE id=$pageid" : ""));
754 $dbh->query("INSERT INTO $nonempty_tbl (id)"
755 . " SELECT $recent_tbl.id"
756 . " FROM $recent_tbl, $version_tbl"
757 . " WHERE $recent_tbl.id=$version_tbl.id"
758 . " AND version=latestversion"
759 // We have some specifics here (Oracle)
760 //. " AND content<>''"
761 . " AND content $notempty"
762 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
770 * Grab a write lock on the tables in the SQL database.
772 * Calls can be nested. The tables won't be unlocked until
773 * _unlock_database() is called as many times as _lock_database().
777 function lock($tables = false, $write_lock = true) {
778 if ($this->_lock_count++ == 0)
779 $this->_lock_tables($write_lock);
783 * Actually lock the required tables.
785 function _lock_tables($write_lock) {
786 trigger_error("virtual", E_USER_ERROR);
790 * Release a write lock on the tables in the SQL database.
794 * @param $force boolean Unlock even if not every call to lock() has been matched
795 * by a call to unlock().
797 * @see _lock_database
799 function unlock($tables = false, $force = false) {
800 if ($this->_lock_count == 0)
802 if (--$this->_lock_count <= 0 || $force) {
803 $this->_unlock_tables();
804 $this->_lock_count = 0;
809 * Actually unlock the required tables.
811 function _unlock_tables($write_lock) {
812 trigger_error("virtual", E_USER_ERROR);
819 function _serialize($data) {
822 assert(is_array($data));
823 return serialize($data);
829 function _unserialize($data) {
830 return empty($data) ? array() : unserialize($data);
834 * Callback for PEAR (DB) errors.
838 * @param A PEAR_error object.
840 function _pear_error_callback($error) {
841 if ($this->_is_false_error($error))
844 $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
846 trigger_error($this->_pear_error_message($error), E_USER_ERROR);
850 * Detect false errors messages from PEAR DB.
852 * The version of PEAR DB which ships with PHP 4.0.6 has a bug in that
853 * it doesn't recognize "LOCK" and "UNLOCK" as SQL commands which don't
854 * return any data. (So when a "LOCK" command doesn't return any data,
855 * DB reports it as an error, when in fact, it's not.)
858 * @return bool True iff error is not really an error.
860 function _is_false_error($error) {
861 if ($error->getCode() != DB_ERROR)
864 $query = $this->_dbh->last_query;
866 if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE'
867 . '|DROP|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s/', $query)) {
868 // Last query was not of the sort which doesn't return any data.
869 //" <--kludge for brain-dead syntax coloring
873 if (! in_array('ismanip', get_class_methods('DB'))) {
874 // Pear shipped with PHP 4.0.4pl1 (and before, presumably)
875 // does not have the DB::isManip method.
879 if (DB::isManip($query)) {
880 // If Pear thinks it's an isManip then it wouldn't have thrown
881 // the error we're testing for....
888 function _pear_error_message($error) {
889 $class = get_class($this);
890 $message = "$class: fatal database error\n"
891 . "\t" . $error->getMessage() . "\n"
892 . "\t(" . $error->getDebugInfo() . ")\n";
894 // Prevent password from being exposed during a connection error
895 $safe_dsn = preg_replace('| ( :// .*? ) : .* (?=@) |xs',
896 '\\1:XXXXXXXX', $this->_dsn);
897 return str_replace($this->_dsn, $safe_dsn, $message);
901 * Filter PHP errors notices from PEAR DB code.
903 * The PEAR DB code which ships with PHP 4.0.6 produces spurious
904 * errors and notices. This is an error callback (for use with
905 * ErrorManager which will filter out those spurious messages.)
906 * @see _is_false_error, ErrorManager
909 function _pear_notice_filter($err) {
910 return ( $err->isNotice()
911 && preg_match('|DB[/\\\\]common.php$|', $err->errfile)
912 && $err->errline == 126
913 && preg_match('/Undefined offset: +0\b/', $err->errstr) );
916 /* some variables and functions for DB backend abstraction (action=upgrade) */
917 function database () {
918 return $this->_dbh->dsn['database'];
920 function backendType() {
921 return $this->_dbh->phptype;
923 function connection() {
924 return $this->_dbh->connection;
927 function listOfTables() {
928 return $this->_dbh->getListOf('tables');
930 function listOfFields($database,$table) {
931 if ($this->backendType() == 'mysql') {
933 assert(!empty($database));
934 assert(!empty($table));
935 $result = mysql_list_fields($database, $table, $this->_dbh->connection) or
936 trigger_error(__FILE__.':'.__LINE__.' '.mysql_error(), E_USER_WARNING);
937 if (!$result) return array();
938 $columns = mysql_num_fields($result);
939 for ($i = 0; $i < $columns; $i++) {
940 $fields[] = mysql_field_name($result, $i);
942 mysql_free_result($result);
945 // TODO: try ADODB version?
946 trigger_error("Unsupported dbtype and backend. Either switch to ADODB or check it manually.");
953 * This class is a generic iterator.
955 * WikiDB_backend_PearDB_iter only iterates over things that have
956 * 'pagename', 'pagedata', etc. etc.
958 * Probably WikiDB_backend_PearDB_iter and this class should be merged
959 * (most of the code is cut-and-paste :-( ), but I am trying to make
960 * changes that could be merged easily.
962 * @author: Dan Frankowski
964 class WikiDB_backend_PearDB_generic_iter
965 extends WikiDB_backend_iterator
967 function WikiDB_backend_PearDB_generic_iter(&$backend, &$query_result) {
968 if (DB::isError($query_result)) {
969 // This shouldn't happen, I thought.
970 $backend->_pear_error_callback($query_result);
973 $this->_backend = &$backend;
974 $this->_result = $query_result;
980 return $this->_result->numRows();
984 $backend = &$this->_backend;
988 $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
998 if ($this->_result) {
999 $this->_result->free();
1000 $this->_result = false;
1005 class WikiDB_backend_PearDB_iter
1006 extends WikiDB_backend_PearDB_generic_iter
1010 $backend = &$this->_backend;
1011 if (!$this->_result)
1014 $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
1020 $pagedata = $backend->_extract_page_data($record);
1021 $rec = array('pagename' => $record['pagename'],
1022 'pagedata' => $pagedata);
1024 if (!empty($record['version'])) {
1025 $rec['versiondata'] = $backend->_extract_version_data($record);
1026 $rec['version'] = $record['version'];
1032 // $Log: not supported by cvs2svn $
1033 // Revision 1.59 2004/07/08 21:32:36 rurban
1034 // Prevent from more warnings, minor db and sort optimizations
1036 // Revision 1.58 2004/07/08 16:56:16 rurban
1037 // use the backendType abstraction
1039 // Revision 1.57 2004/07/05 12:57:54 rurban
1040 // add mysql timeout
1042 // Revision 1.56 2004/07/04 10:24:43 rurban
1043 // forgot the expressions
1045 // Revision 1.55 2004/07/03 16:51:06 rurban
1046 // optional DBADMIN_USER:DBADMIN_PASSWD for action=upgrade (if no ALTER permission)
1047 // added atomic mysql REPLACE for PearDB as in ADODB
1048 // fixed _lock_tables typo links => link
1049 // fixes unserialize ADODB bug in line 180
1051 // Revision 1.54 2004/06/29 08:52:24 rurban
1052 // Use ...version() $need_content argument in WikiDB also:
1053 // To reduce the memory footprint for larger sets of pagelists,
1054 // we don't cache the content (only true or false) and
1055 // we purge the pagedata (_cached_html) also.
1056 // _cached_html is only cached for the current pagename.
1057 // => Vastly improved page existance check, ACL check, ...
1059 // Now only PagedList info=content or size needs the whole content, esp. if sortable.
1061 // Revision 1.53 2004/06/27 10:26:03 rurban
1062 // oci8 patch by Philippe Vanhaesendonck + some ADODB notes+fixes
1064 // Revision 1.52 2004/06/25 14:15:08 rurban
1065 // reduce memory footprint by caching only requested pagedate content (improving most page iterators)
1067 // Revision 1.51 2004/05/12 10:49:55 rurban
1068 // require_once fix for those libs which are loaded before FileFinder and
1069 // its automatic include_path fix, and where require_once doesn't grok
1070 // dirname(__FILE__) != './lib'
1071 // upgrade fix with PearDB
1072 // navbar.tmpl: remove spaces for IE button alignment
1074 // Revision 1.50 2004/05/06 17:30:39 rurban
1075 // CategoryGroup: oops, dos2unix eol
1076 // improved phpwiki_version:
1077 // pre -= .0001 (1.3.10pre: 1030.099)
1078 // -p1 += .001 (1.3.9-p1: 1030.091)
1079 // improved InstallTable for mysql and generic SQL versions and all newer tables so far.
1080 // abstracted more ADODB/PearDB methods for action=upgrade stuff:
1081 // backend->backendType(), backend->database(),
1082 // backend->listOfFields(),
1083 // backend->listOfTables(),
1085 // Revision 1.49 2004/05/03 21:35:30 rurban
1086 // don't use persistent connections with postgres
1088 // Revision 1.48 2004/04/26 20:44:35 rurban
1089 // locking table specific for better databases
1091 // Revision 1.47 2004/04/20 00:06:04 rurban
1092 // themable paging support
1094 // Revision 1.46 2004/04/19 21:51:41 rurban
1095 // php5 compatibility: it works!
1097 // Revision 1.45 2004/04/16 14:19:39 rurban
1098 // updated ADODB notes
1101 // (c-file-style: "gnu")
1105 // c-basic-offset: 4
1106 // c-hanging-comment-ender-p: nil
1107 // indent-tabs-mode: nil