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