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