2 rcs_id('$Id: backend.php,v 1.9 2004-07-09 10:06:49 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.
158 * Delete page (and all it's revisions) from the database.
160 * This should remove all links (from the named page) from
163 * @param $pagename string Page name.
165 function delete_page($pagename) {
166 trigger_error("virtual", E_USER_ERROR);
170 * Delete an old revision of a page.
172 * Note that one is never allowed to delete the most recent version,
173 * but that this requirement is enforced by WikiDB not by the backend.
175 * In fact, to be safe, backends should probably allow the deletion of
176 * the most recent version.
178 * @param $pagename string Page name.
179 * @param $version integer Version to delete.
181 function delete_versiondata($pagename, $version) {
182 trigger_error("virtual", E_USER_ERROR);
186 * Create a new page revision.
188 * If the given ($pagename,$version) is already in the database,
189 * this method completely overwrites any stored data for that version.
191 * @param $pagename string Page name.
192 * @param $version int New revisions content.
193 * @param $data hash New revision metadata.
195 * @see get_versiondata
197 function set_versiondata($pagename, $version, $data) {
198 trigger_error("virtual", E_USER_ERROR);
202 * Update page version meta-data.
204 * If the given ($pagename,$version) is already in the database,
205 * this method only changes those meta-data values whose keys are
206 * explicity listed in $newdata.
208 * @param $pagename string Page name.
209 * @param $version int New revisions content.
210 * @param $newdata hash New revision metadata.
211 * @see set_versiondata, get_versiondata
213 function update_versiondata($pagename, $version, $newdata) {
214 $data = $this->get_versiondata($pagename, $version, true);
219 foreach ($newdata as $key => $val) {
225 $this->set_versiondata($pagename, $version, $data);
229 * Set links for page.
231 * @param $pagename string Page name.
233 * @param $links array List of page(names) which page links to.
235 function set_links($pagename, $links) {
236 trigger_error("virtual", E_USER_ERROR);
240 * Find pages which link to or are linked from a page.
242 * @param $pagename string Page name.
243 * @param $reversed boolean True to get backlinks.
245 * FIXME: array or iterator?
246 * @return object A WikiDB_backend_iterator.
248 function get_links($pagename, $reversed) {
249 //FIXME: implement simple (but slow) link finder.
254 * Get all revisions of a page.
256 * @param $pagename string The page name.
257 * @return object A WikiDB_backend_iterator.
259 function get_all_revisions($pagename) {
260 include_once('lib/WikiDB/backend/dumb/AllRevisionsIter.php');
261 return new WikiDB_backend_dumb_AllRevisionsIter($this, $pagename);
265 * Get all pages in the database.
267 * Pages should be returned in alphabetical order if that is
272 * @param $include_defaulted boolean
273 * If set, even pages with no content will be returned
274 * --- but still only if they have at least one revision (not
275 * counting the default revision 0) entered in the database.
277 * Normally pages whose current revision has empty content
278 * are not returned as these pages are considered to be
281 * @return object A WikiDB_backend_iterator.
283 function get_all_pages($include_defaulted, $orderby=false, $limit=false) {
284 trigger_error("virtual", E_USER_ERROR);
288 * Title or full text search.
290 * Pages should be returned in alphabetical order if that is
295 * @param $search object A TextSearchQuery object describing what pages
296 * are to be searched for.
298 * @param $fullsearch boolean If true, a full text search is performed,
299 * otherwise a title search is performed.
301 * @return object A WikiDB_backend_iterator.
303 * @see WikiDB::titleSearch
305 function text_search($search = '', $fullsearch = false) {
306 // This is method implements a simple linear search
307 // through all the pages in the database.
309 // It is expected that most backends will overload
310 // method with something more efficient.
311 include_once('lib/WikiDB/backend/dumb/TextSearchIter.php');
312 $pages = $this->get_all_pages(false);
313 return new WikiDB_backend_dumb_TextSearchIter($this, $pages, $search, $fullsearch);
317 * Find pages with highest hit counts.
319 * Find the pages with the highest hit counts. The pages should
320 * be returned in reverse order by hit count.
323 * @param $limit integer No more than this many pages
324 * @return object A WikiDB_backend_iterator.
326 function most_popular($limit) {
327 // This is method fetches all pages, then
328 // sorts them by hit count.
329 // (Not very efficient.)
331 // It is expected that most backends will overload
332 // method with something more efficient.
333 include_once('lib/WikiDB/backend/dumb/MostPopularIter.php');
334 $pages = $this->get_all_pages(false, 'hits DESC', $limit);
336 return new WikiDB_backend_dumb_MostPopularIter($this, $pages, $limit);
340 * Find recent changes.
343 * @param $params hash See WikiDB::mostRecent for a description
344 * of parameters which can be included in this hash.
345 * @return object A WikiDB_backend_iterator.
346 * @see WikiDB::mostRecent
348 function most_recent($params) {
349 // This method is very inefficient and searches through
350 // all pages for the most recent changes.
352 // It is expected that most backends will overload
353 // method with something more efficient.
354 include_once('lib/WikiDB/backend/dumb/MostRecentIter.php');
355 $pages = $this->get_all_pages(true,'mtime DESC');
356 return new WikiDB_backend_dumb_MostRecentIter($this, $pages, $params);
360 * Lock backend database.
362 * Calls may be nested.
364 * @param $write_lock boolean Unless this is set to false, a write lock
365 * is acquired, otherwise a read lock. If the backend doesn't support
366 * read locking, then it should make a write lock no matter which type
367 * of lock was requested.
369 * All backends <em>should</em> support write locking.
371 function lock($write_lock = true) {
375 * Unlock backend database.
377 * @param $force boolean Normally, the database is not unlocked until
378 * unlock() is called as many times as lock() has been. If $force is
379 * set to true, the the database is unconditionally unlocked.
381 function unlock($force = false) {
392 * Synchronize with filesystem.
394 * This should flush all unwritten data to the filesystem.
400 * Optimize the database.
402 function optimize() {
406 * Check database integrity.
408 * This should check the validity of the internal structure of the database.
409 * Errors should be reported via:
411 * trigger_error("Message goes here.", E_USER_WARNING);
414 * @return boolean True iff database is in a consistent state.
420 * Put the database into a consistent state.
422 * This should put the database into a consistent state.
423 * (I.e. rebuild indexes, etc...)
425 * @return boolean True iff successful.
430 function _parse_searchwords($search) {
431 $search = strtolower(trim($search));
433 return array(array(),array());
435 $words = preg_split('/\s+/', $search);
437 foreach ($words as $key => $word) {
438 if ($word[0] == '-' && $word != '-') {
439 $word = substr($word, 1);
440 $exclude[] = preg_quote($word);
444 return array($words, $exclude);
448 * Split the given limit parameter into offset,pagesize. (offset is optional. default: 0)
449 * Duplicate the PageList function here to avoid loading the whole PageList.php
451 * list($offset,$pagesize) = $this->limit($args['limit']);
453 function limit($limit) {
454 if (strstr($limit, ','))
455 return split(',', $limit);
457 return array(0, $limit);
461 * Handle sortby requests for the DB iterator and table header links.
462 * Prefix the column with + or - like "+pagename","-mtime", ...
463 * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
464 * 'db' "-pagename" => "pagename DESC"
465 * In PageList all columns are sortable. (patch by DanFr)
466 * Here with the backend only some, the rest is delayed to PageList.
467 * (some kind of DumbIter)
468 * Duplicate the PageList function here to avoid loading the whole
469 * PageList.php, and it forces the backend specific sortable_columns()
471 function sortby ($column, $action) {
472 if (empty($column)) return '';
473 //support multiple comma-delimited sortby args: "+hits,+pagename"
474 if (strstr($column,',')) {
476 foreach (explode(',',$column) as $col) {
477 $result[] = $this->sortby($col,$action);
479 return join(",",$result);
481 if (substr($column,0,1) == '+') {
482 $order = '+'; $column = substr($column,1);
483 } elseif (substr($column,0,1) == '-') {
484 $order = '-'; $column = substr($column,1);
486 // default order: +pagename, -mtime, -hits
488 if (in_array($column,array('mtime','hits')))
492 if ($action == 'flip_order') {
493 return ($order == '+' ? '-' : '+') . $column;
494 } elseif ($action == 'init') {
495 $this->_sortby[$column] = $order;
496 return $order . $column;
497 } elseif ($action == 'check') {
498 return (!empty($this->_sortby[$column]) or
499 ($GLOBALS['request']->getArg('sortby') and
500 strstr($GLOBALS['request']->getArg('sortby'),$column)));
501 } elseif ($action == 'db') {
502 // native sort possible?
503 $sortable_columns = $this->sortable_columns();
504 if (in_array($column, $sortable_columns))
505 // asc or desc: +pagename, -pagename
506 return $column . ($order == '+' ? ' ASC' : ' DESC');
513 function sortable_columns() {
514 return array('pagename'/*,'mtime','author_id','author'*/);
520 * Iterator returned by backend methods which (possibly) return
523 * FIXME: This might be two seperate classes: page_iter and version_iter.
524 * For the versions we have WikiDB_backend_dumb_AllRevisionsIter.
526 class WikiDB_backend_iterator
529 * Get the next record in the iterator set.
531 * This returns a hash. The hash may contain the following keys:
533 * <dt> pagename <dt> (string) the page name
534 * <dt> version <dt> (int) the version number
535 * <dt> pagedata <dt> (hash) page meta-data (as returned from backend::get_pagedata().)
536 * <dt> versiondata <dt> (hash) page meta-data (as returned from backend::get_versiondata().)
538 * If this is a page iterator, it must contain the 'pagename' entry --- the others
541 * If this is a version iterator, the 'pagename', 'version', <strong>and</strong> 'versiondata'
542 * entries are mandatory. ('pagedata' is optional.)
545 trigger_error("virtual", E_USER_ERROR);
549 return count($this->_pages);
553 * Release resources held by this iterator.
564 // c-hanging-comment-ender-p: nil
565 // indent-tabs-mode: nil