public $name;
public $path;
+ private $content = null;
private $perms = null;
/** Build a new page from a PmWiki page name (ie NameSpace.Page).
return self::workDir() . '/cache_' . $this->name . '.tpl';
}
- /** Fetch the permissions.
+ /** Fetch the content of the wiki page.
*/
- private function fetchPerms()
+ private function fetchContent()
{
- if (!is_null($this->perms)) {
+ if (!is_null($this->content)) {
return;
}
$file = $this->filename();
- if (!file_exists($file)) {
- $this->perms = self::$defaulPerms;
+ if (!is_file($file)) {
return;
}
$lines = explode("\n", file_get_contents($file));
+ $this->content = array();
foreach ($lines as $line) {
@list($k, $v) = explode('=', $line, 2);
- if ($k == 'platal_perms') {
- $this->perms = explode(':', $v);
- return;
- }
+ $this->content[$k] = $v;
+ }
+ }
+
+ /** Save the content of the wiki page based on the
+ * fetched content.
+ */
+ private function saveContent()
+ {
+ if (is_null($this->content)) {
+ return false;
+ }
+ $lines = array();
+ foreach ($this->content as $k => $v) {
+ $lines[] = "$k=$v";
+ }
+ return file_put_contents($this->filename(), implode("\n", $lines)) !== false;
+ }
+
+ /** Get a field from the wiki content.
+ */
+ public function getField($name)
+ {
+ $this->fetchContent();
+ return @$this->content[$name];
+ }
+
+ /** Fetch the permissions.
+ */
+ private function fetchPerms()
+ {
+ if (!is_null($this->perms)) {
+ return;
+ }
+ $this->fetchContent();
+ if (isset($this->content['platal_perms'])) {
+ $this->perms = explode(':', $this->content['platal_perms']);
+ } else {
+ $this->perms = self::$defaulPerms;
}
- $this->perms = self::$defaulPerms;
}
/** Return read perms.
*/
public function setPerms($read, $write)
{
- $file = $this->filename();
- if (!file_exists($file)) {
+ $this->fetchContent();
+ if (is_null($this->content)) {
return false;
}
-
- $p = $read . ':' . $write;
- $lines = explode("\n", file_get_contents($file));
- foreach ($lines as $i => $line) {
- list($k, $v) = explode('=', $line, 2);
- if ($k == 'platal_perms') {
- unset($lines[$i]);
- break;
- }
- }
- array_splice($lines, 1, 0, array('platal_perms=' . $p));
- file_put_contents($file, join("\n", $lines));
+ $this->content['platal_perms'] = $read . ':' . $write;
$this->perms = array($read, $write);
- return true;
+ return $this->saveContent();
}
public function rename($newname, $changeLinks = true)
{
$newpage = new PlWikiPage(str_replace('/', '.', $newname));
+ $cache = $this->cacheFilename();
+ if (is_file($cache)) {
+ unlink($cache);
+ }
list($groupname, ) = explode('.', $this->name);
list($newgroupname, ) = explode('.', $newpage->name);
pl_redirect($a . '/' . $b);
}
+
+ /** List wiki pages.
+ */
+ public static function listPages()
+ {
+ $wiki_pages = array();
+ $dir = PlWikiPage::workDir();
+ if (is_dir($dir)) {
+ if ($dh = opendir($dir)) {
+ while (($file = readdir($dh)) !== false) {
+ if ($file{0} >= 'A' && $file{0} <= 'Z') {
+ $wp = new PlWikiPage($file);
+ $wiki_pages[$file] = array('read' => $wp->readPerms(),
+ 'edit' => $wp->writePerms(),
+ 'cached' => is_file($wp->cacheFilename()));
+ }
+ }
+ closedir($dh);
+ }
+ }
+ return $wiki_pages;
+ }
}
// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: