2 rcs_id('$Id: WikiDB.php,v 1.19 2003-02-16 19:43:10 dairiki Exp $');
4 //FIXME: arg on get*Revision to hint that content is wanted.
7 * The classes in the file define the interface to the
11 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
15 * Force the creation of a new revision.
16 * @see WikiDB_Page::createRevision()
18 define('WIKIDB_FORCE_CREATE', -1);
20 // FIXME: used for debugging only. Comment out if cache does not work
21 define('USECACHE', 1);
24 * Abstract base class for the database used by PhpWiki.
26 * A <tt>WikiDB</tt> is a container for <tt>WikiDB_Page</tt>s which in
27 * turn contain <tt>WikiDB_PageRevision</tt>s.
29 * Conceptually a <tt>WikiDB</tt> contains all possible
30 * <tt>WikiDB_Page</tt>s, whether they have been initialized or not.
31 * Since all possible pages are already contained in a WikiDB, a call
32 * to WikiDB::getPage() will never fail (barring bugs and
33 * e.g. filesystem or SQL database problems.)
35 * Also each <tt>WikiDB_Page</tt> always contains at least one
36 * <tt>WikiDB_PageRevision</tt>: the default content (e.g. "Describe
37 * [PageName] here."). This default content has a version number of
40 * <tt>WikiDB_PageRevision</tt>s have read-only semantics. One can
41 * only create new revisions or delete old ones --- one can not modify
42 * an existing revision.
46 * Open a WikiDB database.
48 * This is a static member function. This function inspects its
49 * arguments to determine the proper subclass of WikiDB to
50 * instantiate, and then it instantiates it.
54 * @param hash $dbparams Database configuration parameters.
55 * Some pertinent paramters are:
58 * <dd> The back-end type. Current supported types are:
61 * <dd> Generic SQL backend based on the PEAR/DB database abstraction
64 * <dd> Dba based backend.
68 * <dd> (Used by the SQL backend.)
69 * The DSN specifying which database to connect to.
72 * <dd> Prefix to be prepended to database table (and file names).
75 * <dd> (Used by the dba backend.)
76 * Which directory db files reside in.
79 * <dd> (Used by the dba backend.)
80 * Timeout in seconds for opening (and obtaining lock) on the
84 * <dd> (Used by the dba backend.)
86 * Which dba handler to use. Good choices are probably either
90 * @return WikiDB A WikiDB object.
92 function open ($dbparams) {
93 $dbtype = $dbparams{'dbtype'};
94 include_once("lib/WikiDB/$dbtype.php");
96 $class = 'WikiDB_' . $dbtype;
97 return new $class ($dbparams);
107 function WikiDB ($backend, $dbparams) {
108 $this->_backend = &$backend;
109 $this->_cache = new WikiDB_cache($backend);
111 // If the database doesn't yet have a timestamp, initialize it now.
112 if ($this->get('_timestamp') === false)
115 //FIXME: devel checking.
116 //$this->_backend->check();
120 * Get any user-level warnings about this WikiDB.
122 * Some back-ends, e.g. by default create there data files in the
123 * global /tmp directory. We would like to warn the user when this
124 * happens (since /tmp files tend to get wiped periodically.)
125 * Warnings such as these may be communicated from specific
126 * back-ends through this method.
130 * @return string A warning message (or <tt>false</tt> if there is
133 function genericWarnings() {
138 * Close database connection.
140 * The database may no longer be used after it is closed.
142 * Closing a WikiDB invalidates all <tt>WikiDB_Page</tt>s,
143 * <tt>WikiDB_PageRevision</tt>s and <tt>WikiDB_PageIterator</tt>s
144 * which have been obtained from it.
149 $this->_backend->close();
150 $this->_cache->close();
154 * Get a WikiDB_Page from a WikiDB.
156 * A {@link WikiDB} consists of the (infinite) set of all possible pages,
157 * therefore this method never fails.
160 * @param string $pagename Which page to get.
161 * @return WikiDB_Page The requested WikiDB_Page.
163 function getPage($pagename) {
164 assert(is_string($pagename) && $pagename);
165 return new WikiDB_Page($this, $pagename);
170 //function nPages() {
175 * Determine whether page exists (in non-default form).
178 * $is_page = $dbi->isWikiPage($pagename);
182 * $page = $dbi->getPage($pagename);
183 * $current = $page->getCurrentRevision();
184 * $is_page = ! $current->hasDefaultContents();
186 * however isWikiPage may be implemented in a more efficient
187 * manner in certain back-ends.
191 * @param string $pagename string Which page to check.
193 * @return boolean True if the page actually exists with
194 * non-default contents in the WikiDataBase.
196 function isWikiPage ($pagename) {
197 $page = $this->getPage($pagename);
198 $current = $page->getCurrentRevision();
199 return ! $current->hasDefaultContents();
203 * Delete page from the WikiDB.
205 * Deletes all revisions of the page from the WikiDB. Also resets
206 * all page meta-data to the default values.
210 * @param string $pagename Name of page to delete.
212 function deletePage($pagename) {
213 $this->_cache->delete_page($pagename);
214 $this->_backend->set_links($pagename, false);
218 * Retrieve all pages.
220 * Gets the set of all pages with non-default contents.
222 * FIXME: do we need this? I think so. The simple searches
227 * @param boolean $include_defaulted Normally pages whose most
228 * recent revision has empty content are considered to be
229 * non-existant. Unless $include_defaulted is set to true, those
230 * pages will not be returned.
232 * @return WikiDB_PageIterator A WikiDB_PageIterator which contains all pages
233 * in the WikiDB which have non-default contents.
235 function getAllPages($include_defaulted = false) {
236 $result = $this->_backend->get_all_pages($include_defaulted);
237 return new WikiDB_PageIterator($this, $result);
243 * Search for pages containing (or not containing) certain words
246 * Pages are returned in alphabetical order whenever it is
247 * practical to do so.
249 * FIXME: should titleSearch and fullSearch be combined? I think so.
252 * @param TextSearchQuery $search A TextSearchQuery object
253 * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching pages.
254 * @see TextSearchQuery
256 function titleSearch($search) {
257 $result = $this->_backend->text_search($search);
258 return new WikiDB_PageIterator($this, $result);
264 * Search for pages containing (or not containing) certain words
265 * in their entire text (this includes the page content and the
268 * Pages are returned in alphabetical order whenever it is
269 * practical to do so.
273 * @param TextSearchQuery $search A TextSearchQuery object.
274 * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching pages.
275 * @see TextSearchQuery
277 function fullSearch($search) {
278 $result = $this->_backend->text_search($search, 'full_text');
279 return new WikiDB_PageIterator($this, $result);
283 * Find the pages with the greatest hit counts.
285 * Pages are returned in reverse order by hit count.
289 * @param integer $limit The maximum number of pages to return.
290 * Set $limit to zero to return all pages. If $limit < 0, pages will
291 * be sorted in decreasing order of popularity.
293 * @return WikiDB_PageIterator A WikiDB_PageIterator containing the matching
296 function mostPopular($limit = 20) {
297 $result = $this->_backend->most_popular($limit);
298 return new WikiDB_PageIterator($this, $result);
302 * Find recent page revisions.
304 * Revisions are returned in reverse order by creation time.
308 * @param hash $params This hash is used to specify various optional
312 * <dd> (integer) At most this many revisions will be returned.
314 * <dd> (integer) Only revisions since this time (unix-timestamp) will be returned.
315 * <dt> include_minor_revisions
316 * <dd> (boolean) Also include minor revisions. (Default is not to.)
317 * <dt> exclude_major_revisions
318 * <dd> (boolean) Don't include non-minor revisions.
319 * (Exclude_major_revisions implies include_minor_revisions.)
320 * <dt> include_all_revisions
321 * <dd> (boolean) Return all matching revisions for each page.
322 * Normally only the most recent matching revision is returned
326 * @return WikiDB_PageRevisionIterator A WikiDB_PageRevisionIterator containing the
327 * matching revisions.
329 function mostRecent($params = false) {
330 $result = $this->_backend->most_recent($params);
331 return new WikiDB_PageRevisionIterator($this, $result);
335 * Blog search. (experimental)
337 * Search for blog entries related to a certain page.
339 * FIXME: with pagetype support and perhaps a RegexpSearchQuery
340 * we can make sure we are returning *ONLY* blog pages to the
341 * main routine. Currently, we just use titleSearch which requires
342 * some furher checking in lib/plugin/WikiBlog.php (BAD).
346 * @param string $order 'normal' (chronological) or 'reverse'
347 * @param string $page Find blog entries related to this page.
348 * @return WikiDB_PageIterator A WikiDB_PageIterator containing the relevant pages.
350 function blogSearch($page, $order) {
351 //FIXME: implement ordering
353 require_once('lib/TextSearchQuery.php');
354 $query = new TextSearchQuery ($page . SUBPAGE_SEPARATOR);
356 return $this->titleSearch($query);
359 /** Get timestamp when database was last modified.
361 * @return string A string consisting of two integers,
362 * separated by a space. The first is the time in
363 * unix timestamp format, the second is a modification
364 * count for the database.
366 * The idea is that you can cast the return value to an
367 * int to get a timestamp, or you can use the string value
368 * as a good hash for the entire database.
370 function getTimestamp() {
371 $ts = $this->get('_timestamp');
372 return sprintf("%d %d", $ts[0], $ts[1]);
376 * Update the database timestamp.
380 $ts = $this->get('_timestamp');
381 $this->set('_timestamp', array(time(), $ts[1] + 1));
386 * Access WikiDB global meta-data.
388 * NOTE: this is currently implemented in a hackish and
389 * not very efficient manner.
393 * @param string $key Which meta data to get.
394 * Some reserved meta-data keys are:
396 * <dt>'_timestamp' <dd> Data used by getTimestamp().
399 * @return scalar The requested value, or false if the requested data
403 if (!$key || $key[0] == '%')
406 * Hack Alert: We can use any page (existing or not) to store
407 * this data (as long as we always use the same one.)
409 $gd = $this->getPage('global_data');
410 $data = $gd->get('__global');
412 if ($data && isset($data[$key]))
419 * Set global meta-data.
421 * NOTE: this is currently implemented in a hackish and
422 * not very efficient manner.
427 * @param string $key Meta-data key to set.
428 * @param string $newval New value.
430 function set($key, $newval) {
431 if (!$key || $key[0] == '%')
434 $gd = $this->getPage('global_data');
436 $data = $gd->get('__global');
443 $data[$key] = $newval;
445 $gd->set('__global', $data);
451 * An abstract base class which representing a wiki-page within a
454 * A WikiDB_Page contains a number (at least one) of
455 * WikiDB_PageRevisions.
459 function WikiDB_Page(&$wikidb, $pagename) {
460 $this->_wikidb = &$wikidb;
461 $this->_pagename = $pagename;
462 assert(!empty($this->_pagename));
466 * Get the name of the wiki page.
470 * @return string The page name.
473 return $this->_pagename;
478 * Delete an old revision of a WikiDB_Page.
480 * Deletes the specified revision of the page.
481 * It is a fatal error to attempt to delete the current revision.
485 * @param integer $version Which revision to delete. (You can also
486 * use a WikiDB_PageRevision object here.)
488 function deleteRevision($version) {
489 $backend = &$this->_wikidb->_backend;
490 $cache = &$this->_wikidb->_cache;
491 $pagename = &$this->_pagename;
493 $version = $this->_coerce_to_version($version);
498 $latestversion = $cache->get_latest_version($pagename);
499 if ($latestversion && $version == $latestversion) {
501 trigger_error(sprintf("Attempt to delete most recent revision of '%s'",
502 $pagename), E_USER_ERROR);
506 $cache->delete_versiondata($pagename, $version);
512 * Delete a revision, or possibly merge it with a previous
516 * Suppose an author make a (major) edit to a page. Shortly
517 * after that the same author makes a minor edit (e.g. to fix
518 * spelling mistakes he just made.)
520 * Now some time later, where cleaning out old saved revisions,
521 * and would like to delete his minor revision (since there's
522 * really no point in keeping minor revisions around for a long
525 * Note that the text after the minor revision probably represents
526 * what the author intended to write better than the text after
527 * the preceding major edit.
529 * So what we really want to do is merge the minor edit with the
532 * We will only do this when:
534 * <li>The revision being deleted is a minor one, and
535 * <li>It has the same author as the immediately preceding revision.
538 function mergeRevision($version) {
539 $backend = &$this->_wikidb->_backend;
540 $cache = &$this->_wikidb->_cache;
541 $pagename = &$this->_pagename;
543 $version = $this->_coerce_to_version($version);
548 $latestversion = $backend->get_latest_version($pagename);
549 if ($latestversion && $version == $latestversion) {
551 trigger_error(sprintf("Attempt to merge most recent revision of '%s'",
552 $pagename), E_USER_ERROR);
556 $versiondata = $cache->get_versiondata($pagename, $version, true);
558 // Not there? ... we're done!
563 if ($versiondata['is_minor_edit']) {
564 $previous = $backend->get_previous_version($pagename, $version);
566 $prevdata = $cache->get_versiondata($pagename, $previous);
567 if ($prevdata['author_id'] == $versiondata['author_id']) {
568 // This is a minor revision, previous version is
569 // by the same author. We will merge the
571 $cache->update_versiondata($pagename, $previous,
572 array('%content' => $versiondata['%content'],
573 '_supplanted' => $versiondata['_supplanted']));
578 $cache->delete_versiondata($pagename, $version);
584 * Create a new revision of a {@link WikiDB_Page}.
588 * @param int $version Version number for new revision.
589 * To ensure proper serialization of edits, $version must be
590 * exactly one higher than the current latest version.
591 * (You can defeat this check by setting $version to
592 * {@link WIKIDB_FORCE_CREATE} --- not usually recommended.)
594 * @param string $content Contents of new revision.
596 * @param hash $metadata Metadata for new revision.
597 * All values in the hash should be scalars (strings or integers).
599 * @param array $links List of pagenames which this page links to.
601 * @return WikiDB_PageRevision Returns the new WikiDB_PageRevision object. If
602 * $version was incorrect, returns false
604 function createRevision($version, &$content, $metadata, $links) {
605 $backend = &$this->_wikidb->_backend;
606 $cache = &$this->_wikidb->_cache;
607 $pagename = &$this->_pagename;
611 $latestversion = $backend->get_latest_version($pagename);
612 $newversion = $latestversion + 1;
613 assert($newversion >= 1);
615 if ($version != WIKIDB_FORCE_CREATE && $version != $newversion) {
622 foreach ($data as $key => $val) {
623 if (empty($val) || $key[0] == '_' || $key[0] == '%')
627 assert(!empty($data['author_id']));
628 if (empty($data['author_id']))
629 @$data['author_id'] = $data['author'];
631 if (empty($data['mtime']))
632 $data['mtime'] = time();
634 if ($latestversion) {
635 // Ensure mtimes are monotonic.
636 $pdata = $cache->get_versiondata($pagename, $latestversion);
637 if ($data['mtime'] < $pdata['mtime']) {
638 trigger_error(sprintf(_("%s: Date of new revision is %s"),
639 $pagename,"'non-monotonic'"),
641 $data['orig_mtime'] = $data['mtime'];
642 $data['mtime'] = $pdata['mtime'];
645 // FIXME: use (possibly user specified) 'mtime' time or
647 $cache->update_versiondata($pagename, $latestversion,
648 array('_supplanted' => $data['mtime']));
651 $data['%content'] = &$content;
653 $cache->set_versiondata($pagename, $newversion, $data);
655 //$cache->update_pagedata($pagename, array(':latestversion' => $newversion,
656 //':deleted' => empty($content)));
658 $backend->set_links($pagename, $links);
662 // FIXME: probably should have some global state information
663 // in the backend to control when to optimize.
664 if (time() % 50 == 0) {
665 trigger_error(sprintf(_("Optimizing %s"),'backend'), E_USER_NOTICE);
666 $backend->optimize();
669 return new WikiDB_PageRevision($this->_wikidb, $pagename, $newversion,
674 * Get the most recent revision of a page.
678 * @return WikiDB_PageRevision The current WikiDB_PageRevision object.
680 function getCurrentRevision() {
681 $backend = &$this->_wikidb->_backend;
682 $cache = &$this->_wikidb->_cache;
683 $pagename = &$this->_pagename;
686 $version = $cache->get_latest_version($pagename);
687 $revision = $this->getRevision($version);
694 * Get a specific revision of a WikiDB_Page.
698 * @param integer $version Which revision to get.
700 * @return WikiDB_PageRevision The requested WikiDB_PageRevision object, or
701 * false if the requested revision does not exist in the {@link WikiDB}.
702 * Note that version zero of any page always exists.
704 function getRevision($version) {
705 $cache = &$this->_wikidb->_cache;
706 $pagename = &$this->_pagename;
709 return new WikiDB_PageRevision($this->_wikidb, $pagename, 0);
711 assert($version > 0);
712 $vdata = $cache->get_versiondata($pagename, $version);
715 return new WikiDB_PageRevision($this->_wikidb, $pagename, $version,
720 * Get previous page revision.
722 * This method find the most recent revision before a specified
727 * @param integer $version Find most recent revision before this version.
728 * You can also use a WikiDB_PageRevision object to specify the $version.
730 * @return WikiDB_PageRevision The requested WikiDB_PageRevision object, or false if the
731 * requested revision does not exist in the {@link WikiDB}. Note that
732 * unless $version is greater than zero, a revision (perhaps version zero,
733 * the default revision) will always be found.
735 function getRevisionBefore($version) {
736 $backend = &$this->_wikidb->_backend;
737 $pagename = &$this->_pagename;
739 $version = $this->_coerce_to_version($version);
744 $previous = $backend->get_previous_version($pagename, $version);
745 $revision = $this->getRevision($previous);
752 * Get all revisions of the WikiDB_Page.
754 * This does not include the version zero (default) revision in the
755 * returned revision set.
757 * @return WikiDB_PageRevisionIterator A
758 * WikiDB_PageRevisionIterator containing all revisions of this
759 * WikiDB_Page in reverse order by version number.
761 function getAllRevisions() {
762 $backend = &$this->_wikidb->_backend;
763 $revs = $backend->get_all_revisions($this->_pagename);
764 return new WikiDB_PageRevisionIterator($this->_wikidb, $revs);
768 * Find pages which link to or are linked from a page.
772 * @param boolean $reversed Which links to find: true for backlinks (default).
774 * @return WikiDB_PageIterator A WikiDB_PageIterator containing
775 * all matching pages.
777 function getLinks($reversed = true) {
778 $backend = &$this->_wikidb->_backend;
779 $result = $backend->get_links($this->_pagename, $reversed);
780 return new WikiDB_PageIterator($this->_wikidb, $result);
784 * Access WikiDB_Page meta-data.
788 * @param string $key Which meta data to get.
789 * Some reserved meta-data keys are:
791 * <dt>'locked'<dd> Is page locked?
792 * <dt>'hits' <dd> Page hit counter.
793 * <dt>'pref' <dd> Users preferences, stored in homepages.
794 * <dt>'owner' <dd> Default: first author_id. We might add a group with a dot here:
796 * <dt>'perm' <dd> Permission flag to authorize read/write/execution of
797 * page-headers and content.
798 * <dt>'score' <dd> Page score (not yet implement, do we need?)
801 * @return scalar The requested value, or false if the requested data
805 $cache = &$this->_wikidb->_cache;
806 if (!$key || $key[0] == '%')
808 $data = $cache->get_pagedata($this->_pagename);
809 return isset($data[$key]) ? $data[$key] : false;
813 * Get all the page meta-data as a hash.
815 * @return hash The page meta-data.
817 function getMetaData() {
818 $cache = &$this->_wikidb->_cache;
819 $data = $cache->get_pagedata($this->_pagename);
821 foreach ($data as $key => $val) {
822 if (!empty($val) && $key[0] != '%')
829 * Set page meta-data.
834 * @param string $key Meta-data key to set.
835 * @param string $newval New value.
837 function set($key, $newval) {
838 $cache = &$this->_wikidb->_cache;
839 $pagename = &$this->_pagename;
841 assert($key && $key[0] != '%');
843 $data = $cache->get_pagedata($pagename);
845 if (!empty($newval)) {
846 if (!empty($data[$key]) && $data[$key] == $newval)
847 return; // values identical, skip update.
850 if (empty($data[$key]))
851 return; // values identical, skip update.
854 $cache->update_pagedata($pagename, array($key => $newval));
858 * Increase page hit count.
860 * FIXME: IS this needed? Probably not.
862 * This is a convenience function.
863 * <pre> $page->increaseHitCount(); </pre>
864 * is functionally identical to
865 * <pre> $page->set('hits',$page->get('hits')+1); </pre>
867 * Note that this method may be implemented in more efficient ways
868 * in certain backends.
872 function increaseHitCount() {
873 @$newhits = $this->get('hits') + 1;
874 $this->set('hits', $newhits);
878 * Return a string representation of the WikiDB_Page
880 * This is really only for debugging.
884 * @return string Printable representation of the WikiDB_Page.
886 function asString () {
888 printf("[%s:%s\n", get_class($this), $this->getName());
889 print_r($this->getMetaData());
891 $strval = ob_get_contents();
899 * @param integer_or_object $version_or_pagerevision
900 * Takes either the version number (and int) or a WikiDB_PageRevision
902 * @return integer The version number.
904 function _coerce_to_version($version_or_pagerevision) {
905 if (method_exists($version_or_pagerevision, "getContent"))
906 $version = $version_or_pagerevision->getVersion();
908 $version = (int) $version_or_pagerevision;
910 assert($version >= 0);
914 function isUserPage ($include_empty = true) {
915 return $this->get('pref') ? true : false;
918 $current = $this->getCurrentRevision();
919 return ! $current->hasDefaultContents();
925 * This class represents a specific revision of a WikiDB_Page within
928 * A WikiDB_PageRevision has read-only semantics. You may only create
929 * new revisions (and delete old ones) --- you cannot modify existing
932 class WikiDB_PageRevision
934 function WikiDB_PageRevision(&$wikidb, $pagename, $version,
935 $versiondata = false)
937 $this->_wikidb = &$wikidb;
938 $this->_pagename = $pagename;
939 $this->_version = $version;
940 $this->_data = $versiondata ? $versiondata : array();
944 * Get the WikiDB_Page which this revision belongs to.
948 * @return WikiDB_Page The WikiDB_Page which this revision belongs to.
951 return new WikiDB_Page($this->_wikidb, $this->_pagename);
955 * Get the version number of this revision.
959 * @return integer The version number of this revision.
961 function getVersion() {
962 return $this->_version;
966 * Determine whether this revision has defaulted content.
968 * The default revision (version 0) of each page, as well as any
969 * pages which are created with empty content have their content
970 * defaulted to something like:
972 * Describe [ThisPage] here.
977 * @return boolean Returns true if the page has default content.
979 function hasDefaultContents() {
980 $data = &$this->_data;
981 return empty($data['%content']);
985 * Get the content as an array of lines.
989 * @return array An array of lines.
990 * The lines should contain no trailing white space.
992 function getContent() {
993 return explode("\n", $this->getPackedContent());
997 * Get the pagename of the revision.
1001 * @return string pagename.
1003 function getPageName() {
1004 return $this->_pagename;
1008 * Determine whether revision is the latest.
1012 * @return boolean True iff the revision is the latest (most recent) one.
1014 function isCurrent() {
1015 if (!isset($this->_iscurrent)) {
1016 $page = $this->getPage();
1017 $current = $page->getCurrentRevision();
1018 $this->_iscurrent = $this->getVersion() == $current->getVersion();
1020 return $this->_iscurrent;
1024 * Get the content as a string.
1028 * @return string The page content.
1029 * Lines are separated by new-lines.
1031 function getPackedContent() {
1032 $data = &$this->_data;
1035 if (empty($data['%content'])) {
1036 // Replace empty content with default value.
1037 return sprintf(_("Describe %s here."),
1038 "[". $this->_pagename ."]");
1041 // There is (non-default) content.
1042 assert($this->_version > 0);
1044 if (!is_string($data['%content'])) {
1045 // Content was not provided to us at init time.
1046 // (This is allowed because for some backends, fetching
1047 // the content may be expensive, and often is not wanted
1050 // In any case, now we need to get it.
1051 $data['%content'] = $this->_get_content();
1052 assert(is_string($data['%content']));
1055 return $data['%content'];
1058 function _get_content() {
1059 $cache = &$this->_wikidb->_cache;
1060 $pagename = $this->_pagename;
1061 $version = $this->_version;
1063 assert($version > 0);
1065 $newdata = $cache->get_versiondata($pagename, $version, true);
1067 assert(is_string($newdata['%content']));
1068 return $newdata['%content'];
1071 // else revision has been deleted... What to do?
1072 return __sprintf("Acck! Revision %s of %s seems to have been deleted!",
1073 $version, $pagename);
1078 * Get meta-data for this revision.
1083 * @param string $key Which meta-data to access.
1085 * Some reserved revision meta-data keys are:
1087 * <dt> 'mtime' <dd> Time this revision was created (seconds since midnight Jan 1, 1970.)
1088 * The 'mtime' meta-value is normally set automatically by the database
1089 * backend, but it may be specified explicitly when creating a new revision.
1091 * <dd> To ensure consistency of RecentChanges, the mtimes of the versions
1092 * of a page must be monotonically increasing. If an attempt is
1093 * made to create a new revision with an mtime less than that of
1094 * the preceeding revision, the new revisions timestamp is force
1095 * to be equal to that of the preceeding revision. In that case,
1096 * the originally requested mtime is preserved in 'orig_mtime'.
1097 * <dt> '_supplanted' <dd> Time this revision ceased to be the most recent.
1098 * This meta-value is <em>always</em> automatically maintained by the database
1099 * backend. (It is set from the 'mtime' meta-value of the superceding
1100 * revision.) '_supplanted' has a value of 'false' for the current revision.
1102 * FIXME: this could be refactored:
1104 * <dd> Author of the page (as he should be reported in, e.g. RecentChanges.)
1106 * <dd> Authenticated author of a page. This is used to identify
1107 * the distinctness of authors when cleaning old revisions from
1109 * <dt> 'is_minor_edit' <dd> Set if change was marked as a minor revision by the author.
1110 * <dt> 'summary' <dd> Short change summary entered by page author.
1113 * Meta-data keys must be valid C identifers (they have to start with a letter
1114 * or underscore, and can contain only alphanumerics and underscores.)
1116 * @return string The requested value, or false if the requested value
1119 function get($key) {
1120 if (!$key || $key[0] == '%')
1122 $data = &$this->_data;
1123 return isset($data[$key]) ? $data[$key] : false;
1127 * Get all the revision page meta-data as a hash.
1129 * @return hash The revision meta-data.
1131 function getMetaData() {
1133 foreach ($this->_data as $key => $val) {
1134 if (!empty($val) && $key[0] != '%')
1142 * Return a string representation of the revision.
1144 * This is really only for debugging.
1148 * @return string Printable representation of the WikiDB_Page.
1150 function asString () {
1152 printf("[%s:%d\n", get_class($this), $this->get('version'));
1153 print_r($this->_data);
1154 echo $this->getPackedContent() . "\n]\n";
1155 $strval = ob_get_contents();
1163 * A class which represents a sequence of WikiDB_Pages.
1165 class WikiDB_PageIterator
1167 function WikiDB_PageIterator(&$wikidb, &$pages) {
1168 $this->_pages = $pages;
1169 $this->_wikidb = &$wikidb;
1173 * Get next WikiDB_Page in sequence.
1177 * @return WikiDB_Page The next WikiDB_Page in the sequence.
1180 if ( ! ($next = $this->_pages->next()) )
1183 $pagename = &$next['pagename'];
1184 if (isset($next['pagedata']))
1185 $this->_wikidb->_cache->cache_data($next);
1187 return new WikiDB_Page($this->_wikidb, $pagename);
1191 * Release resources held by this iterator.
1193 * The iterator may not be used after free() is called.
1195 * There is no need to call free(), if next() has returned false.
1196 * (I.e. if you iterate through all the pages in the sequence,
1197 * you do not need to call free() --- you only need to call it
1198 * if you stop before the end of the iterator is reached.)
1203 $this->_pages->free();
1207 function setSortby ($arg = false) {
1209 $arg = @$_GET['sortby'];
1211 $sortby = substr($arg,1);
1212 $order = substr($arg,0,1)=='+' ? 'ASC' : 'DESC';
1215 if (is_array($arg)) { // array('mtime' => 'desc')
1222 // available column types to sort by:
1223 // todo: we must provide access methods for the generic dumb/iterator
1224 $this->_types = explode(',','pagename,mtime,hits,version,author,locked,minor,markup');
1225 if (in_array($sortby,$this->_types))
1226 $this->_options['sortby'] = $sortby;
1228 trigger_error(fmt("Argument %s '%s' ignored",'sortby',$sortby), E_USER_WARNING);
1229 if (in_array(strtoupper($order),'ASC','DESC'))
1230 $this->_options['order'] = strtoupper($order);
1232 trigger_error(fmt("Argument %s '%s' ignored",'order',$order), E_USER_WARNING);
1238 * A class which represents a sequence of WikiDB_PageRevisions.
1240 class WikiDB_PageRevisionIterator
1242 function WikiDB_PageRevisionIterator(&$wikidb, &$revisions) {
1243 $this->_revisions = $revisions;
1244 $this->_wikidb = &$wikidb;
1248 * Get next WikiDB_PageRevision in sequence.
1252 * @return WikiDB_PageRevision
1253 * The next WikiDB_PageRevision in the sequence.
1256 if ( ! ($next = $this->_revisions->next()) )
1259 $this->_wikidb->_cache->cache_data($next);
1261 $pagename = $next['pagename'];
1262 $version = $next['version'];
1263 $versiondata = $next['versiondata'];
1264 assert(!empty($pagename));
1265 assert(is_array($versiondata));
1266 assert($version > 0);
1268 return new WikiDB_PageRevision($this->_wikidb, $pagename, $version,
1273 * Release resources held by this iterator.
1275 * The iterator may not be used after free() is called.
1277 * There is no need to call free(), if next() has returned false.
1278 * (I.e. if you iterate through all the revisions in the sequence,
1279 * you do not need to call free() --- you only need to call it
1280 * if you stop before the end of the iterator is reached.)
1285 $this->_revisions->free();
1291 * Data cache used by WikiDB.
1293 * FIXME: Maybe rename this to caching_backend (or some such).
1299 // FIXME: beautify versiondata cache. Cache only limited data?
1301 function WikiDB_cache (&$backend) {
1302 $this->_backend = &$backend;
1304 $this->_pagedata_cache = array();
1305 $this->_versiondata_cache = array();
1306 array_push ($this->_versiondata_cache, array());
1307 $this->_glv_cache = array();
1311 $this->_pagedata_cache = false;
1312 $this->_versiondata_cache = false;
1313 $this->_glv_cache = false;
1316 function get_pagedata($pagename) {
1317 assert(is_string($pagename) && $pagename);
1318 $cache = &$this->_pagedata_cache;
1320 if (!isset($cache[$pagename]) || !is_array($cache[$pagename])) {
1321 $cache[$pagename] = $this->_backend->get_pagedata($pagename);
1322 if (empty($cache[$pagename]))
1323 $cache[$pagename] = array();
1326 return $cache[$pagename];
1329 function update_pagedata($pagename, $newdata) {
1330 assert(is_string($pagename) && $pagename);
1332 $this->_backend->update_pagedata($pagename, $newdata);
1334 if (is_array($this->_pagedata_cache[$pagename])) {
1335 $cachedata = &$this->_pagedata_cache[$pagename];
1336 foreach($newdata as $key => $val)
1337 $cachedata[$key] = $val;
1341 function invalidate_cache($pagename) {
1342 unset ($this->_pagedata_cache[$pagename]);
1343 unset ($this->_versiondata_cache[$pagename]);
1344 unset ($this->_glv_cache[$pagename]);
1347 function delete_page($pagename) {
1348 $this->_backend->delete_page($pagename);
1349 unset ($this->_pagedata_cache[$pagename]);
1350 unset ($this->_glv_cache[$pagename]);
1354 function cache_data($data) {
1355 if (isset($data['pagedata']))
1356 $this->_pagedata_cache[$data['pagename']] = $data['pagedata'];
1359 function get_versiondata($pagename, $version, $need_content = false) {
1360 // FIXME: Seriously ugly hackage
1361 if (defined ('USECACHE')){ //temporary - for debugging
1362 assert(is_string($pagename) && $pagename);
1363 // there is a bug here somewhere which results in an assertion failure at line 105
1364 // of ArchiveCleaner.php It goes away if we use the next line.
1365 $need_content = true;
1366 $nc = $need_content ? '1':'0';
1367 $cache = &$this->_versiondata_cache;
1368 if (!isset($cache[$pagename][$version][$nc])||
1369 !(is_array ($cache[$pagename])) || !(is_array ($cache[$pagename][$version]))) {
1370 $cache[$pagename][$version][$nc] =
1371 $this->_backend->get_versiondata($pagename,$version, $need_content);
1372 // If we have retrieved all data, we may as well set the cache for $need_content = false
1374 $cache[$pagename][$version]['0'] = $cache[$pagename][$version]['1'];
1377 $vdata = $cache[$pagename][$version][$nc];
1381 $vdata = $this->_backend->get_versiondata($pagename, $version, $need_content);
1384 if ($vdata && !empty($vdata['%pagedata']))
1385 $this->_pagedata_cache[$pagename] = $vdata['%pagedata'];
1389 function set_versiondata($pagename, $version, $data) {
1390 $new = $this->_backend->
1391 set_versiondata($pagename, $version, $data);
1393 $this->_versiondata_cache[$pagename][$version]['1'] = $data;
1395 $this->_versiondata_cache[$pagename][$version]['0'] = $data;
1396 // Is this necessary?
1397 unset($this->_glv_cache[$pagename]);
1401 function update_versiondata($pagename, $version, $data) {
1402 $new = $this->_backend->
1403 update_versiondata($pagename, $version, $data);
1405 $this->_versiondata_cache[$pagename][$version]['1'] = $data;
1407 $this->_versiondata_cache[$pagename][$version]['0'] = $data;
1408 // Is this necessary?
1409 unset($this->_glv_cache[$pagename]);
1413 function delete_versiondata($pagename, $version) {
1414 $new = $this->_backend->
1415 delete_versiondata($pagename, $version);
1416 unset ($this->_versiondata_cache[$pagename][$version]['1']);
1417 unset ($this->_versiondata_cache[$pagename][$version]['0']);
1418 unset ($this->_glv_cache[$pagename]);
1421 function get_latest_version($pagename) {
1422 if(defined('USECACHE')){
1423 assert (is_string($pagename) && $pagename);
1424 $cache = &$this->_glv_cache;
1425 if (!isset($cache[$pagename])) {
1426 $cache[$pagename] = $this->_backend->get_latest_version($pagename);
1427 if (empty($cache[$pagename]))
1428 $cache[$pagename] = 0;
1430 return $cache[$pagename];}
1432 return $this->_backend->get_latest_version($pagename);
1439 * FIXME! Class for externally authenticated users.
1441 * We might have read-only access to the password and/or group membership,
1442 * or we might even be able to update the entries.
1444 * FIXME: This was written before we stored prefs as %pagedata, so
1446 * FIXME: I believe this is not currently used.
1448 // class WikiDB_User
1453 // function WikiDB_User($userid, $authlevel = false) {
1455 // $this->_authdb = new WikiAuthDB($GLOBALS['DBAuthParams']);
1456 // $this->_authmethod = 'AuthDB';
1457 // WikiUser::WikiUser($request, $userid, $authlevel);
1461 // function getPreferences() {
1462 // // external prefs override internal ones?
1463 // if (! $this->_authdb->getPrefs() )
1464 // if ($pref = WikiUser::getPreferences())
1469 // function setPreferences($prefs) {
1470 // if (! $this->_authdb->setPrefs($prefs) )
1471 // return WikiUser::setPreferences();
1475 // function exists() {
1476 // return $this->_authdb->exists($this->_userid);
1479 // // create user and default user homepage
1480 // function createUser ($pref) {
1481 // if ($this->exists()) return;
1482 // if (! $this->_authdb->createUser($pref)) {
1483 // // external auth doesn't allow this.
1484 // // do our policies allow local users instead?
1485 // return WikiUser::createUser($pref);
1489 // function checkPassword($passwd) {
1490 // return $this->_authdb->pwcheck($this->userid, $passwd);
1493 // function changePassword($passwd) {
1494 // if (! $this->mayChangePassword() ) {
1495 // trigger_error(sprintf("Attempt to change an external password for '%s'",
1496 // $this->_userid), E_USER_ERROR);
1499 // return $this->_authdb->changePass($this->userid, $passwd);
1502 // function mayChangePassword() {
1503 // return $this->_authdb->auth_update;
1508 * FIXME: I believe this is not currently used.
1513 // var $auth_dsn = false, $auth_check = false;
1514 // var $auth_crypt_method = 'crypt', $auth_update = false;
1515 // var $group_members = false, $user_groups = false;
1516 // var $pref_update = false, $pref_select = false;
1519 // function WikiAuthDB($DBAuthParams) {
1520 // foreach ($DBAuthParams as $key => $value) {
1521 // $this->$key = $value;
1523 // if (!$this->auth_dsn) {
1524 // trigger_error(_("no \$DBAuthParams['dsn'] provided"), E_USER_ERROR);
1527 // // compare auth DB to the existing page DB. reuse if it's on the same database.
1528 // if (isa($this->_backend, 'WikiDB_backend_PearDB') and
1529 // $this->_backend->_dsn == $this->auth_dsn) {
1530 // $this->_dbh = &$this->_backend->_dbh;
1531 // return $this->_backend;
1533 // include_once("lib/WikiDB/SQL.php");
1534 // return new WikiDB_SQL($DBAuthParams);
1537 // function param_missing ($param) {
1538 // trigger_error(sprintf(_("No \$DBAuthParams['%s'] provided."), $param), E_USER_ERROR);
1542 // function getPrefs($prefs) {
1543 // if ($this->pref_select) {
1544 // $statement = $this->_backend->Prepare($this->pref_select);
1545 // return unserialize($this->_backend->Execute($statement,
1546 // $prefs->get('userid')));
1548 // param_missing('pref_select');
1553 // function setPrefs($prefs) {
1554 // if ($this->pref_write) {
1555 // $statement = $this->_backend->Prepare($this->pref_write);
1556 // return $this->_backend->Execute($statement,
1557 // $prefs->get('userid'), serialize($prefs->_prefs));
1559 // param_missing('pref_write');
1564 // function createUser ($pref) {
1565 // if ($this->user_create) {
1566 // $statement = $this->_backend->Prepare($this->user_create);
1567 // return $this->_backend->Execute($statement,
1568 // $prefs->get('userid'), serialize($prefs->_prefs));
1570 // param_missing('user_create');
1575 // function exists($userid) {
1576 // if ($this->user_check) {
1577 // $statement = $this->_backend->Prepare($this->user_check);
1578 // return $this->_backend->Execute($statement, $prefs->get('userid'));
1580 // param_missing('user_check');
1585 // function pwcheck($userid, $pass) {
1586 // if ($this->auth_check) {
1587 // $statement = $this->_backend->Prepare($this->auth_check);
1588 // return $this->_backend->Execute($statement, $userid, $pass);
1590 // param_missing('auth_check');
1599 // c-basic-offset: 4
1600 // c-hanging-comment-ender-p: nil
1601 // indent-tabs-mode: nil