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 * It does not have to be this way, of course, but the standard WikiDB uses
40 * a WikiDB_backend. (Other WikiDB's could be written which use some other
41 * method to access their underlying data store.)
43 * The interface outlined here seems to work well with both RDBM based
44 * and flat DBM/hash based methods of data storage.
46 * Though it contains some default implementation of certain methods,
47 * this is an abstract base class. It is expected that most efficient
48 * backends will override nearly all the methods in this class.
56 * Get page meta-data from database.
58 * @param $pagename string Page name.
60 * Returns a hash containing the page meta-data.
61 * Returns an empty array if there is no meta-data for the requested page.
62 * Keys which might be present in the hash are:
64 * <dt> locked <dd> If the page is locked.
65 * <dt> hits <dd> The page hit count.
66 * <dt> created <dd> Unix time of page creation. (FIXME: Deprecated: I
67 * don't think we need this...)
70 function get_pagedata($pagename) {
71 trigger_error("virtual", E_USER_ERROR);
75 * Update the page meta-data.
79 * Only meta-data whose keys are preset in $newdata is affected.
83 * $backend->update_pagedata($pagename, array('locked' => 1));
85 * will set the value of 'locked' to 1 for the specified page, but it
86 * will not affect the value of 'hits' (or whatever other meta-data
87 * may have been stored for the page.)
89 * To delete a particular piece of meta-data, set its value to false.
91 * $backend->update_pagedata($pagename, array('locked' => false));
94 * @param $pagename string Page name.
95 * @param $newdata hash New meta-data.
97 function update_pagedata($pagename, $newdata) {
98 trigger_error("virtual", E_USER_ERROR);
103 * Get the current version number for a page.
105 * @param $pagename string Page name.
106 * @return int The latest version number for the page. Returns zero if
107 * no versions of a page exist.
109 function get_latest_version($pagename) {
110 trigger_error("virtual", E_USER_ERROR);
114 * Get preceding version number.
116 * @param $pagename string Page name.
117 * @param $version int Find version before this one.
118 * @return int The version number of the version in the database which
119 * immediately preceeds $version.
121 function get_previous_version($pagename, $version) {
122 trigger_error("virtual", E_USER_ERROR);
126 * Get revision meta-data and content.
128 * @param $pagename string Page name.
129 * @param $version integer Which version to get.
130 * @param $want_content boolean
131 * Indicates the caller really wants the page content. If this
132 * flag is not set, the backend is free to skip fetching of the
133 * page content (as that may be expensive). If the backend omits
134 * the content, the backend might still want to set the value of
135 * '%content' to the empty string if it knows there's no content.
137 * @return hash The version data, or false if specified version does not
140 * Some keys which might be present in the $versiondata hash are:
143 * <dd> This is a pseudo-meta-data element (since it's actually
144 * the page data, get it?) containing the page content.
145 * If the content was not fetched, this key may not be present.
147 * For description of other version meta-data see WikiDB_PageRevision::get().
148 * @see WikiDB_PageRevision::get
150 function get_versiondata($pagename, $version, $want_content = false) {
151 trigger_error("virtual", E_USER_ERROR);
155 * Delete page from the database with backup possibility.
156 * This should remove all links (from the named page) from
159 * @param $pagename string Page name.
160 * i.e save_page('') and DELETE nonempty id
161 * Can be undone and is seen in RecentChanges.
163 function delete_page($pagename) {
165 $user =& $GLOBALS['request']->_user;
166 $vdata = array('author' => $user->getId(),
167 'author_id' => $user->getAuthenticatedId(),
170 $this->lock(); // critical section:
171 $version = $this->get_latest_version($pagename);
172 $this->set_versiondata($pagename, $version+1, $vdata);
173 $this->set_links($pagename, false); // links are purged.
174 // SQL needs to invalidate the non_empty id
175 if (! WIKIDB_NOCACHE_MARKUP) {
176 // need the hits, perms and LOCKED, otherwise you can reset the perm
177 // by action=remove and re-create it with default perms
178 $pagedata = $this->get_pagedata($pagename);
179 unset($pagedata['_cached_html']);
180 $this->update_pagedata($pagename, $pagedata);
186 * Delete page (and all it's revisions) from the database.
189 function purge_page($pagename) {
190 trigger_error("virtual", E_USER_ERROR);
194 * Delete an old revision of a page.
196 * Note that one is never allowed to delete the most recent version,
197 * but that this requirement is enforced by WikiDB not by the backend.
199 * In fact, to be safe, backends should probably allow the deletion of
200 * the most recent version.
202 * @param $pagename string Page name.
203 * @param $version integer Version to delete.
205 function delete_versiondata($pagename, $version) {
206 trigger_error("virtual", E_USER_ERROR);
210 * Create a new page revision.
212 * If the given ($pagename,$version) is already in the database,
213 * this method completely overwrites any stored data for that version.
215 * @param $pagename string Page name.
216 * @param $version int New revisions content.
217 * @param $data hash New revision metadata.
219 * @see get_versiondata
221 function set_versiondata($pagename, $version, $data) {
222 trigger_error("virtual", E_USER_ERROR);
226 * Update page version meta-data.
228 * If the given ($pagename,$version) is already in the database,
229 * this method only changes those meta-data values whose keys are
230 * explicity listed in $newdata.
232 * @param $pagename string Page name.
233 * @param $version int New revisions content.
234 * @param $newdata hash New revision metadata.
235 * @see set_versiondata, get_versiondata
237 function update_versiondata($pagename, $version, $newdata) {
238 $data = $this->get_versiondata($pagename, $version, true);
243 foreach ($newdata as $key => $val) {
249 $this->set_versiondata($pagename, $version, $data);
253 * Set links for page.
255 * @param $pagename string Page name.
257 * @param $links array List of page(names) which page links to.
259 function set_links($pagename, $links) {
260 trigger_error("virtual", E_USER_ERROR);
264 * Find pages which link to or are linked from a page.
266 * @param $pagename string Page name.
267 * @param $reversed boolean True to get backlinks.
269 * FIXME: array or iterator?
270 * @return object A WikiDB_backend_iterator.
272 function get_links($pagename, $reversed, $include_empty=false,
273 $sortby='', $limit='', $exclude='') {
274 //FIXME: implement simple (but slow) link finder.
275 die("FIXME get_links");
279 * Get all revisions of a page.
281 * @param $pagename string The page name.
282 * @return object A WikiDB_backend_iterator.
284 function get_all_revisions($pagename) {
285 include_once('lib/WikiDB/backend/dumb/AllRevisionsIter.php');
286 return new WikiDB_backend_dumb_AllRevisionsIter($this, $pagename);
290 * Get all pages in the database.
292 * Pages should be returned in alphabetical order if that is
297 * @param $include_defaulted boolean
298 * If set, even pages with no content will be returned
299 * --- but still only if they have at least one revision (not
300 * counting the default revision 0) entered in the database.
302 * Normally pages whose current revision has empty content
303 * are not returned as these pages are considered to be
306 * @return object A WikiDB_backend_iterator.
308 function get_all_pages($include_defaulted, $orderby=false, $limit='', $exclude='') {
309 trigger_error("virtual", E_USER_ERROR);
313 * Title or full text search.
315 * Pages should be returned in alphabetical order if that is
320 * @param $search object A TextSearchQuery object describing the parsed query string,
321 * with efficient methods for SQL and PCRE match.
323 * @param $fullsearch boolean If true, a full text search is performed,
324 * otherwise a title search is performed.
326 * @return object A WikiDB_backend_iterator.
328 * @see WikiDB::titleSearch
330 function text_search($search, $fulltext=false, $sortby='',
331 $limit='', $exclude='')
333 // This method implements a simple linear search
334 // through all the pages in the database.
336 // It is expected that most backends will overload
337 // this method with something more efficient.
338 include_once('lib/WikiDB/backend/dumb/TextSearchIter.php');
340 $pages = $this->get_all_pages(false, $sortby, false, $exclude);
341 return new WikiDB_backend_dumb_TextSearchIter($this, $pages, $search, $fulltext,
342 array('limit' => $limit,
343 'exclude' => $exclude));
350 * @param $pages object A TextSearchQuery object.
351 * @param $linkvalue object A TextSearchQuery object for the linkvalues
352 * (linkto, relation or backlinks or attribute values).
353 * @param $linktype string One of the 4 linktypes.
354 * @param $relation object A TextSearchQuery object or false.
355 * @param $options array Currently ignored. hash of sortby, limit, exclude.
356 * @return object A WikiDB_backend_iterator.
357 * @see WikiDB::linkSearch
359 function link_search( $pages, $linkvalue, $linktype, $relation=false, $options=array() ) {
360 include_once('lib/WikiDB/backend/dumb/LinkSearchIter.php');
361 $pageiter = $this->text_search($pages);
362 return new WikiDB_backend_dumb_LinkSearchIter($this, $pageiter, $linkvalue, $linktype, $relation, $options);
366 * Find pages with highest hit counts.
368 * Find the pages with the highest hit counts. The pages should
369 * be returned in reverse order by hit count.
372 * @param integer $limit No more than this many pages
373 * @return object A WikiDB_backend_iterator.
375 function most_popular($limit, $sortby='-hits') {
376 // This is method fetches all pages, then
377 // sorts them by hit count.
378 // (Not very efficient.)
380 // It is expected that most backends will overload
381 // method with something more efficient.
382 include_once('lib/WikiDB/backend/dumb/MostPopularIter.php');
383 $pages = $this->get_all_pages(false, $sortby, false);
384 return new WikiDB_backend_dumb_MostPopularIter($this, $pages, $limit);
388 * Find recent changes.
391 * @param $params hash See WikiDB::mostRecent for a description
392 * of parameters which can be included in this hash.
393 * @return object A WikiDB_backend_iterator.
394 * @see WikiDB::mostRecent
396 function most_recent($params) {
397 // This method is very inefficient and searches through
398 // all pages for the most recent changes.
400 // It is expected that most backends will overload
401 // method with something more efficient.
402 include_once('lib/WikiDB/backend/dumb/MostRecentIter.php');
403 $pages = $this->get_all_pages(true, '-mtime');
404 return new WikiDB_backend_dumb_MostRecentIter($this, $pages, $params);
407 function wanted_pages($exclude_from='', $exclude='', $sortby='', $limit='') {
408 include_once('lib/WikiDB/backend/dumb/WantedPagesIter.php');
409 $allpages = $this->get_all_pages(true,false,false,$exclude_from);
410 return new WikiDB_backend_dumb_WantedPagesIter($this, $allpages, $exclude, $sortby, $limit);
414 * Lock backend database.
416 * Calls may be nested.
418 * @param $write_lock boolean Unless this is set to false, a write lock
419 * is acquired, otherwise a read lock. If the backend doesn't support
420 * read locking, then it should make a write lock no matter which type
421 * of lock was requested.
423 * All backends <em>should</em> support write locking.
425 function lock($write_lock = true) {
429 * Unlock backend database.
431 * @param $force boolean Normally, the database is not unlocked until
432 * unlock() is called as many times as lock() has been. If $force is
433 * set to true, the the database is unconditionally unlocked.
435 function unlock($force = false) {
446 * Synchronize with filesystem.
448 * This should flush all unwritten data to the filesystem.
454 * Optimize the database.
456 function optimize() {
460 * Check database integrity.
462 * This should check the validity of the internal structure of the database.
463 * Errors should be reported via:
465 * trigger_error("Message goes here.", E_USER_WARNING);
468 * @return boolean True iff database is in a consistent state.
470 function check($args=false) {
474 * Put the database into a consistent state
475 * by reparsing and restoring all pages.
477 * This should put the database into a consistent state.
478 * (I.e. rebuild indexes, etc...)
480 * @return boolean True iff successful.
482 function rebuild($args=false) {
484 $dbh = $request->getDbh();
485 $iter = $dbh->getAllPages(false);
486 while ($page = $iter->next()) {
487 $current = $page->getCurrentRevision(true);
488 $pagename = $page->getName();
489 $meta = $current->_data;
490 $version = $current->getVersion();
491 $content =& $meta['%content'];
492 $formatted = new TransformedText($page, $content, $current->getMetaData());
493 $type = $formatted->getType();
494 $meta['pagetype'] = $type->getName();
495 $links = $formatted->getWikiPageLinks(); // linkto => relation
496 $this->lock(array('version','page','recent','link','nonempty'));
497 $this->set_versiondata($pagename, $version, $meta);
498 $this->set_links($pagename, $links);
499 $this->unlock(array('version','page','recent','link','nonempty'));
503 function _parse_searchwords($search) {
504 $search = strtolower(trim($search));
506 return array(array(),array());
508 $words = preg_split('/\s+/', $search);
510 foreach ($words as $key => $word) {
511 if ($word[0] == '-' && $word != '-') {
512 $word = substr($word, 1);
513 $exclude[] = preg_quote($word);
517 return array($words, $exclude);
521 * Split the given limit parameter into offset,limit. (offset is optional. default: 0)
522 * Duplicate the PageList function here to avoid loading the whole PageList.php
524 * list($offset,$count) = $this->limit($args['limit']);
526 function limit($limit) {
527 if (strstr($limit, ',')) {
528 list($from, $limit) = split(',', $limit);
529 if ((!empty($from) && !is_numeric($from)) or (!empty($limit) && !is_numeric($limit))) {
530 return $this->error(_("Illegal 'limit' argument: must be numeric"));
532 return array($from, $limit);
535 if (!empty($limit) && !is_numeric($limit)) {
536 return $this->error(_("Illegal 'limit' argument: must be numeric"));
538 return array(0, $limit);
543 * Handle sortby requests for the DB iterator and table header links.
544 * Prefix the column with + or - like "+pagename","-mtime", ...
545 * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
546 * 'db' "-pagename" => "pagename DESC"
547 * In PageList all columns are sortable. (patch by DanFr)
548 * Here with the backend only some, the rest is delayed to PageList.
549 * (some kind of DumbIter)
550 * Duplicate the PageList function here to avoid loading the whole
551 * PageList.php, and it forces the backend specific sortable_columns()
553 function sortby ($column, $action, $sortable_columns=false) {
554 if (empty($column)) return '';
555 //support multiple comma-delimited sortby args: "+hits,+pagename"
556 if (strstr($column, ',')) {
558 foreach (explode(',', $column) as $col) {
560 $result[] = WikiDB_backend::sortby($col, $action);
562 $result[] = $this->sortby($col, $action);
564 return join(",",$result);
566 if (substr($column,0,1) == '+') {
567 $order = '+'; $column = substr($column,1);
568 } elseif (substr($column,0,1) == '-') {
569 $order = '-'; $column = substr($column,1);
571 // default order: +pagename, -mtime, -hits
573 if (in_array($column,array('mtime','hits')))
577 if ($action == 'flip_order') {
578 return ($order == '+' ? '-' : '+') . $column;
579 } elseif ($action == 'init') {
580 $this->_sortby[$column] = $order;
581 return $order . $column;
582 } elseif ($action == 'check') {
583 return (!empty($this->_sortby[$column]) or
584 ($GLOBALS['request']->getArg('sortby') and
585 strstr($GLOBALS['request']->getArg('sortby'),$column)));
586 } elseif ($action == 'db') {
587 // native sort possible?
588 if (!empty($this) and !$sortable_columns)
589 $sortable_columns = $this->sortable_columns();
590 if (in_array($column, $sortable_columns))
591 // asc or desc: +pagename, -pagename
592 return $column . ($order == '+' ? ' ASC' : ' DESC');
599 function sortable_columns() {
600 return array('pagename'/*,'mtime','author_id','author'*/);
603 // adds surrounding quotes
604 function quote ($s) { return "'".$s."'"; }
605 // no surrounding quotes because we know it's a string
606 function qstr ($s) { return $s; }
609 return in_array(DATABASE_TYPE, array('SQL','ADODB','PDO'));
612 function backendType() {
613 return DATABASE_TYPE;
616 function write_accesslog(&$entry) {
618 if (!$this->isSQL()) return;
620 $log_tbl = $entry->_accesslog->logtable;
621 // duration problem: sprintf "%f" might use comma e.g. "100,201" in european locales
622 $dbh->query("INSERT INTO $log_tbl"
623 . " (time_stamp,remote_host,remote_user,request_method,request_line,request_args,"
624 . "request_uri,request_time,status,bytes_sent,referer,agent,request_duration)"
625 . " VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)",
627 // Problem: date formats are backend specific. Either use unixtime as %d (long),
628 // or the native timestamp format.
632 $entry->request_method,
634 $entry->request_args,
636 $entry->_ncsa_time($entry->time),
646 * Iterator returned by backend methods which (possibly) return
649 * FIXME: This might be two seperate classes: page_iter and version_iter.
650 * For the versions we have WikiDB_backend_dumb_AllRevisionsIter.
652 class WikiDB_backend_iterator
655 * Get the next record in the iterator set.
657 * This returns a hash. The hash may contain the following keys:
659 * <dt> pagename <dt> (string) the page name or linked page name on link iterators
660 * <dt> version <dt> (int) the version number
661 * <dt> pagedata <dt> (hash) page meta-data (as returned from backend::get_pagedata().)
662 * <dt> versiondata <dt> (hash) page meta-data (as returned from backend::get_versiondata().)
663 * <dt> linkrelation <dt> (string) the page naming the relation (e.g. isa:=page <=> isa)
665 * If this is a page iterator, it must contain the 'pagename' entry --- the others
668 * If this is a version iterator, the 'pagename', 'version', <strong>and</strong> 'versiondata'
669 * entries are mandatory. ('pagedata' is optional.)
671 * If this is a link iterator, the 'pagename' is mandatory, 'linkrelation' is optional.
674 trigger_error("virtual", E_USER_ERROR);
678 if (!empty($this->_pages))
679 return count($this->_pages);
685 if (!empty($this->_pages)) {
686 reset($this->_pages);
687 return $this->_pages;
690 while ($page = $this->next())
697 * limit - if empty the pagelist iterator will do nothing.
698 * Some backends limit the result set itself (dba, file, flatfile),
699 * Some SQL based leave it to WikiDB/PageList - deferred filtering in the iterator.
702 return empty($this->_options['limit']) ? 0 : $this->_options['limit'];
706 * Release resources held by this iterator.
713 * search baseclass, pcre-specific
715 class WikiDB_backend_search
717 function WikiDB_backend_search($search, &$dbh) {
719 $this->_case_exact = $search->_case_exact;
720 $this->_stoplist =& $search->_stoplist;
721 $this->stoplisted = array();
723 function _quote($word) {
724 return preg_quote($word, "/");
726 //TODO: use word anchors
727 function EXACT($word) { return "^".$this->_quote($word)."$"; }
728 function STARTS_WITH($word) { return "^".$this->_quote($word); }
729 function ENDS_WITH($word) { return $this->_quote($word)."$"; }
730 function WORD($word) { return $this->_quote($word); }
731 function REGEX($word) { return $word; }
733 function _pagename_match_clause($node) {
735 $word = $this->$method($node->word);
736 return "preg_match(\"/\".$word.\"/\"".($this->_case_exact ? "i":"").")";
738 /* Eliminate stoplist words.
739 * Keep a list of Stoplisted words to inform the poor user.
741 function isStoplisted ($node) {
742 // check only on WORD or EXACT fulltext search
743 if ($node->op != 'WORD' and $node->op != 'EXACT')
745 if (preg_match("/^".$this->_stoplist."$/i", $node->word)) {
746 array_push($this->stoplisted, $node->word);
751 function getStoplisted($word) {
752 return $this->stoplisted;
757 * search baseclass, sql-specific
759 class WikiDB_backend_search_sql extends WikiDB_backend_search
761 function _pagename_match_clause($node) {
762 // word already quoted by TextSearchQuery_node_word::_sql_quote()
763 $word = $node->sql();
764 if ($word == '%') // ALL shortcut
767 return ($this->_case_exact
768 ? "pagename LIKE '$word'"
769 : "LOWER(pagename) LIKE '$word'");
771 function _fulltext_match_clause($node) {
772 // force word-style %word% for fulltext search
773 $word = '%' . $node->_sql_quote($node->word) . '%';
774 // eliminate stoplist words
775 if ($this->isStoplisted($node))
776 return "1=1"; // and (pagename or 1) => and 1
778 return $this->_pagename_match_clause($node)
779 // probably convert this MATCH AGAINST or SUBSTR/POSITION without wildcards
780 . ($this->_case_exact ? " OR content LIKE '$word'"
781 : " OR LOWER(content) LIKE '$word'");
790 // c-hanging-comment-ender-p: nil
791 // indent-tabs-mode: nil