]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB.php
Guard against unwanted DEBUG="DEBUG" logic. In detail (WikiDB),
[SourceForge/phpwiki.git] / lib / WikiDB.php
1 <?php //-*-php-*-
2 rcs_id('$Id: WikiDB.php,v 1.144 2006-10-12 06:36:09 rurban Exp $');
3
4 require_once('lib/PageType.php');
5
6 /**
7  * The classes in the file define the interface to the
8  * page database.
9  *
10  * @package WikiDB
11  * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
12  *         Reini Urban
13  */
14
15 /**
16  * Force the creation of a new revision.
17  * @see WikiDB_Page::createRevision()
18  */
19 if (!defined('WIKIDB_FORCE_CREATE'))
20     define('WIKIDB_FORCE_CREATE', -1);
21
22 /** 
23  * Abstract base class for the database used by PhpWiki.
24  *
25  * A <tt>WikiDB</tt> is a container for <tt>WikiDB_Page</tt>s which in
26  * turn contain <tt>WikiDB_PageRevision</tt>s.
27  *
28  * Conceptually a <tt>WikiDB</tt> contains all possible
29  * <tt>WikiDB_Page</tt>s, whether they have been initialized or not.
30  * Since all possible pages are already contained in a WikiDB, a call
31  * to WikiDB::getPage() will never fail (barring bugs and
32  * e.g. filesystem or SQL database problems.)
33  *
34  * Also each <tt>WikiDB_Page</tt> always contains at least one
35  * <tt>WikiDB_PageRevision</tt>: the default content (e.g. "Describe
36  * [PageName] here.").  This default content has a version number of
37  * zero.
38  *
39  * <tt>WikiDB_PageRevision</tt>s have read-only semantics. One can
40  * only create new revisions or delete old ones --- one can not modify
41  * an existing revision.
42  */
43 class WikiDB {
44     /**
45      * Open a WikiDB database.
46      *
47      * This is a static member function. This function inspects its
48      * arguments to determine the proper subclass of WikiDB to
49      * instantiate, and then it instantiates it.
50      *
51      * @access public
52      *
53      * @param hash $dbparams Database configuration parameters.
54      * Some pertinent paramters are:
55      * <dl>
56      * <dt> dbtype
57      * <dd> The back-end type.  Current supported types are:
58      *   <dl>
59      *   <dt> SQL
60      *     <dd> Generic SQL backend based on the PEAR/DB database abstraction
61      *       library. (More stable and conservative)
62      *   <dt> ADODB
63      *     <dd> Another generic SQL backend. (More current features are tested here. Much faster)
64      *   <dt> dba
65      *     <dd> Dba based backend. The default and by far the fastest.
66      *   <dt> cvs
67      *     <dd> 
68      *   <dt> file
69      *     <dd> flat files
70      *   </dl>
71      *
72      * <dt> dsn
73      * <dd> (Used by the SQL and ADODB backends.)
74      *      The DSN specifying which database to connect to.
75      *
76      * <dt> prefix
77      * <dd> Prefix to be prepended to database tables (and file names).
78      *
79      * <dt> directory
80      * <dd> (Used by the dba backend.)
81      *      Which directory db files reside in.
82      *
83      * <dt> timeout
84      * <dd> Used only by the dba backend so far. 
85      *      And: When optimizing mysql it closes timed out mysql processes.
86      *      otherwise only used for dba: Timeout in seconds for opening (and 
87      *      obtaining lock) on the dbm file.
88      *
89      * <dt> dba_handler
90      * <dd> (Used by the dba backend.)
91      *
92      *      Which dba handler to use. Good choices are probably either
93      *      'gdbm' or 'db2'.
94      * </dl>
95      *
96      * @return WikiDB A WikiDB object.
97      **/
98     function open ($dbparams) {
99         $dbtype = $dbparams{'dbtype'};
100         include_once("lib/WikiDB/$dbtype.php");
101                                 
102         $class = 'WikiDB_' . $dbtype;
103         return new $class ($dbparams);
104     }
105
106
107     /**
108      * Constructor.
109      *
110      * @access private
111      * @see open()
112      */
113     function WikiDB (&$backend, $dbparams) {
114         $this->_backend = &$backend;
115         // don't do the following with the auth_dsn!
116         if (isset($dbparams['auth_dsn'])) return;
117         
118         $this->_cache = new WikiDB_cache($backend);
119         if (!empty($GLOBALS['request'])) $GLOBALS['request']->_dbi = $this;
120
121         // If the database doesn't yet have a timestamp, initialize it now.
122         if ($this->get('_timestamp') === false)
123             $this->touch();
124         
125         // devel checking.
126         if ((int)DEBUG & _DEBUG_SQL) {
127             $this->_backend->check();
128     }
129     }
130     
131     /**
132      * Close database connection.
133      *
134      * The database may no longer be used after it is closed.
135      *
136      * Closing a WikiDB invalidates all <tt>WikiDB_Page</tt>s,
137      * <tt>WikiDB_PageRevision</tt>s and <tt>WikiDB_PageIterator</tt>s
138      * which have been obtained from it.
139      *
140      * @access public
141      */
142     function close () {
143         $this->_backend->close();
144         $this->_cache->close();
145     }
146     
147     /**
148      * Get a WikiDB_Page from a WikiDB.
149      *
150      * A {@link WikiDB} consists of the (infinite) set of all possible pages,
151      * therefore this method never fails.
152      *
153      * @access public
154      * @param string $pagename Which page to get.
155      * @return WikiDB_Page The requested WikiDB_Page.
156      */
157     function getPage($pagename) {
158         static $error_displayed = false;
159         $pagename = (string) $pagename;
160         if ((int)DEBUG) {
161             if ($pagename === '') {
162                 if ($error_displayed) return false;
163                 $error_displayed = true;
164                 if (function_exists("xdebug_get_function_stack"))
165                     var_dump(xdebug_get_function_stack());
166                 trigger_error("empty pagename", E_USER_WARNING);
167                 return false;
168             }
169         } else {
170             assert($pagename != '');
171         }
172         return new WikiDB_Page($this, $pagename);
173     }
174
175     /**
176      * Determine whether page exists (in non-default form).
177      *
178      * <pre>
179      *   $is_page = $dbi->isWikiPage($pagename);
180      * </pre>
181      * is equivalent to
182      * <pre>
183      *   $page = $dbi->getPage($pagename);
184      *   $current = $page->getCurrentRevision();
185      *   $is_page = ! $current->hasDefaultContents();
186      * </pre>
187      * however isWikiPage may be implemented in a more efficient
188      * manner in certain back-ends.
189      *
190      * @access public
191      *
192      * @param string $pagename string Which page to check.
193      *
194      * @return boolean True if the page actually exists with
195      * non-default contents in the WikiDataBase.
196      */
197     function isWikiPage ($pagename) {
198         $page = $this->getPage($pagename);
199         return $page->exists();
200     }
201
202     /**
203      * Delete page from the WikiDB. 
204      *
205      * Deletes the page from the WikiDB with the possibility to revert and diff.
206      * //Also resets all page meta-data to the default values.
207      *
208      * Note: purgePage() effectively destroys all revisions of the page from the WikiDB. 
209      *
210      * @access public
211      *
212      * @param string $pagename Name of page to delete.
213      */
214     function deletePage($pagename) {
215         // don't create empty revisions of already purged pages.
216         if ($this->_backend->get_latest_version($pagename))
217             $result = $this->_cache->delete_page($pagename);
218         else 
219             $result = -1;
220
221         /* Generate notification emails? */
222         if (! $this->isWikiPage($pagename) and !isa($GLOBALS['request'],'MockRequest')) {
223             $notify = $this->get('notify');
224             if (!empty($notify) and is_array($notify)) {
225                 global $request;
226                 //TODO: deferr it (quite a massive load if you remove some pages).
227                 //TODO: notification class which catches all changes,
228                 //  and decides at the end of the request what to mail. 
229                 //  (type, page, who, what, users, emails)
230                 // could be used for PageModeration and RSS2 Cloud xml-rpc also.
231                 $page = new WikiDB_Page($this, $pagename);
232                 list($emails, $userids) = $page->getPageChangeEmails($notify);
233                 if (!empty($emails)) {
234                     $from = $request->_user->getId() . '@' .  $request->get('REMOTE_HOST');
235                     $editedby = sprintf(_("Removed by: %s"), $from);
236                     $emails = join(',', $emails);
237                     $subject = sprintf(_("Page removed %s"), urlencode($pagename));
238                     if (mail("<undisclosed-recipients>","[".WIKI_NAME."] ".$subject, 
239                              $subject."\n".
240                              $editedby."\n\n".
241                              "Deleted $pagename",
242                              "From: $from\r\nBcc: $emails"))
243                         trigger_error(sprintf(_("PageChange Notification of %s sent to %s"),
244                                               $pagename, join(',',$userids)), E_USER_NOTICE);
245                     else
246                         trigger_error(sprintf(_("PageChange Notification Error: Couldn't send %s to %s"),
247                                               $pagename, join(',',$userids)), E_USER_WARNING);
248                 }
249             }
250         }
251
252         //How to create a RecentChanges entry with explaining summary? Dynamically
253         /*
254         $page = $this->getPage($pagename);
255         $current = $page->getCurrentRevision();
256         $meta = $current->_data;
257         $version = $current->getVersion();
258         $meta['summary'] = _("removed");
259         $page->save($current->getPackedContent(), $version + 1, $meta);
260         */
261         return $result;
262     }
263
264     /**
265      * Completely remove the page from the WikiDB, without undo possibility.
266      */
267     function purgePage($pagename) {
268         $result = $this->_cache->purge_page($pagename);
269         $this->deletePage($pagename); // just for the notification
270         return $result;
271     }
272     
273     /**
274      * Retrieve all pages.
275      *
276      * Gets the set of all pages with non-default contents.
277      *
278      * @access public
279      *
280      * @param boolean $include_defaulted Normally pages whose most
281      * recent revision has empty content are considered to be
282      * non-existant. Unless $include_defaulted is set to true, those
283      * pages will not be returned.
284      *
285      * @return WikiDB_PageIterator A WikiDB_PageIterator which contains all pages
286      *     in the WikiDB which have non-default contents.
287      */
288     function getAllPages($include_empty=false, $sortby=false, $limit=false, 
289                          $exclude=false) 
290     {
291         // HACK: memory_limit=8M will fail on too large pagesets. old php on unix only!
292         if (USECACHE) {
293             $mem = ini_get("memory_limit");
294             if ($mem and !$limit and !isWindows() and !check_php_version(4,3)) {
295                 $limit = 450;
296                 $GLOBALS['request']->setArg('limit', $limit);
297                 $GLOBALS['request']->setArg('paging', 'auto');
298             }
299         }
300         $result = $this->_backend->get_all_pages($include_empty, $sortby, $limit, 
301                                                  $exclude);
302         return new WikiDB_PageIterator($this, $result, 
303                                        array('include_empty' => $include_empty, 
304                                              'exclude' => $exclude,
305                                              'limit' => $limit));
306     }
307
308     /**
309      * $include_empty = true: include also empty pages
310      * exclude: comma-seperated list pagenames: TBD: array of pagenames
311      */
312     function numPages($include_empty=false, $exclude='') {
313         if (method_exists($this->_backend, 'numPages'))
314             // FIXME: currently are all args ignored.
315             $count = $this->_backend->numPages($include_empty, $exclude);
316         else {
317             // FIXME: exclude ignored.
318             $iter = $this->getAllPages($include_empty, false, false, $exclude);
319             $count = $iter->count();
320             $iter->free();
321         }
322         return (int)$count;
323     }
324     
325     /**
326      * Title search.
327      *
328      * Search for pages containing (or not containing) certain words
329      * in their names.
330      *
331      * Pages are returned in alphabetical order whenever it is
332      * practical to do so.
333      *
334      * FIXME: clarify $search syntax. provide glob=>TextSearchQuery converters
335      *
336      * @access public
337      * @param TextSearchQuery $search A TextSearchQuery object
338      * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching pages.
339      * @see TextSearchQuery
340      */
341     function titleSearch($search, $sortby='pagename', $limit=false, $exclude=false) {
342         $result = $this->_backend->text_search($search, false, $sortby, $limit, $exclude);
343         return new WikiDB_PageIterator($this, $result,
344                                        array('exclude' => $exclude,
345                                              'limit' => $limit));
346     }
347
348     /**
349      * Full text search.
350      *
351      * Search for pages containing (or not containing) certain words
352      * in their entire text (this includes the page content and the
353      * page name).
354      *
355      * Pages are returned in alphabetical order whenever it is
356      * practical to do so.
357      *
358      * @access public
359      *
360      * @param TextSearchQuery $search A TextSearchQuery object.
361      * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching pages.
362      * @see TextSearchQuery
363      */
364     function fullSearch($search, $sortby='pagename', $limit=false, $exclude=false) {
365         $result = $this->_backend->text_search($search, true, $sortby, $limit, $exclude);
366         return new WikiDB_PageIterator($this, $result,
367                                        array('exclude' => $exclude,
368                                              'limit'   => $limit,
369                                              'stoplisted' => $result->stoplisted
370                                              ));
371     }
372
373     /**
374      * Find the pages with the greatest hit counts.
375      *
376      * Pages are returned in reverse order by hit count.
377      *
378      * @access public
379      *
380      * @param integer $limit The maximum number of pages to return.
381      * Set $limit to zero to return all pages.  If $limit < 0, pages will
382      * be sorted in decreasing order of popularity.
383      *
384      * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching
385      * pages.
386      */
387     function mostPopular($limit = 20, $sortby = '-hits') {
388         $result = $this->_backend->most_popular($limit, $sortby);
389         return new WikiDB_PageIterator($this, $result);
390     }
391
392     /**
393      * Find recent page revisions.
394      *
395      * Revisions are returned in reverse order by creation time.
396      *
397      * @access public
398      *
399      * @param hash $params This hash is used to specify various optional
400      *   parameters:
401      * <dl>
402      * <dt> limit 
403      *    <dd> (integer) At most this many revisions will be returned.
404      * <dt> since
405      *    <dd> (integer) Only revisions since this time (unix-timestamp) will be returned. 
406      * <dt> include_minor_revisions
407      *    <dd> (boolean) Also include minor revisions.  (Default is not to.)
408      * <dt> exclude_major_revisions
409      *    <dd> (boolean) Don't include non-minor revisions.
410      *         (Exclude_major_revisions implies include_minor_revisions.)
411      * <dt> include_all_revisions
412      *    <dd> (boolean) Return all matching revisions for each page.
413      *         Normally only the most recent matching revision is returned
414      *         for each page.
415      * </dl>
416      *
417      * @return WikiDB_PageRevisionIterator A WikiDB_PageRevisionIterator containing the
418      * matching revisions.
419      */
420     function mostRecent($params = false) {
421         $result = $this->_backend->most_recent($params);
422         return new WikiDB_PageRevisionIterator($this, $result);
423     }
424
425     /**
426      * @access public
427      *
428      * @return Iterator A generic iterator containing rows of (duplicate) pagename, wantedfrom.
429      */
430     function wantedPages($exclude_from='', $exclude='', $sortby=false, $limit=false) {
431         return $this->_backend->wanted_pages($exclude_from, $exclude, $sortby, $limit);
432         //return new WikiDB_PageIterator($this, $result);
433     }
434
435     /**
436      * @access public
437      *
438      * @return array of pagename => linkrelation or just all related pages
439      */
440     function listRelations($sortby=false, $limit=false, $exclude=false) {
441         if (method_exists($this->_backend, "list_relations"))
442             return $this->_backend->list_relations($sortby, $limit, $exclude);
443         $iter = $this->getAllPages(false, false, false, $exclude);
444         while ($page = $iter->next()) {
445             $reliter = $page->getRelations();
446             $names = array();
447             while ($rel = $reliter->next()) {
448                 $names[] = $rel->getName();
449             }
450             $relations = array_merge($relations, $names);
451             $reliter->free();
452         }
453         $iter->free();
454         return $relations;
455     }
456
457     /**
458      * Call the appropriate backend method.
459      *
460      * @access public
461      * @param string $from Page to rename
462      * @param string $to   New name
463      * @param boolean $updateWikiLinks If the text in all pages should be replaced.
464      * @return boolean     true or false
465      */
466     function renamePage($from, $to, $updateWikiLinks = false) {
467         assert(is_string($from) && $from != '');
468         assert(is_string($to) && $to != '');
469         $result = false;
470         if (method_exists($this->_backend, 'rename_page')) {
471             $oldpage = $this->getPage($from);
472             $newpage = $this->getPage($to);
473             //update all WikiLinks in existing pages
474             //non-atomic! i.e. if rename fails the links are not undone
475             if ($updateWikiLinks) {
476                 require_once('lib/plugin/WikiAdminSearchReplace.php');
477                 $links = $oldpage->getBackLinks();
478                 while ($linked_page = $links->next()) {
479                     WikiPlugin_WikiAdminSearchReplace::replaceHelper($this,
480                                                                      $linked_page->getName(),
481                                                                      $from, $to);
482                 }
483                 $links = $newpage->getBackLinks();
484                 while ($linked_page = $links->next()) {
485                     WikiPlugin_WikiAdminSearchReplace::replaceHelper($this,
486                                                                      $linked_page->getName(),
487                                                                      $from, $to);
488                 }
489             }
490             if ($oldpage->exists() and ! $newpage->exists()) {
491                 if ($result = $this->_backend->rename_page($from, $to)) {
492                     //create a RecentChanges entry with explaining summary
493                     $page = $this->getPage($to);
494                     $current = $page->getCurrentRevision();
495                     $meta = $current->_data;
496                     $version = $current->getVersion();
497                     $meta['summary'] = sprintf(_("renamed from %s"), $from);
498                     $page->save($current->getPackedContent(), $version + 1, $meta);
499                 }
500             } elseif (!$oldpage->getCurrentRevision(false) and !$newpage->exists()) {
501                 // if a version 0 exists try it also.
502                 $result = $this->_backend->rename_page($from, $to);
503             }
504         } else {
505             trigger_error(_("WikiDB::renamePage() not yet implemented for this backend"),
506                           E_USER_WARNING);
507         }
508         /* Generate notification emails? */
509         if ($result and !isa($GLOBALS['request'], 'MockRequest')) {
510             $notify = $this->get('notify');
511             if (!empty($notify) and is_array($notify)) {
512                 list($emails, $userids) = $oldpage->getPageChangeEmails($notify);
513                 if (!empty($emails)) {
514                     $oldpage->sendPageRenameNotification($to, $meta, $emails, $userids);
515                 }
516             }
517         }
518         return $result;
519     }
520
521     /** Get timestamp when database was last modified.
522      *
523      * @return string A string consisting of two integers,
524      * separated by a space.  The first is the time in
525      * unix timestamp format, the second is a modification
526      * count for the database.
527      *
528      * The idea is that you can cast the return value to an
529      * int to get a timestamp, or you can use the string value
530      * as a good hash for the entire database.
531      */
532     function getTimestamp() {
533         $ts = $this->get('_timestamp');
534         return sprintf("%d %d", $ts[0], $ts[1]);
535     }
536     
537     /**
538      * Update the database timestamp.
539      *
540      */
541     function touch() {
542         $ts = $this->get('_timestamp');
543         $this->set('_timestamp', array(time(), $ts[1] + 1));
544     }
545
546         
547     /**
548      * Access WikiDB global meta-data.
549      *
550      * NOTE: this is currently implemented in a hackish and
551      * not very efficient manner.
552      *
553      * @access public
554      *
555      * @param string $key Which meta data to get.
556      * Some reserved meta-data keys are:
557      * <dl>
558      * <dt>'_timestamp' <dd> Data used by getTimestamp().
559      * </dl>
560      *
561      * @return scalar The requested value, or false if the requested data
562      * is not set.
563      */
564     function get($key) {
565         if (!$key || $key[0] == '%')
566             return false;
567         /*
568          * Hack Alert: We can use any page (existing or not) to store
569          * this data (as long as we always use the same one.)
570          */
571         $gd = $this->getPage('global_data');
572         $data = $gd->get('__global');
573
574         if ($data && isset($data[$key]))
575             return $data[$key];
576         else
577             return false;
578     }
579
580     /**
581      * Set global meta-data.
582      *
583      * NOTE: this is currently implemented in a hackish and
584      * not very efficient manner.
585      *
586      * @see get
587      * @access public
588      *
589      * @param string $key  Meta-data key to set.
590      * @param string $newval  New value.
591      */
592     function set($key, $newval) {
593         if (!$key || $key[0] == '%')
594             return;
595         
596         $gd = $this->getPage('global_data');
597         $data = $gd->get('__global');
598         if ($data === false)
599             $data = array();
600
601         if (empty($newval))
602             unset($data[$key]);
603         else
604             $data[$key] = $newval;
605
606         $gd->set('__global', $data);
607     }
608
609     /* TODO: these are really backend methods */
610
611     // SQL result: for simple select or create/update queries
612     // returns the database specific resource type
613     function genericSqlQuery($sql, $args=false) {
614         if (function_exists('debug_backtrace')) { // >= 4.3.0
615             echo "<pre>", printSimpleTrace(debug_backtrace()), "</pre>\n";
616         }
617         trigger_error("no SQL database", E_USER_ERROR);
618         return false;
619     }
620
621     // SQL iter: for simple select or create/update queries
622     // returns the generic iterator object (count,next)
623     function genericSqlIter($sql, $field_list = NULL) {
624         if (function_exists('debug_backtrace')) { // >= 4.3.0
625             echo "<pre>", printSimpleTrace(debug_backtrace()), "</pre>\n";
626         }
627         trigger_error("no SQL database", E_USER_ERROR);
628         return false;
629     }
630     
631     // see backend upstream methods
632     // ADODB adds surrounding quotes, SQL not yet!
633     function quote ($s) {
634         return $s;
635     }
636
637     function isOpen () {
638         global $request;
639         if (!$request->_dbi) return false;
640         else return false; /* so far only needed for sql so false it. 
641                             later we have to check dba also */
642     }
643
644     function getParam($param) {
645         global $DBParams;
646         if (isset($DBParams[$param])) return $DBParams[$param];
647         elseif ($param == 'prefix') return '';
648         else return false;
649     }
650
651     function getAuthParam($param) {
652         global $DBAuthParams;
653         if (isset($DBAuthParams[$param])) return $DBAuthParams[$param];
654         elseif ($param == 'USER_AUTH_ORDER') return $GLOBALS['USER_AUTH_ORDER'];
655         elseif ($param == 'USER_AUTH_POLICY') return $GLOBALS['USER_AUTH_POLICY'];
656         else return false;
657     }
658 };
659
660
661 /**
662  * An abstract base class which representing a wiki-page within a
663  * WikiDB.
664  *
665  * A WikiDB_Page contains a number (at least one) of
666  * WikiDB_PageRevisions.
667  */
668 class WikiDB_Page 
669 {
670     function WikiDB_Page(&$wikidb, $pagename) {
671         $this->_wikidb = &$wikidb;
672         $this->_pagename = $pagename;
673         if ((int)DEBUG) {
674             if (!(is_string($pagename) and $pagename != '')) {
675                 if (function_exists("xdebug_get_function_stack")) {
676                     echo "xdebug_get_function_stack(): "; var_dump(xdebug_get_function_stack());
677                 } elseif (function_exists("debug_backtrace")) { // >= 4.3.0
678                     printSimpleTrace(debug_backtrace());
679                 }
680                 trigger_error("empty pagename", E_USER_WARNING);
681                 return false;
682             }
683         } else {
684             assert(is_string($pagename) and $pagename != '');
685         }
686     }
687
688     /**
689      * Get the name of the wiki page.
690      *
691      * @access public
692      *
693      * @return string The page name.
694      */
695     function getName() {
696         return $this->_pagename;
697     }
698     
699     // To reduce the memory footprint for larger sets of pagelists,
700     // we don't cache the content (only true or false) and 
701     // we purge the pagedata (_cached_html) also
702     function exists() {
703         if (isset($this->_wikidb->_cache->_id_cache[$this->_pagename])) return true;
704         $current = $this->getCurrentRevision(false);
705         if (!$current) return false;
706         return ! $current->hasDefaultContents();
707     }
708
709     /**
710      * Delete an old revision of a WikiDB_Page.
711      *
712      * Deletes the specified revision of the page.
713      * It is a fatal error to attempt to delete the current revision.
714      *
715      * @access public
716      *
717      * @param integer $version Which revision to delete.  (You can also
718      *  use a WikiDB_PageRevision object here.)
719      */
720     function deleteRevision($version) {
721         $backend = &$this->_wikidb->_backend;
722         $cache = &$this->_wikidb->_cache;
723         $pagename = &$this->_pagename;
724
725         $version = $this->_coerce_to_version($version);
726         if ($version == 0)
727             return;
728
729         $backend->lock(array('page','version'));
730         $latestversion = $cache->get_latest_version($pagename);
731         if ($latestversion && ($version == $latestversion)) {
732             $backend->unlock(array('page','version'));
733             trigger_error(sprintf("Attempt to delete most recent revision of '%s'",
734                                   $pagename), E_USER_ERROR);
735             return;
736         }
737
738         $cache->delete_versiondata($pagename, $version);
739         $backend->unlock(array('page','version'));
740     }
741
742     /*
743      * Delete a revision, or possibly merge it with a previous
744      * revision.
745      *
746      * The idea is this:
747      * Suppose an author make a (major) edit to a page.  Shortly
748      * after that the same author makes a minor edit (e.g. to fix
749      * spelling mistakes he just made.)
750      *
751      * Now some time later, where cleaning out old saved revisions,
752      * and would like to delete his minor revision (since there's
753      * really no point in keeping minor revisions around for a long
754      * time.)
755      *
756      * Note that the text after the minor revision probably represents
757      * what the author intended to write better than the text after
758      * the preceding major edit.
759      *
760      * So what we really want to do is merge the minor edit with the
761      * preceding edit.
762      *
763      * We will only do this when:
764      * <ul>
765      * <li>The revision being deleted is a minor one, and
766      * <li>It has the same author as the immediately preceding revision.
767      * </ul>
768      */
769     function mergeRevision($version) {
770         $backend = &$this->_wikidb->_backend;
771         $cache = &$this->_wikidb->_cache;
772         $pagename = &$this->_pagename;
773
774         $version = $this->_coerce_to_version($version);
775         if ($version == 0)
776             return;
777
778         $backend->lock(array('version'));
779         $latestversion = $cache->get_latest_version($pagename);
780         if ($latestversion && $version == $latestversion) {
781             $backend->unlock(array('version'));
782             trigger_error(sprintf("Attempt to merge most recent revision of '%s'",
783                                   $pagename), E_USER_ERROR);
784             return;
785         }
786
787         $versiondata = $cache->get_versiondata($pagename, $version, true);
788         if (!$versiondata) {
789             // Not there? ... we're done!
790             $backend->unlock(array('version'));
791             return;
792         }
793
794         if ($versiondata['is_minor_edit']) {
795             $previous = $backend->get_previous_version($pagename, $version);
796             if ($previous) {
797                 $prevdata = $cache->get_versiondata($pagename, $previous);
798                 if ($prevdata['author_id'] == $versiondata['author_id']) {
799                     // This is a minor revision, previous version is
800                     // by the same author. We will merge the
801                     // revisions.
802                     $cache->update_versiondata($pagename, $previous,
803                                                array('%content' => $versiondata['%content'],
804                                                      '_supplanted' => $versiondata['_supplanted']));
805                 }
806             }
807         }
808
809         $cache->delete_versiondata($pagename, $version);
810         $backend->unlock(array('version'));
811     }
812
813     
814     /**
815      * Create a new revision of a {@link WikiDB_Page}.
816      *
817      * @access public
818      *
819      * @param int $version Version number for new revision.  
820      * To ensure proper serialization of edits, $version must be
821      * exactly one higher than the current latest version.
822      * (You can defeat this check by setting $version to
823      * {@link WIKIDB_FORCE_CREATE} --- not usually recommended.)
824      *
825      * @param string $content Contents of new revision.
826      *
827      * @param hash $metadata Metadata for new revision.
828      * All values in the hash should be scalars (strings or integers).
829      *
830      * @param hash $links List of linkto=>pagename, relation=>pagename which this page links to.
831      *
832      * @return WikiDB_PageRevision  Returns the new WikiDB_PageRevision object. If
833      * $version was incorrect, returns false
834      */
835     function createRevision($version, &$content, $metadata, $links) {
836         $backend = &$this->_wikidb->_backend;
837         $cache = &$this->_wikidb->_cache;
838         $pagename = &$this->_pagename;
839         $cache->invalidate_cache($pagename);
840         
841         $backend->lock(array('version','page','recent','link','nonempty'));
842
843         $latestversion = $backend->get_latest_version($pagename);
844         $newversion = ($latestversion ? $latestversion : 0) + 1;
845         assert($newversion >= 1);
846
847         if ($version != WIKIDB_FORCE_CREATE and $version != $newversion) {
848             $backend->unlock(array('version','page','recent','link','nonempty'));
849             return false;
850         }
851
852         $data = $metadata;
853         
854         foreach ($data as $key => $val) {
855             if (empty($val) || $key[0] == '_' || $key[0] == '%')
856                 unset($data[$key]);
857         }
858                         
859         assert(!empty($data['author']));
860         if (empty($data['author_id']))
861             @$data['author_id'] = $data['author'];
862                 
863         if (empty($data['mtime']))
864             $data['mtime'] = time();
865
866         if ($latestversion and $version != WIKIDB_FORCE_CREATE) {
867             // Ensure mtimes are monotonic.
868             $pdata = $cache->get_versiondata($pagename, $latestversion);
869             if ($data['mtime'] < $pdata['mtime']) {
870                 trigger_error(sprintf(_("%s: Date of new revision is %s"),
871                                       $pagename,"'non-monotonic'"),
872                               E_USER_NOTICE);
873                 $data['orig_mtime'] = $data['mtime'];
874                 $data['mtime'] = $pdata['mtime'];
875             }
876             
877             // FIXME: use (possibly user specified) 'mtime' time or
878             // time()?
879             $cache->update_versiondata($pagename, $latestversion,
880                                        array('_supplanted' => $data['mtime']));
881         }
882
883         $data['%content'] = &$content;
884
885         $cache->set_versiondata($pagename, $newversion, $data);
886
887         //$cache->update_pagedata($pagename, array(':latestversion' => $newversion,
888         //':deleted' => empty($content)));
889         
890         $backend->set_links($pagename, $links);
891
892         $backend->unlock(array('version','page','recent','link','nonempty'));
893
894         return new WikiDB_PageRevision($this->_wikidb, $pagename, $newversion,
895                                        $data);
896     }
897
898     /** A higher-level interface to createRevision.
899      *
900      * This takes care of computing the links, and storing
901      * a cached version of the transformed wiki-text.
902      *
903      * @param string $wikitext  The page content.
904      *
905      * @param int $version Version number for new revision.  
906      * To ensure proper serialization of edits, $version must be
907      * exactly one higher than the current latest version.
908      * (You can defeat this check by setting $version to
909      * {@link WIKIDB_FORCE_CREATE} --- not usually recommended.)
910      *
911      * @param hash $meta  Meta-data for new revision.
912      */
913     function save($wikitext, $version, $meta) {
914         $formatted = new TransformedText($this, $wikitext, $meta);
915         $type = $formatted->getType();
916         $meta['pagetype'] = $type->getName();
917         $links = $formatted->getWikiPageLinks(); // linkto => relation
918
919         $backend = &$this->_wikidb->_backend;
920         $newrevision = $this->createRevision($version, $wikitext, $meta, $links);
921         if ($newrevision and !WIKIDB_NOCACHE_MARKUP)
922             $this->set('_cached_html', $formatted->pack());
923
924         // FIXME: probably should have some global state information
925         // in the backend to control when to optimize.
926         //
927         // We're doing this here rather than in createRevision because
928         // postgresql can't optimize while locked.
929         if (((int)DEBUG & _DEBUG_SQL)
930             or (DATABASE_OPTIMISE_FREQUENCY > 0 and 
931                 (time() % DATABASE_OPTIMISE_FREQUENCY == 0))) {
932             if ($backend->optimize()) {
933                 if ((int)DEBUG)
934                     trigger_error(_("Optimizing database"), E_USER_NOTICE);
935             }
936         }
937
938         /* Generate notification emails? */
939         if (isa($newrevision, 'WikiDB_PageRevision')) {
940             // Save didn't fail because of concurrent updates.
941             $notify = $this->_wikidb->get('notify');
942             if (!empty($notify) and is_array($notify) and !isa($GLOBALS['request'],'MockRequest')) {
943                 list($emails, $userids) = $this->getPageChangeEmails($notify);
944                 if (!empty($emails)) {
945                     $this->sendPageChangeNotification($wikitext, $version, $meta, $emails, $userids);
946                 }
947             }
948             $newrevision->_transformedContent = $formatted;
949         }
950
951         return $newrevision;
952     }
953
954     function getPageChangeEmails($notify) {
955         $emails = array(); $userids = array();
956         foreach ($notify as $page => $users) {
957             if (glob_match($page, $this->_pagename)) {
958                 foreach ($users as $userid => $user) {
959                     if (!$user) { // handle the case for ModeratePage: no prefs, just userid's.
960                         global $request;
961                         $u = $request->getUser();
962                         if ($u->UserName() == $userid) {
963                             $prefs = $u->getPreferences();
964                         } else {
965                             // not current user
966                             if (ENABLE_USER_NEW) {
967                                 $u = WikiUser($userid);
968                                 $u->getPreferences();
969                                 $prefs = &$u->_prefs;
970                             } else {
971                                 $u = new WikiUser($GLOBALS['request'], $userid);
972                                 $prefs = $u->getPreferences();
973                             }
974                         }
975                         $emails[] = $prefs->get('email');
976                         $userids[] = $userid;
977                     } else {
978                       if (!empty($user['verified']) and !empty($user['email'])) {
979                         $emails[]  = $user['email'];
980                         $userids[] = $userid;
981                       } elseif (!empty($user['email'])) {
982                         global $request;
983                         // do a dynamic emailVerified check update
984                         $u = $request->getUser();
985                         if ($u->UserName() == $userid) {
986                             if ($request->_prefs->get('emailVerified')) {
987                                 $emails[] = $user['email'];
988                                 $userids[] = $userid;
989                                 $notify[$page][$userid]['verified'] = 1;
990                                 $request->_dbi->set('notify', $notify);
991                             }
992                         } else {
993                             // not current user
994                             if (ENABLE_USER_NEW) {
995                                 $u = WikiUser($userid);
996                                 $u->getPreferences();
997                                 $prefs = &$u->_prefs;
998                             } else {
999                                 $u = new WikiUser($GLOBALS['request'], $userid);
1000                                 $prefs = $u->getPreferences();
1001                             }
1002                             if ($prefs->get('emailVerified')) {
1003                                 $emails[] = $user['email'];
1004                                 $userids[] = $userid;
1005                                 $notify[$page][$userid]['verified'] = 1;
1006                                 $request->_dbi->set('notify', $notify);
1007                             }
1008                         }
1009                         // ignore verification
1010                         /*
1011                         if (DEBUG) {
1012                             if (!in_array($user['email'],$emails))
1013                                 $emails[] = $user['email'];
1014                         }
1015                         */
1016                     }
1017                   }
1018                 }
1019             }
1020         }
1021         $emails = array_unique($emails);
1022         $userids = array_unique($userids);
1023         return array($emails, $userids);
1024     }
1025
1026     /**
1027      * Send udiff for a changed page to multiple users.
1028      * See rename and remove methods also
1029      */
1030     function sendPageChangeNotification(&$wikitext, $version, $meta, $emails, $userids) {
1031         global $request;
1032         if (@is_array($request->_deferredPageChangeNotification)) {
1033             // collapse multiple changes (loaddir) into one email
1034             $request->_deferredPageChangeNotification[] 
1035                 = array($this->_pagename, $emails, $userids);
1036             return;
1037         }
1038         $backend = &$this->_wikidb->_backend;
1039         //$backend = &$request->_dbi->_backend;
1040         $subject = _("Page change").' '.urlencode($this->_pagename);
1041         $previous = $backend->get_previous_version($this->_pagename, $version);
1042         if (!isset($meta['mtime'])) $meta['mtime'] = time();
1043         if ($previous) {
1044             $difflink = WikiURL($this->_pagename, array('action'=>'diff'), true);
1045             $cache = &$this->_wikidb->_cache;
1046             //$cache = &$request->_dbi->_cache;
1047             $this_content = explode("\n", $wikitext);
1048             $prevdata = $cache->get_versiondata($this->_pagename, $previous, true);
1049             if (empty($prevdata['%content']))
1050                 $prevdata = $backend->get_versiondata($this->_pagename, $previous, true);
1051             $other_content = explode("\n", $prevdata['%content']);
1052             
1053             include_once("lib/difflib.php");
1054             $diff2 = new Diff($other_content, $this_content);
1055             //$context_lines = max(4, count($other_content) + 1,
1056             //                     count($this_content) + 1);
1057             $fmt = new UnifiedDiffFormatter(/*$context_lines*/);
1058             $content  = $this->_pagename . " " . $previous . " " . 
1059                 Iso8601DateTime($prevdata['mtime']) . "\n";
1060             $content .= $this->_pagename . " " . $version . " " .  
1061                 Iso8601DateTime($meta['mtime']) . "\n";
1062             $content .= $fmt->format($diff2);
1063             
1064         } else {
1065             $difflink = WikiURL($this->_pagename,array(),true);
1066             $content = $this->_pagename . " " . $version . " " .  
1067                 Iso8601DateTime($meta['mtime']) . "\n";
1068             $content .= _("New page");
1069         }
1070         $from = $request->_user->getId() . '@' .  $request->get('REMOTE_HOST');
1071         $editedby = sprintf(_("Edited by: %s"), $from);
1072         $emails = join(',',$emails);
1073         if (mail("<undisclosed-recipients>",
1074                  "[".WIKI_NAME."] ".$subject, 
1075                  $subject."\n". $editedby."\n". $difflink."\n\n". $content,
1076                  "From: $from\r\nBcc: $emails"))
1077             trigger_error(sprintf(_("PageChange Notification of %s sent to %s"),
1078                                   $this->_pagename, join(',',$userids)), E_USER_NOTICE);
1079         else
1080             trigger_error(sprintf(_("PageChange Notification Error: Couldn't send %s to %s"),
1081                                   $this->_pagename, join(',',$userids)), E_USER_WARNING);
1082     }
1083
1084     /** support mass rename / remove (not yet tested)
1085      */
1086     function sendPageRenameNotification($to, &$meta, $emails, $userids) {
1087         global $request;
1088         if (@is_array($request->_deferredPageRenameNotification)) {
1089             $request->_deferredPageRenameNotification[] = array($this->_pagename, 
1090                                                                 $to, $meta, $emails, $userids);
1091         } else {
1092             $oldname = $this->_pagename;
1093             $from = $request->_user->getId() . '@' .  $request->get('REMOTE_HOST');
1094             $editedby = sprintf(_("Edited by: %s"), $from);
1095             $emails = join(',',$emails);
1096             $subject = sprintf(_("Page rename %s to %s"), urlencode($oldname), urlencode($to));
1097             $link = WikiURL($to, true);
1098             if (mail("<undisclosed-recipients>",
1099                      "[".WIKI_NAME."] ".$subject, 
1100                      $subject."\n".$editedby."\n".$link."\n\n"."Renamed $from to $to",
1101                      "From: $from\r\nBcc: $emails"))
1102                 trigger_error(sprintf(_("PageChange Notification of %s sent to %s"),
1103                                       $oldname, join(',',$userids)), E_USER_NOTICE);
1104             else
1105                 trigger_error(sprintf(_("PageChange Notification Error: Couldn't send %s to %s"),
1106                                       $oldname, join(',',$userids)), E_USER_WARNING);
1107         }
1108     }
1109
1110     /**
1111      * Get the most recent revision of a page.
1112      *
1113      * @access public
1114      *
1115      * @return WikiDB_PageRevision The current WikiDB_PageRevision object. 
1116      */
1117     function getCurrentRevision ($need_content = true) {
1118         $backend = &$this->_wikidb->_backend;
1119         $cache = &$this->_wikidb->_cache;
1120         $pagename = &$this->_pagename;
1121         
1122         // Prevent deadlock in case of memory exhausted errors
1123         // Pure selection doesn't really need locking here.
1124         //   sf.net bug#927395
1125         // I know it would be better to lock, but with lots of pages this deadlock is more 
1126         // severe than occasionally get not the latest revision.
1127         // In spirit to wikiwiki: read fast, edit slower.
1128         //$backend->lock();
1129         $version = $cache->get_latest_version($pagename);
1130         // getRevision gets the content also!
1131         $revision = $this->getRevision($version, $need_content);
1132         //$backend->unlock();
1133         assert($revision);
1134         return $revision;
1135     }
1136
1137     /**
1138      * Get a specific revision of a WikiDB_Page.
1139      *
1140      * @access public
1141      *
1142      * @param integer $version  Which revision to get.
1143      *
1144      * @return WikiDB_PageRevision The requested WikiDB_PageRevision object, or
1145      * false if the requested revision does not exist in the {@link WikiDB}.
1146      * Note that version zero of any page always exists.
1147      */
1148     function getRevision ($version, $need_content=true) {
1149         $cache = &$this->_wikidb->_cache;
1150         $pagename = &$this->_pagename;
1151         
1152         if (! $version or $version == -1) // 0 or false
1153             return new WikiDB_PageRevision($this->_wikidb, $pagename, 0);
1154
1155         assert($version > 0);
1156         $vdata = $cache->get_versiondata($pagename, $version, $need_content);
1157         if (!$vdata) {
1158             return new WikiDB_PageRevision($this->_wikidb, $pagename, 0);
1159         }
1160         return new WikiDB_PageRevision($this->_wikidb, $pagename, $version,
1161                                        $vdata);
1162     }
1163
1164     /**
1165      * Get previous page revision.
1166      *
1167      * This method find the most recent revision before a specified
1168      * version.
1169      *
1170      * @access public
1171      *
1172      * @param integer $version  Find most recent revision before this version.
1173      *  You can also use a WikiDB_PageRevision object to specify the $version.
1174      *
1175      * @return WikiDB_PageRevision The requested WikiDB_PageRevision object, or false if the
1176      * requested revision does not exist in the {@link WikiDB}.  Note that
1177      * unless $version is greater than zero, a revision (perhaps version zero,
1178      * the default revision) will always be found.
1179      */
1180     function getRevisionBefore ($version=false, $need_content=true) {
1181         $backend = &$this->_wikidb->_backend;
1182         $pagename = &$this->_pagename;
1183         if ($version === false)
1184             $version = $this->_wikidb->_cache->get_latest_version($pagename);
1185         else
1186             $version = $this->_coerce_to_version($version);
1187
1188         if ($version == 0)
1189             return false;
1190         //$backend->lock();
1191         $previous = $backend->get_previous_version($pagename, $version);
1192         $revision = $this->getRevision($previous, $need_content);
1193         //$backend->unlock();
1194         assert($revision);
1195         return $revision;
1196     }
1197
1198     /**
1199      * Get all revisions of the WikiDB_Page.
1200      *
1201      * This does not include the version zero (default) revision in the
1202      * returned revision set.
1203      *
1204      * @return WikiDB_PageRevisionIterator A
1205      *   WikiDB_PageRevisionIterator containing all revisions of this
1206      *   WikiDB_Page in reverse order by version number.
1207      */
1208     function getAllRevisions() {
1209         $backend = &$this->_wikidb->_backend;
1210         $revs = $backend->get_all_revisions($this->_pagename);
1211         return new WikiDB_PageRevisionIterator($this->_wikidb, $revs);
1212     }
1213     
1214     /**
1215      * Find pages which link to or are linked from a page.
1216      * relations: $backend->get_links is responsible to add the relation to the pagehash 
1217      * as 'linkrelation' key as pagename. See WikiDB_PageIterator::next 
1218      *   if (isset($next['linkrelation']))
1219      *
1220      * @access public
1221      *
1222      * @param boolean $reversed Which links to find: true for backlinks (default).
1223      *
1224      * @return WikiDB_PageIterator A WikiDB_PageIterator containing
1225      * all matching pages.
1226      */
1227     function getLinks ($reversed = true, $include_empty=false, $sortby=false, 
1228                        $limit=false, $exclude=false, $want_relations=false) 
1229     {
1230         $backend = &$this->_wikidb->_backend;
1231         $result =  $backend->get_links($this->_pagename, $reversed, 
1232                                        $include_empty, $sortby, $limit, $exclude,
1233                                        $want_relations);
1234         return new WikiDB_PageIterator($this->_wikidb, $result, 
1235                                        array('include_empty' => $include_empty,
1236                                              'sortby'        => $sortby, 
1237                                              'limit'         => $limit, 
1238                                              'exclude'       => $exclude,
1239                                              'want_relations'=> $want_relations));
1240     }
1241
1242     /**
1243      * All Links from other pages to this page.
1244      */
1245     function getBackLinks($include_empty=false, $sortby=false, $limit=false, $exclude=false, 
1246                           $want_relations=false) 
1247     {
1248         return $this->getLinks(true, $include_empty, $sortby, $limit, $exclude);
1249     }
1250     /**
1251      * Forward Links: All Links from this page to other pages.
1252      */
1253     function getPageLinks($include_empty=false, $sortby=false, $limit=false, $exclude=false, 
1254                           $want_relations=false) 
1255     {
1256         return $this->getLinks(false, $include_empty, $sortby, $limit, $exclude);
1257     }
1258     /**
1259      * Relations: All links from this page to other pages with relation <> 0. 
1260      * Like isa:=page
1261      */
1262     function getRelations($sortby=false, $limit=false, $exclude=false) {
1263         $backend = &$this->_wikidb->_backend;
1264         $result =  $backend->get_links($this->_pagename, false, true,
1265                                        $sortby, $limit, $exclude, 
1266                                        true);
1267         // we do not care for the linked page versiondata, just the pagename and linkrelation
1268         return new WikiDB_PageIterator($this->_wikidb, $result, 
1269                                        array('include_empty' => true,
1270                                              'sortby'        => $sortby, 
1271                                              'limit'         => $limit, 
1272                                              'exclude'       => $exclude,
1273                                              'want_relations'=> true));
1274     }
1275     
1276     /**
1277      * possibly faster link existance check. not yet accelerated.
1278      */
1279     function existLink($link, $reversed=false) {
1280         $backend = &$this->_wikidb->_backend;
1281         if (method_exists($backend,'exists_link'))
1282             return $backend->exists_link($this->_pagename, $link, $reversed);
1283         //$cache = &$this->_wikidb->_cache;
1284         // TODO: check cache if it is possible
1285         $iter = $this->getLinks($reversed, false);
1286         while ($page = $iter->next()) {
1287             if ($page->getName() == $link)
1288                 return $page;
1289         }
1290         $iter->free();
1291         return false;
1292     }
1293
1294     /* Semantic relations are links with the relation pointing to another page,
1295        the so called "RDF Triple".
1296        [San Diego] is%20a::city
1297        => "At the page San Diego there is a relation link of 'is a' to the page 'city'."
1298      */
1299
1300     /* Semantic attributes for a page. 
1301        [San Diego] population:=1,305,736
1302        Attributes are links with the relation pointing to another page.
1303     */
1304             
1305     /**
1306      * Access WikiDB_Page non version-specific meta-data.
1307      *
1308      * @access public
1309      *
1310      * @param string $key Which meta data to get.
1311      * Some reserved meta-data keys are:
1312      * <dl>
1313      * <dt>'date'  <dd> Created as unixtime
1314      * <dt>'locked'<dd> Is page locked? 'yes' or 'no'
1315      * <dt>'hits'  <dd> Page hit counter.
1316      * <dt>'_cached_html' <dd> Transformed CachedMarkup object, serialized + optionally gzipped.
1317      *                         In SQL stored now in an extra column.
1318      * Optional data:
1319      * <dt>'pref'  <dd> Users preferences, stored only in homepages.
1320      * <dt>'owner' <dd> Default: first author_id. We might add a group with a dot here:
1321      *                  E.g. "owner.users"
1322      * <dt>'perm'  <dd> Permission flag to authorize read/write/execution of 
1323      *                  page-headers and content.
1324      + <dt>'moderation'<dd> ModeratedPage data
1325      * <dt>'score' <dd> Page score (not yet implement, do we need?)
1326      * </dl>
1327      *
1328      * @return scalar The requested value, or false if the requested data
1329      * is not set.
1330      */
1331     function get($key) {
1332         $cache = &$this->_wikidb->_cache;
1333         $backend = &$this->_wikidb->_backend;
1334         if (!$key || $key[0] == '%')
1335             return false;
1336         // several new SQL backends optimize this.
1337         if (!WIKIDB_NOCACHE_MARKUP
1338             and $key == '_cached_html' 
1339             and method_exists($backend, 'get_cached_html')) 
1340         {
1341             return $backend->get_cached_html($this->_pagename);
1342         }
1343         $data = $cache->get_pagedata($this->_pagename);
1344         return isset($data[$key]) ? $data[$key] : false;
1345     }
1346
1347     /**
1348      * Get all the page meta-data as a hash.
1349      *
1350      * @return hash The page meta-data.
1351      */
1352     function getMetaData() {
1353         $cache = &$this->_wikidb->_cache;
1354         $data = $cache->get_pagedata($this->_pagename);
1355         $meta = array();
1356         foreach ($data as $key => $val) {
1357             if (/*!empty($val) &&*/ $key[0] != '%')
1358                 $meta[$key] = $val;
1359         }
1360         return $meta;
1361     }
1362
1363     /**
1364      * Set page meta-data.
1365      *
1366      * @see get
1367      * @access public
1368      *
1369      * @param string $key  Meta-data key to set.
1370      * @param string $newval  New value.
1371      */
1372     function set($key, $newval) {
1373         $cache = &$this->_wikidb->_cache;
1374         $backend = &$this->_wikidb->_backend;
1375         $pagename = &$this->_pagename;
1376         
1377         assert($key && $key[0] != '%');
1378
1379         // several new SQL backends optimize this.
1380         if (!WIKIDB_NOCACHE_MARKUP 
1381             and $key == '_cached_html' 
1382             and method_exists($backend, 'set_cached_html'))
1383         {
1384             return $backend->set_cached_html($pagename, $newval);
1385         }
1386
1387         $data = $cache->get_pagedata($pagename);
1388
1389         if (!empty($newval)) {
1390             if (!empty($data[$key]) && $data[$key] == $newval)
1391                 return;         // values identical, skip update.
1392         }
1393         else {
1394             if (empty($data[$key]))
1395                 return;         // values identical, skip update.
1396         }
1397
1398         $cache->update_pagedata($pagename, array($key => $newval));
1399     }
1400
1401     /**
1402      * Increase page hit count.
1403      *
1404      * FIXME: IS this needed?  Probably not.
1405      *
1406      * This is a convenience function.
1407      * <pre> $page->increaseHitCount(); </pre>
1408      * is functionally identical to
1409      * <pre> $page->set('hits',$page->get('hits')+1); </pre>
1410      * but less expensive (ignores the pagadata string)
1411      *
1412      * Note that this method may be implemented in more efficient ways
1413      * in certain backends.
1414      *
1415      * @access public
1416      */
1417     function increaseHitCount() {
1418         if (method_exists($this->_wikidb->_backend, 'increaseHitCount'))
1419             $this->_wikidb->_backend->increaseHitCount($this->_pagename);
1420         else {
1421             @$newhits = $this->get('hits') + 1;
1422             $this->set('hits', $newhits);
1423         }
1424     }
1425
1426     /**
1427      * Return a string representation of the WikiDB_Page
1428      *
1429      * This is really only for debugging.
1430      *
1431      * @access public
1432      *
1433      * @return string Printable representation of the WikiDB_Page.
1434      */
1435     function asString () {
1436         ob_start();
1437         printf("[%s:%s\n", get_class($this), $this->getName());
1438         print_r($this->getMetaData());
1439         echo "]\n";
1440         $strval = ob_get_contents();
1441         ob_end_clean();
1442         return $strval;
1443     }
1444
1445
1446     /**
1447      * @access private
1448      * @param integer_or_object $version_or_pagerevision
1449      * Takes either the version number (and int) or a WikiDB_PageRevision
1450      * object.
1451      * @return integer The version number.
1452      */
1453     function _coerce_to_version($version_or_pagerevision) {
1454         if (method_exists($version_or_pagerevision, "getContent"))
1455             $version = $version_or_pagerevision->getVersion();
1456         else
1457             $version = (int) $version_or_pagerevision;
1458
1459         assert($version >= 0);
1460         return $version;
1461     }
1462
1463     function isUserPage ($include_empty = true) {
1464         if (!$include_empty and !$this->exists()) return false;
1465         return $this->get('pref') ? true : false;
1466     }
1467
1468     // May be empty. Either the stored owner (/Chown), or the first authorized author
1469     function getOwner() {
1470         if ($owner = $this->get('owner'))
1471             return ($owner == "The PhpWiki programming team") ? ADMIN_USER : $owner;
1472         // check all revisions forwards for the first author_id
1473         $backend = &$this->_wikidb->_backend;
1474         $pagename = &$this->_pagename;
1475         $latestversion = $backend->get_latest_version($pagename);
1476         for ($v=1; $v <= $latestversion; $v++) {
1477             $rev = $this->getRevision($v,false);
1478             if ($rev and $owner = $rev->get('author_id')) {
1479                 return ($owner == "The PhpWiki programming team") ? ADMIN_USER : $owner;
1480             }
1481         }
1482         return '';
1483     }
1484
1485     // The authenticated author of the first revision or empty if not authenticated then.
1486     function getCreator() {
1487         if ($current = $this->getRevision(1,false)) return $current->get('author_id');
1488         else return '';
1489     }
1490
1491     // The authenticated author of the current revision.
1492     function getAuthor() {
1493         if ($current = $this->getCurrentRevision(false)) return $current->get('author_id');
1494         else return '';
1495     }
1496
1497 };
1498
1499 /**
1500  * This class represents a specific revision of a WikiDB_Page within
1501  * a WikiDB.
1502  *
1503  * A WikiDB_PageRevision has read-only semantics. You may only create
1504  * new revisions (and delete old ones) --- you cannot modify existing
1505  * revisions.
1506  */
1507 class WikiDB_PageRevision
1508 {
1509     //var $_transformedContent = false; // set by WikiDB_Page::save()
1510     
1511     function WikiDB_PageRevision(&$wikidb, $pagename, $version, $versiondata = false) {
1512         $this->_wikidb = &$wikidb;
1513         $this->_pagename = $pagename;
1514         $this->_version = $version;
1515         $this->_data = $versiondata ? $versiondata : array();
1516         $this->_transformedContent = false; // set by WikiDB_Page::save()
1517     }
1518     
1519     /**
1520      * Get the WikiDB_Page which this revision belongs to.
1521      *
1522      * @access public
1523      *
1524      * @return WikiDB_Page The WikiDB_Page which this revision belongs to.
1525      */
1526     function getPage() {
1527         return new WikiDB_Page($this->_wikidb, $this->_pagename);
1528     }
1529
1530     /**
1531      * Get the version number of this revision.
1532      *
1533      * @access public
1534      *
1535      * @return integer The version number of this revision.
1536      */
1537     function getVersion() {
1538         return $this->_version;
1539     }
1540     
1541     /**
1542      * Determine whether this revision has defaulted content.
1543      *
1544      * The default revision (version 0) of each page, as well as any
1545      * pages which are created with empty content have their content
1546      * defaulted to something like:
1547      * <pre>
1548      *   Describe [ThisPage] here.
1549      * </pre>
1550      *
1551      * @access public
1552      *
1553      * @return boolean Returns true if the page has default content.
1554      */
1555     function hasDefaultContents() {
1556         $data = &$this->_data;
1557         return empty($data['%content']); // FIXME: what if it's the number 0? <>'' or === false
1558     }
1559
1560     /**
1561      * Get the content as an array of lines.
1562      *
1563      * @access public
1564      *
1565      * @return array An array of lines.
1566      * The lines should contain no trailing white space.
1567      */
1568     function getContent() {
1569         return explode("\n", $this->getPackedContent());
1570     }
1571         
1572    /**
1573      * Get the pagename of the revision.
1574      *
1575      * @access public
1576      *
1577      * @return string pagename.
1578      */
1579     function getPageName() {
1580         return $this->_pagename;
1581     }
1582     function getName() {
1583         return $this->_pagename;
1584     }
1585
1586     /**
1587      * Determine whether revision is the latest.
1588      *
1589      * @access public
1590      *
1591      * @return boolean True iff the revision is the latest (most recent) one.
1592      */
1593     function isCurrent() {
1594         if (!isset($this->_iscurrent)) {
1595             $page = $this->getPage();
1596             $current = $page->getCurrentRevision(false);
1597             $this->_iscurrent = $this->getVersion() == $current->getVersion();
1598         }
1599         return $this->_iscurrent;
1600     }
1601
1602     /**
1603      * Get the transformed content of a page.
1604      *
1605      * @param string $pagetype  Override the page-type of the revision.
1606      *
1607      * @return object An XmlContent-like object containing the page transformed
1608      * contents.
1609      */
1610     function getTransformedContent($pagetype_override=false) {
1611         $backend = &$this->_wikidb->_backend;
1612         
1613         if ($pagetype_override) {
1614             // Figure out the normal page-type for this page.
1615             $type = PageType::GetPageType($this->get('pagetype'));
1616             if ($type->getName() == $pagetype_override)
1617                 $pagetype_override = false; // Not really an override...
1618         }
1619
1620         if ($pagetype_override) {
1621             // Overriden page type, don't cache (or check cache).
1622             return new TransformedText($this->getPage(),
1623                                        $this->getPackedContent(),
1624                                        $this->getMetaData(),
1625                                        $pagetype_override);
1626         }
1627
1628         $possibly_cache_results = true;
1629
1630         if (!USECACHE or WIKIDB_NOCACHE_MARKUP) {
1631             if (WIKIDB_NOCACHE_MARKUP == 'purge') {
1632                 // flush cache for this page.
1633                 $page = $this->getPage();
1634                 $page->set('_cached_html', ''); // ignored with !USECACHE 
1635             }
1636             $possibly_cache_results = false;
1637         }
1638         elseif (USECACHE and !$this->_transformedContent) {
1639             //$backend->lock();
1640             if ($this->isCurrent()) {
1641                 $page = $this->getPage();
1642                 $this->_transformedContent = TransformedText::unpack($page->get('_cached_html'));
1643             }
1644             else {
1645                 $possibly_cache_results = false;
1646             }
1647             //$backend->unlock();
1648         }
1649         
1650         if (!$this->_transformedContent) {
1651             $this->_transformedContent
1652                 = new TransformedText($this->getPage(),
1653                                       $this->getPackedContent(),
1654                                       $this->getMetaData());
1655             
1656             if ($possibly_cache_results and !WIKIDB_NOCACHE_MARKUP) {
1657                 // If we're still the current version, cache the transfomed page.
1658                 //$backend->lock();
1659                 if ($this->isCurrent()) {
1660                     $page->set('_cached_html', $this->_transformedContent->pack());
1661                 }
1662                 //$backend->unlock();
1663             }
1664         }
1665
1666         return $this->_transformedContent;
1667     }
1668
1669     /**
1670      * Get the content as a string.
1671      *
1672      * @access public
1673      *
1674      * @return string The page content.
1675      * Lines are separated by new-lines.
1676      */
1677     function getPackedContent() {
1678         $data = &$this->_data;
1679
1680         
1681         if (empty($data['%content'])) {
1682             include_once('lib/InlineParser.php');
1683
1684             // A feature similar to taglines at http://www.wlug.org.nz/
1685             // Lib from http://www.aasted.org/quote/
1686             if (defined('FORTUNE_DIR') 
1687                 and is_dir(FORTUNE_DIR) 
1688                 and in_array($GLOBALS['request']->getArg('action'), 
1689                              array('create','edit')))
1690             {
1691                 include_once("lib/fortune.php");
1692                 $fortune = new Fortune();
1693                 $quote = str_replace("\n<br>","\n", $fortune->quoteFromDir(FORTUNE_DIR));
1694                 return sprintf("<verbatim>\n%s</verbatim>\n\n"._("Describe %s here."), 
1695                                $quote, "[" . WikiEscape($this->_pagename) . "]");
1696             }
1697             // Replace empty content with default value.
1698             return sprintf(_("Describe %s here."), 
1699                            "[" . WikiEscape($this->_pagename) . "]");
1700         }
1701
1702         // There is (non-default) content.
1703         assert($this->_version > 0);
1704         
1705         if (!is_string($data['%content'])) {
1706             // Content was not provided to us at init time.
1707             // (This is allowed because for some backends, fetching
1708             // the content may be expensive, and often is not wanted
1709             // by the user.)
1710             //
1711             // In any case, now we need to get it.
1712             $data['%content'] = $this->_get_content();
1713             assert(is_string($data['%content']));
1714         }
1715         
1716         return $data['%content'];
1717     }
1718
1719     function _get_content() {
1720         $cache = &$this->_wikidb->_cache;
1721         $pagename = $this->_pagename;
1722         $version = $this->_version;
1723
1724         assert($version > 0);
1725         
1726         $newdata = $cache->get_versiondata($pagename, $version, true);
1727         if ($newdata) {
1728             assert(is_string($newdata['%content']));
1729             return $newdata['%content'];
1730         }
1731         else {
1732             // else revision has been deleted... What to do?
1733             return __sprintf("Oops! Revision %s of %s seems to have been deleted!",
1734                              $version, $pagename);
1735         }
1736     }
1737
1738     /**
1739      * Get meta-data for this revision.
1740      *
1741      *
1742      * @access public
1743      *
1744      * @param string $key Which meta-data to access.
1745      *
1746      * Some reserved revision meta-data keys are:
1747      * <dl>
1748      * <dt> 'mtime' <dd> Time this revision was created (seconds since midnight Jan 1, 1970.)
1749      *        The 'mtime' meta-value is normally set automatically by the database
1750      *        backend, but it may be specified explicitly when creating a new revision.
1751      * <dt> orig_mtime
1752      *  <dd> To ensure consistency of RecentChanges, the mtimes of the versions
1753      *       of a page must be monotonically increasing.  If an attempt is
1754      *       made to create a new revision with an mtime less than that of
1755      *       the preceeding revision, the new revisions timestamp is force
1756      *       to be equal to that of the preceeding revision.  In that case,
1757      *       the originally requested mtime is preserved in 'orig_mtime'.
1758      * <dt> '_supplanted' <dd> Time this revision ceased to be the most recent.
1759      *        This meta-value is <em>always</em> automatically maintained by the database
1760      *        backend.  (It is set from the 'mtime' meta-value of the superceding
1761      *        revision.)  '_supplanted' has a value of 'false' for the current revision.
1762      *
1763      * FIXME: this could be refactored:
1764      * <dt> author
1765      *  <dd> Author of the page (as he should be reported in, e.g. RecentChanges.)
1766      * <dt> author_id
1767      *  <dd> Authenticated author of a page.  This is used to identify
1768      *       the distinctness of authors when cleaning old revisions from
1769      *       the database.
1770      * <dt> 'is_minor_edit' <dd> Set if change was marked as a minor revision by the author.
1771      * <dt> 'summary' <dd> Short change summary entered by page author.
1772      * </dl>
1773      *
1774      * Meta-data keys must be valid C identifers (they have to start with a letter
1775      * or underscore, and can contain only alphanumerics and underscores.)
1776      *
1777      * @return string The requested value, or false if the requested value
1778      * is not defined.
1779      */
1780     function get($key) {
1781         if (!$key || $key[0] == '%')
1782             return false;
1783         $data = &$this->_data;
1784         return isset($data[$key]) ? $data[$key] : false;
1785     }
1786
1787     /**
1788      * Get all the revision page meta-data as a hash.
1789      *
1790      * @return hash The revision meta-data.
1791      */
1792     function getMetaData() {
1793         $meta = array();
1794         foreach ($this->_data as $key => $val) {
1795             if (!empty($val) && $key[0] != '%')
1796                 $meta[$key] = $val;
1797         }
1798         return $meta;
1799     }
1800     
1801             
1802     /**
1803      * Return a string representation of the revision.
1804      *
1805      * This is really only for debugging.
1806      *
1807      * @access public
1808      *
1809      * @return string Printable representation of the WikiDB_Page.
1810      */
1811     function asString () {
1812         ob_start();
1813         printf("[%s:%d\n", get_class($this), $this->get('version'));
1814         print_r($this->_data);
1815         echo $this->getPackedContent() . "\n]\n";
1816         $strval = ob_get_contents();
1817         ob_end_clean();
1818         return $strval;
1819     }
1820 };
1821
1822
1823 /**
1824  * Class representing a sequence of WikiDB_Pages.
1825  * TODO: Enhance to php5 iterators
1826  * TODO: 
1827  *   apply filters for options like 'sortby', 'limit', 'exclude'
1828  *   for simple queries like titleSearch, where the backend is not ready yet.
1829  */
1830 class WikiDB_PageIterator
1831 {
1832     function WikiDB_PageIterator(&$wikidb, &$iter, $options=false) {
1833         $this->_iter = $iter; // a WikiDB_backend_iterator
1834         $this->_wikidb = &$wikidb;
1835         $this->_options = $options;
1836     }
1837     
1838     function count () {
1839         return $this->_iter->count();
1840     }
1841
1842     /**
1843      * Get next WikiDB_Page in sequence.
1844      *
1845      * @access public
1846      *
1847      * @return WikiDB_Page The next WikiDB_Page in the sequence.
1848      */
1849     function next () {
1850         if ( ! ($next = $this->_iter->next()) )
1851             return false;
1852
1853         $pagename = &$next['pagename'];
1854         if (!is_string($pagename)) { // Bug #1327912 fixed by Joachim Lous
1855             $pagename = strval($pagename);
1856         }
1857         if (!$pagename) {
1858             trigger_error('empty pagename in WikiDB_PageIterator::next()', E_USER_WARNING);
1859             var_dump($next);
1860             return false;
1861         }
1862         // There's always hits, but we cache only if more 
1863         // (well not with file, cvs and dba)
1864         if (isset($next['pagedata']) and count($next['pagedata']) > 1) {
1865             $this->_wikidb->_cache->cache_data($next);
1866         // cache existing page id's since we iterate over all links in GleanDescription 
1867         // and need them later for LinkExistingWord
1868         } elseif ($this->_options and array_key_exists('include_empty', $this->_options)
1869                   and !$this->_options['include_empty'] and isset($next['id'])) {
1870             $this->_wikidb->_cache->_id_cache[$next['pagename']] = $next['id'];
1871         }
1872         $page = new WikiDB_Page($this->_wikidb, $pagename);
1873         if (isset($next['linkrelation']))
1874             $page->set('linkrelation', $next['linkrelation']);
1875         return $page;
1876     }
1877
1878     /**
1879      * Release resources held by this iterator.
1880      *
1881      * The iterator may not be used after free() is called.
1882      *
1883      * There is no need to call free(), if next() has returned false.
1884      * (I.e. if you iterate through all the pages in the sequence,
1885      * you do not need to call free() --- you only need to call it
1886      * if you stop before the end of the iterator is reached.)
1887      *
1888      * @access public
1889      */
1890     function free() {
1891         $this->_iter->free();
1892     }
1893     
1894     function asArray() {
1895         $result = array();
1896         while ($page = $this->next())
1897             $result[] = $page;
1898         //$this->reset();
1899         return $result;
1900     }
1901     
1902     /**
1903      * Apply filters for options like 'sortby', 'limit', 'exclude'
1904      * for simple queries like titleSearch, where the backend is not ready yet.
1905      * Since iteration is usually destructive for SQL results,
1906      * we have to generate a copy.
1907      */
1908     function applyFilters($options = false) {
1909         if (!$options) $options = $this->_options;
1910         if (isset($options['sortby'])) {
1911             $array = array();
1912             /* this is destructive */
1913             while ($page = $this->next())
1914                 $result[] = $page->getName();
1915             $this->_doSort($array, $options['sortby']);
1916         }
1917         /* the rest is not destructive.
1918          * reconstruct a new iterator 
1919          */
1920         $pagenames = array(); $i = 0;
1921         if (isset($options['limit']))
1922             $limit = $options['limit'];
1923         else 
1924             $limit = 0;
1925         if (isset($options['exclude']))
1926             $exclude = $options['exclude'];
1927         if (is_string($exclude) and !is_array($exclude))
1928             $exclude = PageList::explodePageList($exclude, false, false, $limit);
1929         foreach($array as $pagename) {
1930             if ($limit and $i++ > $limit)
1931                 return new WikiDB_Array_PageIterator($pagenames);
1932             if (!empty($exclude) and !in_array($pagename, $exclude))
1933                 $pagenames[] = $pagename;
1934             elseif (empty($exclude))
1935                 $pagenames[] = $pagename;
1936         }
1937         return new WikiDB_Array_PageIterator($pagenames);
1938     }
1939
1940     /* pagename only */
1941     function _doSort(&$array, $sortby) {
1942         $sortby = PageList::sortby($sortby, 'init');
1943         if ($sortby == '+pagename')
1944             sort($array, SORT_STRING);
1945         elseif ($sortby == '-pagename')
1946             rsort($array, SORT_STRING);
1947         reset($array);
1948     }
1949
1950 };
1951
1952 /**
1953  * A class which represents a sequence of WikiDB_PageRevisions.
1954  * TODO: Enhance to php5 iterators
1955  */
1956 class WikiDB_PageRevisionIterator
1957 {
1958     function WikiDB_PageRevisionIterator(&$wikidb, &$revisions, $options=false) {
1959         $this->_revisions = $revisions;
1960         $this->_wikidb = &$wikidb;
1961         $this->_options = $options;
1962     }
1963     
1964     function count () {
1965         return $this->_revisions->count();
1966     }
1967
1968     /**
1969      * Get next WikiDB_PageRevision in sequence.
1970      *
1971      * @access public
1972      *
1973      * @return WikiDB_PageRevision
1974      * The next WikiDB_PageRevision in the sequence.
1975      */
1976     function next () {
1977         if ( ! ($next = $this->_revisions->next()) )
1978             return false;
1979
1980         //$this->_wikidb->_cache->cache_data($next);
1981
1982         $pagename = $next['pagename'];
1983         $version = $next['version'];
1984         $versiondata = $next['versiondata'];
1985         if ((int)DEBUG) {
1986             if (!(is_string($pagename) and $pagename != '')) {
1987                 trigger_error("empty pagename",E_USER_WARNING);
1988                 return false;
1989             }
1990         } else assert(is_string($pagename) and $pagename != '');
1991         if ((int)DEBUG) {
1992             if (!is_array($versiondata)) {
1993                 trigger_error("empty versiondata",E_USER_WARNING);
1994                 return false;
1995             }
1996         } else assert(is_array($versiondata));
1997         if ((int)DEBUG) {
1998             if (!($version > 0)) {
1999                 trigger_error("invalid version",E_USER_WARNING);
2000                 return false;
2001             }
2002         } else assert($version > 0);
2003
2004         return new WikiDB_PageRevision($this->_wikidb, $pagename, $version,
2005                                        $versiondata);
2006     }
2007
2008     /**
2009      * Release resources held by this iterator.
2010      *
2011      * The iterator may not be used after free() is called.
2012      *
2013      * There is no need to call free(), if next() has returned false.
2014      * (I.e. if you iterate through all the revisions in the sequence,
2015      * you do not need to call free() --- you only need to call it
2016      * if you stop before the end of the iterator is reached.)
2017      *
2018      * @access public
2019      */
2020     function free() { 
2021         $this->_revisions->free();
2022     }
2023
2024     function asArray() {
2025         $result = array();
2026         while ($rev = $this->next())
2027             $result[] = $rev;
2028         $this->free();
2029         return $result;
2030     }
2031 };
2032
2033 /** pseudo iterator
2034  */
2035 class WikiDB_Array_PageIterator
2036 {
2037     function WikiDB_Array_PageIterator($pagenames) {
2038         global $request;
2039         $this->_dbi = $request->getDbh();
2040         $this->_pages = $pagenames;
2041         reset($this->_pages);
2042     }
2043     function next() {
2044         $c =& current($this->_pages);
2045         next($this->_pages);
2046         return $c !== false ? $this->_dbi->getPage($c) : false;
2047     }
2048     function count() {
2049         return count($this->_pages);
2050     }
2051     function free() {}
2052     function asArray() {
2053         reset($this->_pages);
2054         return $this->_pages;
2055     }
2056 }
2057
2058 class WikiDB_Array_generic_iter
2059 {
2060     function WikiDB_Array_generic_iter($result) {
2061         // $result may be either an array or a query result
2062         if (is_array($result)) {
2063             $this->_array = $result;
2064         } elseif (is_object($result)) {
2065             $this->_array = $result->asArray();
2066         } else {
2067             $this->_array = array();
2068         }
2069         if (!empty($this->_array))
2070             reset($this->_array);
2071     }
2072     function next() {
2073         $c =& current($this->_array);
2074         next($this->_array);
2075         return $c !== false ? $c : false;
2076     }
2077     function count() {
2078         return count($this->_array);
2079     }
2080     function free() {}
2081     function asArray() {
2082         if (!empty($this->_array))
2083             reset($this->_array);
2084         return $this->_array;
2085     }
2086 }
2087
2088 /**
2089  * Data cache used by WikiDB.
2090  *
2091  * FIXME: Maybe rename this to caching_backend (or some such).
2092  *
2093  * @access private
2094  */
2095 class WikiDB_cache 
2096 {
2097     // FIXME: beautify versiondata cache.  Cache only limited data?
2098
2099     function WikiDB_cache (&$backend) {
2100         $this->_backend = &$backend;
2101
2102         $this->_pagedata_cache = array();
2103         $this->_versiondata_cache = array();
2104         array_push ($this->_versiondata_cache, array());
2105         $this->_glv_cache = array();
2106         $this->_id_cache = array(); // formerly ->_dbi->_iwpcache (nonempty pages => id)
2107     }
2108     
2109     function close() {
2110         $this->_pagedata_cache = array();
2111         $this->_versiondata_cache = array();
2112         $this->_glv_cache = array();
2113         $this->_id_cache = array();
2114     }
2115
2116     function get_pagedata($pagename) {
2117         assert(is_string($pagename) && $pagename != '');
2118         if (USECACHE) {
2119             $cache = &$this->_pagedata_cache;
2120             if (!isset($cache[$pagename]) || !is_array($cache[$pagename])) {
2121                 $cache[$pagename] = $this->_backend->get_pagedata($pagename);
2122                 if (empty($cache[$pagename]))
2123                     $cache[$pagename] = array();
2124             }
2125             return $cache[$pagename];
2126         } else {
2127             return $this->_backend->get_pagedata($pagename);
2128         }
2129     }
2130     
2131     function update_pagedata($pagename, $newdata) {
2132         assert(is_string($pagename) && $pagename != '');
2133        
2134         $this->_backend->update_pagedata($pagename, $newdata);
2135
2136         if (USECACHE) {
2137             if (!empty($this->_pagedata_cache[$pagename]) 
2138                 and is_array($this->_pagedata_cache[$pagename])) 
2139             {
2140                 $cachedata = &$this->_pagedata_cache[$pagename];
2141                 foreach($newdata as $key => $val)
2142                     $cachedata[$key] = $val;
2143             } else 
2144                 $this->_pagedata_cache[$pagename] = $newdata;
2145         }
2146     }
2147
2148     function invalidate_cache($pagename) {
2149         unset ($this->_pagedata_cache[$pagename]);
2150         unset ($this->_versiondata_cache[$pagename]);
2151         unset ($this->_glv_cache[$pagename]);
2152         unset ($this->_id_cache[$pagename]);
2153         //unset ($this->_backend->_page_data);
2154     }
2155     
2156     function delete_page($pagename) {
2157         $result = $this->_backend->delete_page($pagename);
2158         $this->invalidate_cache($pagename);
2159         return $result;
2160     }
2161
2162     function purge_page($pagename) {
2163         $result = $this->_backend->purge_page($pagename);
2164         $this->invalidate_cache($pagename);
2165         return $result;
2166     }
2167
2168     // FIXME: ugly and wrong. may overwrite full cache with partial cache
2169     function cache_data($data) {
2170         ;
2171         //if (isset($data['pagedata']))
2172         //    $this->_pagedata_cache[$data['pagename']] = $data['pagedata'];
2173     }
2174     
2175     function get_versiondata($pagename, $version, $need_content = false) {
2176         //  FIXME: Seriously ugly hackage
2177         $readdata = false;
2178         if (USECACHE) {   //temporary - for debugging
2179             assert(is_string($pagename) && $pagename != '');
2180             // There is a bug here somewhere which results in an assertion failure at line 105
2181             // of ArchiveCleaner.php  It goes away if we use the next line.
2182             //$need_content = true;
2183             $nc = $need_content ? '1':'0';
2184             $cache = &$this->_versiondata_cache;
2185             if (!isset($cache[$pagename][$version][$nc]) 
2186                 || !(is_array ($cache[$pagename])) 
2187                 || !(is_array ($cache[$pagename][$version]))) 
2188             {
2189                 $cache[$pagename][$version][$nc] = 
2190                     $this->_backend->get_versiondata($pagename, $version, $need_content);
2191                 $readdata = true;
2192                 // If we have retrieved all data, we may as well set the cache for 
2193                 // $need_content = false
2194                 if ($need_content){
2195                     $cache[$pagename][$version]['0'] =& $cache[$pagename][$version]['1'];
2196                 }
2197             }
2198             $vdata = $cache[$pagename][$version][$nc];
2199         } else {
2200             $vdata = $this->_backend->get_versiondata($pagename, $version, $need_content);
2201             $readdata = true;
2202         }
2203         if ($readdata && $vdata && !empty($vdata['%pagedata'])) {
2204             $this->_pagedata_cache[$pagename] =& $vdata['%pagedata'];
2205         }
2206         return $vdata;
2207     }
2208
2209     function set_versiondata($pagename, $version, $data) {
2210         //unset($this->_versiondata_cache[$pagename][$version]);
2211         
2212         $new = $this->_backend->set_versiondata($pagename, $version, $data);
2213         // Update the cache
2214         $this->_versiondata_cache[$pagename][$version]['1'] = $data;
2215         $this->_versiondata_cache[$pagename][$version]['0'] = $data;
2216         // Is this necessary?
2217         unset($this->_glv_cache[$pagename]);
2218     }
2219
2220     function update_versiondata($pagename, $version, $data) {
2221         $new = $this->_backend->update_versiondata($pagename, $version, $data);
2222         // Update the cache
2223         $this->_versiondata_cache[$pagename][$version]['1'] = $data;
2224         // FIXME: hack
2225         $this->_versiondata_cache[$pagename][$version]['0'] = $data;
2226         // Is this necessary?
2227         unset($this->_glv_cache[$pagename]);
2228     }
2229
2230     function delete_versiondata($pagename, $version) {
2231         $new = $this->_backend->delete_versiondata($pagename, $version);
2232         if (isset($this->_versiondata_cache[$pagename][$version]))
2233             unset ($this->_versiondata_cache[$pagename][$version]);
2234         // dirty latest version cache only if latest version gets deleted
2235         if (isset($this->_glv_cache[$pagename]) and $this->_glv_cache[$pagename] == $version)
2236             unset ($this->_glv_cache[$pagename]);
2237     }
2238         
2239     function get_latest_version($pagename)  {
2240         if (USECACHE) {
2241             assert (is_string($pagename) && $pagename != '');
2242             $cache = &$this->_glv_cache;
2243             if (!isset($cache[$pagename])) {
2244                 $cache[$pagename] = $this->_backend->get_latest_version($pagename);
2245                 if (empty($cache[$pagename]))
2246                     $cache[$pagename] = 0;
2247             }
2248             return $cache[$pagename];
2249         } else {
2250             return $this->_backend->get_latest_version($pagename); 
2251         }
2252     }
2253 };
2254
2255 function _sql_debuglog($msg, $newline=true, $shutdown=false) {
2256     static $fp = false;
2257     static $i = 0;
2258     if (!$fp) {
2259         $stamp = strftime("%y%m%d-%H%M%S");
2260         $fp = fopen("/tmp/sql-$stamp.log", "a");
2261         register_shutdown_function("_sql_debuglog_shutdown_function");
2262     } elseif ($shutdown) {
2263         fclose($fp);
2264         return;
2265     }
2266     if ($newline) fputs($fp, "[$i++] $msg");
2267     else fwrite($fp, $msg);
2268 }
2269
2270 function _sql_debuglog_shutdown_function() {
2271     _sql_debuglog('',false,true);
2272 }
2273
2274 // $Log: not supported by cvs2svn $
2275 // Revision 1.143  2006/09/06 05:46:40  rurban
2276 // do db backend check on _DEBUG_SQL
2277 //
2278 // Revision 1.142  2006/06/10 11:55:58  rurban
2279 // print optimize only when DEBUG
2280 //
2281 // Revision 1.141  2006/04/17 17:28:21  rurban
2282 // honor getWikiPageLinks change linkto=>relation
2283 //
2284 // Revision 1.140  2006/03/19 14:23:51  rurban
2285 // sf.net patch #1377011 by Matt Brown: add DATABASE_OPTIMISE_FREQUENCY
2286 //
2287 // Revision 1.139  2006/01/12 16:38:07  rurban
2288 // add page method listRelations()
2289 // fix bug #1327912 numeric pagenames can break plugins (Joachim Lous)
2290 //
2291 // Revision 1.138  2005/11/14 22:27:07  rurban
2292 // add linkrelation support
2293 //   getPageLinks returns now an array of hashes
2294 // pass stoplist through iterator
2295 //
2296 // Revision 1.137  2005/10/12 06:16:18  rurban
2297 // better From header
2298 //
2299 // Revision 1.136  2005/10/03 16:14:57  rurban
2300 // improve description
2301 //
2302 // Revision 1.135  2005/09/11 14:19:44  rurban
2303 // enable LIMIT support for fulltext search
2304 //
2305 // Revision 1.134  2005/09/10 21:28:10  rurban
2306 // applyFilters hack to use filters after methods, which do not support them (titleSearch)
2307 //
2308 // Revision 1.133  2005/08/27 09:39:10  rurban
2309 // dumphtml when not at admin page: dump the current or given page
2310 //
2311 // Revision 1.132  2005/08/07 10:10:07  rurban
2312 // clean whole version cache
2313 //
2314 // Revision 1.131  2005/04/23 11:30:12  rurban
2315 // allow emtpy WikiDB::getRevisionBefore(), for simplier templates (revert)
2316 //
2317 // Revision 1.130  2005/04/06 06:19:30  rurban
2318 // Revert the previous wrong bugfix #1175761: USECACHE was mixed with WIKIDB_NOCACHE_MARKUP.
2319 // Fix WIKIDB_NOCACHE_MARKUP in main (always set it) and clarify it in WikiDB
2320 //
2321 // Revision 1.129  2005/04/06 05:50:29  rurban
2322 // honor !USECACHE for _cached_html, fixes #1175761
2323 //
2324 // Revision 1.128  2005/04/01 16:11:42  rurban
2325 // just whitespace
2326 //
2327 // Revision 1.127  2005/02/18 20:43:40  uckelman
2328 // WikiDB::genericWarnings() is no longer used.
2329 //
2330 // Revision 1.126  2005/02/04 17:58:06  rurban
2331 // minor versioncache improvement. part 2/3 of Charles Corrigan cache patch. not sure about the 0/1 issue
2332 //
2333 // Revision 1.125  2005/02/03 05:08:39  rurban
2334 // ref fix by Charles Corrigan
2335 //
2336 // Revision 1.124  2005/01/29 20:43:32  rurban
2337 // protect against empty request: on some occasion this happens
2338 //
2339 // Revision 1.123  2005/01/25 06:58:21  rurban
2340 // reformatting
2341 //
2342 // Revision 1.122  2005/01/20 10:18:17  rurban
2343 // reformatting
2344 //
2345 // Revision 1.121  2005/01/04 20:25:01  rurban
2346 // remove old [%pagedata][_cached_html] code
2347 //
2348 // Revision 1.120  2004/12/23 14:12:31  rurban
2349 // dont email on unittest
2350 //
2351 // Revision 1.119  2004/12/20 16:05:00  rurban
2352 // gettext msg unification
2353 //
2354 // Revision 1.118  2004/12/13 13:22:57  rurban
2355 // new BlogArchives plugin for the new blog theme. enable default box method
2356 // for all plugins. Minor search improvement.
2357 //
2358 // Revision 1.117  2004/12/13 08:15:09  rurban
2359 // false is wrong. null might be better but lets play safe.
2360 //
2361 // Revision 1.116  2004/12/10 22:15:00  rurban
2362 // fix $page->get('_cached_html)
2363 // refactor upgrade db helper _convert_cached_html() to be able to call them from WikiAdminUtils also.
2364 // support 2nd genericSqlQuery param (bind huge arg)
2365 //
2366 // Revision 1.115  2004/12/10 02:45:27  rurban
2367 // SQL optimization:
2368 //   put _cached_html from pagedata into a new seperate blob, not huge serialized string.
2369 //   it is only rarelely needed: for current page only, if-not-modified
2370 //   but was extracted for every simple page iteration.
2371 //
2372 // Revision 1.114  2004/12/09 22:24:44  rurban
2373 // optimize on _DEBUG_SQL only. but now again on every 50th request, not just save.
2374 //
2375 // Revision 1.113  2004/12/06 19:49:55  rurban
2376 // enable action=remove which is undoable and seeable in RecentChanges: ADODB ony for now.
2377 // renamed delete_page to purge_page.
2378 // enable action=edit&version=-1 to force creation of a new version.
2379 // added BABYCART_PATH config
2380 // fixed magiqc in adodb.inc.php
2381 // and some more docs
2382 //
2383 // Revision 1.112  2004/11/30 17:45:53  rurban
2384 // exists_links backend implementation
2385 //
2386 // Revision 1.111  2004/11/28 20:39:43  rurban
2387 // deactivate pagecache overwrite: it is wrong
2388 //
2389 // Revision 1.110  2004/11/26 18:39:01  rurban
2390 // new regex search parser and SQL backends (90% complete, glob and pcre backends missing)
2391 //
2392 // Revision 1.109  2004/11/25 17:20:50  rurban
2393 // and again a couple of more native db args: backlinks
2394 //
2395 // Revision 1.108  2004/11/23 13:35:31  rurban
2396 // add case_exact search
2397 //
2398 // Revision 1.107  2004/11/21 11:59:16  rurban
2399 // remove final \n to be ob_cache independent
2400 //
2401 // Revision 1.106  2004/11/20 17:35:56  rurban
2402 // improved WantedPages SQL backends
2403 // PageList::sortby new 3rd arg valid_fields (override db fields)
2404 // WantedPages sql pager inexact for performance reasons:
2405 //   assume 3 wantedfrom per page, to be correct, no getTotal()
2406 // support exclude argument for get_all_pages, new _sql_set()
2407 //
2408 // Revision 1.105  2004/11/20 09:16:27  rurban
2409 // Fix bad-style Cut&Paste programming errors, detected by Charles Corrigan.
2410 //
2411 // Revision 1.104  2004/11/19 19:22:03  rurban
2412 // ModeratePage part1: change status
2413 //
2414 // Revision 1.103  2004/11/16 17:29:04  rurban
2415 // fix remove notification error
2416 // fix creation + update id_cache update
2417 //
2418 // Revision 1.102  2004/11/11 18:31:26  rurban
2419 // add simple backtrace on such general failures to get at least an idea where
2420 //
2421 // Revision 1.101  2004/11/10 19:32:22  rurban
2422 // * optimize increaseHitCount, esp. for mysql.
2423 // * prepend dirs to the include_path (phpwiki_dir for faster searches)
2424 // * Pear_DB version logic (awful but needed)
2425 // * fix broken ADODB quote
2426 // * _extract_page_data simplification
2427 //
2428 // Revision 1.100  2004/11/10 15:29:20  rurban
2429 // * requires newer Pear_DB (as the internal one): quote() uses now escapeSimple for strings
2430 // * ACCESS_LOG_SQL: fix cause request not yet initialized
2431 // * WikiDB: moved SQL specific methods upwards
2432 // * new Pear_DB quoting: same as ADODB and as newer Pear_DB.
2433 //   fixes all around: WikiGroup, WikiUserNew SQL methods, SQL logging
2434 //
2435 // Revision 1.99  2004/11/09 17:11:05  rurban
2436 // * revert to the wikidb ref passing. there's no memory abuse there.
2437 // * use new wikidb->_cache->_id_cache[] instead of wikidb->_iwpcache, to effectively
2438 //   store page ids with getPageLinks (GleanDescription) of all existing pages, which
2439 //   are also needed at the rendering for linkExistingWikiWord().
2440 //   pass options to pageiterator.
2441 //   use this cache also for _get_pageid()
2442 //   This saves about 8 SELECT count per page (num all pagelinks).
2443 // * fix passing of all page fields to the pageiterator.
2444 // * fix overlarge session data which got broken with the latest ACCESS_LOG_SQL changes
2445 //
2446 // Revision 1.98  2004/11/07 18:34:29  rurban
2447 // more logging fixes
2448 //
2449 // Revision 1.97  2004/11/07 16:02:51  rurban
2450 // new sql access log (for spam prevention), and restructured access log class
2451 // dbh->quote (generic)
2452 // pear_db: mysql specific parts seperated (using replace)
2453 //
2454 // Revision 1.96  2004/11/05 22:32:15  rurban
2455 // encode the subject to be 7-bit safe
2456 //
2457 // Revision 1.95  2004/11/05 20:53:35  rurban
2458 // login cleanup: better debug msg on failing login,
2459 // checked password less immediate login (bogo or anon),
2460 // checked olduser pref session error,
2461 // better PersonalPage without password warning on minimal password length=0
2462 //   (which is default now)
2463 //
2464 // Revision 1.94  2004/11/01 10:43:56  rurban
2465 // seperate PassUser methods into seperate dir (memory usage)
2466 // fix WikiUser (old) overlarge data session
2467 // remove wikidb arg from various page class methods, use global ->_dbi instead
2468 // ...
2469 //
2470 // Revision 1.93  2004/10/14 17:17:57  rurban
2471 // remove dbi WikiDB_Page param: use global request object instead. (memory)
2472 // allow most_popular sortby arguments
2473 //
2474 // Revision 1.92  2004/10/05 17:00:04  rurban
2475 // support paging for simple lists
2476 // fix RatingDb sql backend.
2477 // remove pages from AllPages (this is ListPages then)
2478 //
2479 // Revision 1.91  2004/10/04 23:41:19  rurban
2480 // delete notify: fix, @unset syntax error
2481 //
2482 // Revision 1.90  2004/09/28 12:50:22  rurban
2483 // https://sourceforge.net/forum/forum.php?thread_id=1150924&forum_id=18929
2484 //
2485 // Revision 1.89  2004/09/26 10:54:42  rurban
2486 // silence deferred check
2487 //
2488 // Revision 1.88  2004/09/25 18:16:40  rurban
2489 // unset more unneeded _cached_html. (Guess this should fix sf.net now)
2490 //
2491 // Revision 1.87  2004/09/25 16:25:40  rurban
2492 // notify on rename and remove (to be improved)
2493 //
2494 // Revision 1.86  2004/09/23 18:52:06  rurban
2495 // only fortune at create
2496 //
2497 // Revision 1.85  2004/09/16 08:00:51  rurban
2498 // just some comments
2499 //
2500 // Revision 1.84  2004/09/14 10:34:30  rurban
2501 // fix TransformedText call to use refs
2502 //
2503 // Revision 1.83  2004/09/08 13:38:00  rurban
2504 // improve loadfile stability by using markup=2 as default for undefined markup-style.
2505 // use more refs for huge objects.
2506 // fix debug=static issue in WikiPluginCached
2507 //
2508 // Revision 1.82  2004/09/06 12:08:49  rurban
2509 // memory_limit on unix workaround
2510 // VisualWiki: default autosize image
2511 //
2512 // Revision 1.81  2004/09/06 08:28:00  rurban
2513 // rename genericQuery to genericSqlQuery
2514 //
2515 // Revision 1.80  2004/07/09 13:05:34  rurban
2516 // just aesthetics
2517 //
2518 // Revision 1.79  2004/07/09 10:06:49  rurban
2519 // Use backend specific sortby and sortable_columns method, to be able to
2520 // select between native (Db backend) and custom (PageList) sorting.
2521 // Fixed PageList::AddPageList (missed the first)
2522 // Added the author/creator.. name to AllPagesBy...
2523 //   display no pages if none matched.
2524 // Improved dba and file sortby().
2525 // Use &$request reference
2526 //
2527 // Revision 1.78  2004/07/08 21:32:35  rurban
2528 // Prevent from more warnings, minor db and sort optimizations
2529 //
2530 // Revision 1.77  2004/07/08 19:04:42  rurban
2531 // more unittest fixes (file backend, metadata RatingsDb)
2532 //
2533 // Revision 1.76  2004/07/08 17:31:43  rurban
2534 // improve numPages for file (fixing AllPagesTest)
2535 //
2536 // Revision 1.75  2004/07/05 13:56:22  rurban
2537 // sqlite autoincrement fix
2538 //
2539 // Revision 1.74  2004/07/03 16:51:05  rurban
2540 // optional DBADMIN_USER:DBADMIN_PASSWD for action=upgrade (if no ALTER permission)
2541 // added atomic mysql REPLACE for PearDB as in ADODB
2542 // fixed _lock_tables typo links => link
2543 // fixes unserialize ADODB bug in line 180
2544 //
2545 // Revision 1.73  2004/06/29 08:52:22  rurban
2546 // Use ...version() $need_content argument in WikiDB also:
2547 // To reduce the memory footprint for larger sets of pagelists,
2548 // we don't cache the content (only true or false) and
2549 // we purge the pagedata (_cached_html) also.
2550 // _cached_html is only cached for the current pagename.
2551 // => Vastly improved page existance check, ACL check, ...
2552 //
2553 // Now only PagedList info=content or size needs the whole content, esp. if sortable.
2554 //
2555 // Revision 1.72  2004/06/25 14:15:08  rurban
2556 // reduce memory footprint by caching only requested pagedate content (improving most page iterators)
2557 //
2558 // Revision 1.71  2004/06/21 16:22:30  rurban
2559 // add DEFAULT_DUMP_DIR and HTML_DUMP_DIR constants, for easier cmdline dumps,
2560 // fixed dumping buttons locally (images/buttons/),
2561 // support pages arg for dumphtml,
2562 // optional directory arg for dumpserial + dumphtml,
2563 // fix a AllPages warning,
2564 // show dump warnings/errors on DEBUG,
2565 // don't warn just ignore on wikilens pagelist columns, if not loaded.
2566 // RateIt pagelist column is called "rating", not "ratingwidget" (Dan?)
2567 //
2568 // Revision 1.70  2004/06/18 14:39:31  rurban
2569 // actually check USECACHE
2570 //
2571 // Revision 1.69  2004/06/13 15:33:20  rurban
2572 // new support for arguments owner, author, creator in most relevant
2573 // PageList plugins. in WikiAdmin* via preSelectS()
2574 //
2575 // Revision 1.68  2004/06/08 21:03:20  rurban
2576 // updated RssParser for XmlParser quirks (store parser object params in globals)
2577 //
2578 // Revision 1.67  2004/06/07 19:12:49  rurban
2579 // fixed rename version=0, bug #966284
2580 //
2581 // Revision 1.66  2004/06/07 18:57:27  rurban
2582 // fix rename: Change pagename in all linked pages
2583 //
2584 // Revision 1.65  2004/06/04 20:32:53  rurban
2585 // Several locale related improvements suggested by Pierrick Meignen
2586 // LDAP fix by John Cole
2587 // reanable admin check without ENABLE_PAGEPERM in the admin plugins
2588 //
2589 // Revision 1.64  2004/06/04 16:50:00  rurban
2590 // add random quotes to empty pages
2591 //
2592 // Revision 1.63  2004/06/04 11:58:38  rurban
2593 // added USE_TAGLINES
2594 //
2595 // Revision 1.62  2004/06/03 22:24:41  rurban
2596 // reenable admin check on !ENABLE_PAGEPERM, honor s=Wildcard arg, fix warning after Remove
2597 //
2598 // Revision 1.61  2004/06/02 17:13:48  rurban
2599 // fix getRevisionBefore assertion
2600 //
2601 // Revision 1.60  2004/05/28 10:09:58  rurban
2602 // fix bug #962117, incorrect init of auth_dsn
2603 //
2604 // Revision 1.59  2004/05/27 17:49:05  rurban
2605 // renamed DB_Session to DbSession (in CVS also)
2606 // added WikiDB->getParam and WikiDB->getAuthParam method to get rid of globals
2607 // remove leading slash in error message
2608 // added force_unlock parameter to File_Passwd (no return on stale locks)
2609 // fixed adodb session AffectedRows
2610 // added FileFinder helpers to unify local filenames and DATA_PATH names
2611 // editpage.php: new edit toolbar javascript on ENABLE_EDIT_TOOLBAR
2612 //
2613 // Revision 1.58  2004/05/18 13:59:14  rurban
2614 // rename simpleQuery to genericQuery
2615 //
2616 // Revision 1.57  2004/05/16 22:07:35  rurban
2617 // check more config-default and predefined constants
2618 // various PagePerm fixes:
2619 //   fix default PagePerms, esp. edit and view for Bogo and Password users
2620 //   implemented Creator and Owner
2621 //   BOGOUSERS renamed to BOGOUSER
2622 // fixed syntax errors in signin.tmpl
2623 //
2624 // Revision 1.56  2004/05/15 22:54:49  rurban
2625 // fixed important WikiDB bug with DEBUG > 0: wrong assertion
2626 // improved SetAcl (works) and PagePerms, some WikiGroup helpers.
2627 //
2628 // Revision 1.55  2004/05/12 19:27:47  rurban
2629 // revert wrong inline optimization.
2630 //
2631 // Revision 1.54  2004/05/12 10:49:55  rurban
2632 // require_once fix for those libs which are loaded before FileFinder and
2633 //   its automatic include_path fix, and where require_once doesn't grok
2634 //   dirname(__FILE__) != './lib'
2635 // upgrade fix with PearDB
2636 // navbar.tmpl: remove spaces for IE &nbsp; button alignment
2637 //
2638 // Revision 1.53  2004/05/08 14:06:12  rurban
2639 // new support for inlined image attributes: [image.jpg size=50x30 align=right]
2640 // minor stability and portability fixes
2641 //
2642 // Revision 1.52  2004/05/06 19:26:16  rurban
2643 // improve stability, trying to find the InlineParser endless loop on sf.net
2644 //
2645 // remove end-of-zip comments to fix sf.net bug #777278 and probably #859628
2646 //
2647 // Revision 1.51  2004/05/06 17:30:37  rurban
2648 // CategoryGroup: oops, dos2unix eol
2649 // improved phpwiki_version:
2650 //   pre -= .0001 (1.3.10pre: 1030.099)
2651 //   -p1 += .001 (1.3.9-p1: 1030.091)
2652 // improved InstallTable for mysql and generic SQL versions and all newer tables so far.
2653 // abstracted more ADODB/PearDB methods for action=upgrade stuff:
2654 //   backend->backendType(), backend->database(),
2655 //   backend->listOfFields(),
2656 //   backend->listOfTables(),
2657 //
2658 // Revision 1.50  2004/05/04 22:34:25  rurban
2659 // more pdf support
2660 //
2661 // Revision 1.49  2004/05/03 11:16:40  rurban
2662 // fixed sendPageChangeNotification
2663 // subject rewording
2664 //
2665 // Revision 1.48  2004/04/29 23:03:54  rurban
2666 // fixed sf.net bug #940996
2667 //
2668 // Revision 1.47  2004/04/29 19:39:44  rurban
2669 // special support for formatted plugins (one-liners)
2670 //   like <small><plugin BlaBla ></small>
2671 // iter->asArray() helper for PopularNearby
2672 // db_session for older php's (no &func() allowed)
2673 //
2674 // Revision 1.46  2004/04/26 20:44:34  rurban
2675 // locking table specific for better databases
2676 //
2677 // Revision 1.45  2004/04/20 00:06:03  rurban
2678 // themable paging support
2679 //
2680 // Revision 1.44  2004/04/19 18:27:45  rurban
2681 // Prevent from some PHP5 warnings (ref args, no :: object init)
2682 //   php5 runs now through, just one wrong XmlElement object init missing
2683 // Removed unneccesary UpgradeUser lines
2684 // Changed WikiLink to omit version if current (RecentChanges)
2685 //
2686 // Revision 1.43  2004/04/18 01:34:20  rurban
2687 // protect most_popular from sortby=mtime
2688 //
2689 // Revision 1.42  2004/04/18 01:11:51  rurban
2690 // more numeric pagename fixes.
2691 // fixed action=upload with merge conflict warnings.
2692 // charset changed from constant to global (dynamic utf-8 switching)
2693 //
2694
2695 // Local Variables:
2696 // mode: php
2697 // tab-width: 8
2698 // c-basic-offset: 4
2699 // c-hanging-comment-ender-p: nil
2700 // indent-tabs-mode: nil
2701 // End:   
2702 ?>