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