From ec537891a3f18af89489062c0907523d3d57cced Mon Sep 17 00:00:00 2001 From: Florent Bruneau Date: Sun, 27 Jul 2008 21:55:35 +0200 Subject: [PATCH] Fixes and improvements to the Wiki engine. Signed-off-by: Florent Bruneau --- classes/plwikipage.php | 100 +++++++++++++++++++++++++++++++++----------- include/wiki.engine.inc.php | 4 +- 2 files changed, 77 insertions(+), 27 deletions(-) diff --git a/classes/plwikipage.php b/classes/plwikipage.php index 20bcf97..6f28191 100644 --- a/classes/plwikipage.php +++ b/classes/plwikipage.php @@ -36,6 +36,7 @@ class PlWikiPage public $name; public $path; + private $content = null; private $perms = null; /** Build a new page from a PmWiki page name (ie NameSpace.Page). @@ -63,27 +64,61 @@ class PlWikiPage 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. @@ -124,24 +159,13 @@ class PlWikiPage */ 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(); } @@ -254,6 +278,10 @@ class PlWikiPage 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); @@ -409,6 +437,28 @@ class PlWikiPage 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: diff --git a/include/wiki.engine.inc.php b/include/wiki.engine.inc.php index 956d39c..c1cbcba 100644 --- a/include/wiki.engine.inc.php +++ b/include/wiki.engine.inc.php @@ -43,14 +43,14 @@ switch (Env::v('action')) { if ($p = Post::v('setrperms')) { $wp->applyPerms('admin'); if ($wp->setPerms($p, $wp->writePerms())) { - $page->trigSuccess('Permissions mises à jour'); + Platal::page()->trigSuccess('Permissions mises à jour'); } } if ($p = Post::v('setwperms')) { $wp->applyPerms('admin'); if ($wp->setPerms($wp->readPerms(), $p)) { - $page->trigSuccess('Permissions mises à jour'); + Platal::page()->trigSuccess('Permissions mises à jour'); } } -- 2.1.4