]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend.php
and again a couple of more native db args: backlinks
[SourceForge/phpwiki.git] / lib / WikiDB / backend.php
1 <?php // -*-php-*-
2 rcs_id('$Id: backend.php,v 1.15 2004-11-25 17:20:51 rurban Exp $');
3
4 /*
5   Pagedata
6
7    maintained by WikiPage 
8     //:latestversion
9     //:deleted (*)     (Set if latest content is empty.)
10     //:pagename (*)
11
12     hits
13     is_locked
14
15   Versiondata
16
17     %content (?should this be here?)
18     _supplanted : Time version ceased to be the current version
19
20     mtime (*)   : Time of version edit.
21     orig_mtime
22     is_minor_edit (*)
23     author      : nominal author
24     author_id   : authenticated author
25     summary
26
27     //version
28     //created (*)
29     //%superceded
30         
31     //:serial
32
33      (types are scalars: strings, ints, bools)
34 */     
35
36 /**
37  * A WikiDB_backend handles the storage and retrieval of data for a WikiDB.
38  *
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.)
43  *
44  * The interface outlined here seems to work well with both RDBM based
45  * and flat DBM/hash based methods of data storage.
46  *
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.
50  *
51  * @access protected
52  * @see WikiDB
53  */
54 class WikiDB_backend
55 {
56     /**
57      * Get page meta-data from database.
58      *
59      * @param $pagename string Page name.
60      * @return hash
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:
64      * <dl>
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...) 
69      * </dl>
70      */
71     function get_pagedata($pagename) {
72         trigger_error("virtual", E_USER_ERROR);
73     }
74
75     /**
76      * Update the page meta-data.
77      *
78      * Set page meta-data.
79      *
80      * Only meta-data whose keys are preset in $newdata is affected.
81      *
82      * For example:
83      * <pre>
84      *   $backend->update_pagedata($pagename, array('locked' => 1)); 
85      * </pre>
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.)
89      *
90      * To delete a particular piece of meta-data, set it's value to false.
91      * <pre>
92      *   $backend->update_pagedata($pagename, array('locked' => false)); 
93      * </pre>
94      *
95      * @param $pagename string Page name.
96      * @param $newdata hash New meta-data.
97      */
98     function update_pagedata($pagename, $newdata) {
99         trigger_error("virtual", E_USER_ERROR);
100     }
101     
102
103     /**
104      * Get the current version number for a page.
105      *
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.
109      */
110     function get_latest_version($pagename) {
111         trigger_error("virtual", E_USER_ERROR);
112     }
113     
114     /**
115      * Get preceding version number.
116      *
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.
121      */
122     function get_previous_version($pagename, $version) {
123         trigger_error("virtual", E_USER_ERROR);
124     }
125     
126     /**
127      * Get revision meta-data and content.
128      *
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.
137      *
138      * @return hash The version data, or false if specified version does not
139      *    exist.
140      *
141      * Some keys which might be present in the $versiondata hash are:
142      * <dl>
143      * <dt> %content
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.
147      * </dl>
148      * For description of other version meta-data see WikiDB_PageRevision::get().
149      * @see WikiDB_PageRevision::get
150      */
151     function get_versiondata($pagename, $version, $want_content = false) {
152         trigger_error("virtual", E_USER_ERROR);
153     }
154
155     /**
156      * Delete page from the database.
157      *
158      * Delete page (and all it's revisions) from the database.
159      *
160      * This should remove all links (from the named page) from
161      * the link database.
162      *
163      * @param $pagename string Page name.
164      */
165     function delete_page($pagename) {
166         trigger_error("virtual", E_USER_ERROR);
167     }
168             
169     /**
170      * Delete an old revision of a page.
171      *
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.
174      *
175      * In fact, to be safe, backends should probably allow the deletion of
176      * the most recent version.
177      *
178      * @param $pagename string Page name.
179      * @param $version integer Version to delete.
180      */
181     function delete_versiondata($pagename, $version) {
182         trigger_error("virtual", E_USER_ERROR);
183     }
184
185     /**
186      * Create a new page revision.
187      *
188      * If the given ($pagename,$version) is already in the database,
189      * this method completely overwrites any stored data for that version.
190      *
191      * @param $pagename string Page name.
192      * @param $version int New revisions content.
193      * @param $data hash New revision metadata.
194      *
195      * @see get_versiondata
196      */
197     function set_versiondata($pagename, $version, $data) {
198         trigger_error("virtual", E_USER_ERROR);
199     }
200
201     /**
202      * Update page version meta-data.
203      *
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.
207      *
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
212      */
213     function update_versiondata($pagename, $version, $newdata) {
214         $data = $this->get_versiondata($pagename, $version, true);
215         if (!$data) {
216             assert($data);
217             return;
218         }
219         foreach ($newdata as $key => $val) {
220             if (empty($val))
221                 unset($data[$key]);
222             else
223                 $data[$key] = $val;
224         }
225         $this->set_versiondata($pagename, $version, $data);
226     }
227     
228     /**
229      * Set links for page.
230      *
231      * @param $pagename string Page name.
232      *
233      * @param $links array List of page(names) which page links to.
234      */
235     function set_links($pagename, $links) {
236         trigger_error("virtual", E_USER_ERROR);
237     }
238         
239     /**
240      * Find pages which link to or are linked from a page.
241      *
242      * @param $pagename string Page name.
243      * @param $reversed boolean True to get backlinks.
244      *
245      * FIXME: array or iterator?
246      * @return object A WikiDB_backend_iterator.
247      */
248     function get_links($pagename, $reversed, $include_empty=false,
249                        $sortby=false, $limit=false, $exclude=false) {
250         //FIXME: implement simple (but slow) link finder.
251         die("FIXME");
252     }
253
254     /**
255      * Get all revisions of a page.
256      *
257      * @param $pagename string The page name.
258      * @return object A WikiDB_backend_iterator.
259      */
260     function get_all_revisions($pagename) {
261         include_once('lib/WikiDB/backend/dumb/AllRevisionsIter.php');
262         return new WikiDB_backend_dumb_AllRevisionsIter($this, $pagename);
263     }
264     
265     /**
266      * Get all pages in the database.
267      *
268      * Pages should be returned in alphabetical order if that is
269      * feasable.
270      *
271      * @access protected
272      *
273      * @param $include_defaulted boolean
274      * If set, even pages with no content will be returned
275      * --- but still only if they have at least one revision (not
276      * counting the default revision 0) entered in the database.
277      *
278      * Normally pages whose current revision has empty content
279      * are not returned as these pages are considered to be
280      * non-existing.
281      *
282      * @return object A WikiDB_backend_iterator.
283      */
284     function get_all_pages($include_defaulted, $orderby=false, $limit=false, $exclude=false) {
285         trigger_error("virtual", E_USER_ERROR);
286     }
287         
288     /**
289      * Title or full text search.
290      *
291      * Pages should be returned in alphabetical order if that is
292      * feasable.
293      *
294      * @access protected
295      *
296      * @param $search object A TextSearchQuery object describing what pages
297      * are to be searched for.
298      *
299      * @param $fullsearch boolean If true, a full text search is performed,
300      *  otherwise a title search is performed.
301      *
302      * @return object A WikiDB_backend_iterator.
303      *
304      * @see WikiDB::titleSearch
305      */
306     function text_search($search='', $fulltext=false, $case_exact=false) {
307         // This is method implements a simple linear search
308         // through all the pages in the database.
309         //
310         // It is expected that most backends will overload
311         // method with something more efficient.
312         include_once('lib/WikiDB/backend/dumb/TextSearchIter.php');
313         $pages = $this->get_all_pages(false);
314         return new WikiDB_backend_dumb_TextSearchIter($this, $pages, $search, $fulltext, $case_exact);
315     }
316
317     /**
318      * Find pages with highest hit counts.
319      *
320      * Find the pages with the highest hit counts.  The pages should
321      * be returned in reverse order by hit count.
322      *
323      * @access protected
324      * @param $limit integer  No more than this many pages
325      * @return object A WikiDB_backend_iterator.
326      */
327     function most_popular($limit, $sortby='-hits') {
328         // This is method fetches all pages, then
329         // sorts them by hit count.
330         // (Not very efficient.)
331         //
332         // It is expected that most backends will overload
333         // method with something more efficient.
334         include_once('lib/WikiDB/backend/dumb/MostPopularIter.php');
335         $pages = $this->get_all_pages(false, $sortby, $limit);
336         
337         return new WikiDB_backend_dumb_MostPopularIter($this, $pages, $limit);
338     }
339
340     /**
341      * Find recent changes.
342      *
343      * @access protected
344      * @param $params hash See WikiDB::mostRecent for a description
345      *  of parameters which can be included in this hash.
346      * @return object A WikiDB_backend_iterator.
347      * @see WikiDB::mostRecent
348      */
349     function most_recent($params) {
350         // This method is very inefficient and searches through
351         // all pages for the most recent changes.
352         //
353         // It is expected that most backends will overload
354         // method with something more efficient.
355         include_once('lib/WikiDB/backend/dumb/MostRecentIter.php');
356         $pages = $this->get_all_pages(true, '-mtime');
357         return new WikiDB_backend_dumb_MostRecentIter($this, $pages, $params);
358     }
359
360     function wanted_pages($exclude_from='', $exclude='', $sortby=false, $limit=false) {
361         include_once('lib/WikiDB/backend/dumb/WantedPagesIter.php');
362         $allpages = $this->get_all_pages(true,false,false,$exclude_from);
363         return new WikiDB_backend_dumb_WantedPagesIter($this, $allpages, $exclude, $sortby, $limit);
364     }
365
366     /**
367      * Lock backend database.
368      *
369      * Calls may be nested.
370      *
371      * @param $write_lock boolean Unless this is set to false, a write lock
372      *     is acquired, otherwise a read lock.  If the backend doesn't support
373      *     read locking, then it should make a write lock no matter which type
374      *     of lock was requested.
375      *
376      *     All backends <em>should</em> support write locking.
377      */
378     function lock($write_lock = true) {
379     }
380
381     /**
382      * Unlock backend database.
383      *
384      * @param $force boolean Normally, the database is not unlocked until
385      *  unlock() is called as many times as lock() has been.  If $force is
386      *  set to true, the the database is unconditionally unlocked.
387      */
388     function unlock($force = false) {
389     }
390
391
392     /**
393      * Close database.
394      */
395     function close () {
396     }
397
398     /**
399      * Synchronize with filesystem.
400      *
401      * This should flush all unwritten data to the filesystem.
402      */
403     function sync() {
404     }
405
406     /**
407      * Optimize the database.
408      */
409     function optimize() {
410     }
411
412     /**
413      * Check database integrity.
414      *
415      * This should check the validity of the internal structure of the database.
416      * Errors should be reported via:
417      * <pre>
418      *   trigger_error("Message goes here.", E_USER_WARNING);
419      * </pre>
420      *
421      * @return boolean True iff database is in a consistent state.
422      */
423     function check() {
424     }
425
426     /**
427      * Put the database into a consistent state.
428      *
429      * This should put the database into a consistent state.
430      * (I.e. rebuild indexes, etc...)
431      *
432      * @return boolean True iff successful.
433      */
434     function rebuild() {
435     }
436
437     function _parse_searchwords($search) {
438         $search = strtolower(trim($search));
439         if (!$search)
440             return array(array(),array());
441         
442         $words = preg_split('/\s+/', $search);
443         $exclude = array();
444         foreach ($words as $key => $word) {
445             if ($word[0] == '-' && $word != '-') {
446                 $word = substr($word, 1);
447                 $exclude[] = preg_quote($word);
448                 unset($words[$key]);
449             }
450         }
451         return array($words, $exclude);
452     }
453
454     /** 
455      * Split the given limit parameter into offset,pagesize. (offset is optional. default: 0)
456      * Duplicate the PageList function here to avoid loading the whole PageList.php 
457      * Usage: 
458      *   list($offset,$pagesize) = $this->limit($args['limit']);
459      */
460     function limit($limit) {
461         if (strstr($limit, ','))
462             return split(',', $limit);
463         else
464             return array(0, $limit);
465     }
466     
467     /** 
468      * Handle sortby requests for the DB iterator and table header links.
469      * Prefix the column with + or - like "+pagename","-mtime", ...
470      * supported actions: 'flip_order' "mtime" => "+mtime" => "-mtime" ...
471      *                    'db'         "-pagename" => "pagename DESC"
472      * In PageList all columns are sortable. (patch by DanFr)
473      * Here with the backend only some, the rest is delayed to PageList.
474      * (some kind of DumbIter)
475      * Duplicate the PageList function here to avoid loading the whole 
476      * PageList.php, and it forces the backend specific sortable_columns()
477      */
478     function sortby ($column, $action) {
479         if (empty($column)) return '';
480         //support multiple comma-delimited sortby args: "+hits,+pagename"
481         if (strstr($column,',')) {
482             $result = array();
483             foreach (explode(',',$column) as $col) {
484                 $result[] = $this->sortby($col,$action);
485             }
486             return join(",",$result);
487         }
488         if (substr($column,0,1) == '+') {
489             $order = '+'; $column = substr($column,1);
490         } elseif (substr($column,0,1) == '-') {
491             $order = '-'; $column = substr($column,1);
492         }
493         // default order: +pagename, -mtime, -hits
494         if (empty($order))
495             if (in_array($column,array('mtime','hits')))
496                 $order = '-';
497             else
498                 $order = '+';
499         if ($action == 'flip_order') {
500             return ($order == '+' ? '-' : '+') . $column;
501         } elseif ($action == 'init') {
502             $this->_sortby[$column] = $order;
503             return $order . $column;
504         } elseif ($action == 'check') {
505             return (!empty($this->_sortby[$column]) or 
506                     ($GLOBALS['request']->getArg('sortby') and 
507                      strstr($GLOBALS['request']->getArg('sortby'),$column)));
508         } elseif ($action == 'db') {
509             // native sort possible?
510             $sortable_columns = $this->sortable_columns();
511             if (in_array($column, $sortable_columns))
512                 // asc or desc: +pagename, -pagename
513                 return $column . ($order == '+' ? ' ASC' : ' DESC');
514             else 
515                 return '';
516         }
517         return '';
518     }
519
520     function sortable_columns() {
521         return array('pagename'/*,'mtime','author_id','author'*/);
522     }
523
524     // quote only strings or do smartquote? add ' or not? (NULL)
525     // ADODB adds surrounding quotes, SQL not yet!
526     function quote ($s) {
527         return $s;
528     }
529
530 };
531
532 /**
533  * Iterator returned by backend methods which (possibly) return
534  * multiple records.
535  *
536  * FIXME: This might be two seperate classes: page_iter and version_iter.
537  * For the versions we have WikiDB_backend_dumb_AllRevisionsIter.
538  */
539 class WikiDB_backend_iterator
540 {
541     /**
542      * Get the next record in the iterator set.
543      *
544      * This returns a hash. The hash may contain the following keys:
545      * <dl>
546      * <dt> pagename <dt> (string) the page name
547      * <dt> version  <dt> (int) the version number
548      * <dt> pagedata <dt> (hash) page meta-data (as returned from backend::get_pagedata().)
549      * <dt> versiondata <dt> (hash) page meta-data (as returned from backend::get_versiondata().)
550      *
551      * If this is a page iterator, it must contain the 'pagename' entry --- the others
552      * are optional.
553      *
554      * If this is a version iterator, the 'pagename', 'version', <strong>and</strong> 'versiondata'
555      * entries are mandatory.  ('pagedata' is optional.)
556      */
557     function next() {
558         trigger_error("virtual", E_USER_ERROR);
559     }
560
561     function count() {
562         return count($this->_pages);
563     }
564
565     /**
566      * Release resources held by this iterator.
567      */
568     function free() {
569     }
570 };
571
572 // For emacs users
573 // Local Variables:
574 // mode: php
575 // tab-width: 8
576 // c-basic-offset: 4
577 // c-hanging-comment-ender-p: nil
578 // indent-tabs-mode: nil
579 // End:
580 ?>