]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/PearDB.php
fix wanted_pages SQL syntax
[SourceForge/phpwiki.git] / lib / WikiDB / backend / PearDB.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PearDB.php,v 1.93 2005-10-10 19:42:15 rurban Exp $');
3
4 require_once('lib/WikiDB/backend.php');
5 //require_once('lib/FileFinder.php');
6 //require_once('lib/ErrorManager.php');
7
8 class WikiDB_backend_PearDB
9 extends WikiDB_backend
10 {
11     var $_dbh;
12
13     function WikiDB_backend_PearDB ($dbparams) {
14         // Find and include PEAR's DB.php. maybe we should force our private version again...
15         // if DB would have exported its version number, it would be easier.
16         @require_once('DB/common.php'); // Either our local pear copy or the system one
17         // check the version!
18         $name = check_php_version(5) ? "escapeSimple" : strtolower("escapeSimple");
19         // TODO: apparently some Pear::Db version adds LIMIT 1,0 to getOne(), 
20         // which is invalid for "select version()"
21         if (!in_array($name, get_class_methods("DB_common"))) {
22             $finder = new FileFinder;
23             $dir = dirname(__FILE__)."/../../pear";
24             $finder->_prepend_to_include_path($dir);
25             include_once("$dir/DB/common.php"); // use our version instead.
26             if (!in_array($name, get_class_methods("DB_common"))) {
27                 $pearFinder = new PearFileFinder("lib/pear");
28                 $pearFinder->includeOnce('DB.php');
29             } else {
30                 include_once("$dir/DB.php");
31             }
32         } else {
33           include_once("DB.php");
34         }
35
36         // Install filter to handle bogus error notices from buggy DB.php's.
37         // TODO: check the Pear_DB version, but how?
38         if (0) {
39             global $ErrorManager;
40             $ErrorManager->pushErrorHandler(new WikiMethodCb($this, '_pear_notice_filter'));
41             $this->_pearerrhandler = true;
42         }
43         
44         // Open connection to database
45         $this->_dsn = $dbparams['dsn'];
46         $this->_dbparams = $dbparams;
47         $this->_lock_count = 0;
48
49         // persistent is usually a DSN option: we override it with a config value.
50         //   phptype://username:password@hostspec/database?persistent=false
51         $dboptions = array('persistent' => DATABASE_PERSISTENT,
52                            'debug' => 2);
53         //if (preg_match('/^pgsql/', $this->_dsn)) $dboptions['persistent'] = false;
54         $this->_dbh = DB::connect($this->_dsn, $dboptions);
55         $dbh = &$this->_dbh;
56         if (DB::isError($dbh)) {
57             trigger_error(sprintf("Can't connect to database: %s",
58                                   $this->_pear_error_message($dbh)),
59                           E_USER_ERROR);
60         }
61         $dbh->setErrorHandling(PEAR_ERROR_CALLBACK,
62                                array($this, '_pear_error_callback'));
63         $dbh->setFetchMode(DB_FETCHMODE_ASSOC);
64
65         $prefix = isset($dbparams['prefix']) ? $dbparams['prefix'] : '';
66         $this->_table_names
67             = array('page_tbl'     => $prefix . 'page',
68                     'version_tbl'  => $prefix . 'version',
69                     'link_tbl'     => $prefix . 'link',
70                     'recent_tbl'   => $prefix . 'recent',
71                     'nonempty_tbl' => $prefix . 'nonempty');
72         $page_tbl = $this->_table_names['page_tbl'];
73         $version_tbl = $this->_table_names['version_tbl'];
74         $this->page_tbl_fields = "$page_tbl.id AS id, $page_tbl.pagename AS pagename, $page_tbl.hits AS hits";
75         $this->version_tbl_fields = "$version_tbl.version AS version, $version_tbl.mtime AS mtime, ".
76             "$version_tbl.minor_edit AS minor_edit, $version_tbl.content AS content, $version_tbl.versiondata AS versiondata";
77
78         $this->_expressions
79             = array('maxmajor'     => "MAX(CASE WHEN minor_edit=0 THEN version END)",
80                     'maxminor'     => "MAX(CASE WHEN minor_edit<>0 THEN version END)",
81                     'maxversion'   => "MAX(version)",
82                     'notempty'     => "<>''",
83                     'iscontent'    => "content<>''");
84         
85     }
86     
87     /**
88      * Close database connection.
89      */
90     function close () {
91         if (!$this->_dbh)
92             return;
93         if ($this->_lock_count) {
94             trigger_error( "WARNING: database still locked " . '(lock_count = $this->_lock_count)' . "\n<br />",
95                           E_USER_WARNING);
96         }
97         $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT);        // prevent recursive loops.
98         $this->unlock('force');
99
100         $this->_dbh->disconnect();
101
102         if (!empty($this->_pearerrhandler)) {
103             $GLOBALS['ErrorManager']->popErrorHandler();
104         }
105     }
106
107
108     /*
109      * Test fast wikipage.
110      */
111     function is_wiki_page($pagename) {
112         $dbh = &$this->_dbh;
113         extract($this->_table_names);
114         return $dbh->getOne(sprintf("SELECT $page_tbl.id as id"
115                                     . " FROM $nonempty_tbl, $page_tbl"
116                                     . " WHERE $nonempty_tbl.id=$page_tbl.id"
117                                     . "   AND pagename='%s'",
118                                     $dbh->escapeSimple($pagename)));
119     }
120         
121     function get_all_pagenames() {
122         $dbh = &$this->_dbh;
123         extract($this->_table_names);
124         return $dbh->getCol("SELECT pagename"
125                             . " FROM $nonempty_tbl, $page_tbl"
126                             . " WHERE $nonempty_tbl.id=$page_tbl.id");
127     }
128
129     function numPages($filter=false, $exclude='') {
130         $dbh = &$this->_dbh;
131         extract($this->_table_names);
132         return $dbh->getOne("SELECT count(*)"
133                             . " FROM $nonempty_tbl, $page_tbl"
134                             . " WHERE $nonempty_tbl.id=$page_tbl.id");
135     }
136     
137     function increaseHitCount($pagename) {
138         $dbh = &$this->_dbh;
139         // Hits is the only thing we can update in a fast manner.
140         // Note that this will fail silently if the page does not
141         // have a record in the page table.  Since it's just the
142         // hit count, who cares?
143         $dbh->query(sprintf("UPDATE %s SET hits=hits+1 WHERE pagename='%s'",
144                             $this->_table_names['page_tbl'],
145                             $dbh->escapeSimple($pagename)));
146         return;
147     }
148
149     /**
150      * Read page information from database.
151      */
152     function get_pagedata($pagename) {
153         $dbh = &$this->_dbh;
154         //trigger_error("GET_PAGEDATA $pagename", E_USER_NOTICE);
155         $result = $dbh->getRow(sprintf("SELECT hits,pagedata FROM %s WHERE pagename='%s'",
156                                        $this->_table_names['page_tbl'],
157                                        $dbh->escapeSimple($pagename)),
158                                DB_FETCHMODE_ASSOC);
159         return $result ? $this->_extract_page_data($result) : false;
160     }
161
162     function  _extract_page_data($data) {
163         if (empty($data)) return array();
164         elseif (empty($data['pagedata'])) return $data;
165         else {
166             $data = array_merge($data, $this->_unserialize($data['pagedata']));
167             unset($data['pagedata']);
168             return $data;
169         }
170     }
171
172     function update_pagedata($pagename, $newdata) {
173         $dbh = &$this->_dbh;
174         $page_tbl = $this->_table_names['page_tbl'];
175
176         // Hits is the only thing we can update in a fast manner.
177         if (count($newdata) == 1 && isset($newdata['hits'])) {
178             // Note that this will fail silently if the page does not
179             // have a record in the page table.  Since it's just the
180             // hit count, who cares?
181             $dbh->query(sprintf("UPDATE $page_tbl SET hits=%d WHERE pagename='%s'",
182                                 $newdata['hits'], $dbh->escapeSimple($pagename)));
183             return;
184         }
185
186         $this->lock(array($page_tbl), true);
187         $data = $this->get_pagedata($pagename);
188         if (!$data) {
189             $data = array();
190             $this->_get_pageid($pagename, true); // Creates page record
191         }
192         
193         @$hits = (int)$data['hits'];
194         unset($data['hits']);
195
196         foreach ($newdata as $key => $val) {
197             if ($key == 'hits')
198                 $hits = (int)$val;
199             else if (empty($val))
200                 unset($data[$key]);
201             else
202                 $data[$key] = $val;
203         }
204
205         /* Portability issue -- not all DBMS supports huge strings 
206          * so we need to 'bind' instead of building a simple SQL statment.
207          * Note that we do not need to escapeSimple when we bind
208         $dbh->query(sprintf("UPDATE $page_tbl"
209                             . " SET hits=%d, pagedata='%s'"
210                             . " WHERE pagename='%s'",
211                             $hits,
212                             $dbh->escapeSimple($this->_serialize($data)),
213                             $dbh->escapeSimple($pagename)));
214         */
215         $sth = $dbh->query("UPDATE $page_tbl"
216                            . " SET hits=?, pagedata=?"
217                            . " WHERE pagename=?",
218                            array($hits, $this->_serialize($data), $pagename));
219         $this->unlock(array($page_tbl));
220     }
221
222     function get_cached_html($pagename) {
223         $dbh = &$this->_dbh;
224         $page_tbl = $this->_table_names['page_tbl'];
225         return $dbh->GetOne(sprintf("SELECT cached_html FROM $page_tbl WHERE pagename='%s'",
226                                     $dbh->escapeSimple($pagename)));
227     }
228
229     function set_cached_html($pagename, $data) {
230         $dbh = &$this->_dbh;
231         $page_tbl = $this->_table_names['page_tbl'];
232         $sth = $dbh->query("UPDATE $page_tbl"
233                            . " SET cached_html=?"
234                            . " WHERE pagename=?",
235                            array($data, $pagename));
236     }
237
238     function _get_pageid($pagename, $create_if_missing = false) {
239         
240         // check id_cache
241         global $request;
242         $cache =& $request->_dbi->_cache->_id_cache;
243         if (isset($cache[$pagename])) {
244             if ($cache[$pagename] or !$create_if_missing) {
245                 return $cache[$pagename];
246             }
247         }
248
249         $dbh = &$this->_dbh;
250         $page_tbl = $this->_table_names['page_tbl'];
251         
252         $query = sprintf("SELECT id FROM $page_tbl WHERE pagename='%s'",
253                          $dbh->escapeSimple($pagename));
254
255         if (!$create_if_missing)
256             return $dbh->getOne($query);
257
258         $id = $dbh->getOne($query);
259         if (empty($id)) {
260             $this->lock(array($page_tbl), true); // write lock
261             $max_id = $dbh->getOne("SELECT MAX(id) FROM $page_tbl");
262             $id = $max_id + 1;
263             $dbh->query(sprintf("INSERT INTO $page_tbl"
264                                 . " (id,pagename,hits)"
265                                 . " VALUES (%d,'%s',0)",
266                                 $id, $dbh->escapeSimple($pagename)));
267             $this->unlock(array($page_tbl));
268         }
269         return $id;
270     }
271
272     function get_latest_version($pagename) {
273         $dbh = &$this->_dbh;
274         extract($this->_table_names);
275         return
276             (int)$dbh->getOne(sprintf("SELECT latestversion"
277                                       . " FROM $page_tbl, $recent_tbl"
278                                       . " WHERE $page_tbl.id=$recent_tbl.id"
279                                       . "  AND pagename='%s'",
280                                       $dbh->escapeSimple($pagename)));
281     }
282
283     function get_previous_version($pagename, $version) {
284         $dbh = &$this->_dbh;
285         extract($this->_table_names);
286         
287         return
288             (int)$dbh->getOne(sprintf("SELECT version"
289                                       . " FROM $version_tbl, $page_tbl"
290                                       . " WHERE $version_tbl.id=$page_tbl.id"
291                                       . "  AND pagename='%s'"
292                                       . "  AND version < %d"
293                                       . " ORDER BY version DESC",
294                                       /* Non portable and useless anyway with getOne
295                                       . " LIMIT 1",
296                                       */
297                                       $dbh->escapeSimple($pagename),
298                                       $version));
299     }
300     
301     /**
302      * Get version data.
303      *
304      * @param $version int Which version to get.
305      *
306      * @return hash The version data, or false if specified version does not
307      *              exist.
308      */
309     function get_versiondata($pagename, $version, $want_content = false) {
310         $dbh = &$this->_dbh;
311         extract($this->_table_names);
312         extract($this->_expressions);
313
314         assert(is_string($pagename) and $pagename != "");
315         assert($version > 0);
316         
317         //trigger_error("GET_REVISION $pagename $version $want_content", E_USER_NOTICE);
318         // FIXME: optimization: sometimes don't get page data?
319         if ($want_content) {
320             $fields = $this->page_tbl_fields 
321                 . ",$page_tbl.pagedata as pagedata," 
322                 . $this->version_tbl_fields;
323         }
324         else {
325             $fields = $this->page_tbl_fields . ","
326                 . "mtime, minor_edit, versiondata,"
327                 . "$iscontent AS have_content";
328         }
329
330         $result = $dbh->getRow(sprintf("SELECT $fields"
331                                        . " FROM $page_tbl, $version_tbl"
332                                        . " WHERE $page_tbl.id=$version_tbl.id"
333                                        . "  AND pagename='%s'"
334                                        . "  AND version=%d",
335                                        $dbh->escapeSimple($pagename), $version),
336                                DB_FETCHMODE_ASSOC);
337
338         return $this->_extract_version_data($result);
339     }
340
341     function _extract_version_data($query_result) {
342         if (!$query_result)
343             return false;
344
345         $data = $this->_unserialize($query_result['versiondata']);
346         
347         $data['mtime'] = $query_result['mtime'];
348         $data['is_minor_edit'] = !empty($query_result['minor_edit']);
349         
350         if (isset($query_result['content']))
351             $data['%content'] = $query_result['content'];
352         elseif ($query_result['have_content'])
353             $data['%content'] = true;
354         else
355             $data['%content'] = '';
356
357         // FIXME: this is ugly.
358         if (isset($query_result['pagedata'])) {
359             // Query also includes page data.
360             // We might as well send that back too...
361             unset($query_result['versiondata']);
362             $data['%pagedata'] = $this->_extract_page_data($query_result);
363         }
364
365         return $data;
366     }
367
368
369     /**
370      * Create a new revision of a page.
371      */
372     function set_versiondata($pagename, $version, $data) {
373         $dbh = &$this->_dbh;
374         $version_tbl = $this->_table_names['version_tbl'];
375         
376         $minor_edit = (int) !empty($data['is_minor_edit']);
377         unset($data['is_minor_edit']);
378         
379         $mtime = (int)$data['mtime'];
380         unset($data['mtime']);
381         assert(!empty($mtime));
382
383         @$content = (string) $data['%content'];
384         unset($data['%content']);
385
386         unset($data['%pagedata']);
387         
388         $this->lock();
389         $id = $this->_get_pageid($pagename, true);
390
391         // FIXME: optimize: mysql can do this with one REPLACE INTO (I think).
392         $dbh->query(sprintf("DELETE FROM $version_tbl"
393                             . " WHERE id=%d AND version=%d",
394                             $id, $version));
395
396         /* mysql optimized version. 
397         $dbh->query(sprintf("INSERT INTO $version_tbl"
398                             . " (id,version,mtime,minor_edit,content,versiondata)"
399                             . " VALUES(%d,%d,%d,%d,'%s','%s')",
400                             $id, $version, $mtime, $minor_edit,
401                             $dbh->quoteSmart($content),
402                             $dbh->quoteSmart($this->_serialize($data))));
403         */
404         // generic slow PearDB bind eh quoting.
405         $dbh->query("INSERT INTO $version_tbl"
406                     . " (id,version,mtime,minor_edit,content,versiondata)"
407                     . " VALUES(?, ?, ?, ?, ?, ?)",
408                     array($id, $version, $mtime, $minor_edit, $content,
409                     $this->_serialize($data)));
410
411         $this->_update_recent_table($id);
412         $this->_update_nonempty_table($id);
413         
414         $this->unlock();
415     }
416     
417     /**
418      * Delete an old revision of a page.
419      */
420     function delete_versiondata($pagename, $version) {
421         $dbh = &$this->_dbh;
422         extract($this->_table_names);
423
424         $this->lock();
425         if ( ($id = $this->_get_pageid($pagename)) ) {
426             $dbh->query("DELETE FROM $version_tbl"
427                         . " WHERE id=$id AND version=$version");
428             $this->_update_recent_table($id);
429             // This shouldn't be needed (as long as the latestversion
430             // never gets deleted.)  But, let's be safe.
431             $this->_update_nonempty_table($id);
432         }
433         $this->unlock();
434     }
435
436     /**
437      * Delete page from the database with backup possibility.
438      * i.e save_page('') and DELETE nonempty id
439      * Can be undone and is seen in RecentChanges.
440      */
441     /* // see parent backend.php
442     function delete_page($pagename) {
443         $mtime = time();
444         $user =& $GLOBALS['request']->_user;
445         $vdata = array('author' => $user->getId(),
446                        'author_id' => $user->getAuthenticatedId(),
447                        'mtime' => $mtime);
448
449         $this->lock();
450         $version = $this->get_latest_version($pagename);
451         $this->set_versiondata($pagename, $version+1, $vdata);
452         $this->set_links($pagename, false);
453         $pagedata = get_pagedata($pagename);
454         $this->update_pagedata($pagename, array('hits' => $pagedata['hits']));
455         $this->unlock();
456     }
457     */
458
459     /**
460      * Delete page completely from the database.
461      * I'm not sure if this is what we want. Maybe just delete the revisions
462      */
463     function purge_page($pagename) {
464         $dbh = &$this->_dbh;
465         extract($this->_table_names);
466         
467         $this->lock();
468         if ( ($id = $this->_get_pageid($pagename, false)) ) {
469             $dbh->query("DELETE FROM $version_tbl  WHERE id=$id");
470             $dbh->query("DELETE FROM $recent_tbl   WHERE id=$id");
471             $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$id");
472             $dbh->query("DELETE FROM $link_tbl     WHERE linkfrom=$id");
473             $nlinks = $dbh->getOne("SELECT COUNT(*) FROM $link_tbl WHERE linkto=$id");
474             if ($nlinks) {
475                 // We're still in the link table (dangling link) so we can't delete this
476                 // altogether.
477                 $dbh->query("UPDATE $page_tbl SET hits=0, pagedata='' WHERE id=$id");
478                 $result = 0;
479             }
480             else {
481                 $dbh->query("DELETE FROM $page_tbl WHERE id=$id");
482                 $result = 1;
483             }
484             $this->_update_recent_table();
485             $this->_update_nonempty_table();
486         } else {
487             $result = -1; // already purged or not existing
488         }
489         $this->unlock();
490         return $result;
491     }
492
493     // The only thing we might be interested in updating which we can
494     // do fast in the flags (minor_edit).   I think the default
495     // update_versiondata will work fine...
496     //function update_versiondata($pagename, $version, $data) {
497     //}
498
499     function set_links($pagename, $links) {
500         // Update link table.
501         // FIXME: optimize: mysql can do this all in one big INSERT.
502
503         $dbh = &$this->_dbh;
504         extract($this->_table_names);
505
506         $this->lock();
507         $pageid = $this->_get_pageid($pagename, true);
508
509         $dbh->query("DELETE FROM $link_tbl WHERE linkfrom=$pageid");
510
511         if ($links) {
512             foreach($links as $link) {
513                 if (isset($linkseen[$link]))
514                     continue;
515                 $linkseen[$link] = true;
516                 $linkid = $this->_get_pageid($link, true);
517                 $dbh->query("INSERT INTO $link_tbl (linkfrom, linkto)"
518                             . " VALUES ($pageid, $linkid)");
519             }
520         }
521         $this->unlock();
522     }
523     
524     /**
525      * Find pages which link to or are linked from a page.
526      */
527     function get_links($pagename, $reversed=true, $include_empty=false,
528                        $sortby=false, $limit=false, $exclude='') {
529         $dbh = &$this->_dbh;
530         extract($this->_table_names);
531
532         if ($reversed)
533             list($have,$want) = array('linkee', 'linker');
534         else
535             list($have,$want) = array('linker', 'linkee');
536         $orderby = $this->sortby($sortby, 'db', array('pagename'));
537         if ($orderby) $orderby = ' ORDER BY $want.' . $orderby;
538         if ($exclude) // array of pagenames
539             $exclude = " AND $want.pagename NOT IN ".$this->_sql_set($exclude);
540         else 
541             $exclude='';
542
543         $qpagename = $dbh->escapeSimple($pagename);
544         $sql = "SELECT $want.id as id, $want.pagename as pagename, $want.hits as hits"
545             // Looks like 'AS' in column alias is a MySQL thing, Oracle does not like it
546             // and the PostgresSQL manual does not have it either
547             // Since it is optional in mySQL, just remove it...
548             . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee"
549             . (!$include_empty ? ", $nonempty_tbl" : '')
550             . " WHERE linkfrom=linker.id AND linkto=linkee.id"
551             . "  AND $have.pagename='$qpagename'"
552             . (!$include_empty ? " AND $nonempty_tbl.id=$want.id" : "")
553             //. " GROUP BY $want.id"
554             . $exclude
555             . $orderby;
556         if ($limit) {
557             // extract from,count from limit
558             list($from,$count) = $this->limit($limit);
559             $result = $dbh->limitQuery($sql, $from, $count);
560         } else {
561             $result = $dbh->query($sql);
562         }
563         
564         return new WikiDB_backend_PearDB_iter($this, $result);
565     }
566
567     /**
568      * Find if a page links to another page
569      */
570     function exists_link($pagename, $link, $reversed=false) {
571         $dbh = &$this->_dbh;
572         extract($this->_table_names);
573
574         if ($reversed)
575             list($have, $want) = array('linkee', 'linker');
576         else
577             list($have, $want) = array('linker', 'linkee');
578         $qpagename = $dbh->escapeSimple($pagename);
579         $qlink = $dbh->escapeSimple($link);
580         $row = $dbh->GetRow("SELECT IF($want.pagename,1,0) as result"
581                                 . " FROM $link_tbl, $page_tbl linker, $page_tbl linkee, $nonempty_tbl"
582                                 . " WHERE linkfrom=linker.id AND linkto=linkee.id"
583                                 . " AND $have.pagename='$qpagename'"
584                                 . " AND $want.pagename='$qlink'"
585                                 . " LIMIT 1");
586         return $row['result'];
587     }
588
589     function get_all_pages($include_empty=false, $sortby=false, $limit=false, $exclude='') {
590         $dbh = &$this->_dbh;
591         extract($this->_table_names);
592         $orderby = $this->sortby($sortby, 'db');
593         if ($orderby) $orderby = ' ORDER BY ' . $orderby;
594         if ($exclude) // array of pagenames
595             $exclude = " AND $page_tbl.pagename NOT IN ".$this->_sql_set($exclude);
596         else 
597             $exclude='';
598
599         if (strstr($orderby, 'mtime ')) { // multiple columns possible
600             if ($include_empty) {
601                 $sql = "SELECT "
602                     . $this->page_tbl_fields
603                     . " FROM $page_tbl, $recent_tbl, $version_tbl"
604                     . " WHERE $page_tbl.id=$recent_tbl.id"
605                     . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
606                     . $exclude
607                     . $orderby;
608             }
609             else {
610                 $sql = "SELECT "
611                     . $this->page_tbl_fields
612                     . " FROM $nonempty_tbl, $page_tbl, $recent_tbl, $version_tbl"
613                     . " WHERE $nonempty_tbl.id=$page_tbl.id"
614                     . " AND $page_tbl.id=$recent_tbl.id"
615                     . " AND $page_tbl.id=$version_tbl.id AND latestversion=version"
616                     . $exclude
617                     . $orderby;
618             }
619         } else {
620             if ($include_empty) {
621                 $sql = "SELECT "
622                     . $this->page_tbl_fields 
623                     ." FROM $page_tbl"
624                     . ($exclude ? " WHERE $exclude" : '')
625                     . $orderby;
626             }
627             else {
628                 $sql = "SELECT "
629                     . $this->page_tbl_fields
630                     . " FROM $nonempty_tbl, $page_tbl"
631                     . " WHERE $nonempty_tbl.id=$page_tbl.id"
632                     . $exclude
633                     . $orderby;
634             }
635         }
636         if ($limit) {
637             // extract from,count from limit
638             list($from,$count) = $this->limit($limit);
639             $result = $dbh->limitQuery($sql, $from, $count);
640         } else {
641             $result = $dbh->query($sql);
642         }
643         return new WikiDB_backend_PearDB_iter($this, $result);
644     }
645         
646     /**
647      * Title search.
648      */
649     function text_search($search, $fulltext=false, $sortby=false, $limit=false, $exclude=false) {
650         $dbh = &$this->_dbh;
651         extract($this->_table_names);
652         $orderby = $this->sortby($sortby, 'db');
653         if ($orderby) $orderby = ' ORDER BY ' . $orderby;
654
655         $searchclass = get_class($this)."_search";
656         // no need to define it everywhere and then fallback. memory!
657         if (!class_exists($searchclass))
658             $searchclass = "WikiDB_backend_PearDB_search";
659         $searchobj = new $searchclass($search, $dbh);
660         
661         $table = "$nonempty_tbl, $page_tbl";
662         $join_clause = "$nonempty_tbl.id=$page_tbl.id";
663         $fields = $this->page_tbl_fields;
664
665         if ($fulltext) {
666             $table .= ", $recent_tbl";
667             $join_clause .= " AND $page_tbl.id=$recent_tbl.id";
668
669             $table .= ", $version_tbl";
670             $join_clause .= " AND $page_tbl.id=$version_tbl.id AND latestversion=version";
671
672             $fields .= ", $page_tbl.pagedata as pagedata, " . $this->version_tbl_fields;
673             $callback = new WikiMethodCb($searchobj, "_fulltext_match_clause");
674         } else {
675             $callback = new WikiMethodCb($searchobj, "_pagename_match_clause");
676         }
677         $search_clause = $search->makeSqlClauseObj($callback);
678         
679         $sql = "SELECT $fields FROM $table"
680             . " WHERE $join_clause"
681             . "  AND ($search_clause)"
682             . $orderby;
683          if ($limit) {
684              list($from, $count) = $this->limit($limit);
685              $result = $dbh->limitQuery($sql, $from, $count);
686          } else {
687              $result = $dbh->query($sql);
688          }
689         
690         return new WikiDB_backend_PearDB_iter($this, $result);
691     }
692
693     //Todo: check if the better Mysql MATCH operator is supported,
694     // (ranked search) and also google like expressions.
695     function _sql_match_clause($word) {
696         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
697         $word = $this->_dbh->escapeSimple($word);
698         //$page_tbl = $this->_table_names['page_tbl'];
699         //Note: Mysql 4.1.0 has a bug which fails with binary fields.
700         //      e.g. if word is lowercased.
701         // http://bugs.mysql.com/bug.php?id=1491
702         return "LOWER(pagename) LIKE '%$word%'";
703     }
704     function _sql_casematch_clause($word) {
705         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
706         $word = $this->_dbh->escapeSimple($word);
707         return "pagename LIKE '%$word%'";
708     }
709     function _fullsearch_sql_match_clause($word) {
710         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
711         $word = $this->_dbh->escapeSimple($word);
712         //$page_tbl = $this->_table_names['page_tbl'];
713         //Mysql 4.1.1 has a bug which fails here if word is lowercased.
714         return "LOWER(pagename) LIKE '%$word%' OR content LIKE '%$word%'";
715     }
716     function _fullsearch_sql_casematch_clause($word) {
717         $word = preg_replace('/(?=[%_\\\\])/', "\\", $word);
718         $word = $this->_dbh->escapeSimple($word);
719         return "pagename LIKE '%$word%' OR content LIKE '%$word%'";
720     }
721
722     /**
723      * Find highest or lowest hit counts.
724      */
725     function most_popular($limit=20, $sortby='-hits') {
726         $dbh = &$this->_dbh;
727         extract($this->_table_names);
728         if ($limit < 0){ 
729             $order = "hits ASC";
730             $limit = -$limit;
731             $where = ""; 
732         } else {
733             $order = "hits DESC";
734             $where = " AND hits > 0";
735         }
736         $orderby = '';
737         if ($sortby != '-hits') {
738             if ($order = $this->sortby($sortby, 'db'))
739                 $orderby = " ORDER BY " . $order;
740         } else {
741             $orderby = " ORDER BY $order";
742         }
743         //$limitclause = $limit ? " LIMIT $limit" : '';
744         $sql = "SELECT "
745             . $this->page_tbl_fields
746             . " FROM $nonempty_tbl, $page_tbl"
747             . " WHERE $nonempty_tbl.id=$page_tbl.id" 
748             . $where
749             . $orderby;
750          if ($limit) {
751              list($from, $count) = $this->limit($limit);
752              $result = $dbh->limitQuery($sql, $from, $count);
753          } else {
754              $result = $dbh->query($sql);
755          }
756
757         return new WikiDB_backend_PearDB_iter($this, $result);
758     }
759
760     /**
761      * Find recent changes.
762      */
763     function most_recent($params) {
764         $limit = 0;
765         $since = 0;
766         $include_minor_revisions = false;
767         $exclude_major_revisions = false;
768         $include_all_revisions = false;
769         extract($params);
770
771         $dbh = &$this->_dbh;
772         extract($this->_table_names);
773
774         $pick = array();
775         if ($since)
776             $pick[] = "mtime >= $since";
777                         
778         
779         if ($include_all_revisions) {
780             // Include all revisions of each page.
781             $table = "$page_tbl, $version_tbl";
782             $join_clause = "$page_tbl.id=$version_tbl.id";
783
784             if ($exclude_major_revisions) {
785                 // Include only minor revisions
786                 $pick[] = "minor_edit <> 0";
787             }
788             elseif (!$include_minor_revisions) {
789                 // Include only major revisions
790                 $pick[] = "minor_edit = 0";
791             }
792         }
793         else {
794             $table = "$page_tbl, $recent_tbl";
795             $join_clause = "$page_tbl.id=$recent_tbl.id";
796             $table .= ", $version_tbl";
797             $join_clause .= " AND $version_tbl.id=$page_tbl.id";
798             
799             if ($exclude_major_revisions) {
800                 // Include only most recent minor revision
801                 $pick[] = 'version=latestminor';
802             }
803             elseif (!$include_minor_revisions) {
804                 // Include only most recent major revision
805                 $pick[] = 'version=latestmajor';
806             }
807             else {
808                 // Include only the latest revision (whether major or minor).
809                 $pick[] ='version=latestversion';
810             }
811         }
812         $order = "DESC";
813         if($limit < 0){
814             $order = "ASC";
815             $limit = -$limit;
816         }
817         // $limitclause = $limit ? " LIMIT $limit" : '';
818         $where_clause = $join_clause;
819         if ($pick)
820             $where_clause .= " AND " . join(" AND ", $pick);
821
822         // FIXME: use SQL_BUFFER_RESULT for mysql?
823         $sql = "SELECT " 
824                . $this->page_tbl_fields . ", " . $this->version_tbl_fields
825                . " FROM $table"
826                . " WHERE $where_clause"
827                . " ORDER BY mtime $order";
828         if ($limit) {
829              list($from, $count) = $this->limit($limit);
830              $result = $dbh->limitQuery($sql, $from, $count);
831         } else {
832             $result = $dbh->query($sql);
833         }
834         return new WikiDB_backend_PearDB_iter($this, $result);
835     }
836
837     /**
838      * Find referenced empty pages.
839      */
840     function wanted_pages($exclude_from='', $exclude='', $sortby=false, $limit=false) {
841         $dbh = &$this->_dbh;
842         extract($this->_table_names);
843         if ($orderby = $this->sortby($sortby, 'db', array('pagename','wantedfrom')))
844             $orderby = 'ORDER BY ' . $orderby;
845
846         if ($exclude_from) // array of pagenames
847             $exclude_from = " AND pp.pagename NOT IN ".$this->_sql_set($exclude_from);
848         if ($exclude) // array of pagenames
849             $exclude = " AND p.pagename NOT IN ".$this->_sql_set($exclude);
850         $sql = "SELECT p.pagename, pp.pagename as wantedfrom"
851             . " FROM $page_tbl p, $link_tbl linked "
852             . " LEFT JOIN $page_tbl pp ON linked.linkto = pp.id"
853             . " LEFT JOIN $nonempty_tbl ne ON linked.linkto = ne.id" 
854             . " WHERE ne.id is NULL"
855             .       " AND p.id = linked.linkfrom"
856             . $exclude_from
857             . $exclude
858             . $orderby;
859         if ($limit) {
860             list($from, $count) = $this->limit($limit);
861             $result = $dbh->limitQuery($sql, $from, $count * 3);
862         } else {
863             $result = $dbh->query($sql);
864         }
865         return new WikiDB_backend_PearDB_generic_iter($this, $result);
866     }
867
868     function _sql_set(&$pagenames) {
869         $s = '(';
870         foreach ($pagenames as $p) {
871             $s .= ("'".$this->_dbh->escapeSimple($p)."',");
872         }
873         return substr($s,0,-1).")";
874     }
875
876     /**
877      * Rename page in the database.
878      */
879     function rename_page($pagename, $to) {
880         $dbh = &$this->_dbh;
881         extract($this->_table_names);
882         
883         $this->lock();
884         if (($id = $this->_get_pageid($pagename, false)) ) {
885             if ($new = $this->_get_pageid($to, false)) {
886                 // Cludge Alert!
887                 // This page does not exist (already verified before), but exists in the page table.
888                 // So we delete this page.
889                 $dbh->query("DELETE FROM $page_tbl WHERE id=$new");
890                 $dbh->query("DELETE FROM $version_tbl WHERE id=$new");
891                 $dbh->query("DELETE FROM $recent_tbl WHERE id=$new");
892                 $dbh->query("DELETE FROM $nonempty_tbl WHERE id=$new");
893                 // We have to fix all referring tables to the old id
894                 $dbh->query("UPDATE $link_tbl SET linkfrom=$id WHERE linkfrom=$new");
895                 $dbh->query("UPDATE $link_tbl SET linkto=$id WHERE linkto=$new");
896             }
897             $dbh->query(sprintf("UPDATE $page_tbl SET pagename='%s' WHERE id=$id",
898                                 $dbh->escapeSimple($to)));
899         }
900         $this->unlock();
901         return $id;
902     }
903
904     function _update_recent_table($pageid = false) {
905         $dbh = &$this->_dbh;
906         extract($this->_table_names);
907         extract($this->_expressions);
908
909         $pageid = (int)$pageid;
910
911         $this->lock();
912         $dbh->query("DELETE FROM $recent_tbl"
913                     . ( $pageid ? " WHERE id=$pageid" : ""));
914         $dbh->query( "INSERT INTO $recent_tbl"
915                      . " (id, latestversion, latestmajor, latestminor)"
916                      . " SELECT id, $maxversion, $maxmajor, $maxminor"
917                      . " FROM $version_tbl"
918                      . ( $pageid ? " WHERE id=$pageid" : "")
919                      . " GROUP BY id" );
920         $this->unlock();
921     }
922
923     function _update_nonempty_table($pageid = false) {
924         $dbh = &$this->_dbh;
925         extract($this->_table_names);
926
927         $pageid = (int)$pageid;
928
929         extract($this->_expressions);
930         $this->lock();
931         $dbh->query("DELETE FROM $nonempty_tbl"
932                     . ( $pageid ? " WHERE id=$pageid" : ""));
933         $dbh->query("INSERT INTO $nonempty_tbl (id)"
934                     . " SELECT $recent_tbl.id"
935                     . " FROM $recent_tbl, $version_tbl"
936                     . " WHERE $recent_tbl.id=$version_tbl.id"
937                     . "       AND version=latestversion"
938                     // We have some specifics here (Oracle)
939                     //. "  AND content<>''"
940                     . "  AND content $notempty"
941                     . ( $pageid ? " AND $recent_tbl.id=$pageid" : ""));
942         
943         $this->unlock();
944     }
945
946
947     /**
948      * Grab a write lock on the tables in the SQL database.
949      *
950      * Calls can be nested.  The tables won't be unlocked until
951      * _unlock_database() is called as many times as _lock_database().
952      *
953      * @access protected
954      */
955     function lock($tables = false, $write_lock = true) {
956         if ($this->_lock_count++ == 0)
957             $this->_lock_tables($write_lock);
958     }
959
960     /**
961      * Actually lock the required tables.
962      */
963     function _lock_tables($write_lock) {
964         trigger_error("virtual", E_USER_ERROR);
965     }
966     
967     /**
968      * Release a write lock on the tables in the SQL database.
969      *
970      * @access protected
971      *
972      * @param $force boolean Unlock even if not every call to lock() has been matched
973      * by a call to unlock().
974      *
975      * @see _lock_database
976      */
977     function unlock($tables = false, $force = false) {
978         if ($this->_lock_count == 0)
979             return;
980         if (--$this->_lock_count <= 0 || $force) {
981             $this->_unlock_tables();
982             $this->_lock_count = 0;
983         }
984     }
985
986     /**
987      * Actually unlock the required tables.
988      */
989     function _unlock_tables($write_lock) {
990         trigger_error("virtual", E_USER_ERROR);
991     }
992
993
994     /**
995      * Serialize data
996      */
997     function _serialize($data) {
998         if (empty($data))
999             return '';
1000         assert(is_array($data));
1001         return serialize($data);
1002     }
1003
1004     /**
1005      * Unserialize data
1006      */
1007     function _unserialize($data) {
1008         return empty($data) ? array() : unserialize($data);
1009     }
1010     
1011     /**
1012      * Callback for PEAR (DB) errors.
1013      *
1014      * @access protected
1015      *
1016      * @param A PEAR_error object.
1017      */
1018     function _pear_error_callback($error) {
1019         if ($this->_is_false_error($error))
1020             return;
1021         
1022         $this->_dbh->setErrorHandling(PEAR_ERROR_PRINT);        // prevent recursive loops.
1023         $this->close();
1024         trigger_error($this->_pear_error_message($error), E_USER_ERROR);
1025     }
1026
1027     /**
1028      * Detect false errors messages from PEAR DB.
1029      *
1030      * The version of PEAR DB which ships with PHP 4.0.6 has a bug in that
1031      * it doesn't recognize "LOCK" and "UNLOCK" as SQL commands which don't
1032      * return any data.  (So when a "LOCK" command doesn't return any data,
1033      * DB reports it as an error, when in fact, it's not.)
1034      *
1035      * @access private
1036      * @return bool True iff error is not really an error.
1037      */
1038     function _is_false_error($error) {
1039         if ($error->getCode() != DB_ERROR)
1040             return false;
1041
1042         $query = $this->_dbh->last_query;
1043
1044         if (! preg_match('/^\s*"?(INSERT|UPDATE|DELETE|REPLACE|CREATE'
1045                          . '|DROP|ALTER|GRANT|REVOKE|LOCK|UNLOCK)\s/', $query)) {
1046             // Last query was not of the sort which doesn't return any data.
1047             //" <--kludge for brain-dead syntax coloring
1048             return false;
1049         }
1050         
1051         if (! in_array('ismanip', get_class_methods('DB'))) {
1052             // Pear shipped with PHP 4.0.4pl1 (and before, presumably)
1053             // does not have the DB::isManip method.
1054             return true;
1055         }
1056         
1057         if (DB::isManip($query)) {
1058             // If Pear thinks it's an isManip then it wouldn't have thrown
1059             // the error we're testing for....
1060             return false;
1061         }
1062
1063         return true;
1064     }
1065
1066     function _pear_error_message($error) {
1067         $class = get_class($this);
1068         $message = "$class: fatal database error\n"
1069              . "\t" . $error->getMessage() . "\n"
1070              . "\t(" . $error->getDebugInfo() . ")\n";
1071
1072         // Prevent password from being exposed during a connection error
1073         $safe_dsn = preg_replace('| ( :// .*? ) : .* (?=@) |xs',
1074                                  '\\1:XXXXXXXX', $this->_dsn);
1075         return str_replace($this->_dsn, $safe_dsn, $message);
1076     }
1077
1078     /**
1079      * Filter PHP errors notices from PEAR DB code.
1080      *
1081      * The PEAR DB code which ships with PHP 4.0.6 produces spurious
1082      * errors and notices.  This is an error callback (for use with
1083      * ErrorManager which will filter out those spurious messages.)
1084      * @see _is_false_error, ErrorManager
1085      * @access private
1086      */
1087     function _pear_notice_filter($err) {
1088         return ( $err->isNotice()
1089                  && preg_match('|DB[/\\\\]common.php$|', $err->errfile)
1090                  && $err->errline == 126
1091                  && preg_match('/Undefined offset: +0\b/', $err->errstr) );
1092     }
1093
1094     /* some variables and functions for DB backend abstraction (action=upgrade) */
1095     function database () {
1096         return $this->_dbh->dsn['database'];
1097     }
1098     function backendType() {
1099         return $this->_dbh->phptype;
1100     }
1101     function connection() {
1102         return $this->_dbh->connection;
1103     }
1104     function getRow($query) {
1105         return $this->_dbh->getRow($query);
1106     }
1107
1108     function listOfTables() {
1109         return $this->_dbh->getListOf('tables');
1110     }
1111     function listOfFields($database,$table) {
1112         if ($this->backendType() == 'mysql') {
1113             $fields = array();
1114             assert(!empty($database));
1115             assert(!empty($table));
1116             $result = mysql_list_fields($database, $table, $this->_dbh->connection) or 
1117                 trigger_error(__FILE__.':'.__LINE__.' '.mysql_error(), E_USER_WARNING);
1118             if (!$result) return array();
1119               $columns = mysql_num_fields($result);
1120             for ($i = 0; $i < $columns; $i++) {
1121                 $fields[] = mysql_field_name($result, $i);
1122             }
1123             mysql_free_result($result);
1124             return $fields;
1125         } else {
1126             // TODO: try ADODB version?
1127             trigger_error("Unsupported dbtype and backend. Either switch to ADODB or check it manually.");
1128         }
1129     }
1130 };
1131
1132 /**
1133  * This class is a generic iterator.
1134  *
1135  * WikiDB_backend_PearDB_iter only iterates over things that have
1136  * 'pagename', 'pagedata', etc. etc.
1137  *
1138  * Probably WikiDB_backend_PearDB_iter and this class should be merged
1139  * (most of the code is cut-and-paste :-( ), but I am trying to make
1140  * changes that could be merged easily.
1141  *
1142  * @author: Dan Frankowski
1143  */
1144 class WikiDB_backend_PearDB_generic_iter
1145 extends WikiDB_backend_iterator
1146 {
1147     function WikiDB_backend_PearDB_generic_iter($backend, $query_result, $field_list = NULL) {
1148         if (DB::isError($query_result)) {
1149             // This shouldn't happen, I thought.
1150             $backend->_pear_error_callback($query_result);
1151         }
1152         
1153         $this->_backend = &$backend;
1154         $this->_result = $query_result;
1155     }
1156
1157     function count() {
1158         if (!$this->_result)
1159             return false;
1160         return $this->_result->numRows();
1161     }
1162     
1163     function next() {
1164         $backend = &$this->_backend;
1165         if (!$this->_result)
1166             return false;
1167
1168         $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
1169         if (!$record) {
1170             $this->free();
1171             return false;
1172         }
1173         
1174         return $record;
1175     }
1176
1177     function free () {
1178         if ($this->_result) {
1179             $this->_result->free();
1180             $this->_result = false;
1181         }
1182     }
1183 }
1184
1185 class WikiDB_backend_PearDB_iter
1186 extends WikiDB_backend_PearDB_generic_iter
1187 {
1188
1189     function next() {
1190         $backend = &$this->_backend;
1191         if (!$this->_result)
1192             return false;
1193
1194         $record = $this->_result->fetchRow(DB_FETCHMODE_ASSOC);
1195         if (!$record) {
1196             $this->free();
1197             return false;
1198         }
1199         
1200         $pagedata = $backend->_extract_page_data($record);
1201         $rec = array('pagename' => $record['pagename'],
1202                      'pagedata' => $pagedata);
1203
1204         if (!empty($record['version'])) {
1205             $rec['versiondata'] = $backend->_extract_version_data($record);
1206             $rec['version'] = $record['version'];
1207         }
1208         
1209         return $rec;
1210     }
1211 }
1212
1213 // word search
1214 class WikiDB_backend_PearDB_search
1215 extends WikiDB_backend_search
1216 {
1217     function WikiDB_backend_PearDB_search(&$search, &$dbh) {
1218         $this->_dbh = $dbh;
1219         $this->_case_exact = $search->_case_exact;
1220         $this->_stoplist =& $search->_stoplist;
1221     }
1222     function _pagename_match_clause($node) { 
1223         $word = $node->sql();
1224         if ($word == '%')
1225             return "1=1";
1226         else
1227             return ($this->_case_exact 
1228                     ? "pagename LIKE '$word'" 
1229                     : "LOWER(pagename) LIKE '$word'");
1230     }
1231     function _fulltext_match_clause($node) { 
1232         $word = $node->sql();
1233         if ($word == '%')
1234             return "1=1";
1235         // eliminate stoplist words
1236         if (preg_match("/^%".$this->_stoplist."%/i", $word) 
1237             or preg_match("/^".$this->_stoplist."$/i", $word))
1238             return $this->_pagename_match_clause($node);
1239         else
1240             return $this->_pagename_match_clause($node)
1241                 // probably convert this MATCH AGAINST or SUBSTR/POSITION without wildcards
1242                 . ($this->_case_exact ? " OR content LIKE '$word'" 
1243                                       : " OR LOWER(content) LIKE '$word'");
1244     }
1245 }
1246
1247 // $Log: not supported by cvs2svn $
1248 // Revision 1.92  2005/09/14 06:04:43  rurban
1249 // optimize searching for ALL (ie %), use the stoplist on PDO
1250 //
1251 // Revision 1.91  2005/09/11 14:55:05  rurban
1252 // implement fulltext stoplist
1253 //
1254 // Revision 1.90  2005/09/11 13:25:12  rurban
1255 // enhance LIMIT support
1256 //
1257 // Revision 1.89  2005/09/10 21:30:16  rurban
1258 // enhance titleSearch
1259 //
1260 // Revision 1.88  2005/08/06 13:20:05  rurban
1261 // add comments
1262 //
1263 // Revision 1.87  2005/02/10 19:04:24  rurban
1264 // move getRow up one level to our backend class
1265 //
1266 // Revision 1.86  2005/01/29 19:51:02  rurban
1267 // Bugs item #1077769 fixed by frugal.
1268 // Deleted the wrong page. Fix all other tables also.
1269 //
1270 // Revision 1.85  2005/01/25 08:03:35  rurban
1271 // support DATABASE_PERSISTENT besides dsn database?persistent=false; move lock_count up (per Charles Corrigan)
1272 //
1273 // Revision 1.84  2005/01/18 20:55:47  rurban
1274 // reformatting and two bug fixes: adding missing parens
1275 //
1276 // Revision 1.83  2005/01/18 10:11:29  rurban
1277 // Oops. Again thanks to Charles Corrigan
1278 //
1279 // Revision 1.82  2005/01/18 08:55:51  rurban
1280 // fix quoting
1281 //
1282 // Revision 1.81  2005/01/17 08:53:09  rurban
1283 // pagedata fix by Charles Corrigan
1284 //
1285 // Revision 1.80  2004/12/22 18:33:31  rurban
1286 // fix page _id_cache logic for _get_pageid create_if_missing
1287 //
1288 // Revision 1.79  2004/12/10 02:45:27  rurban
1289 // SQL optimization:
1290 //   put _cached_html from pagedata into a new seperate blob, not huge serialized string.
1291 //   it is only rarelely needed: for current page only, if-not-modified
1292 //   but was extracted for every simple page iteration.
1293 //
1294 // Revision 1.78  2004/12/08 12:55:51  rurban
1295 // support new non-destructive delete_page via generic backend method
1296 //
1297 // Revision 1.77  2004/12/06 19:50:04  rurban
1298 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
1299 // renamed delete_page to purge_page.
1300 // enable action=edit&version=-1 to force creation of a new version.
1301 // added BABYCART_PATH config
1302 // fixed magiqc in adodb.inc.php
1303 // and some more docs
1304 //
1305 // Revision 1.76  2004/11/30 17:45:53  rurban
1306 // exists_links backend implementation
1307 //
1308 // Revision 1.75  2004/11/28 20:42:33  rurban
1309 // Optimize PearDB _extract_version_data and _extract_page_data.
1310 //
1311 // Revision 1.74  2004/11/27 14:39:05  rurban
1312 // simpified regex search architecture:
1313 //   no db specific node methods anymore,
1314 //   new sql() method for each node
1315 //   parallel to regexp() (which returns pcre)
1316 //   regex types bitmasked (op's not yet)
1317 // new regex=sql
1318 // clarified WikiDB::quote() backend methods:
1319 //   ->quote() adds surrounsing quotes
1320 //   ->qstr() (new method) assumes strings and adds no quotes! (in contrast to ADODB)
1321 //   pear and adodb have now unified quote methods for all generic queries.
1322 //
1323 // Revision 1.73  2004/11/26 18:39:02  rurban
1324 // new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
1325 //
1326 // Revision 1.72  2004/11/25 17:20:51  rurban
1327 // and again a couple of more native db args: backlinks
1328 //
1329 // Revision 1.71  2004/11/23 13:35:48  rurban
1330 // add case_exact search
1331 //
1332 // Revision 1.70  2004/11/21 11:59:26  rurban
1333 // remove final \n to be ob_cache independent
1334 //
1335 // Revision 1.69  2004/11/20 17:49:39  rurban
1336 // add fast exclude support to SQL get_all_pages
1337 //
1338 // Revision 1.68  2004/11/20 17:35:58  rurban
1339 // improved WantedPages SQL backends
1340 // PageList::sortby new 3rd arg valid_fields (override db fields)
1341 // WantedPages sql pager inexact for performance reasons:
1342 //   assume 3 wantedfrom per page, to be correct, no getTotal()
1343 // support exclude argument for get_all_pages, new _sql_set()
1344 //
1345 // Revision 1.67  2004/11/10 19:32:24  rurban
1346 // * optimize increaseHitCount, esp. for mysql.
1347 // * prepend dirs to the include_path (phpwiki_dir for faster searches)
1348 // * Pear_DB version logic (awful but needed)
1349 // * fix broken ADODB quote
1350 // * _extract_page_data simplification
1351 //
1352 // Revision 1.66  2004/11/10 15:29:21  rurban
1353 // * requires newer Pear_DB (as the internal one): quote() uses now escapeSimple for strings
1354 // * ACCESS_LOG_SQL: fix cause request not yet initialized
1355 // * WikiDB: moved SQL specific methods upwards
1356 // * new Pear_DB quoting: same as ADODB and as newer Pear_DB.
1357 //   fixes all around: WikiGroup, WikiUserNew SQL methods, SQL logging
1358 //
1359 // Revision 1.65  2004/11/09 17:11:17  rurban
1360 // * revert to the wikidb ref passing. there's no memory abuse there.
1361 // * use new wikidb->_cache->_id_cache[] instead of wikidb->_iwpcache, to effectively
1362 //   store page ids with getPageLinks (GleanDescription) of all existing pages, which
1363 //   are also needed at the rendering for linkExistingWikiWord().
1364 //   pass options to pageiterator.
1365 //   use this cache also for _get_pageid()
1366 //   This saves about 8 SELECT count per page (num all pagelinks).
1367 // * fix passing of all page fields to the pageiterator.
1368 // * fix overlarge session data which got broken with the latest ACCESS_LOG_SQL changes
1369 //
1370 // Revision 1.64  2004/11/07 16:02:52  rurban
1371 // new sql access log (for spam prevention), and restructured access log class
1372 // dbh->quote (generic)
1373 // pear_db: mysql specific parts seperated (using replace)
1374 //
1375 // Revision 1.63  2004/11/01 10:43:58  rurban
1376 // seperate PassUser methods into seperate dir (memory usage)
1377 // fix WikiUser (old) overlarge data session
1378 // remove wikidb arg from various page class methods, use global ->_dbi instead
1379 // ...
1380 //
1381 // Revision 1.62  2004/10/14 19:19:34  rurban
1382 // loadsave: check if the dumped file will be accessible from outside.
1383 // and some other minor fixes. (cvsclient native not yet ready)
1384 //
1385 // Revision 1.61  2004/10/14 17:19:17  rurban
1386 // allow most_popular sortby arguments
1387 //
1388 // Revision 1.60  2004/07/09 10:06:50  rurban
1389 // Use backend specific sortby and sortable_columns method, to be able to
1390 // select between native (Db backend) and custom (PageList) sorting.
1391 // Fixed PageList::AddPageList (missed the first)
1392 // Added the author/creator.. name to AllPagesBy...
1393 //   display no pages if none matched.
1394 // Improved dba and file sortby().
1395 // Use &$request reference
1396 //
1397 // Revision 1.59  2004/07/08 21:32:36  rurban
1398 // Prevent from more warnings, minor db and sort optimizations
1399 //
1400 // Revision 1.58  2004/07/08 16:56:16  rurban
1401 // use the backendType abstraction
1402 //
1403 // Revision 1.57  2004/07/05 12:57:54  rurban
1404 // add mysql timeout
1405 //
1406 // Revision 1.56  2004/07/04 10:24:43  rurban
1407 // forgot the expressions
1408 //
1409 // Revision 1.55  2004/07/03 16:51:06  rurban
1410 // optional DBADMIN_USER:DBADMIN_PASSWD for action=upgrade (if no ALTER permission)
1411 // added atomic mysql REPLACE for PearDB as in ADODB
1412 // fixed _lock_tables typo links => link
1413 // fixes unserialize ADODB bug in line 180
1414 //
1415 // Revision 1.54  2004/06/29 08:52:24  rurban
1416 // Use ...version() $need_content argument in WikiDB also:
1417 // To reduce the memory footprint for larger sets of pagelists,
1418 // we don't cache the content (only true or false) and
1419 // we purge the pagedata (_cached_html) also.
1420 // _cached_html is only cached for the current pagename.
1421 // => Vastly improved page existance check, ACL check, ...
1422 //
1423 // Now only PagedList info=content or size needs the whole content, esp. if sortable.
1424 //
1425 // Revision 1.53  2004/06/27 10:26:03  rurban
1426 // oci8 patch by Philippe Vanhaesendonck + some ADODB notes+fixes
1427 //
1428 // Revision 1.52  2004/06/25 14:15:08  rurban
1429 // reduce memory footprint by caching only requested pagedate content (improving most page iterators)
1430 //
1431 // Revision 1.51  2004/05/12 10:49:55  rurban
1432 // require_once fix for those libs which are loaded before FileFinder and
1433 //   its automatic include_path fix, and where require_once doesn't grok
1434 //   dirname(__FILE__) != './lib'
1435 // upgrade fix with PearDB
1436 // navbar.tmpl: remove spaces for IE &nbsp; button alignment
1437 //
1438 // Revision 1.50  2004/05/06 17:30:39  rurban
1439 // CategoryGroup: oops, dos2unix eol
1440 // improved phpwiki_version:
1441 //   pre -= .0001 (1.3.10pre: 1030.099)
1442 //   -p1 += .001 (1.3.9-p1: 1030.091)
1443 // improved InstallTable for mysql and generic SQL versions and all newer tables so far.
1444 // abstracted more ADODB/PearDB methods for action=upgrade stuff:
1445 //   backend->backendType(), backend->database(),
1446 //   backend->listOfFields(),
1447 //   backend->listOfTables(),
1448 //
1449 // Revision 1.49  2004/05/03 21:35:30  rurban
1450 // don't use persistent connections with postgres
1451 //
1452 // Revision 1.48  2004/04/26 20:44:35  rurban
1453 // locking table specific for better databases
1454 //
1455 // Revision 1.47  2004/04/20 00:06:04  rurban
1456 // themable paging support
1457 //
1458 // Revision 1.46  2004/04/19 21:51:41  rurban
1459 // php5 compatibility: it works!
1460 //
1461 // Revision 1.45  2004/04/16 14:19:39  rurban
1462 // updated ADODB notes
1463 //
1464
1465 // (c-file-style: "gnu")
1466 // Local Variables:
1467 // mode: php
1468 // tab-width: 8
1469 // c-basic-offset: 4
1470 // c-hanging-comment-ender-p: nil
1471 // indent-tabs-mode: nil
1472 // End:   
1473 ?>