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