2 rcs_id('$Id: flatfile.php,v 1.1 2007-01-02 13:19:47 rurban Exp $');
5 Copyright 1999,2005,2006 $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 as pure, readable flatfiles,
26 * as with PageDump. All other methods are taken from file, which handles serialized pages.
28 * Author: Reini Urban, based on the file backend by Jochen Kalmbach
31 require_once('lib/WikiDB/backend/file.php');
32 require_once('lib/loadsave.php');
34 class WikiDB_backend_flatfile
35 extends WikiDB_backend_file
37 // *********************************************************************
38 // common file load / save functions:
39 // FilenameForPage is from loadsave.php
40 function _pagename2filename($type, $pagename, $version) {
41 return $this->_dir_names[$type].'/'.FilenameForPage($pagename);
43 return $this->_dir_names[$type].'/'.FilenameForPage($pagename);
45 return $this->_dir_names[$type].'/'.FilenameForPage($pagename).'--'.$version;
49 // Load/Save Page-Data
50 function _loadPageData($pagename) {
51 if ($this->_page_data != NULL) {
52 if ($this->_page_data['pagename'] == $pagename) {
53 return $this->_page_data;
56 //$pd = $this->_loadPage('page_data', $pagename, 0);
58 $filename = $this->_pagename2filename('page_data', $pagename, 0);
59 if (!file_exists($filename)) return NULL;
60 if (!filesize($filename)) return array();
61 if ($fd = @fopen($filename, "rb")) {
62 $locked = flock($fd, 1); // Read lock
64 ExitWiki("Timeout while obtaining lock. Please try again");
66 if ($data = fread($fd, filesize($filename))) {
68 // This is the only difference from file:
69 if ($parts = ParseMimeifiedPages($data)) {
72 //if ($set_pagename == true)
73 $pd['pagename'] = $pagename;
74 //if ($version != 0) $pd['version'] = $version;
76 ExitWiki(sprintf(gettext("'%s': corrupt file"),
77 htmlspecialchars($filename)));
83 $this->_page_data = $pd;
84 if ($this->_page_data != NULL) {
85 if ($this->_page_data['pagename'] == $pagename) {
86 return $this->_page_data;
89 return array(); // no values found
92 function _saveVersionData($pagename, $version, $data) {
93 // check if this is a newer version:
94 if ($this->_getLatestVersion($pagename) < $version) {
95 // write new latest-version-info
96 $this->_setLatestVersion($pagename, $version);
97 $this->_savePageData($pagename, $data);
99 $this->_savePage('ver_data', $pagename, $version, $data);
103 // This is different to file.
104 function _savePageData($pagename, $data) {
108 $filename = $this->_pagename2filename($type, $pagename, $version);
110 // Construct a dummy page_revision object
111 $page = new WikiDB_Page($this->_wikidb, $pagename);
112 if (USECACHE and empty($data['pagedata'])) {
113 $cache =& $this->_wikidb->_cache;
114 if (!empty($cache->_pagedata_cache[$pagename])
115 and is_array($cache->_pagedata_cache[$pagename]))
117 $cachedata = &$cache->_pagedata_cache[$pagename];
118 foreach($data as $key => $val)
119 $cachedata[$key] = $val;
121 $cache->_pagedata_cache[$pagename] = $data;
124 //unset ($data['pagedata']);
125 if (empty($data['versiondata']))
126 $data['versiondata'] = false;
127 $current = new WikiDB_PageRevision($wikidb, $pagename, $version, $data['versiondata']);
128 unset ($data['versiondata']);
129 /*if (!empty($data) and is_array($data))
130 foreach ($data as $k => $v) {
131 $current->_data["%$k"] = $v;
134 $pagedata = "Date: " . Rfc2822DateTime($current->get('mtime')) . "\r\n";
135 $pagedata .= sprintf("Mime-Version: 1.0 (Produced by PhpWiki %s)\r\n",
137 $pagedata .= MimeifyPageRevision($page, $current);
139 $len = strlen($pagedata);
140 if ($fd = fopen($filename, 'a+b')) {
141 $locked = flock($fd, 2); // Exclusive blocking lock
143 ExitWiki("Timeout while obtaining lock. Please try again");
147 $num = fwrite($fd, $pagedata, $len);
148 assert($num == $len);
151 ExitWiki("Error while writing page '$pagename'");
156 //class WikiDB_backend_flatfile_iter extends WikiDB_backend_file_iter {};
158 // $Log: not supported by cvs2svn $
166 // c-hanging-comment-ender-p: nil
167 // indent-tabs-mode: nil