2 rcs_id('$Id: backend.php,v 1.28 2006-09-06 05:47:58 rurban Exp $');
9 //:deleted (*) (Set if latest content is empty.)
17 %content (?should this be here?)
18 _supplanted : Time version ceased to be the current version
20 mtime (*) : Time of version edit.
23 author : nominal author
24 author_id : authenticated author
33 (types are scalars: strings, ints, bools)
37 * A WikiDB_backend handles the storage and retrieval of data for a WikiDB.
39 * A WikiDB_backend handles the storage and retrieval of data for a WikiDB.
40 * It does not have to be this way, of course, but the standard WikiDB uses
41 * a WikiDB_backend. (Other WikiDB's could be written which use some other
42 * method to access their underlying data store.)
44 * The interface outlined here seems to work well with both RDBM based
45 * and flat DBM/hash based methods of data storage.
47 * Though it contains some default implementation of certain methods,
48 * this is an abstract base class. It is expected that most effificient
49 * backends will override nearly all the methods in this class.
57 * Get page meta-data from database.
59 * @param $pagename string Page name.
61 * Returns a hash containing the page meta-data.
62 * Returns an empty array if there is no meta-data for the requested page.
63 * Keys which might be present in the hash are:
65 * <dt> locked <dd> If the page is locked.
66 * <dt> hits <dd> The page hit count.
67 * <dt> created <dd> Unix time of page creation. (FIXME: Deprecated: I
68 * don't think we need this...)
71 function get_pagedata($pagename) {
72 trigger_error("virtual", E_USER_ERROR);
76 * Update the page meta-data.
80 * Only meta-data whose keys are preset in $newdata is affected.
84 * $backend->update_pagedata($pagename, array('locked' => 1));
86 * will set the value of 'locked' to 1 for the specified page, but it
87 * will not affect the value of 'hits' (or whatever other meta-data
88 * may have been stored for the page.)
90 * To delete a particular piece of meta-data, set it's value to false.
92 * $backend->update_pagedata($pagename, array('locked' => false));
95 * @param $pagename string Page name.
96 * @param $newdata hash New meta-data.
98 function update_pagedata($pagename, $newdata) {
99 trigger_error("virtual", E_USER_ERROR);
104 * Get the current version number for a page.
106 * @param $pagename string Page name.
107 * @return int The latest version number for the page. Returns zero if
108 * no versions of a page exist.
110 function get_latest_version($pagename) {
111 trigger_error("virtual", E_USER_ERROR);
115 * Get preceding version number.
117 * @param $pagename string Page name.
118 * @param $version int Find version before this one.
119 * @return int The version number of the version in the database which
120 * immediately preceeds $version.
122 function get_previous_version($pagename, $version) {
123 trigger_error("virtual", E_USER_ERROR);
127 * Get revision meta-data and content.
129 * @param $pagename string Page name.
130 * @param $version integer Which version to get.
131 * @param $want_content boolean
132 * Indicates the caller really wants the page content. If this
133 * flag is not set, the backend is free to skip fetching of the
134 * page content (as that may be expensive). If the backend omits
135 * the content, the backend might still want to set the value of
136 * '%content' to the empty string if it knows there's no content.
138 * @return hash The version data, or false if specified version does not
141 * Some keys which might be present in the $versiondata hash are:
144 * <dd> This is a pseudo-meta-data element (since it's actually
145 * the page data, get it?) containing the page content.
146 * If the content was not fetched, this key may not be present.
148 * For description of other version meta-data see WikiDB_PageRevision::get().
149 * @see WikiDB_PageRevision::get
151 function get_versiondata($pagename, $version, $want_content = false) {
152 trigger_error("virtual", E_USER_ERROR);
156 * Delete page from the database with backup possibility.
157 * This should remove all links (from the named page) from
160 * @param $pagename string Page name.
161 * i.e save_page('') and DELETE nonempty id
162 * Can be undone and is seen in RecentChanges.
164 function delete_page($pagename) {
166 $user =& $GLOBALS['request']->_user;
167 $vdata = array('author' => $user->getId(),
168 'author_id' => $user->getAuthenticatedId(),
171 $this->lock(); // critical section:
172 $version = $this->get_latest_version($pagename);
173 $this->set_versiondata($pagename, $version+1, $vdata);
174 $this->set_links($pagename, false); // links are purged.
175 // SQL needs to invalidate the non_empty id
176 if (! WIKIDB_NOCACHE_MARKUP) {
177 // need the hits, perms and LOCKED, otherwise you can reset the perm
178 // by action=remove and re-create it with default perms
179 $pagedata = $this->get_pagedata($pagename);
180 unset($pagedata['_cached_html']);
181 $this->update_pagedata($pagename, $pagedata);
187 * Delete page (and all it's revisions) from the database.
190 function purge_page($pagename) {
191 trigger_error("virtual", E_USER_ERROR);
195 * Delete an old revision of a page.
197 * Note that one is never allowed to delete the most recent version,
198 * but that this requirement is enforced by WikiDB not by the backend.
200 * In fact, to be safe, backends should probably allow the deletion of
201 * the most recent version.
203 * @param $pagename string Page name.
204 * @param $version integer Version to delete.
206 function delete_versiondata($pagename, $version) {
207 trigger_error("virtual", E_USER_ERROR);
211 * Create a new page revision.
213 * If the given ($pagename,$version) is already in the database,
214 * this method completely overwrites any stored data for that version.
216 * @param $pagename string Page name.
217 * @param $version int New revisions content.
218 * @param $data hash New revision metadata.
220 * @see get_versiondata
222 function set_versiondata($pagename, $version, $data) {
223 trigger_error("virtual", E_USER_ERROR);
227 * Update page version meta-data.
229 * If the given ($pagename,$version) is already in the database,
230 * this method only changes those meta-data values whose keys are
231 * explicity listed in $newdata.
233 * @param $pagename string Page name.
234 * @param $version int New revisions content.
235 * @param $newdata hash New revision metadata.
236 * @see set_versiondata, get_versiondata
238 function update_versiondata($pagename, $version, $newdata) {
239 $data = $this->get_versiondata($pagename, $version, true);
244 foreach ($newdata as $key => $val) {
250 $this->set_versiondata($pagename, $version, $data);
254 * Set links for page.
256 * @param $pagename string Page name.
258 * @param $links array List of page(names) which page links to.
260 function set_links($pagename, $links) {
261 trigger_error("virtual", E_USER_ERROR);
265 * Find pages which link to or are linked from a page.
267 * @param $pagename string Page name.
268 * @param $reversed boolean True to get backlinks.
270 * FIXME: array or iterator?
271 * @return object A WikiDB_backend_iterator.
273 function get_links($pagename, $reversed, $include_empty=false,
274 $sortby=false, $limit=false, $exclude=false) {
275 //FIXME: implement simple (but slow) link finder.
276 die("FIXME get_links");
280 * Get all revisions of a page.
282 * @param $pagename string The page name.
283 * @return object A WikiDB_backend_iterator.
285 function get_all_revisions($pagename) {
286 include_once('lib/WikiDB/backend/dumb/AllRevisionsIter.php');
287 return new WikiDB_backend_dumb_AllRevisionsIter($this, $pagename);
291 * Get all pages in the database.
293 * Pages should be returned in alphabetical order if that is
298 * @param $include_defaulted boolean
299 * If set, even pages with no content will be returned
300 * --- but still only if they have at least one revision (not
301 * counting the default revision 0) entered in the database.
303 * Normally pages whose current revision has empty content
304 * are not returned as these pages are considered to be
307 * @return object A WikiDB_backend_iterator.
309 function get_all_pages($include_defaulted, $orderby=false, $limit=false, $exclude=false) {
310 trigger_error("virtual", E_USER_ERROR);
314 * Title or full text search.
316 * Pages should be returned in alphabetical order if that is
321 * @param $search object A TextSearchQuery object describing the parsed query string,
322 * with efficient methods for SQL and PCRE match.
324 * @param $fullsearch boolean If true, a full text search is performed,
325 * otherwise a title search is performed.
327 * @return object A WikiDB_backend_iterator.
329 * @see WikiDB::titleSearch
331 function text_search($search, $fulltext=false, $sortby=false, $limit=false, $exclude=false) {
332 // This is method implements a simple linear search
333 // through all the pages in the database.
335 // It is expected that most backends will overload
336 // this method with something more efficient.
337 include_once('lib/WikiDB/backend/dumb/TextSearchIter.php');
339 $pages = $this->get_all_pages(false, $sortby, false, $exclude);
340 return new WikiDB_backend_dumb_TextSearchIter($this, $pages, $search, $fulltext,
341 array('limit' => $limit,
342 'exclude' => $exclude));
346 * Find pages with highest hit counts.
348 * Find the pages with the highest hit counts. The pages should
349 * be returned in reverse order by hit count.
352 * @param $limit integer No more than this many pages
353 * @return object A WikiDB_backend_iterator.
355 function most_popular($limit, $sortby='-hits') {
356 // This is method fetches all pages, then
357 // sorts them by hit count.
358 // (Not very efficient.)
360 // It is expected that most backends will overload
361 // method with something more efficient.
362 include_once('lib/WikiDB/backend/dumb/MostPopularIter.php');
363 $pages = $this->get_all_pages(false, $sortby, false);
364 return new WikiDB_backend_dumb_MostPopularIter($this, $pages, $limit);
368 * Find recent changes.
371 * @param $params hash See WikiDB::mostRecent for a description
372 * of parameters which can be included in this hash.
373 * @return object A WikiDB_backend_iterator.
374 * @see WikiDB::mostRecent
376 function most_recent($params) {
377 // This method is very inefficient and searches through
378 // all pages for the most recent changes.
380 // It is expected that most backends will overload
381 // method with something more efficient.
382 include_once('lib/WikiDB/backend/dumb/MostRecentIter.php');
383 $pages = $this->get_all_pages(true, '-mtime');
384 return new WikiDB_backend_dumb_MostRecentIter($this, $pages, $params);
387 function wanted_pages($exclude_from='', $exclude='', $sortby=false, $limit=false) {
388 include_once('lib/WikiDB/backend/dumb/WantedPagesIter.php');
389 $allpages = $this->get_all_pages(true,false,false,$exclude_from);
390 return new WikiDB_backend_dumb_WantedPagesIter($this, $allpages, $exclude, $sortby, $limit);
394 * Lock backend database.
396 * Calls may be nested.
398 * @param $write_lock boolean Unless this is set to false, a write lock
399 * is acquired, otherwise a read lock. If the backend doesn't support
400 * read locking, then it should make a write lock no matter which type
401 * of lock was requested.
403 * All backends <em>should</em> support write locking.
405 function lock($write_lock = true) {
409 * Unlock backend database.
411 * @param $force boolean Normally, the database is not unlocked until
412 * unlock() is called as many times as lock() has been. If $force is
413 * set to true, the the database is unconditionally unlocked.
415 function unlock($force = false) {
426 * Synchronize with filesystem.
428 * This should flush all unwritten data to the filesystem.
434 * Optimize the database.
436 function optimize() {
440 * Check database integrity.
442 * This should check the validity of the internal structure of the database.
443 * Errors should be reported via:
445 * trigger_error("Message goes here.", E_USER_WARNING);
448 * @return boolean True iff database is in a consistent state.
454 * Put the database into a consistent state.
456 * This should put the database into a consistent state.
457 * (I.e. rebuild indexes, etc...)
459 * @return boolean True iff successful.
464 function _parse_searchwords($search) {
465 $search = strtolower(trim($search));
467 return array(array(),array());
469 $words = preg_split('/\s+/', $search);
471 foreach ($words as $key => $word) {
472 if ($word[0] == '-' && $word != '-') {
473 $word = substr($word, 1);
474 $exclude[] = preg_quote($word);
478 return array($words, $exclude);
482 * Split the given limit parameter into offset,limit. (offset is optional. default: 0)
483 * Duplicate the PageList function here to avoid loading the whole PageList.php
485 * list($offset,$count) = $this->limit($args['limit']);
487 function limit($limit) {
488 if (strstr($limit, ','))
489 return split(',', $limit);
491 return array(0, $limit);
495 * Handle sortby requests for the DB iterator and table header links.
496 * Prefix the column with + or - like "+pagename","-mtime", ...
497 * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
498 * 'db' "-pagename" => "pagename DESC"
499 * In PageList all columns are sortable. (patch by DanFr)
500 * Here with the backend only some, the rest is delayed to PageList.
501 * (some kind of DumbIter)
502 * Duplicate the PageList function here to avoid loading the whole
503 * PageList.php, and it forces the backend specific sortable_columns()
505 function sortby ($column, $action, $sortable_columns=false) {
506 if (empty($column)) return '';
507 //support multiple comma-delimited sortby args: "+hits,+pagename"
508 if (strstr($column, ',')) {
510 foreach (explode(',', $column) as $col) {
512 $result[] = WikiDB_backend::sortby($col, $action);
514 $result[] = $this->sortby($col, $action);
516 return join(",",$result);
518 if (substr($column,0,1) == '+') {
519 $order = '+'; $column = substr($column,1);
520 } elseif (substr($column,0,1) == '-') {
521 $order = '-'; $column = substr($column,1);
523 // default order: +pagename, -mtime, -hits
525 if (in_array($column,array('mtime','hits')))
529 if ($action == 'flip_order') {
530 return ($order == '+' ? '-' : '+') . $column;
531 } elseif ($action == 'init') {
532 $this->_sortby[$column] = $order;
533 return $order . $column;
534 } elseif ($action == 'check') {
535 return (!empty($this->_sortby[$column]) or
536 ($GLOBALS['request']->getArg('sortby') and
537 strstr($GLOBALS['request']->getArg('sortby'),$column)));
538 } elseif ($action == 'db') {
539 // native sort possible?
540 if (!empty($this) and !$sortable_columns)
541 $sortable_columns = $this->sortable_columns();
542 if (in_array($column, $sortable_columns))
543 // asc or desc: +pagename, -pagename
544 return $column . ($order == '+' ? ' ASC' : ' DESC');
551 function sortable_columns() {
552 return array('pagename'/*,'mtime','author_id','author'*/);
555 // adds surrounding quotes
556 function quote ($s) { return "'".$s."'"; }
557 // no surrounding quotes because we know it's a string
558 function qstr ($s) { return $s; }
561 return in_array(DATABASE_TYPE, array('SQL','ADODB','PDO'));
566 * Iterator returned by backend methods which (possibly) return
569 * FIXME: This might be two seperate classes: page_iter and version_iter.
570 * For the versions we have WikiDB_backend_dumb_AllRevisionsIter.
572 class WikiDB_backend_iterator
575 * Get the next record in the iterator set.
577 * This returns a hash. The hash may contain the following keys:
579 * <dt> pagename <dt> (string) the page name or linked page name on link iterators
580 * <dt> version <dt> (int) the version number
581 * <dt> pagedata <dt> (hash) page meta-data (as returned from backend::get_pagedata().)
582 * <dt> versiondata <dt> (hash) page meta-data (as returned from backend::get_versiondata().)
583 * <dt> linkrelation <dt> (string) the page naming the relation (e.g. isa:=page <=> isa)
585 * If this is a page iterator, it must contain the 'pagename' entry --- the others
588 * If this is a version iterator, the 'pagename', 'version', <strong>and</strong> 'versiondata'
589 * entries are mandatory. ('pagedata' is optional.)
591 * If this is a link iterator, the 'pagename' is mandatory, 'linkrelation' is optional.
594 trigger_error("virtual", E_USER_ERROR);
598 return count($this->_pages);
602 reset($this->_pages);
603 return $this->_pages;
607 * Release resources held by this iterator.
614 * search baseclass, pcre-specific
616 class WikiDB_backend_search
618 function WikiDB_backend_search($search, &$dbh) {
620 $this->_case_exact = $search->_case_exact;
621 $this->_stoplist =& $search->_stoplist;
622 $this->_stoplisted = array();
624 function _quote($word) {
625 return preg_quote($word, "/");
627 //TODO: use word anchors
628 function EXACT($word) { return "^".$this->_quote($word)."$"; }
629 function STARTS_WITH($word) { return "^".$this->_quote($word); }
630 function ENDS_WITH($word) { return $this->_quote($word)."$"; }
631 function WORD($word) { return $this->_quote($word); }
632 function REGEX($word) { return $word; }
634 function _pagename_match_clause($node) {
636 $word = $this->$method($node->word);
637 return "preg_match(\"/\".$word.\"/\"".($this->_case_exact ? "i":"").")";
639 /* Eliminate stoplist words.
640 * Keep a list of Stoplisted words to inform the poor user.
642 function isStoplisted ($node) {
643 // check only on WORD or EXACT fulltext search
644 if ($node->op != 'WORD' and $node->op != 'EXACT')
646 if (preg_match("/^".$this->_stoplist."$/i", $node->word)) {
647 array_push($this->_stoplisted, $node->word);
652 function getStoplisted($word) {
653 return $this->_stoplisted;
658 * search baseclass, sql-specific
660 class WikiDB_backend_search_sql extends WikiDB_backend_search
662 function _pagename_match_clause($node) {
663 // word already quoted by TextSearchQuery_node_word::_sql_quote()
664 $word = $node->sql();
665 if ($word == '%') // ALL shortcut
668 return ($this->_case_exact
669 ? "pagename LIKE '$word'"
670 : "LOWER(pagename) LIKE '$word'");
672 function _fulltext_match_clause($node) {
673 // force word-style %word% for fulltext search
674 $word = '%' . $node->_sql_quote($node->word) . '%';
675 // eliminate stoplist words
676 if ($this->isStoplisted($node))
677 return "1=1"; // and (pagename or 1) => and 1
679 return $this->_pagename_match_clause($node)
680 // probably convert this MATCH AGAINST or SUBSTR/POSITION without wildcards
681 . ($this->_case_exact ? " OR content LIKE '$word'"
682 : " OR LOWER(content) LIKE '$word'");
691 // c-hanging-comment-ender-p: nil
692 // indent-tabs-mode: nil