2 rcs_id('$Id: WikiDB.php,v 1.13 2002-08-24 13:18:56 rurban Exp $');
4 //FIXME: arg on get*Revision to hint that content is wanted.
6 define('WIKIDB_FORCE_CREATE', -1);
8 // FIXME: used for debugging only. Comment out if cache does not work
12 * Abstract base class for the database used by PhpWiki.
14 * A <tt>WikiDB</tt> is a container for <tt>WikiDB_Page</tt>s which in
15 * turn contain <tt>WikiDB_PageRevision</tt>s.
17 * Conceptually a <tt>WikiDB</tt> contains all possible
18 * <tt>WikiDB_Page</tt>s, whether they have been initialized or not.
19 * Since all possible pages are already contained in a WikiDB, a call
20 * to WikiDB::getPage() will never fail (barring bugs and
21 * e.g. filesystem or SQL database problems.)
23 * Also each <tt>WikiDB_Page</tt> always contains at least one
24 * <tt>WikiDB_PageRevision</tt>: the default content (e.g. "Describe
25 * [PageName] here."). This default content has a version number of
28 * <tt>WikiDB_PageRevision</tt>s have read-only semantics. One can
29 * only create new revisions or delete old ones --- one can not modify
30 * an existing revision.
34 * Open a WikiDB database.
36 * This is a static member function. This function inspects its
37 * arguments to determine the proper subclass of WikiDB to
38 * instantiate, and then it instantiates it.
42 * @param $dbparams hash Database configuration parameters.
43 * Some pertinent paramters are:
46 * <dd> The back-end type. Current supported types are:
49 * <dd> Generic SQL backend based on the PEAR/DB database abstraction
52 * <dd> Dba based backend.
56 * <dd> (Used by the SQL backend.)
57 * The DSN specifying which database to connect to.
60 * <dd> Prefix to be prepended to database table (and file names).
63 * <dd> (Used by the dba backend.)
64 * Which directory db files reside in.
67 * <dd> (Used by the dba backend.)
68 * Timeout in seconds for opening (and obtaining lock) on the
72 * <dd> (Used by the dba backend.)
74 * Which dba handler to use. Good choices are probably either
78 * @return object A WikiDB object.
80 function open ($dbparams) {
81 $dbtype = $dbparams{'dbtype'};
82 include_once("lib/WikiDB/$dbtype.php");
84 $class = 'WikiDB_' . $dbtype;
85 return new $class ($dbparams);
93 function WikiDB ($backend, $dbparams) {
94 $this->_backend = &$backend;
95 $this->_cache = new WikiDB_cache($backend);
97 //FIXME: devel checking.
98 //$this->_backend->check();
102 * Get any user-level warnings about this WikiDB.
104 * Some back-ends, e.g. by default create there data files in the
105 * global /tmp directory. We would like to warn the user when this
106 * happens (since /tmp files tend to get wiped periodically.)
107 * Warnings such as these may be communicated from specific
108 * back-ends through this method.
112 * @return string A warning message (or <tt>false</tt> if there is
115 function genericWarnings() {
120 * Close database connection.
122 * The database may no longer be used after it is closed.
124 * Closing a WikiDB invalidates all <tt>WikiDB_Page</tt>s,
125 * <tt>WikiDB_PageRevision</tt>s and <tt>WikiDB_PageIterator</tt>s
126 * which have been obtained from it.
131 $this->_backend->close();
132 $this->_cache->close();
136 * Get a WikiDB_Page from a WikiDB.
138 * A WikiDB consists of the (infinite) set of all possible pages,
139 * therefore this method never fails.
142 * @param $pagename string Which page to get.
143 * @return object The requested WikiDB_Page.
145 function getPage($pagename) {
146 assert(is_string($pagename) && $pagename);
147 return new WikiDB_Page($this, $pagename);
152 //function nPages() {
157 * Determine whether page exists (in non-default form).
160 * $is_page = $dbi->isWikiPage($pagename);
164 * $page = $dbi->getPage($pagename);
165 * $current = $page->getCurrentRevision();
166 * $is_page = ! $current->hasDefaultContents();
168 * however isWikiPage may be implemented in a more efficient
169 * manner in certain back-ends.
173 * @param $pagename string Which page to check.
175 * @return boolean True if the page actually exists with
176 * non-default contents in the WikiDataBase.
178 function isWikiPage ($pagename) {
179 $page = $this->getPage($pagename);
180 $current = $page->getCurrentRevision();
181 return ! $current->hasDefaultContents();
185 * Delete page from the WikiDB.
187 * Deletes all revisions of the page from the WikiDB. Also resets
188 * all page meta-data to the default values.
192 * @param $pagename string Name of page to delete.
194 function deletePage($pagename) {
195 $this->_cache->delete_page($pagename);
196 $this->_backend->set_links($pagename, false);
200 * Retrieve all pages.
202 * Gets the set of all pages with non-default contents.
204 * FIXME: do we need this? I think so. The simple searches
209 * @param $include_defaulted boolean Normally pages whose most
210 * recent revision has empty content are considered to be
211 * non-existant. Unless $include_defaulted is set to true, those
212 * pages will not be returned.
214 * @return object A WikiDB_PageIterator which contains all pages
215 * in the WikiDB which have non-default contents.
217 function getAllPages($include_defaulted = false) {
218 $result = $this->_backend->get_all_pages($include_defaulted);
219 return new WikiDB_PageIterator($this, $result);
225 * Search for pages containing (or not containing) certain words
228 * Pages are returned in alphabetical order whenever it is
229 * practical to do so.
231 * FIXME: should titleSearch and fullSearch be combined? I think so.
234 * @param $search object A TextSearchQuery
235 * @return object A WikiDB_PageIterator containing the matching pages.
236 * @see TextSearchQuery
238 function titleSearch($search) {
239 $result = $this->_backend->text_search($search);
240 return new WikiDB_PageIterator($this, $result);
246 * Search for pages containing (or not containing) certain words
247 * in their entire text (this includes the page content and the
250 * Pages are returned in alphabetical order whenever it is
251 * practical to do so.
255 * @param $search object A TextSearchQuery object.
256 * @return object A WikiDB_PageIterator containing the matching pages.
257 * @see TextSearchQuery
259 function fullSearch($search) {
260 $result = $this->_backend->text_search($search, 'full_text');
261 return new WikiDB_PageIterator($this, $result);
265 * Find the pages with the greatest hit counts.
267 * Pages are returned in reverse order by hit count.
271 * @param $limit signed integer The maximum number of pages to return.
272 * Set $limit to zero to return all pages. If $limit < 0, pages will
273 * be sorted in decreasing order of popularity.
275 * @return object A WikiDB_PageIterator containing the matching
278 function mostPopular($limit = 20) {
279 $result = $this->_backend->most_popular($limit);
280 return new WikiDB_PageIterator($this, $result);
284 * Find recent page revisions.
286 * Revisions are returned in reverse order by creation time.
290 * @param $params hash This hash is used to specify various optional
294 * <dd> (integer) At most this many revisions will be returned.
296 * <dd> (integer) Only revisions since this time (unix-timestamp) will be returned.
297 * <dt> include_minor_revisions
298 * <dd> (boolean) Also include minor revisions. (Default is not to.)
299 * <dt> exclude_major_revisions
300 * <dd> (boolean) Don't include non-minor revisions.
301 * (Exclude_major_revisions implies include_minor_revisions.)
302 * <dt> include_all_revisions
303 * <dd> (boolean) Return all matching revisions for each page.
304 * Normally only the most recent matching revision is returned
308 * @return object A WikiDB_PageRevisionIterator containing the
309 * matching revisions.
311 function mostRecent($params = false) {
312 $result = $this->_backend->most_recent($params);
313 return new WikiDB_PageRevisionIterator($this, $result);
320 * An abstract base class which representing a wiki-page within a
323 * A WikiDB_Page contains a number (at least one) of
324 * WikiDB_PageRevisions.
328 function WikiDB_Page(&$wikidb, $pagename) {
329 $this->_wikidb = &$wikidb;
330 $this->_pagename = $pagename;
331 assert(!empty($this->_pagename));
335 * Get the name of the wiki page.
339 * @return string The page name.
342 return $this->_pagename;
347 * Delete an old revision of a WikiDB_Page.
349 * Deletes the specified revision of the page.
350 * It is a fatal error to attempt to delete the current revision.
354 * @param $version integer Which revision to delete. (You can also
355 * use a WikiDB_PageRevision object here.)
357 function deleteRevision($version) {
358 $backend = &$this->_wikidb->_backend;
359 $cache = &$this->_wikidb->_cache;
360 $pagename = &$this->_pagename;
362 $version = $this->_coerce_to_version($version);
367 $latestversion = $cache->get_latest_version($pagename);
368 if ($latestversion && $version == $latestversion) {
370 trigger_error(sprintf("Attempt to delete most recent revision of '%s'",
371 $pagename), E_USER_ERROR);
375 $cache->delete_versiondata($pagename, $version);
381 * Delete a revision, or possibly merge it with a previous
385 * Suppose an author make a (major) edit to a page. Shortly
386 * after that the same author makes a minor edit (e.g. to fix
387 * spelling mistakes he just made.)
389 * Now some time later, where cleaning out old saved revisions,
390 * and would like to delete his minor revision (since there's
391 * really no point in keeping minor revisions around for a long
394 * Note that the text after the minor revision probably represents
395 * what the author intended to write better than the text after
396 * the preceding major edit.
398 * So what we really want to do is merge the minor edit with the
401 * We will only do this when:
403 * <li>The revision being deleted is a minor one, and
404 * <li>It has the same author as the immediately preceding revision.
407 function mergeRevision($version) {
408 $backend = &$this->_wikidb->_backend;
409 $cache = &$this->_wikidb->_cache;
410 $pagename = &$this->_pagename;
412 $version = $this->_coerce_to_version($version);
417 $latestversion = $backend->get_latest_version($pagename);
418 if ($latestversion && $version == $latestversion) {
420 trigger_error(sprintf("Attempt to merge most recent revision of '%s'",
421 $pagename), E_USER_ERROR);
425 $versiondata = $cache->get_versiondata($pagename, $version, true);
427 // Not there? ... we're done!
432 if ($versiondata['is_minor_edit']) {
433 $previous = $backend->get_previous_version($pagename, $version);
435 $prevdata = $cache->get_versiondata($pagename, $previous);
436 if ($prevdata['author_id'] == $versiondata['author_id']) {
437 // This is a minor revision, previous version is
438 // by the same author. We will merge the
440 $cache->update_versiondata($pagename, $previous,
441 array('%content' => $versiondata['%content'],
442 '_supplanted' => $versiondata['_supplanted']));
447 $cache->delete_versiondata($pagename, $version);
453 * Create a new revision of a WikiDB_Page.
457 * @param $content string Contents of new revision.
459 * @param $metadata hash Metadata for new revision.
460 * All values in the hash should be scalars (strings or integers).
463 * @param $version int Version number for new revision.
464 * To ensure proper serialization of edits, $version must be
465 * exactly one higher than the current latest version.
466 * (You can defeat this check by setting $version to
467 * WIKIDB_FORCE_CREATE --- not usually recommended.)
469 * @param $links array List of pagenames which this page links to.
471 * @return object Returns the new WikiDB_PageRevision object. If
472 * $version was incorrect, returns false
474 function createRevision($version, &$content, $metadata, $links) {
475 $backend = &$this->_wikidb->_backend;
476 $cache = &$this->_wikidb->_cache;
477 $pagename = &$this->_pagename;
481 $latestversion = $backend->get_latest_version($pagename);
482 $newversion = $latestversion + 1;
483 assert($newversion >= 1);
485 if ($version != WIKIDB_FORCE_CREATE && $version != $newversion) {
492 foreach ($data as $key => $val) {
493 if (empty($val) || $key[0] == '_' || $key[0] == '%')
497 assert(!empty($data['author_id']));
498 if (empty($data['author_id']))
499 @$data['author_id'] = $data['author'];
501 if (empty($data['mtime']))
502 $data['mtime'] = time();
504 if ($latestversion) {
505 // Ensure mtimes are monotonic.
506 $pdata = $cache->get_versiondata($pagename, $latestversion);
507 if ($data['mtime'] < $pdata['mtime']) {
508 trigger_error(sprintf(_("%s: Date of new revision is %s"),
509 $pagename,"'non-monotonic'"),
511 $data['orig_mtime'] = $data['mtime'];
512 $data['mtime'] = $pdata['mtime'];
515 // FIXME: use (possibly user specified) 'mtime' time or
517 $cache->update_versiondata($pagename, $latestversion,
518 array('_supplanted' => $data['mtime']));
521 $data['%content'] = &$content;
523 $cache->set_versiondata($pagename, $newversion, $data);
525 //$cache->update_pagedata($pagename, array(':latestversion' => $newversion,
526 //':deleted' => empty($content)));
528 $backend->set_links($pagename, $links);
532 // FIXME: probably should have some global state information
533 // in the backend to control when to optimize.
534 if (time() % 50 == 0) {
535 trigger_error(sprintf(_("Optimizing %s"),'backend'), E_USER_NOTICE);
536 $backend->optimize();
539 return new WikiDB_PageRevision($this->_wikidb, $pagename, $newversion,
544 * Get the most recent revision of a page.
548 * @return object The current WikiDB_PageRevision object.
550 function getCurrentRevision() {
551 $backend = &$this->_wikidb->_backend;
552 $cache = &$this->_wikidb->_cache;
553 $pagename = &$this->_pagename;
556 $version = $cache->get_latest_version($pagename);
557 $revision = $this->getRevision($version);
564 * Get a specific revision of a WikiDB_Page.
568 * @param $version integer Which revision to get.
570 * @return object The requested WikiDB_PageRevision object, or
571 * false if the requested revision does not exist in the WikiDB.
572 * Note that version zero of any page always exists.
574 function getRevision($version) {
575 $cache = &$this->_wikidb->_cache;
576 $pagename = &$this->_pagename;
579 return new WikiDB_PageRevision($this->_wikidb, $pagename, 0);
581 assert($version > 0);
582 $vdata = $cache->get_versiondata($pagename, $version);
585 return new WikiDB_PageRevision($this->_wikidb, $pagename, $version,
590 * Get previous page revision.
592 * This method find the most recent revision before a specified
597 * @param $version integer Find most recent revision before this version.
598 * You can also use a WikiDB_PageRevision object to specify the $version.
600 * @return object The requested WikiDB_PageRevision object, or false if the
601 * requested revision does not exist in the WikiDB. Note that
602 * unless $version is greater than zero, a revision (perhaps version zero,
603 * the default revision) will always be found.
605 function getRevisionBefore($version) {
606 $backend = &$this->_wikidb->_backend;
607 $pagename = &$this->_pagename;
609 $version = $this->_coerce_to_version($version);
614 $previous = $backend->get_previous_version($pagename, $version);
615 $revision = $this->getRevision($previous);
622 * Get all revisions of the WikiDB_Page.
624 * This does not include the version zero (default) revision in the
625 * returned revision set.
627 * @return object a WikiDB_PageRevisionIterator containing all
628 * revisions of this WikiDB_Page in reverse order by version
631 function getAllRevisions() {
632 $backend = &$this->_wikidb->_backend;
633 $revs = $backend->get_all_revisions($this->_pagename);
634 return new WikiDB_PageRevisionIterator($this->_wikidb, $revs);
638 * Find pages which link to or are linked from a page.
642 * @param $reversed enum Which links to find: true for backlinks (default).
644 * @return object A WikiDB_PageIterator containing all matching pages.
646 function getLinks($reversed = true) {
647 $backend = &$this->_wikidb->_backend;
648 $result = $backend->get_links($this->_pagename, $reversed);
649 return new WikiDB_PageIterator($this->_wikidb, $result);
653 * Access WikiDB_Page meta-data.
657 * @param $key string Which meta data to get.
658 * Some reserved meta-data keys are:
660 * <dt>'locked'<dd> Is page locked?
661 * <dt>'hits' <dd> Page hit counter.
662 * <dt>'pref' <dd> Users preferences, stored in homepages.
663 * <dt>'owner' <dd> Default: first author_id. We might add a group with a dot here:
665 * <dt>'perm' <dd> Permission flag to authorize read/write/execution of
666 * page-headers and content.
667 * <dt>'score' <dd> Page score (not yet implement, do we need?)
670 * @return scalar The requested value, or false if the requested data
674 $cache = &$this->_wikidb->_cache;
675 if (!$key || $key[0] == '%')
677 $data = $cache->get_pagedata($this->_pagename);
678 return isset($data[$key]) ? $data[$key] : false;
682 * Get all the page meta-data as a hash.
684 * @return hash The page meta-data.
686 function getMetaData() {
687 $cache = &$this->_wikidb->_cache;
688 $data = $cache->get_pagedata($this->_pagename);
690 foreach ($data as $key => $val) {
691 if (!empty($val) && $key[0] != '%')
698 * Set page meta-data.
703 * @param $key string Meta-data key to set.
704 * @param $newval string New value.
706 function set($key, $newval) {
707 $cache = &$this->_wikidb->_cache;
708 $pagename = &$this->_pagename;
710 assert($key && $key[0] != '%');
712 $data = $cache->get_pagedata($pagename);
714 if (!empty($newval)) {
715 if (!empty($data[$key]) && $data[$key] == $newval)
716 return; // values identical, skip update.
719 if (empty($data[$key]))
720 return; // values identical, skip update.
723 $cache->update_pagedata($pagename, array($key => $newval));
727 * Increase page hit count.
729 * FIXME: IS this needed? Probably not.
731 * This is a convenience function.
732 * <pre> $page->increaseHitCount(); </pre>
733 * is functionally identical to
734 * <pre> $page->set('hits',$page->get('hits')+1); </pre>
736 * Note that this method may be implemented in more efficient ways
737 * in certain backends.
741 function increaseHitCount() {
742 @$newhits = $this->get('hits') + 1;
743 $this->set('hits', $newhits);
747 * Return a string representation of the WikiDB_Page
749 * This is really only for debugging.
753 * @return string Printable representation of the WikiDB_Page.
755 function asString () {
757 printf("[%s:%s\n", get_class($this), $this->getName());
758 print_r($this->getMetaData());
760 $strval = ob_get_contents();
768 * @param $version_or_pagerevision int or object
769 * Takes either the version number (and int) or a WikiDB_PageRevision
771 * @return int The version number.
773 function _coerce_to_version($version_or_pagerevision) {
774 if (method_exists($version_or_pagerevision, "getContent"))
775 $version = $version_or_pagerevision->getVersion();
777 $version = (int) $version_or_pagerevision;
779 assert($version >= 0);
783 function isUserPage ($include_empty = true) {
784 return $this->get('pref') ? true : false;
787 $current = $this->getCurrentRevision();
788 return ! $current->hasDefaultContents();
794 * This class represents a specific revision of a WikiDB_Page within
797 * A WikiDB_PageRevision has read-only semantics. You may only create
798 * new revisions (and delete old ones) --- you cannot modify existing
801 class WikiDB_PageRevision
803 function WikiDB_PageRevision(&$wikidb, $pagename, $version,
804 $versiondata = false)
806 $this->_wikidb = &$wikidb;
807 $this->_pagename = $pagename;
808 $this->_version = $version;
809 $this->_data = $versiondata ? $versiondata : array();
813 * Get the WikiDB_Page which this revision belongs to.
817 * @return object The WikiDB_Page which this revision belongs to.
820 return new WikiDB_Page($this->_wikidb, $this->_pagename);
824 * Get the version number of this revision.
828 * @return int The version number of this revision.
830 function getVersion() {
831 return $this->_version;
835 * Determine whether this revision has defaulted content.
837 * The default revision (version 0) of each page, as well as any
838 * pages which are created with empty content have their content
839 * defaulted to something like:
841 * Describe [ThisPage] here.
846 * @return boolean Returns true if the page has default content.
848 function hasDefaultContents() {
849 $data = &$this->_data;
850 return empty($data['%content']);
854 * Get the content as an array of lines.
858 * @return array An array of lines.
859 * The lines should contain no trailing white space.
861 function getContent() {
862 return explode("\n", $this->getPackedContent());
866 * Determine whether revision is the latest.
870 * @return bool True iff the revision is the latest (most recent) one.
872 function isCurrent() {
873 if (!isset($this->_iscurrent)) {
874 $page = $this->getPage();
875 $current = $page->getCurrentRevision();
876 $this->_iscurrent = $this->getVersion() == $current->getVersion();
878 return $this->_iscurrent;
882 * Get the content as a string.
886 * @return string The page content.
887 * Lines are separated by new-lines.
889 function getPackedContent() {
890 $data = &$this->_data;
893 if (empty($data['%content'])) {
894 // Replace empty content with default value.
895 return sprintf(_("Describe %s here."),
896 "[". $this->_pagename ."]");
899 // There is (non-default) content.
900 assert($this->_version > 0);
902 if (!is_string($data['%content'])) {
903 // Content was not provided to us at init time.
904 // (This is allowed because for some backends, fetching
905 // the content may be expensive, and often is not wanted
908 // In any case, now we need to get it.
909 $data['%content'] = $this->_get_content();
910 assert(is_string($data['%content']));
913 return $data['%content'];
916 function _get_content() {
917 $cache = &$this->_wikidb->_cache;
918 $pagename = $this->_pagename;
919 $version = $this->_version;
921 assert($version > 0);
923 $newdata = $cache->get_versiondata($pagename, $version, true);
925 assert(is_string($newdata['%content']));
926 return $newdata['%content'];
929 // else revision has been deleted... What to do?
930 return __sprintf("Acck! Revision %s of %s seems to have been deleted!",
931 $version, $pagename);
936 * Get meta-data for this revision.
941 * @param $key string Which meta-data to access.
943 * Some reserved revision meta-data keys are:
945 * <dt> 'mtime' <dd> Time this revision was created (seconds since midnight Jan 1, 1970.)
946 * The 'mtime' meta-value is normally set automatically by the database
947 * backend, but it may be specified explicitly when creating a new revision.
949 * <dd> To ensure consistency of RecentChanges, the mtimes of the versions
950 * of a page must be monotonically increasing. If an attempt is
951 * made to create a new revision with an mtime less than that of
952 * the preceeding revision, the new revisions timestamp is force
953 * to be equal to that of the preceeding revision. In that case,
954 * the originally requested mtime is preserved in 'orig_mtime'.
955 * <dt> '_supplanted' <dd> Time this revision ceased to be the most recent.
956 * This meta-value is <em>always</em> automatically maintained by the database
957 * backend. (It is set from the 'mtime' meta-value of the superceding
958 * revision.) '_supplanted' has a value of 'false' for the current revision.
960 * FIXME: this could be refactored:
962 * <dd> Author of the page (as he should be reported in, e.g. RecentChanges.)
964 * <dd> Authenticated author of a page. This is used to identify
965 * the distinctness of authors when cleaning old revisions from
967 * <dt> 'is_minor_edit' <dd> Set if change was marked as a minor revision by the author.
968 * <dt> 'summary' <dd> Short change summary entered by page author.
971 * Meta-data keys must be valid C identifers (they have to start with a letter
972 * or underscore, and can contain only alphanumerics and underscores.)
974 * @return string The requested value, or false if the requested value
978 if (!$key || $key[0] == '%')
980 $data = &$this->_data;
981 return isset($data[$key]) ? $data[$key] : false;
985 * Get all the revision page meta-data as a hash.
987 * @return hash The revision meta-data.
989 function getMetaData() {
991 foreach ($this->_data as $key => $val) {
992 if (!empty($val) && $key[0] != '%')
1000 * Return a string representation of the revision.
1002 * This is really only for debugging.
1006 * @return string Printable representation of the WikiDB_Page.
1008 function asString () {
1010 printf("[%s:%d\n", get_class($this), $this->get('version'));
1011 print_r($this->_data);
1012 echo $this->getPackedContent() . "\n]\n";
1013 $strval = ob_get_contents();
1021 * A class which represents a sequence of WikiDB_Pages.
1023 class WikiDB_PageIterator
1025 function WikiDB_PageIterator(&$wikidb, &$pages) {
1026 $this->_pages = $pages;
1027 $this->_wikidb = &$wikidb;
1031 * Get next WikiDB_Page in sequence.
1035 * @return object The next WikiDB_Page in the sequence.
1038 if ( ! ($next = $this->_pages->next()) )
1041 $pagename = &$next['pagename'];
1042 if (isset($next['pagedata']))
1043 $this->_wikidb->_cache->cache_data($next);
1045 return new WikiDB_Page($this->_wikidb, $pagename);
1049 * Release resources held by this iterator.
1051 * The iterator may not be used after free() is called.
1053 * There is no need to call free(), if next() has returned false.
1054 * (I.e. if you iterate through all the pages in the sequence,
1055 * you do not need to call free() --- you only need to call it
1056 * if you stop before the end of the iterator is reached.)
1061 $this->_pages->free();
1066 * A class which represents a sequence of WikiDB_PageRevisions.
1068 class WikiDB_PageRevisionIterator
1070 function WikiDB_PageRevisionIterator(&$wikidb, &$revisions) {
1071 $this->_revisions = $revisions;
1072 $this->_wikidb = &$wikidb;
1076 * Get next WikiDB_PageRevision in sequence.
1080 * @return object The next WikiDB_PageRevision in the sequence.
1083 if ( ! ($next = $this->_revisions->next()) )
1086 $this->_wikidb->_cache->cache_data($next);
1088 $pagename = $next['pagename'];
1089 $version = $next['version'];
1090 $versiondata = $next['versiondata'];
1091 assert(!empty($pagename));
1092 assert(is_array($versiondata));
1093 assert($version > 0);
1095 return new WikiDB_PageRevision($this->_wikidb, $pagename, $version,
1100 * Release resources held by this iterator.
1102 * The iterator may not be used after free() is called.
1104 * There is no need to call free(), if next() has returned false.
1105 * (I.e. if you iterate through all the revisions in the sequence,
1106 * you do not need to call free() --- you only need to call it
1107 * if you stop before the end of the iterator is reached.)
1112 $this->_revisions->free();
1118 * Data cache used by WikiDB.
1120 * FIXME: Maybe rename this to caching_backend (or some such).
1126 // FIXME: beautify versiondata cache. Cache only limited data?
1128 function WikiDB_cache (&$backend) {
1129 $this->_backend = &$backend;
1131 $this->_pagedata_cache = array();
1132 $this->_versiondata_cache = array();
1133 array_push ($this->_versiondata_cache, array());
1134 $this->_glv_cache = array();
1138 $this->_pagedata_cache = false;
1139 $this->_versiondata_cache = false;
1140 $this->_glv_cache = false;
1143 function get_pagedata($pagename) {
1144 assert(is_string($pagename) && $pagename);
1145 $cache = &$this->_pagedata_cache;
1147 if (!isset($cache[$pagename]) || !is_array($cache[$pagename])) {
1148 $cache[$pagename] = $this->_backend->get_pagedata($pagename);
1149 if (empty($cache[$pagename]))
1150 $cache[$pagename] = array();
1153 return $cache[$pagename];
1156 function update_pagedata($pagename, $newdata) {
1157 assert(is_string($pagename) && $pagename);
1159 $this->_backend->update_pagedata($pagename, $newdata);
1161 if (is_array($this->_pagedata_cache[$pagename])) {
1162 $cachedata = &$this->_pagedata_cache[$pagename];
1163 foreach($newdata as $key => $val)
1164 $cachedata[$key] = $val;
1168 function invalidate_cache($pagename) {
1169 unset ($this->_pagedata_cache[$pagename]);
1170 unset ($this->_versiondata_cache[$pagename]);
1171 unset ($this->_glv_cache[$pagename]);
1174 function delete_page($pagename) {
1175 $this->_backend->delete_page($pagename);
1176 unset ($this->_pagedata_cache[$pagename]);
1177 unset ($this->_glv_cache[$pagename]);
1181 function cache_data($data) {
1182 if (isset($data['pagedata']))
1183 $this->_pagedata_cache[$data['pagename']] = $data['pagedata'];
1186 function get_versiondata($pagename, $version, $need_content = false) {
1187 // FIXME: Seriously ugly hackage
1188 if (defined ('USECACHE')){ //temporary - for debugging
1189 assert(is_string($pagename) && $pagename);
1190 // there is a bug here somewhere which results in an assertion failure at line 105
1191 // of ArchiveCleaner.php It goes away if we use the next line.
1192 $need_content = true;
1193 $nc = $need_content ? '1':'0';
1194 $cache = &$this->_versiondata_cache;
1195 if (!isset($cache[$pagename][$version][$nc])||
1196 !(is_array ($cache[$pagename])) || !(is_array ($cache[$pagename][$version]))) {
1197 $cache[$pagename][$version][$nc] =
1198 $this->_backend->get_versiondata($pagename,$version, $need_content);
1199 // If we have retrieved all data, we may as well set the cache for $need_content = false
1201 $cache[$pagename][$version]['0'] = $cache[$pagename][$version]['1'];
1204 $vdata = $cache[$pagename][$version][$nc];
1208 $vdata = $this->_backend->get_versiondata($pagename, $version, $need_content);
1211 if ($vdata && !empty($vdata['%pagedata']))
1212 $this->_pagedata_cache[$pagename] = $vdata['%pagedata'];
1216 function set_versiondata($pagename, $version, $data) {
1217 $new = $this->_backend->
1218 set_versiondata($pagename, $version, $data);
1220 $this->_versiondata_cache[$pagename][$version]['1'] = $data;
1222 $this->_versiondata_cache[$pagename][$version]['0'] = $data;
1223 // Is this necessary?
1224 unset($this->_glv_cache[$pagename]);
1228 function update_versiondata($pagename, $version, $data) {
1229 $new = $this->_backend->
1230 update_versiondata($pagename, $version, $data);
1232 $this->_versiondata_cache[$pagename][$version]['1'] = $data;
1234 $this->_versiondata_cache[$pagename][$version]['0'] = $data;
1235 // Is this necessary?
1236 unset($this->_glv_cache[$pagename]);
1240 function delete_versiondata($pagename, $version) {
1241 $new = $this->_backend->
1242 delete_versiondata($pagename, $version);
1243 unset ($this->_versiondata_cache[$pagename][$version]['1']);
1244 unset ($this->_versiondata_cache[$pagename][$version]['0']);
1245 unset ($this->_glv_cache[$pagename]);
1248 function get_latest_version($pagename) {
1249 if(defined('USECACHE')){
1250 assert (is_string($pagename) && $pagename);
1251 $cache = &$this->_glv_cache;
1252 if (!isset($cache[$pagename])) {
1253 $cache[$pagename] = $this->_backend->get_latest_version($pagename);
1254 if (empty($cache[$pagename]))
1255 $cache[$pagename] = 0;
1257 return $cache[$pagename];}
1259 return $this->_backend->get_latest_version($pagename);
1266 * FIXME! Class for externally authenticated users.
1268 * We might have read-only access to the password and/or group membership,
1269 * or we might even be able to update the entries.
1271 * Group: list of userid's in a special WikiDB_Page
1272 * WikiDB_Page: owner.group permission_flag in page metadata
1274 * FIXME: This was written before we stored prefs as %pagedata, so
1281 function WikiDB_User($userid, $authlevel = false) {
1282 $this->_authdb = new WikiAuthDB($GLOBALS['DBAuthParams']);
1283 $this->_authmethod = 'AuthDB';
1284 WikiUser::WikiUser($userid, $authlevel);
1287 function getPreferences() {
1288 // external prefs override internal ones?
1289 if (! $this->_authdb->getPrefs() )
1290 if ($pref = WikiUser::getPreferences())
1295 function setPreferences($prefs) {
1296 if (! $this->_authdb->setPrefs($prefs) )
1297 return WikiUser::setPreferences();
1301 return $this->_authdb->exists($this->_userid);
1304 // create user and default user homepage
1305 function createUser ($pref) {
1306 if ($this->exists()) return;
1307 if (! $this->_authdb->createUser($pref)) {
1308 // external auth doesn't allow this.
1309 // do our policies allow local users instead?
1310 return WikiUser::createUser($pref);
1314 function checkPassword($passwd) {
1315 return $this->_authdb->pwcheck($passwd);
1318 function changePassword($passwd) {
1319 if (! $this->mayChangePassword() ) {
1320 trigger_error(sprintf("Attempt to change an external password for '%s'",
1321 $this->_userid), E_USER_ERROR);
1324 return $this->_authdb->changePass($passwd);
1327 function mayChangePassword() {
1328 return $this->_authdb->auth_update;
1335 var $auth_dsn = false, $auth_check = false;
1336 var $auth_crypt_method = 'crypt', $auth_update = false;
1337 var $group_members = false, $user_groups = false;
1338 var $pref_update = false, $pref_select = false;
1340 function WikiAuthDB($DBAuthParams) {
1341 foreach ($DBAuthParams as $key => $value) {
1342 $this->$key = $value;
1346 function getPrefs($prefs) {
1347 if ($this->pref_select) {
1348 trigger_error(sprintf("WikiAuthDB->getPrefs() not yet written for '%s'",
1349 $this->_userid), E_USER_WARNING);
1351 trigger_error(sprintf("WikiAuthDB->getPrefs() not defined for '%s'",
1352 $this->_userid), E_USER_ERROR);
1356 function setPrefs($prefs) {
1357 if ($this->pref_write) {
1358 trigger_error(sprintf("WikiAuthDB->setPrefs() not allowed for '%s'",
1359 $this->_userid), E_USER_ERROR);
1361 trigger_error(sprintf("WikiAuthDB->setPrefs() not yet written for '%s'",
1362 $this->_userid), E_USER_WARNING);
1366 function createUser ($pref) {
1367 trigger_error(sprintf("WikiAuthDB->createUser() not yet written for '%s'",
1368 $this->_userid), E_USER_WARNING);
1372 trigger_error(sprintf("WikiAuthDB->exists() not yet written for '%s'",
1373 $this->_userid), E_USER_WARNING);
1376 function pwcheck($pass) {
1377 trigger_error(sprintf("WikiAuthDB->pwcheck() not yet written for '%s'",
1378 $this->_userid), E_USER_WARNING);
1386 // c-basic-offset: 4
1387 // c-hanging-comment-ender-p: nil
1388 // indent-tabs-mode: nil