2 rcs_id('$Id: PearDB.php,v 1.54 2004-06-29 08:52:24 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 $dboptions = array('persistent' => true,
27 if (preg_match('/^pgsql/',$this->_dsn))
28 $dboptions['persistent'] = false;
29 $this->_dbh = DB::connect($this->_dsn, $dboptions);
31 if (DB::isError($dbh)) {
32 trigger_error(sprintf("Can't connect to database: %s",
33 $this->_pear_error_message($dbh)),
36 $dbh->setErrorHandling(PEAR_ERROR_CALLBACK,
37 array(&$this, '_pear_error_callback'));
38 $dbh->setFetchMode(DB_FETCHMODE_ASSOC);
40 $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
43 = array('page_tbl' => $prefix . 'page',
44 'version_tbl' => $prefix . 'version',
45 'link_tbl' => $prefix . 'link',
46 'recent_tbl' => $prefix . 'recent',
47 'nonempty_tbl' => $prefix . 'nonempty');
48 $page_tbl = $this->_table_names['page_tbl'];
49 $version_tbl = $this->_table_names['version_tbl'];
50 $this->page_tbl_fields = "$page_tbl.id AS id, $page_tbl.pagename AS pagename, $page_tbl.hits AS hits";
51 $this->version_tbl_fields = "$version_tbl.version AS version, $version_tbl.mtime AS mtime, ".
52 "$version_tbl.minor_edit AS minor_edit, $version_tbl.content AS content, $version_tbl.versiondata AS versiondata";
55 = array('maxmajor' => "MAX(CASE WHEN minor_edit=0 THEN version END)",
56 'maxminor' => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
57 'maxversion' => "MAX(version)",
59 'iscontent' => "content<>''");
61 $this->_lock_count = 0;
65 * Close database connection.
70 if ($this->_lock_count) {
71 trigger_error( "WARNING: database still locked " . '(lock_count = $this->_lock_count)' . "\n<br />",
74 $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
75 $this->unlock('force');
77 $this->_dbh->disconnect();
84 function is_wiki_page($pagename) {
86 extract($this->_table_names);
87 return $dbh->getOne(sprintf("SELECT $page_tbl.id as id"
88 . " FROM $nonempty_tbl, $page_tbl"
89 . " WHERE $nonempty_tbl.id=$page_tbl.id"
90 . " AND pagename='%s'",
91 $dbh->quoteString($pagename)));
94 function get_all_pagenames() {
96 extract($this->_table_names);
97 return $dbh->getCol("SELECT pagename"
98 . " FROM $nonempty_tbl, $page_tbl"
99 . " WHERE $nonempty_tbl.id=$page_tbl.id");
102 function numPages($filter=false, $exclude='') {
104 extract($this->_table_names);
105 return $dbh->getOne("SELECT count(*)"
106 . " FROM $nonempty_tbl, $page_tbl"
107 . " WHERE $nonempty_tbl.id=$page_tbl.id");
111 * Read page information from database.
113 function get_pagedata($pagename) {
115 $page_tbl = $this->_table_names['page_tbl'];
117 //trigger_error("GET_PAGEDATA $pagename", E_USER_NOTICE);
119 $result = $dbh->getRow(sprintf("SELECT %s FROM $page_tbl WHERE pagename='%s'",
120 $this->page_tbl_fields.",pagedata",
121 $dbh->quoteString($pagename)),
125 return $this->_extract_page_data($result);
128 function _extract_page_data(&$query_result) {
129 extract($query_result);
130 if (isset($query_result['pagedata'])) {
131 $data = $this->_unserialize($query_result['pagedata']);
132 // Memory optimization:
133 // Only store the cached_html for the current pagename
134 // Do it here or unset it in the Cache?
136 $data['hits'] = $query_result['hits'];
140 function update_pagedata($pagename, $newdata) {
142 $page_tbl = $this->_table_names['page_tbl'];
144 // Hits is the only thing we can update in a fast manner.
145 if (count($newdata) == 1 && isset($newdata['hits'])) {
146 // Note that this will fail silently if the page does not
147 // have a record in the page table. Since it's just the
148 // hit count, who cares?
149 $dbh->query(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename='%s'",
150 $newdata['hits'], $dbh->quoteString($pagename)));
155 $data = $this->get_pagedata($pagename);
158 $this->_get_pageid($pagename, true); // Creates page record
161 @$hits = (int)$data['hits'];
162 unset($data['hits']);
164 foreach ($newdata as $key => $val) {
167 else if (empty($val))
173 /* Portability issue -- not all DBMS supports huge strings
174 * so we need to 'bind' instead of building a SQL statment.
175 * Note that we do not need to quoteString when we bind
176 $dbh->query(sprintf("UPDATE $page_tbl"
177 . " SET hits=%d, pagedata='%s'"
178 . " WHERE pagename='%s'",
180 $dbh->quoteString($this->_serialize($data)),
181 $dbh->quoteString($pagename)));
183 $sth = $dbh->query("UPDATE $page_tbl"
184 . " SET hits=?, pagedata=?"
185 . " WHERE pagename=?",
186 array($hits, $this->_serialize($data), $pagename));
190 function _get_pageid($pagename, $create_if_missing = false) {
193 $page_tbl = $this->_table_names['page_tbl'];
195 $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
196 $dbh->quoteString($pagename));
198 if (!$create_if_missing)
199 return $dbh->getOne($query);
202 $id = $dbh->getOne($query);
204 $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
206 $dbh->query(sprintf("INSERT INTO $page_tbl"
207 . " (id,pagename,hits)"
208 . " VALUES (%d,'%s',0)",
209 $id, $dbh->quoteString($pagename)));
215 function get_latest_version($pagename) {
217 extract($this->_table_names);
219 (int)$dbh->getOne(sprintf("SELECT latestversion"
220 . " FROM $page_tbl, $recent_tbl"
221 . " WHERE $page_tbl.id=$recent_tbl.id"
222 . " AND pagename='%s'",
223 $dbh->quoteString($pagename)));
226 function get_previous_version($pagename, $version) {
228 extract($this->_table_names);
231 (int)$dbh->getOne(sprintf("SELECT version"
232 . " FROM $version_tbl, $page_tbl"
233 . " WHERE $version_tbl.id=$page_tbl.id"
234 . " AND pagename='%s'"
235 . " AND version < %d"
236 . " ORDER BY version DESC",
237 /* Non portable and useless anyway with getOne
240 $dbh->quoteString($pagename),
247 * @param $version int Which version to get.
249 * @return hash The version data, or false if specified version does not
252 function get_versiondata($pagename, $version, $want_content = false) {
254 extract($this->_table_names);
255 extract($this->_expressions);
257 assert(is_string($pagename) and $pagename != "");
258 assert($version > 0);
260 //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
261 // FIXME: optimization: sometimes don't get page data?
263 $fields = $this->page_tbl_fields . ",$page_tbl.pagedata as pagedata," . $this->version_tbl_fields;
266 $fields = $this->page_tbl_fields . ","
267 . "mtime, minor_edit, versiondata,"
268 . "$iscontent AS have_content";
271 $result = $dbh->getRow(sprintf("SELECT $fields"
272 . " FROM $page_tbl, $version_tbl"
273 . " WHERE $page_tbl.id=$version_tbl.id"
274 . " AND pagename='%s'"
276 $dbh->quoteString($pagename), $version),
279 return $this->_extract_version_data($result);
282 function _extract_version_data(&$query_result) {
286 extract($query_result);
287 $data = $this->_unserialize($versiondata);
289 $data['mtime'] = $mtime;
290 $data['is_minor_edit'] = !empty($minor_edit);
293 $data['%content'] = $content;
294 elseif ($have_content)
295 $data['%content'] = true;
297 $data['%content'] = '';
299 // FIXME: this is ugly.
300 if (isset($pagename)) {
301 // Query also includes page data.
302 // We might as well send that back too...
303 $data['%pagedata'] = $this->_extract_page_data($query_result);
311 * Create a new revision of a page.
313 function set_versiondata($pagename, $version, $data) {
315 $version_tbl = $this->_table_names['version_tbl'];
317 $minor_edit = (int) !empty($data['is_minor_edit']);
318 unset($data['is_minor_edit']);
320 $mtime = (int)$data['mtime'];
321 unset($data['mtime']);
322 assert(!empty($mtime));
324 @$content = (string) $data['%content'];
325 unset($data['%content']);
327 unset($data['%pagedata']);
330 $id = $this->_get_pageid($pagename, true);
332 // FIXME: optimize: mysql can do this with one REPLACE INTO (I think).
333 $dbh->query(sprintf("DELETE FROM $version_tbl"
334 . " WHERE id=%d AND version=%d",
337 /* Same as above -- bind!
338 $dbh->query(sprintf("INSERT INTO $version_tbl"
339 . " (id,version,mtime,minor_edit,content,versiondata)"
340 . " VALUES(%d,%d,%d,%d,'%s','%s')",
341 $id, $version, $mtime, $minor_edit,
342 $dbh->quoteString($content),
343 $dbh->quoteString($this->_serialize($data))));
345 $dbh->query("INSERT INTO $version_tbl"
346 . " (id,version,mtime,minor_edit,content,versiondata)"
347 . " VALUES(?, ?, ?, ?, ?, ?)",
348 array($id, $version, $mtime, $minor_edit, $content,
349 $this->_serialize($data)));
351 $this->_update_recent_table($id);
352 $this->_update_nonempty_table($id);
358 * Delete an old revision of a page.
360 function delete_versiondata($pagename, $version) {
362 extract($this->_table_names);
365 if ( ($id = $this->_get_pageid($pagename)) ) {
366 $dbh->query("DELETE FROM $version_tbl"
367 . " WHERE id=$id AND version=$version");
368 $this->_update_recent_table($id);
369 // This shouldn't be needed (as long as the latestversion
370 // never gets deleted.) But, let's be safe.
371 $this->_update_nonempty_table($id);
377 * Delete page completely from the database.
378 * I'm not sure if this is what we want. Maybe just delete the revisions
380 function delete_page($pagename) {
382 extract($this->_table_names);
385 if ( ($id = $this->_get_pageid($pagename, false)) ) {
386 $dbh->query("DELETE FROM $version_tbl WHERE id=$id");
387 $dbh->query("DELETE FROM $recent_tbl WHERE id=$id");
388 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
389 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$id");
390 $nlinks = $dbh->getOne("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
392 // We're still in the link table (dangling link) so we can't delete this
394 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
397 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
399 $this->_update_recent_table();
400 $this->_update_nonempty_table();
406 // The only thing we might be interested in updating which we can
407 // do fast in the flags (minor_edit). I think the default
408 // update_versiondata will work fine...
409 //function update_versiondata($pagename, $version, $data) {
412 function set_links($pagename, $links) {
413 // Update link table.
414 // FIXME: optimize: mysql can do this all in one big INSERT.
417 extract($this->_table_names);
420 $pageid = $this->_get_pageid($pagename, true);
422 $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
425 foreach($links as $link) {
426 if (isset($linkseen[$link]))
428 $linkseen[$link] = true;
429 $linkid = $this->_get_pageid($link, true);
430 $dbh->query("INSERT INTO $link_tbl (linkfrom, linkto)"
431 . " VALUES ($pageid, $linkid)");
438 * Find pages which link to or are linked from a page.
440 function get_links($pagename, $reversed = true) {
442 extract($this->_table_names);
445 list($have,$want) = array('linkee', 'linker');
447 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 if ($sortby) $orderby = 'ORDER BY ' . PageList::sortby($sortby,'db');
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 require_once("lib/PageList.php");
508 list($from,$count) = PageList::limit($limit);
509 $result = $dbh->limitQuery($sql, $from, $count);
511 $result = $dbh->query($sql);
513 return new WikiDB_backend_PearDB_iter($this, $result);
519 function text_search($search = '', $fullsearch = false) {
521 extract($this->_table_names);
523 $table = "$nonempty_tbl, $page_tbl";
524 $join_clause = "$nonempty_tbl.id=$page_tbl.id";
525 $fields = $this->page_tbl_fields;
526 $callback = new WikiMethodCb($this, '_sql_match_clause');
529 $table .= ", $recent_tbl";
530 $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
532 $table .= ", $version_tbl";
533 $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
535 $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
536 $callback = new WikiMethodCb($this, '_fullsearch_sql_match_clause');
539 $search_clause = $search->makeSqlClause($callback);
541 $result = $dbh->query("SELECT $fields FROM $table"
542 . " WHERE $join_clause"
543 . " AND ($search_clause)"
544 . " ORDER BY pagename");
546 return new WikiDB_backend_PearDB_iter($this, $result);
549 //Todo: check if the better Mysql MATCH operator is supported,
550 // (ranked search) and also google like expressions.
551 function _sql_match_clause($word) {
552 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
553 $word = $this->_dbh->quoteString($word);
554 //$page_tbl = $this->_table_names['page_tbl'];
555 //Note: Mysql 4.1.0 has a bug which fails with binary fields.
556 // e.g. if word is lowercased.
557 // http://bugs.mysql.com/bug.php?id=1491
558 return "LOWER(pagename) LIKE '%$word%'";
561 function _fullsearch_sql_match_clause($word) {
562 $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
563 $word = $this->_dbh->quoteString($word);
564 //$page_tbl = $this->_table_names['page_tbl'];
565 //Mysql 4.1.1 has a bug which fails here if word is lowercased.
566 return "LOWER(pagename) LIKE '%$word%' OR content LIKE '%$word%'";
570 * Find highest or lowest hit counts.
572 function most_popular($limit=false,$sortby = '') {
574 extract($this->_table_names);
581 $where = " AND hits > 0";
583 if ($sortby) $orderby = ' ORDER BY ' . PageList::sortby($sortby,'db');
584 else $orderby = " ORDER BY hits $order";
585 //$limitclause = $limit ? " LIMIT $limit" : '';
587 . $this->page_tbl_fields
588 . " FROM $nonempty_tbl, $page_tbl"
589 . " WHERE $nonempty_tbl.id=$page_tbl.id"
593 require_once("lib/PageList.php");
594 list($from,$count) = PageList::limit($limit);
595 $result = $dbh->limitQuery($sql, $from, $count);
597 $result = $dbh->query($sql);
600 return new WikiDB_backend_PearDB_iter($this, $result);
604 * Find recent changes.
606 function most_recent($params) {
609 $include_minor_revisions = false;
610 $exclude_major_revisions = false;
611 $include_all_revisions = false;
615 extract($this->_table_names);
619 $pick[] = "mtime >= $since";
622 if ($include_all_revisions) {
623 // Include all revisions of each page.
624 $table = "$page_tbl, $version_tbl";
625 $join_clause = "$page_tbl.id=$version_tbl.id";
627 if ($exclude_major_revisions) {
628 // Include only minor revisions
629 $pick[] = "minor_edit <> 0";
631 elseif (!$include_minor_revisions) {
632 // Include only major revisions
633 $pick[] = "minor_edit = 0";
637 $table = "$page_tbl, $recent_tbl";
638 $join_clause = "$page_tbl.id=$recent_tbl.id";
639 $table .= ", $version_tbl";
640 $join_clause .= " AND $version_tbl.id=$page_tbl.id";
642 if ($exclude_major_revisions) {
643 // Include only most recent minor revision
644 $pick[] = 'version=latestminor';
646 elseif (!$include_minor_revisions) {
647 // Include only most recent major revision
648 $pick[] = 'version=latestmajor';
651 // Include only the latest revision (whether major or minor).
652 $pick[] ='version=latestversion';
660 // $limitclause = $limit ? " LIMIT $limit" : '';
661 $where_clause = $join_clause;
663 $where_clause .= " AND " . join(" AND ", $pick);
665 // FIXME: use SQL_BUFFER_RESULT for mysql?
667 . $this->page_tbl_fields . ", " . $this->version_tbl_fields
669 . " WHERE $where_clause"
670 . " ORDER BY mtime $order";
673 $result = $dbh->limitQuery($sql, 0, $limit);
675 $result = $dbh->query($sql);
678 return new WikiDB_backend_PearDB_iter($this, $result);
682 * Rename page in the database.
684 function rename_page($pagename, $to) {
686 extract($this->_table_names);
689 if ( ($id = $this->_get_pageid($pagename, false)) ) {
690 if ($new = $this->_get_pageid($to, false)) {
692 //this page does not exist (already verified before), but exists in the page table.
693 //so we delete this page.
694 $dbh->query(sprintf("DELETE FROM $page_tbl WHERE id=$id",
695 $dbh->quoteString($to)));
697 $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
698 $dbh->quoteString($to)));
704 function _update_recent_table($pageid = false) {
706 extract($this->_table_names);
707 extract($this->_expressions);
709 $pageid = (int)$pageid;
713 $dbh->query("DELETE FROM $recent_tbl"
714 . ( $pageid ? " WHERE id=$pageid" : ""));
716 $dbh->query( "INSERT INTO $recent_tbl"
717 . " (id, latestversion, latestmajor, latestminor)"
718 . " SELECT id, $maxversion, $maxmajor, $maxminor"
719 . " FROM $version_tbl"
720 . ( $pageid ? " WHERE id=$pageid" : "")
725 function _update_nonempty_table($pageid = false) {
727 extract($this->_table_names);
728 extract($this->_expressions);
730 $pageid = (int)$pageid;
734 $dbh->query("DELETE FROM $nonempty_tbl"
735 . ( $pageid ? " WHERE id=$pageid" : ""));
737 $dbh->query("INSERT INTO $nonempty_tbl (id)"
738 . " SELECT $recent_tbl.id"
739 . " FROM $recent_tbl, $version_tbl"
740 . " WHERE $recent_tbl.id=$version_tbl.id"
741 . " AND version=latestversion"
742 // We have some specifics here (Oracle)
743 //. " AND content<>''"
744 . " AND content $notempty"
745 . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
752 * Grab a write lock on the tables in the SQL database.
754 * Calls can be nested. The tables won't be unlocked until
755 * _unlock_database() is called as many times as _lock_database().
759 function lock($tables = false, $write_lock = true) {
760 if ($this->_lock_count++ == 0)
761 $this->_lock_tables($write_lock);
765 * Actually lock the required tables.
767 function _lock_tables($write_lock) {
768 trigger_error("virtual", E_USER_ERROR);
772 * Release a write lock on the tables in the SQL database.
776 * @param $force boolean Unlock even if not every call to lock() has been matched
777 * by a call to unlock().
779 * @see _lock_database
781 function unlock($tables = false, $force = false) {
782 if ($this->_lock_count == 0)
784 if (--$this->_lock_count <= 0 || $force) {
785 $this->_unlock_tables();
786 $this->_lock_count = 0;
791 * Actually unlock the required tables.
793 function _unlock_tables($write_lock) {
794 trigger_error("virtual", E_USER_ERROR);
801 function _serialize($data) {
804 assert(is_array($data));
805 return serialize($data);
811 function _unserialize($data) {
812 return empty($data) ? array() : unserialize($data);
816 * Callback for PEAR (DB) errors.
820 * @param A PEAR_error object.
822 function _pear_error_callback($error) {
823 if ($this->_is_false_error($error))
826 $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT); // prevent recursive loops.
828 trigger_error($this->_pear_error_message($error), E_USER_ERROR);
832 * Detect false errors messages from PEAR DB.
834 * The version of PEAR DB which ships with PHP 4.0.6 has a bug in that
835 * it doesn't recognize "LOCK" and "UNLOCK" as SQL commands which don't
836 * return any data. (So when a "LOCK" command doesn't return any data,
837 * DB reports it as an error, when in fact, it's not.)
840 * @return bool True iff error is not really an error.
842 function _is_false_error($error) {
843 if ($error->getCode() != DB_ERROR)
846 $query = $this->_dbh->last_query;
848 if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE'
849 . '|DROP|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s/', $query)) {
850 // Last query was not of the sort which doesn't return any data.
851 //" <--kludge for brain-dead syntax coloring
855 if (! in_array('ismanip', get_class_methods('DB'))) {
856 // Pear shipped with PHP 4.0.4pl1 (and before, presumably)
857 // does not have the DB::isManip method.
861 if (DB::isManip($query)) {
862 // If Pear thinks it's an isManip then it wouldn't have thrown
863 // the error we're testing for....
870 function _pear_error_message($error) {
871 $class = get_class($this);
872 $message = "$class: fatal database error\n"
873 . "\t" . $error->getMessage() . "\n"
874 . "\t(" . $error->getDebugInfo() . ")\n";
876 // Prevent password from being exposed during a connection error
877 $safe_dsn = preg_replace('| ( :// .*? ) : .* (?=@) |xs',
878 '\\1:XXXXXXXX', $this->_dsn);
879 return str_replace($this->_dsn, $safe_dsn, $message);
883 * Filter PHP errors notices from PEAR DB code.
885 * The PEAR DB code which ships with PHP 4.0.6 produces spurious
886 * errors and notices. This is an error callback (for use with
887 * ErrorManager which will filter out those spurious messages.)
888 * @see _is_false_error, ErrorManager
891 function _pear_notice_filter($err) {
892 return ( $err->isNotice()
893 && preg_match('|DB[/\\\\]common.php$|', $err->errfile)
894 && $err->errline == 126
895 && preg_match('/Undefined offset: +0\b/', $err->errstr) );
898 /* some variables and functions for DB backend abstraction (action=upgrade) */
899 function database () {
900 return $this->_dbh->dsn['database'];
902 function backendType() {
903 return $this->_dbh->phptype;
905 function connection() {
906 return $this->_dbh->connection;
909 function listOfTables() {
910 return $this->_dbh->getListOf('tables');
912 function listOfFields($database,$table) {
913 if ($this->backendType() == 'mysql') {
915 assert(!empty($database));
916 assert(!empty($table));
917 $result = mysql_list_fields($database, $table, $this->_dbh->connection) or
918 trigger_error(__FILE__.':'.__LINE__.' '.mysql_error(), E_USER_WARNING);
919 if (!$result) return array();
920 $columns = mysql_num_fields($result);
921 for ($i = 0; $i < $columns; $i++) {
922 $fields[] = mysql_field_name($result, $i);
924 mysql_free_result($result);
927 // TODO: try ADODB version?
928 trigger_error("Unsupported dbtype and backend. Either switch to ADODB or check it manually.");
935 * This class is a generic iterator.
937 * WikiDB_backend_PearDB_iter only iterates over things that have
938 * 'pagename', 'pagedata', etc. etc.
940 * Probably WikiDB_backend_PearDB_iter and this class should be merged
941 * (most of the code is cut-and-paste :-( ), but I am trying to make
942 * changes that could be merged easily.
944 * @author: Dan Frankowski
946 class WikiDB_backend_PearDB_generic_iter
947 extends WikiDB_backend_iterator
949 function WikiDB_backend_PearDB_generic_iter(&$backend, &$query_result) {
950 if (DB::isError($query_result)) {
951 // This shouldn't happen, I thought.
952 $backend->_pear_error_callback($query_result);
955 $this->_backend = &$backend;
956 $this->_result = $query_result;
962 return $this->_result->numRows();
966 $backend = &$this->_backend;
970 $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
980 if ($this->_result) {
981 $this->_result->free();
982 $this->_result = false;
987 class WikiDB_backend_PearDB_iter
988 extends WikiDB_backend_PearDB_generic_iter
992 $backend = &$this->_backend;
996 $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
1002 $pagedata = $backend->_extract_page_data($record);
1003 $rec = array('pagename' => $record['pagename'],
1004 'pagedata' => $pagedata);
1006 if (!empty($record['version'])) {
1007 $rec['versiondata'] = $backend->_extract_version_data($record);
1008 $rec['version'] = $record['version'];
1014 // $Log: not supported by cvs2svn $
1015 // Revision 1.53 2004/06/27 10:26:03 rurban
1016 // oci8 patch by Philippe Vanhaesendonck + some ADODB notes+fixes
1018 // Revision 1.52 2004/06/25 14:15:08 rurban
1019 // reduce memory footprint by caching only requested pagedate content (improving most page iterators)
1021 // Revision 1.51 2004/05/12 10:49:55 rurban
1022 // require_once fix for those libs which are loaded before FileFinder and
1023 // its automatic include_path fix, and where require_once doesn't grok
1024 // dirname(__FILE__) != './lib'
1025 // upgrade fix with PearDB
1026 // navbar.tmpl: remove spaces for IE button alignment
1028 // Revision 1.50 2004/05/06 17:30:39 rurban
1029 // CategoryGroup: oops, dos2unix eol
1030 // improved phpwiki_version:
1031 // pre -= .0001 (1.3.10pre: 1030.099)
1032 // -p1 += .001 (1.3.9-p1: 1030.091)
1033 // improved InstallTable for mysql and generic SQL versions and all newer tables so far.
1034 // abstracted more ADODB/PearDB methods for action=upgrade stuff:
1035 // backend->backendType(), backend->database(),
1036 // backend->listOfFields(),
1037 // backend->listOfTables(),
1039 // Revision 1.49 2004/05/03 21:35:30 rurban
1040 // don't use persistent connections with postgres
1042 // Revision 1.48 2004/04/26 20:44:35 rurban
1043 // locking table specific for better databases
1045 // Revision 1.47 2004/04/20 00:06:04 rurban
1046 // themable paging support
1048 // Revision 1.46 2004/04/19 21:51:41 rurban
1049 // php5 compatibility: it works!
1051 // Revision 1.45 2004/04/16 14:19:39 rurban
1052 // updated ADODB notes
1055 // (c-file-style: "gnu")
1059 // c-basic-offset: 4
1060 // c-hanging-comment-ender-p: nil
1061 // indent-tabs-mode: nil