5 Copyright 1999, 2000, 2001, 2002, 2003 $ThePhpWikiProgrammingTeam
7 This file is part of PhpWiki.
9 PhpWiki is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 PhpWiki is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with PhpWiki; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Backend for handling file storage.
27 * Author: Jochen Kalmbach, Jochen@kalmbachnet.de
32 * - Implement "optimize" / "sync" / "check" / "rebuild"
33 * - Optimize "get_previous_version"
34 * - Optimize "get_links" (reversed = true)
35 * - Optimize "get_all_revisions"
36 * - Optimize "most_popular" (separate file for "hitcount",
37 * which contains all pages)
38 * - Optimize "most_recent"
39 * - What should be done in "lock"/"unlock"/"close" ?
40 * - "WikiDB_backend_file_iter": Do I need to return 'version' and 'versiondata' ?
44 require_once('lib/WikiDB/backend.php');
45 require_once('lib/ErrorManager.php');
47 class WikiDB_backend_file
48 extends WikiDB_backend
53 var $_page_data; // temporarily stores the pagedata (via _loadPageData)
54 var $_page_version_data; // temporarily stores the versiondata (via _loadVersionData)
55 var $_latest_versions; // temporarily stores the latest version-numbers (for every pagename)
57 function WikiDB_backend_file( $dbparam )
59 $this->data_dir = $dbparam['directory'];
60 if (file_exists($this->data_dir) and is_file($this->data_dir))
61 unlink($this->data_dir);
62 if (is_dir($this->data_dir) == false) {
63 mkdir($this->data_dir, 0755);
67 = array('ver_data' => $this->data_dir.'/'.'ver_data',
68 'page_data' => $this->data_dir.'/'.'page_data',
69 'latest_ver' => $this->data_dir.'/'.'latest_ver',
70 'links' => $this->data_dir.'/'.'links' );
72 foreach ($this->_dir_names as $key => $val) {
73 if (file_exists($val) and is_file($val))
75 if (is_dir($val) == false)
79 $this->_page_data = NULL;
80 $this->_page_version_data = NULL;
81 $this->_latest_versions = NULL;
86 // *********************************************************************
87 // common file load / save functions:
88 function _pagename2filename($type, $pagename, $version) {
90 return $this->_dir_names[$type].'/'.urlencode($pagename);
92 return $this->_dir_names[$type].'/'.urlencode($pagename).'--'.$version;
95 function _loadPage($type, $pagename, $version, $set_pagename = true) {
96 $filename = $this->_pagename2filename($type, $pagename, $version);
97 if (!file_exists($filename)) return NULL;
98 if (!filesize($filename)) return array();
99 if ($fd = @fopen($filename, "rb")) {
100 $locked = flock($fd, 1); # Read lock
102 ExitWiki("Timeout while obtaining lock. Please try again");
104 if ($data = fread($fd, filesize($filename))) {
105 $pd = unserialize($data);
106 if ($set_pagename == true)
107 $pd['pagename'] = $pagename;
109 $pd['version'] = $version;
111 ExitWiki(sprintf(gettext("'%s': corrupt file"),
112 htmlspecialchars($filename)));
121 function _savePage($type, $pagename, $version, $data) {
122 $filename = $this->_pagename2filename($type, $pagename, $version);
123 if($fd = fopen($filename, 'a+b')) {
124 $locked = flock($fd,2); // Exclusive blocking lock
126 ExitWiki("Timeout while obtaining lock. Please try again");
131 $pagedata = serialize($data);
132 $len = strlen($pagedata);
133 $num = fwrite($fd, $pagedata, $len);
134 assert($num == $len);
137 ExitWiki("Error while writing page '$pagename'");
141 function _removePage($type, $pagename, $version) {
142 $filename = $this->_pagename2filename($type, $pagename, $version);
143 if (!file_exists($filename)) return NULL;
144 $f = @unlink($filename);
146 trigger_error("delete file failed: ".$filename." ver: ".$version, E_USER_WARNING);
149 // *********************************************************************
151 // *********************************************************************
152 // Load/Save Version-Data
153 function _loadVersionData($pagename, $version) {
154 if ($this->_page_version_data != NULL) {
155 if ( ($this->_page_version_data['pagename'] == $pagename) &&
156 ($this->_page_version_data['version'] == $version) ) {
157 return $this->_page_version_data;
160 $vd = $this->_loadPage('ver_data', $pagename, $version);
162 $this->_page_version_data = $vd;
163 if ( ($this->_page_version_data['pagename'] == $pagename) &&
164 ($this->_page_version_data['version'] == $version) ) {
165 return $this->_page_version_data;
171 function _saveVersionData($pagename, $version, $data) {
172 $this->_savePage('ver_data', $pagename, $version, $data);
174 // check if this is a newer version:
175 if ($this->_getLatestVersion($pagename) < $version) {
176 // write new latest-version-info
177 $this->_setLatestVersion($pagename, $version);
182 // *********************************************************************
183 // Load/Save Page-Data
184 function _loadPageData($pagename) {
185 if ($this->_page_data != NULL) {
186 if ($this->_page_data['pagename'] == $pagename) {
187 return $this->_page_data;
190 $pd = $this->_loadPage('page_data', $pagename, 0);
192 $this->_page_data = $pd;
193 if ($this->_page_data != NULL) {
194 if ($this->_page_data['pagename'] == $pagename) {
195 return $this->_page_data;
198 return array(); // no values found
201 function _savePageData($pagename, $data) {
202 $this->_savePage('page_data', $pagename, 0, $data);
205 // *********************************************************************
206 // Load/Save Latest-Version
207 function _saveLatestVersions() {
208 $data = $this->_latest_versions;
211 $this->_savePage('latest_ver', 'latest_versions', 0, $data);
214 function _setLatestVersion($pagename, $version) {
215 // make sure the page version list is loaded:
216 $this->_getLatestVersion($pagename);
218 $this->_getLatestVersion($pagename);
219 $this->_latest_versions[$pagename] = $version;
222 // Remove this page from the Latest-Version-List:
223 unset($this->_latest_versions[$pagename]);
225 $this->_saveLatestVersions();
228 function _loadLatestVersions() {
229 if ($this->_latest_versions != NULL)
232 $pd = $this->_loadPage('latest_ver', 'latest_versions', 0, false);
234 $this->_latest_versions = $pd;
236 $this->_latest_versions = array(); // empty array
239 function _getLatestVersion($pagename) {
240 $this->_loadLatestVersions();
241 if (array_key_exists($pagename, $this->_latest_versions) == false)
242 return 0; // do version exists
243 return $this->_latest_versions[$pagename];
247 // *********************************************************************
248 // Load/Save Page-Links
249 function _loadPageLinks($pagename) {
250 $pd = $this->_loadPage('links', $pagename, 0, false);
253 return array(); // no values found
256 function _savePageLinks($pagename, $links) {
257 $this->_savePage('links', $pagename, 0, $links);
263 * Get page meta-data from database.
265 * @param $pagename string Page name.
267 * Returns a hash containing the page meta-data.
268 * Returns an empty array if there is no meta-data for the requested page.
269 * Keys which might be present in the hash are:
271 * <dt> locked <dd> If the page is locked.
272 * <dt> hits <dd> The page hit count.
273 * <dt> created <dd> Unix time of page creation. (FIXME: Deprecated: I
274 * don't think we need this...)
277 function get_pagedata($pagename) {
278 return $this->_loadPageData($pagename);
282 * Update the page meta-data.
284 * Set page meta-data.
286 * Only meta-data whose keys are preset in $newdata is affected.
290 * $backend->update_pagedata($pagename, array('locked' => 1));
292 * will set the value of 'locked' to 1 for the specified page, but it
293 * will not affect the value of 'hits' (or whatever other meta-data
294 * may have been stored for the page.)
296 * To delete a particular piece of meta-data, set it's value to false.
298 * $backend->update_pagedata($pagename, array('locked' => false));
301 * @param $pagename string Page name.
302 * @param $newdata hash New meta-data.
305 * This will create a new page if page being requested does not
308 function update_pagedata($pagename, $newdata) {
309 $data = $this->get_pagedata($pagename);
310 if (count($data) == 0) {
311 $this->_savePageData($pagename, $newdata); // create a new pagedata-file
315 foreach ($newdata as $key => $val) {
321 $this->_savePageData($pagename, $data); // write new pagedata-file
326 * Get the current version number for a page.
328 * @param $pagename string Page name.
329 * @return int The latest version number for the page. Returns zero if
330 * no versions of a page exist.
332 function get_latest_version($pagename) {
333 return $this->_getLatestVersion($pagename);
337 * Get preceding version number.
339 * @param $pagename string Page name.
340 * @param $version int Find version before this one.
341 * @return int The version number of the version in the database which
342 * immediately preceeds $version.
344 * FIXED: Check if this version really exists!
346 function get_previous_version($pagename, $version) {
347 $prev = ($version > 0 ? $version - 1 : 0);
348 while ($prev and !file_exists($this->_pagename2filename('ver_data', $pagename, $prev))) {
355 * Get revision meta-data and content.
357 * @param $pagename string Page name.
358 * @param $version integer Which version to get.
359 * @param $want_content boolean
360 * Indicates the caller really wants the page content. If this
361 * flag is not set, the backend is free to skip fetching of the
362 * page content (as that may be expensive). If the backend omits
363 * the content, the backend might still want to set the value of
364 * '%content' to the empty string if it knows there's no content.
366 * @return hash The version data, or false if specified version does not
369 * Some keys which might be present in the $versiondata hash are:
372 * <dd> This is a pseudo-meta-data element (since it's actually
373 * the page data, get it?) containing the page content.
374 * If the content was not fetched, this key may not be present.
376 * For description of other version meta-data see WikiDB_PageRevision::get().
377 * @see WikiDB_PageRevision::get
379 function get_versiondata($pagename, $version, $want_content = false) {
380 $vd = $this->_loadVersionData($pagename, $version);
387 * Rename all files for this page
389 * @access protected Via WikiDB
391 function rename_page($pagename, $to) {
392 $version = _getLatestVersion($pagename);
393 foreach ($this->_dir_names as $type => $path) {
395 $filename = $this->_pagename2filename($type, $pagename, $version);
396 $new = $this->_pagename2filename($type, $to, $version);
397 @rename($filename,$new);
400 $this->update_pagedata($pagename, array('pagename' => $to));
405 * See ADODB for a better delete_page(), which can be undone and is seen in RecentChanges.
407 function delete_page($pagename) {
408 $this->purge_page($pagename);
412 * Delete page from the database.
414 * Delete page (and all it's revisions) from the database.
416 * @param $pagename string Page name.
418 function purge_page($pagename) {
419 $ver = $this->get_latest_version($pagename);
421 $this->_removePage('ver_data', $pagename, $ver);
422 $ver = $this->get_previous_version($pagename, $ver);
424 $this->_removePage('page_data', $pagename, 0);
425 $this->_removePage('links', $pagename, 0);
426 // remove page from latest_version...
427 $this->_setLatestVersion($pagename, 0);
431 * Delete an old revision of a page.
433 * Note that one is never allowed to delete the most recent version,
434 * but that this requirement is enforced by WikiDB not by the backend.
436 * In fact, to be safe, backends should probably allow the deletion of
437 * the most recent version.
439 * @param $pagename string Page name.
440 * @param $version integer Version to delete.
442 function delete_versiondata($pagename, $version) {
443 if ($this->get_latest_version($pagename) == $version) {
444 // try to delete the latest version!
445 // so check if an older version exist:
446 if ($this->get_versiondata($pagename,
447 $this->get_previous_version($pagename, $version),
449 // there is no older version....
450 // so the completely page will be removed:
451 $this->delete_page($pagename);
455 $this->_removePage('ver_data', $pagename, $version);
459 * Create a new page revision.
461 * If the given ($pagename,$version) is already in the database,
462 * this method completely overwrites any stored data for that version.
464 * @param $pagename string Page name.
465 * @param $version int New revisions content.
466 * @param $data hash New revision metadata.
468 * @see get_versiondata
470 function set_versiondata($pagename, $version, $data) {
471 $this->_saveVersionData($pagename, $version, $data);
475 * Update page version meta-data.
477 * If the given ($pagename,$version) is already in the database,
478 * this method only changes those meta-data values whose keys are
479 * explicity listed in $newdata.
481 * @param $pagename string Page name.
482 * @param $version int New revisions content.
483 * @param $newdata hash New revision metadata.
484 * @see set_versiondata, get_versiondata
486 function update_versiondata($pagename, $version, $newdata) {
487 $data = $this->get_versiondata($pagename, $version, true);
492 foreach ($newdata as $key => $val) {
498 $this->set_versiondata($pagename, $version, $data);
502 * Set links for page.
504 * @param $pagename string Page name.
506 * @param $links array List of page(names) which page links to.
508 function set_links($pagename, $links) {
509 $this->_savePageLinks($pagename, $links);
513 * Find pages which link to or are linked from a page.
515 * @param $pagename string Page name.
516 * @param $reversed boolean True to get backlinks.
518 * FIXME: array or iterator?
519 * @return object A WikiDB_backend_iterator.
521 function get_links($pagename, $reversed=true, $include_empty=false,
522 $sortby='', $limit='', $exclude='',
523 $want_relations=false)
525 if ($reversed == false)
526 return new WikiDB_backend_file_iter($this, $this->_loadPageLinks($pagename));
528 $this->_loadLatestVersions();
529 $pagenames = $this->_latest_versions; // now we have an array with the key is the pagename of all pages
531 $out = array(); // create empty out array
533 foreach ($pagenames as $key => $val) {
534 $links = $this->_loadPageLinks($key);
535 foreach ($links as $key2 => $val2) {
536 if ($val2['linkto'] == $pagename)
537 array_push($out, $key);
540 return new WikiDB_backend_file_iter($this, $out);
544 * Get all revisions of a page.
546 * @param $pagename string The page name.
547 * @return object A WikiDB_backend_iterator.
550 function get_all_revisions($pagename) {
551 include_once('lib/WikiDB/backend/dumb/AllRevisionsIter.php');
552 return new WikiDB_backend_dumb_AllRevisionsIter($this, $pagename);
557 * Get all pages in the database.
559 * Pages should be returned in alphabetical order if that is
564 * @param $include_defaulted boolean
565 * If set, even pages with no content will be returned
566 * --- but still only if they have at least one revision (not
567 * counting the default revision 0) entered in the database.
569 * Normally pages whose current revision has empty content
570 * are not returned as these pages are considered to be
573 * @return object A WikiDB_backend_iterator.
575 function get_all_pages($include_empty=false, $sortby='', $limit='', $exclude='') {
576 require_once("lib/PageList.php");
577 $this->_loadLatestVersions();
578 $a = array_keys($this->_latest_versions);
580 return new WikiDB_backend_file_iter($this, $a);
581 $sortby = $this->sortby($sortby, 'db', $this->sortable_columns());
584 case 'pagename ASC': sort($a); break;
585 case 'pagename DESC': rsort($a); break;
587 return new WikiDB_backend_file_iter($this, $a);
590 function sortable_columns() {
591 return array('pagename');
594 function numPages($filter=false, $exclude='') {
595 $this->_loadLatestVersions();
596 return count($this->_latest_versions);
600 * Lock backend database.
602 * Calls may be nested.
604 * @param $write_lock boolean Unless this is set to false, a write lock
605 * is acquired, otherwise a read lock. If the backend doesn't support
606 * read locking, then it should make a write lock no matter which type
607 * of lock was requested.
609 * All backends <em>should</em> support write locking.
611 function lock($write_lock = true) {
612 //trigger_error("lock: Not Implemented", E_USER_WARNING);
616 * Unlock backend database.
618 * @param $force boolean Normally, the database is not unlocked until
619 * unlock() is called as many times as lock() has been. If $force is
620 * set to true, the the database is unconditionally unlocked.
622 function unlock($force = false) {
623 //trigger_error("unlock: Not Implemented", E_USER_WARNING);
631 //trigger_error("close: Not Implemented", E_USER_WARNING);
635 * Synchronize with filesystem.
637 * This should flush all unwritten data to the filesystem.
640 //trigger_error("sync: Not Implemented", E_USER_WARNING);
644 * Optimize the database.
646 function optimize() {
647 return 0;//trigger_error("optimize: Not Implemented", E_USER_WARNING);
651 * Check database integrity.
653 * This should check the validity of the internal structure of the database.
654 * Errors should be reported via:
656 * trigger_error("Message goes here.", E_USER_WARNING);
659 * @return boolean True iff database is in a consistent state.
662 //trigger_error("check: Not Implemented", E_USER_WARNING);
666 * Put the database into a consistent state.
668 * This should put the database into a consistent state.
669 * (I.e. rebuild indexes, etc...)
671 * @return boolean True iff successful.
674 //trigger_error("rebuild: Not Implemented", E_USER_WARNING);
677 function _parse_searchwords($search) {
678 $search = strtolower(trim($search));
680 return array(array(),array());
682 $words = preg_split('/\s+/', $search);
684 foreach ($words as $key => $word) {
685 if ($word[0] == '-' && $word != '-') {
686 $word = substr($word, 1);
687 $exclude[] = preg_quote($word);
691 return array($words, $exclude);
696 class WikiDB_backend_file_iter extends WikiDB_backend_iterator
698 function WikiDB_backend_file_iter(&$backend, &$query_result) {
699 $this->_backend = &$backend;
700 $this->_result = $query_result;
702 if (count($this->_result) > 0)
703 reset($this->_result);
709 if (count($this->_result) <= 0)
712 $e = each($this->_result);
718 if (is_array($pn) and isset($pn['linkto'])) { // support relation link iterator
721 $pagedata = $this->_backend->get_pagedata($pn);
722 // don't pass _cached_html via iterators
723 if (isset($pagedata['_cached_html']))
724 unset($pagedata['_cached_html']);
725 unset($pagedata['pagename']);
726 $rec = array('pagename' => $pn,
727 'pagedata' => $pagedata);
728 if (is_array($e[1])) {
729 $rec['linkrelation'] = $e[1]['relation'];
731 //$rec['version'] = $backend->get_latest_version($pn);
732 //$rec['versiondata'] = $backend->get_versiondata($pn, $rec['version'], true);
736 reset($this->_result);
737 return $this->_result;
740 return count($this->_result);
743 $this->_result = array();
752 // c-hanging-comment-ender-p: nil
753 // indent-tabs-mode: nil