]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PearDB.php
Cleaned up the password hiding, so that the rest of the DSN is still
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PearDB.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PearDB.php,v 1.22 2002-01-31 17:32:50 dairiki Exp $');
3
4 //require_once('DB.php');
5 require_once('lib/WikiDB/backend.php');
6 //require_once('lib/FileFinder.php');
7 require_once('lib/ErrorManager.php');
8 require_once('lib/pear/DB.php');
9
10 class WikiDB_backend_PearDB
11 extends WikiDB_backend
12 {
13     function WikiDB_backend_PearDB ($dbparams) {
14         // Find and include PEAR's DB.php.
15         //$pearFinder = new PearFileFinder;
16         //$pearFinder->includeOnce('DB.php');
17
18         // Install filter to handle bogus error notices from buggy DB.php's.
19         global $ErrorManager;
20         $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_pear_notice_filter'));
21         
22         // Open connection to database
23         $this->_dsn = $dbparams['dsn'];
24         $dboptions = array('persistent' => true,
25                            'debug' => 2);
26         $this->_dbh = DB::connect($this->_dsn, $dboptions);
27         $dbh = &$this->_dbh;
28         if (DB::isError($dbh)) {
29             trigger_error(sprintf("Can't connect to database: %s",
30                                   $this->_pear_error_message($dbh)),
31                           E_USER_ERROR);
32         }
33         $dbh->setErrorHandling(PEAR_ERROR_CALLBACK,
34                                array($this, '_pear_error_callback'));
35         $dbh->setFetchMode(DB_FETCHMODE_ASSOC);
36
37         $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
38
39         $this->_table_names
40             = array('page_tbl'     => $prefix . 'page',
41                     'version_tbl'  => $prefix . 'version',
42                     'link_tbl'     => $prefix . 'link',
43                     'recent_tbl'   => $prefix . 'recent',
44                     'nonempty_tbl' => $prefix . 'nonempty');
45
46         $this->_expressions
47             = array('maxmajor'     => "MAX(CASE WHEN minor_edit=0 THEN version END)",
48                     'maxminor'     => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
49                     'maxversion'   => "MAX(version)");
50         
51         $this->_lock_count = 0;
52     }
53     
54     /**
55      * Close database connection.
56      */
57     function close () {
58         if (!$this->_dbh)
59             return;
60         if ($this->_lock_count) {
61             trigger_error( "WARNING: database still locked " . '(lock_count = $this->_lock_count)' . "\n<br />",
62                           E_USER_WARNING);
63         }
64         $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT);        // prevent recursive loops.
65         $this->unlock('force');
66
67         $this->_dbh->disconnect();
68         $this->_dbh = false;
69     }
70
71
72     /*
73      * Test fast wikipage.
74      */
75     function is_wiki_page($pagename) {
76         $dbh = &$this->_dbh;
77         extract($this->_table_names);
78         return $dbh->getOne(sprintf("SELECT $page_tbl.id"
79                                     . " FROM $nonempty_tbl, $page_tbl"
80                                     . " WHERE $nonempty_tbl.id=$page_tbl.id"
81                                     . "   AND pagename='%s'",
82                                     $dbh->quoteString($pagename)));
83     }
84         
85     function get_all_pagenames() {
86         $dbh = &$this->_dbh;
87         extract($this->_table_names);
88         return $dbh->getCol("SELECT pagename"
89                             . " FROM $nonempty_tbl, $page_tbl"
90                             . " WHERE $nonempty_tbl.id=$page_tbl.id");
91     }
92             
93     /**
94      * Read page information from database.
95      */
96     function get_pagedata($pagename) {
97         $dbh = &$this->_dbh;
98         $page_tbl = $this->_table_names['page_tbl'];
99
100         //trigger_error("GET_PAGEDATA $pagename", E_USER_NOTICE);
101
102         $result = $dbh->getRow(sprintf("SELECT * FROM $page_tbl WHERE pagename='%s'",
103                                        $dbh->quoteString($pagename)),
104                                DB_FETCHMODE_ASSOC);
105         if (!$result)
106             return false;
107         return $this->_extract_page_data($result);
108     }
109
110     function  _extract_page_data(&$query_result) {
111         extract($query_result);
112         $data = empty($pagedata) ? array() : unserialize($pagedata);
113         $data['hits'] = $hits;
114         return $data;
115     }
116
117     function update_pagedata($pagename, $newdata) {
118         $dbh = &$this->_dbh;
119         $page_tbl = $this->_table_names['page_tbl'];
120
121         // Hits is the only thing we can update in a fast manner.
122         if (count($newdata) == 1 && isset($newdata['hits'])) {
123             // Note that this will fail silently if the page does not
124             // have a record in the page table.  Since it's just the
125             // hit count, who cares?
126             $dbh->query(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename='%s'",
127                                 $newdata['hits'], $dbh->quoteString($pagename)));
128             return;
129         }
130
131         $this->lock();
132         $data = $this->get_pagedata($pagename);
133         if (!$data) {
134             $data = array();
135             $this->_get_pageid($pagename, true); // Creates page record
136         }
137         
138         @$hits = (int)$data['hits'];
139         unset($data['hits']);
140
141         foreach ($newdata as $key => $val) {
142             if ($key == 'hits')
143                 $hits = (int)$val;
144             else if (empty($val))
145                 unset($data[$key]);
146             else
147                 $data[$key] = $val;
148         }
149
150         $dbh->query(sprintf("UPDATE $page_tbl"
151                             . " SET hits=%d, pagedata='%s'"
152                             . " WHERE pagename='%s'",
153                             $hits,
154                             $dbh->quoteString(serialize($data)),
155                             $dbh->quoteString($pagename)));
156
157         $this->unlock();
158     }
159
160     function _get_pageid($pagename, $create_if_missing = false) {
161         
162         $dbh = &$this->_dbh;
163         $page_tbl = $this->_table_names['page_tbl'];
164         
165         $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
166                          $dbh->quoteString($pagename));
167
168         if (!$create_if_missing)
169             return $dbh->getOne($query);
170
171         $this->lock();
172         $id = $dbh->getOne($query);
173         if (empty($id)) {
174             $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
175             $id = $max_id + 1;
176             $dbh->query(sprintf("INSERT INTO $page_tbl"
177                                 . " (id,pagename,hits)"
178                                 . " VALUES (%d,'%s',0)",
179                                 $id, $dbh->quoteString($pagename)));
180         }
181         $this->unlock();
182         return $id;
183     }
184
185     function get_latest_version($pagename) {
186         $dbh = &$this->_dbh;
187         extract($this->_table_names);
188         return
189             (int)$dbh->getOne(sprintf("SELECT latestversion"
190                                       . " FROM $page_tbl, $recent_tbl"
191                                       . " WHERE $page_tbl.id=$recent_tbl.id"
192                                       . "  AND pagename='%s'",
193                                       $dbh->quoteString($pagename)));
194     }
195
196     function get_previous_version($pagename, $version) {
197         $dbh = &$this->_dbh;
198         extract($this->_table_names);
199         
200         return
201             (int)$dbh->getOne(sprintf("SELECT version"
202                                       . " FROM $version_tbl, $page_tbl"
203                                       . " WHERE $version_tbl.id=$page_tbl.id"
204                                       . "  AND pagename='%s'"
205                                       . "  AND version < %d"
206                                       . " ORDER BY version DESC"
207                                       . " LIMIT 1",
208                                       $dbh->quoteString($pagename),
209                                       $version));
210     }
211     
212     /**
213      * Get version data.
214      *
215      * @param $version int Which version to get.
216      *
217      * @return hash The version data, or false if specified version does not
218      *              exist.
219      */
220     function get_versiondata($pagename, $version, $want_content = false) {
221         $dbh = &$this->_dbh;
222         extract($this->_table_names);
223                 
224         assert(!empty($pagename));
225         assert($version > 0);
226         
227         //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
228         // FIXME: optimization: sometimes don't get page data?
229
230         if ($want_content) {
231             $fields = "*";
232         }
233         else {
234             $fields = ("$page_tbl.*,"
235                        . "mtime,minor_edit,versiondata,"
236                        . "content<>'' AS have_content");
237         }
238
239         $result = $dbh->getRow(sprintf("SELECT $fields"
240                                        . " FROM $page_tbl, $version_tbl"
241                                        . " WHERE $page_tbl.id=$version_tbl.id"
242                                        . "  AND pagename='%s'"
243                                        . "  AND version=%d",
244                                        $dbh->quoteString($pagename), $version),
245                                DB_FETCHMODE_ASSOC);
246
247         return $this->_extract_version_data($result);
248     }
249
250     function _extract_version_data(&$query_result) {
251         if (!$query_result)
252             return false;
253
254         extract($query_result);
255         $data = empty($versiondata) ? array() : unserialize($versiondata);
256
257         $data['mtime'] = $mtime;
258         $data['is_minor_edit'] = !empty($minor_edit);
259         
260         if (isset($content))
261             $data['%content'] = $content;
262         elseif ($have_content)
263             $data['%content'] = true;
264         else
265             $data['%content'] = '';
266
267         // FIXME: this is ugly.
268         if (isset($pagename)) {
269             // Query also includes page data.
270             // We might as well send that back too...
271             $data['%pagedata'] = $this->_extract_page_data($query_result);
272         }
273
274         return $data;
275     }
276
277
278     /**
279      * Create a new revision of a page.
280      */
281     function set_versiondata($pagename, $version, $data) {
282         $dbh = &$this->_dbh;
283         $version_tbl = $this->_table_names['version_tbl'];
284         
285         $minor_edit = (int) !empty($data['is_minor_edit']);
286         unset($data['is_minor_edit']);
287         
288         $mtime = (int)$data['mtime'];
289         unset($data['mtime']);
290         assert(!empty($mtime));
291
292         @$content = (string) $data['%content'];
293         unset($data['%content']);
294
295         unset($data['%pagedata']);
296         
297         $this->lock();
298         $id = $this->_get_pageid($pagename, true);
299
300         // FIXME: optimize: mysql can do this with one REPLACE INTO (I think).
301         $dbh->query(sprintf("DELETE FROM $version_tbl"
302                             . " WHERE id=%d AND version=%d",
303                             $id, $version));
304
305         $dbh->query(sprintf("INSERT INTO $version_tbl"
306                             . " (id,version,mtime,minor_edit,content,versiondata)"
307                             . " VALUES(%d,%d,%d,%d,'%s','%s')",
308                             $id, $version, $mtime, $minor_edit,
309                             $dbh->quoteString($content),
310                             $dbh->quoteString(serialize($data))));
311
312         $this->_update_recent_table($id);
313         $this->_update_nonempty_table($id);
314         
315         $this->unlock();
316     }
317     
318     /**
319      * Delete an old revision of a page.
320      */
321     function delete_versiondata($pagename, $version) {
322         $dbh = &$this->_dbh;
323         extract($this->_table_names);
324
325         $this->lock();
326         if ( ($id = $this->_get_pageid($pagename)) ) {
327             $dbh->query("DELETE FROM $version_tbl"
328                         . " WHERE id=$id AND version=$version");
329             $this->_update_recent_table($id);
330             // This shouldn't be needed (as long as the latestversion
331             // never gets deleted.)  But, let's be safe.
332             $this->_update_nonempty_table($id);
333         }
334         $this->unlock();
335     }
336
337     /**
338      * Delete page from the database.
339      */
340     function delete_page($pagename) {
341         $dbh = &$this->_dbh;
342         extract($this->_table_names);
343         
344         $this->lock();
345         if ( ($id = $this->_get_pageid($pagename, 'id')) ) {
346             $dbh->query("DELETE FROM $version_tbl  WHERE id=$id");
347             $dbh->query("DELETE FROM $recent_tbl   WHERE id=$id");
348             $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
349             $dbh->query("DELETE FROM $link_tbl     WHERE linkfrom=$id");
350             $nlinks = $dbh->getOne("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
351             if ($nlinks) {
352                 // We're still in the link table (dangling link) so we can't delete this
353                 // altogether.
354                 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
355             }
356             else {
357                 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
358             }
359             $this->_update_recent_table();
360             $this->_update_nonempty_table();
361         }
362         $this->unlock();
363     }
364             
365
366     // The only thing we might be interested in updating which we can
367     // do fast in the flags (minor_edit).   I think the default
368     // update_versiondata will work fine...
369     //function update_versiondata($pagename, $version, $data) {
370     //}
371
372     function set_links($pagename, $links) {
373         // Update link table.
374         // FIXME: optimize: mysql can do this all in one big INSERT.
375
376         $dbh = &$this->_dbh;
377         extract($this->_table_names);
378
379         $this->lock();
380         $pageid = $this->_get_pageid($pagename, true);
381
382         $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
383
384         if ($links) {
385             foreach($links as $link) {
386                 if (isset($linkseen[$link]))
387                     continue;
388                 $linkseen[$link] = true;
389                 $linkid = $this->_get_pageid($link, true);
390                 $dbh->query("INSERT INTO $link_tbl (linkfrom, linkto)"
391                             . " VALUES ($pageid, $linkid)");
392             }
393         }
394         $this->unlock();
395     }
396     
397     /**
398      * Find pages which link to or are linked from a page.
399      */
400     function get_links($pagename, $reversed = true) {
401         $dbh = &$this->_dbh;
402         extract($this->_table_names);
403
404         if ($reversed)
405             list($have,$want) = array('linkee', 'linker');
406         else
407             list($have,$want) = array('linker', 'linkee');
408
409         $qpagename = $dbh->quoteString($pagename);
410         
411         $result = $dbh->query("SELECT $want.*"
412                               . " FROM $link_tbl, $page_tbl AS linker, $page_tbl AS linkee"
413                               . " WHERE linkfrom=linker.id AND linkto=linkee.id"
414                               . "  AND $have.pagename='$qpagename'"
415                               //. " GROUP BY $want.id"
416                               . " ORDER BY $want.pagename",
417                               DB_FETCHMODE_ASSOC);
418         
419         return new WikiDB_backend_PearDB_iter($this, $result);
420     }
421
422     function get_all_pages($include_deleted) {
423         $dbh = &$this->_dbh;
424         extract($this->_table_names);
425
426         if ($include_deleted) {
427             $result = $dbh->query("SELECT * FROM $page_tbl ORDER BY pagename");
428         }
429         else {
430             $result = $dbh->query("SELECT $page_tbl.*"
431                                   . " FROM $nonempty_tbl, $page_tbl"
432                                   . " WHERE $nonempty_tbl.id=$page_tbl.id"
433                                   . " ORDER BY pagename");
434         }
435
436         return new WikiDB_backend_PearDB_iter($this, $result);
437     }
438         
439     /**
440      * Title search.
441      */
442     function text_search($search = '', $fullsearch = false) {
443         $dbh = &$this->_dbh;
444         extract($this->_table_names);
445         
446         $table = "$nonempty_tbl, $page_tbl";
447         $join_clause = "$nonempty_tbl.id=$page_tbl.id";
448         $fields = "$page_tbl.*";
449         $callback = new WikiMethodCb($this, '_sql_match_clause');
450         
451         if ($fullsearch) {
452             $table .= ", $recent_tbl";
453             $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
454
455             $table .= ", $version_tbl";
456             $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
457
458             $fields .= ",$version_tbl.*";
459             $callback = new WikiMethodCb($this, '_fullsearch_sql_match_clause');
460         }
461         
462         $search_clause = $search->makeSqlClause($callback);
463         
464         $result = $dbh->query("SELECT $fields FROM $table"
465                               . " WHERE $join_clause"
466                               . "  AND ($search_clause)"
467                               . " ORDER BY pagename");
468         
469         return new WikiDB_backend_PearDB_iter($this, $result);
470     }
471
472     function _sql_match_clause($word) {
473         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
474         $word = $this->_dbh->quoteString($word);
475         return "LOWER(pagename) LIKE '%$word%'";
476     }
477
478     function _fullsearch_sql_match_clause($word) {
479         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
480         $word = $this->_dbh->quoteString($word);
481         return "LOWER(pagename) LIKE '%$word%' OR content LIKE '%$word%'";
482     }
483
484     /**
485      * Find highest hit counts.
486      */
487     function most_popular($limit) {
488         $dbh = &$this->_dbh;
489         extract($this->_table_names);
490
491         $limitclause = $limit ? " LIMIT $limit" : '';
492         $result = $dbh->query("SELECT $page_tbl.*"
493                               . " FROM $nonempty_tbl, $page_tbl"
494                               . " WHERE $nonempty_tbl.id=$page_tbl.id"
495                               . " ORDER BY hits DESC"
496                               . " $limitclause");
497
498         return new WikiDB_backend_PearDB_iter($this, $result);
499     }
500
501     /**
502      * Find recent changes.
503      */
504     function most_recent($params) {
505         $limit = 0;
506         $since = 0;
507         $include_minor_revisions = false;
508         $exclude_major_revisions = false;
509         $include_all_revisions = false;
510         extract($params);
511
512         $dbh = &$this->_dbh;
513         extract($this->_table_names);
514
515         $pick = array();
516         if ($since)
517             $pick[] = "mtime >= $since";
518         
519         if ($include_all_revisions) {
520             // Include all revisions of each page.
521             $table = "$page_tbl, $version_tbl";
522             $join_clause = "$page_tbl.id=$version_tbl.id";
523
524             if ($exclude_major_revisions) {
525                 // Include only minor revisions
526                 $pick[] = "minor_edit <> 0";
527             }
528             elseif (!$include_minor_revisions) {
529                 // Include only major revisions
530                 $pick[] = "minor_edit = 0";
531             }
532         }
533         else {
534             $table = "$page_tbl, $recent_tbl";
535             $join_clause = "$page_tbl.id=$recent_tbl.id";
536             $table .= ", $version_tbl";
537             $join_clause .= " AND $version_tbl.id=$page_tbl.id";
538                 
539             if ($exclude_major_revisions) {
540                 // Include only most recent minor revision
541                 $pick[] = 'version=latestminor';
542             }
543             elseif (!$include_minor_revisions) {
544                 // Include only most recent major revision
545                 $pick[] = 'version=latestmajor';
546             }
547             else {
548                 // Include only the latest revision (whether major or minor).
549                 $pick[] ='version=latestversion';
550             }
551         }
552
553         $limitclause = $limit ? " LIMIT $limit" : '';
554         $where_clause = $join_clause;
555         if ($pick)
556             $where_clause .= " AND " . join(" AND ", $pick);
557
558         // FIXME: use SQL_BUFFER_RESULT for mysql?
559         $result = $dbh->query("SELECT $page_tbl.*,$version_tbl.*"
560                               . " FROM $table"
561                               . " WHERE $where_clause"
562                               . " ORDER BY mtime DESC"
563                               . $limitclause);
564
565         return new WikiDB_backend_PearDB_iter($this, $result);
566     }
567
568     function _update_recent_table($pageid = false) {
569         $dbh = &$this->_dbh;
570         extract($this->_table_names);
571         extract($this->_expressions);
572
573         $pageid = (int)$pageid;
574
575         $this->lock();
576
577         $dbh->query("DELETE FROM $recent_tbl"
578                     . ( $pageid ? " WHERE id=$pageid" : ""));
579         
580         $dbh->query( "INSERT INTO $recent_tbl"
581                      . " (id, latestversion, latestmajor, latestminor)"
582                      . " SELECT id, $maxversion, $maxmajor, $maxminor"
583                      . " FROM $version_tbl"
584                      . ( $pageid ? " WHERE id=$pageid" : "")
585                      . " GROUP BY id" );
586         $this->unlock();
587     }
588
589     function _update_nonempty_table($pageid = false) {
590         $dbh = &$this->_dbh;
591         extract($this->_table_names);
592
593         $pageid = (int)$pageid;
594
595         $this->lock();
596
597         $dbh->query("DELETE FROM $nonempty_tbl"
598                     . ( $pageid ? " WHERE id=$pageid" : ""));
599
600         $dbh->query("INSERT INTO $nonempty_tbl (id)"
601                     . " SELECT $recent_tbl.id"
602                     . " FROM $recent_tbl, $version_tbl"
603                     . " WHERE $recent_tbl.id=$version_tbl.id"
604                     . "       AND version=latestversion"
605                     . "  AND content<>''"
606                     . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
607
608         $this->unlock();
609     }
610
611
612     /**
613      * Grab a write lock on the tables in the SQL database.
614      *
615      * Calls can be nested.  The tables won't be unlocked until
616      * _unlock_database() is called as many times as _lock_database().
617      *
618      * @access protected
619      */
620     function lock($write_lock = true) {
621         if ($this->_lock_count++ == 0)
622             $this->_lock_tables($write_lock);
623     }
624
625     /**
626      * Actually lock the required tables.
627      */
628     function _lock_tables($write_lock) {
629         trigger_error("virtual", E_USER_ERROR);
630     }
631     
632     /**
633      * Release a write lock on the tables in the SQL database.
634      *
635      * @access protected
636      *
637      * @param $force boolean Unlock even if not every call to lock() has been matched
638      * by a call to unlock().
639      *
640      * @see _lock_database
641      */
642     function unlock($force = false) {
643         if ($this->_lock_count == 0)
644             return;
645         if (--$this->_lock_count <= 0 || $force) {
646             $this->_unlock_tables();
647             $this->_lock_count = 0;
648         }
649     }
650
651     /**
652      * Actually unlock the required tables.
653      */
654     function _unlock_tables($write_lock) {
655         trigger_error("virtual", E_USER_ERROR);
656     }
657     
658     /**
659      * Callback for PEAR (DB) errors.
660      *
661      * @access protected
662      *
663      * @param A PEAR_error object.
664      */
665     function _pear_error_callback($error) {
666         if ($this->_is_false_error($error))
667             return;
668         
669         $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT);        // prevent recursive loops.
670         $this->close();
671         trigger_error($this->_pear_error_message($error), E_USER_ERROR);
672     }
673
674     /**
675      * Detect false errors messages from PEAR DB.
676      *
677      * The version of PEAR DB which ships with PHP 4.0.6 has a bug in that
678      * it doesn't recognize "LOCK" and "UNLOCK" as SQL commands which don't
679      * return any data.  (So when a "LOCK" command doesn't return any data,
680      * DB reports it as an error, when in fact, it's not.)
681      *
682      * @access private
683      * @return bool True iff error is not really an error.
684      */
685     function _is_false_error($error) {
686         if ($error->getCode() != DB_ERROR)
687             return false;
688
689         $query = $this->_dbh->last_query;
690
691         if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE'
692                          . '|DROP|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s/', $query)) {
693             // Last query was not of the sort which doesn't return any data.
694             //" <--kludge for brain-dead syntax coloring
695             return false;
696         }
697         
698         if (! in_array('ismanip', get_class_methods('DB'))) {
699             // Pear shipped with PHP 4.0.4pl1 (and before, presumably)
700             // does not have the DB::isManip method.
701             return true;
702         }
703         
704         if (DB::isManip($query)) {
705             // If Pear thinks it's an isManip then it wouldn't have thrown
706             // the error we're testing for....
707             return false;
708         }
709
710         return true;
711     }
712
713     function _pear_error_message($error) {
714         $class = get_class($this);
715         $message = "$class: fatal database error\n"
716              . "\t" . $error->getMessage() . "\n"
717              . "\t(" . $error->getDebugInfo() . ")\n";
718
719         // Prevent password from being exposed during a connection error
720         $safe_dsn = preg_replace('| ( :// .*? ) : .* (?=@) |xs',
721                                  '\\1:XXXXXXXX', $this->_dsn);
722         return str_replace($this->_dsn, $safe_dsn, $message);
723     }
724
725     /**
726      * Filter PHP errors notices from PEAR DB code.
727      *
728      * The PEAR DB code which ships with PHP 4.0.6 produces spurious
729      * errors and notices.  This is an error callback (for use with
730      * ErrorManager which will filter out those spurious messages.)
731      * @see _is_false_error, ErrorManager
732      * @access private
733      */
734     function _pear_notice_filter($err) {
735         return ( $err->isNotice()
736                  && preg_match('|DB[/\\\\]common.php$|', $err->errfile)
737                  && $err->errline == 126
738                  && preg_match('/Undefined offset: +0\b/', $err->errstr) );
739     }
740 };
741
742 class WikiDB_backend_PearDB_iter
743 extends WikiDB_backend_iterator
744 {
745     function WikiDB_backend_PearDB_iter(&$backend, &$query_result) {
746         if (DB::isError($query_result)) {
747             // This shouldn't happen, I thought.
748             $backend->_pear_error_callback($query_result);
749         }
750         
751         $this->_backend = &$backend;
752         $this->_result = $query_result;
753     }
754     
755     function next() {
756         $backend = &$this->_backend;
757         if (!$this->_result)
758             return false;
759
760         $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
761         if (!$record) {
762             $this->free();
763             return false;
764         }
765         
766         $pagedata = $backend->_extract_page_data($record);
767         $rec = array('pagename' => $record['pagename'],
768                      'pagedata' => $pagedata);
769
770         if (!empty($record['version'])) {
771             $rec['versiondata'] = $backend->_extract_version_data($record);
772             $rec['version'] = $record['version'];
773         }
774         
775         return $rec;
776     }
777
778     function free () {
779         if ($this->_result) {
780             $this->_result->free();
781             $this->_result = false;
782         }
783     }
784 }
785
786 // (c-file-style: "gnu")
787 // Local Variables:
788 // mode: php
789 // tab-width: 8
790 // c-basic-offset: 4
791 // c-hanging-comment-ender-p: nil
792 // indent-tabs-mode: nil
793 // End:   
794 ?>