]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiDB/backend/flatfile.php
new flatfile backend (readable page files)
[SourceForge/phpwiki.git] / lib / WikiDB / backend / flatfile.php
1 <?php // -*-php-*-
2 rcs_id('$Id: flatfile.php,v 1.1 2007-01-02 13:19:47 rurban Exp $');
3
4 /**
5  Copyright 1999,2005,2006 $ThePhpWikiProgrammingTeam
6
7  This file is part of PhpWiki.
8
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.
13
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.
18
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
22  */
23
24 /**
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.
27  *
28  * Author: Reini Urban, based on the file backend by Jochen Kalmbach
29  */
30
31 require_once('lib/WikiDB/backend/file.php');
32 require_once('lib/loadsave.php');
33
34 class WikiDB_backend_flatfile
35 extends WikiDB_backend_file
36 {
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);
42 /*       if ($version == 0)
43              return $this->_dir_names[$type].'/'.FilenameForPage($pagename);
44          else
45              return $this->_dir_names[$type].'/'.FilenameForPage($pagename).'--'.$version;
46 */             
47     }
48
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;
54              }
55        }
56         //$pd = $this->_loadPage('page_data', $pagename, 0);
57         
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
63            if (!$locked) { 
64                ExitWiki("Timeout while obtaining lock. Please try again"); 
65            }
66            if ($data = fread($fd, filesize($filename))) {
67
68                // This is the only difference from file:
69                if ($parts = ParseMimeifiedPages($data)) {
70                    $pd = $parts[0];
71                }
72                //if ($set_pagename == true)
73                $pd['pagename'] = $pagename;
74                //if ($version != 0) $pd['version'] = $version;
75                if (!is_array($pd))
76                    ExitWiki(sprintf(gettext("'%s': corrupt file"),
77                                     htmlspecialchars($filename)));
78            }
79            fclose($fd);
80        }
81         
82        if ($pd != NULL)
83             $this->_page_data = $pd;
84        if ($this->_page_data != NULL) {
85             if ($this->_page_data['pagename'] == $pagename) {
86                 return $this->_page_data;
87              }
88        }
89        return array();  // no values found
90     }
91     
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);
98         } else {
99             $this->_savePage('ver_data', $pagename, $version, $data);
100         }
101     }
102     
103     // This is different to file.
104     function _savePageData($pagename, $data) {
105
106         $type = 'page_data';
107         $version = 1;
108         $filename = $this->_pagename2filename($type, $pagename, $version);
109
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])) 
116             {
117                 $cachedata = &$cache->_pagedata_cache[$pagename];
118                 foreach($data as $key => $val)
119                     $cachedata[$key] = $val;
120             } else {
121                 $cache->_pagedata_cache[$pagename] = $data;
122             }
123         }
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;
132           }
133         */
134         $pagedata = "Date: " . Rfc2822DateTime($current->get('mtime')) . "\r\n";
135         $pagedata .= sprintf("Mime-Version: 1.0 (Produced by PhpWiki %s)\r\n",
136                          PHPWIKI_VERSION);
137         $pagedata .= MimeifyPageRevision($page, $current);
138         
139         $len = strlen($pagedata);
140         if ($fd = fopen($filename, 'a+b')) {
141             $locked = flock($fd, 2); // Exclusive blocking lock 
142             if (!$locked) { 
143                 ExitWiki("Timeout while obtaining lock. Please try again"); 
144             }
145             rewind($fd);
146             ftruncate($fd, 0);
147             $num = fwrite($fd, $pagedata, $len); 
148             assert($num == $len);
149             fclose($fd);
150         } else {
151             ExitWiki("Error while writing page '$pagename'");
152         }
153     }
154 };
155
156 //class WikiDB_backend_flatfile_iter extends WikiDB_backend_file_iter {};
157
158 // $Log: not supported by cvs2svn $
159 //
160
161 // For emacs users
162 // Local Variables:
163 // mode: php
164 // tab-width: 8
165 // c-basic-offset: 4
166 // c-hanging-comment-ender-p: nil
167 // indent-tabs-mode: nil
168 // End:
169 ?>