From d02c803d605a419d9c747aa19fbe2961049cd358 Mon Sep 17 00:00:00 2001 From: Shish Date: Tue, 28 Jul 2009 04:49:24 +0100 Subject: [PATCH] a bunch of wiki fixes --- contrib/wiki/main.php | 58 +++++++++++++++++++++++------------------- contrib/wiki/theme.php | 38 ++++++++++++--------------- 2 files changed, 48 insertions(+), 48 deletions(-) diff --git a/contrib/wiki/main.php b/contrib/wiki/main.php index 7db31e35..cb9fe833 100644 --- a/contrib/wiki/main.php +++ b/contrib/wiki/main.php @@ -26,22 +26,20 @@ class WikiPage { var $locked; var $body; - public function WikiPage($row=null) { - if(!is_null($row)) { - $this->id = $row['id']; - $this->owner_id = $row['owner_id']; - $this->owner_ip = $row['owner_ip']; - $this->date = $row['date']; - $this->title = $row['title']; - $this->revision = $row['revision']; - $this->locked = ($row['locked'] == 'Y'); - $this->body = $row['body']; - } + public function WikiPage($row) { + assert(!empty($row)); + + $this->id = $row['id']; + $this->owner_id = $row['owner_id']; + $this->owner_ip = $row['owner_ip']; + $this->date = $row['date']; + $this->title = $row['title']; + $this->revision = $row['revision']; + $this->locked = ($row['locked'] == 'Y'); + $this->body = $row['body']; } public function get_owner() { - global $config; - global $database; return User::by_id($this->owner_id); } @@ -75,11 +73,10 @@ class Wiki extends SimpleExtension { $lock = isset($_POST['lock']) && ($_POST['lock'] == "on"); if($this->can_edit($user, $this->get_page($title))) { - $wikipage = new WikiPage(); - $wikipage->title = $title; + $wikipage = $this->get_page($title); $wikipage->rev = $rev; $wikipage->body = $body; - $wikipage->lock = $user->is_admin() ? $lock : false; + $wikipage->locked = $user->is_admin() ? $lock : false; send_event(new WikiUpdateEvent($user, $wikipage)); $u_title = url_escape($title); @@ -115,13 +112,24 @@ class Wiki extends SimpleExtension { $event->panel->add_block($sb); } - private function can_edit($user, $page) { + /** + * See if the given user is allowed to edit the given page + * + * @retval boolean + */ + public static function can_edit(User $user, WikiPage $page) { global $config; - if(!is_null($page) && $page->is_locked() && !$user->is_admin()) return false; + // admins can edit everything + if($user->is_admin()) return true; + + // anon / user can't ever edit locked pages + if($page->is_locked()) return false; + + // anon / user can edit if allowed by config if($config->get_bool("wiki_edit_anon", false) && $user->is_anonymous()) return true; if($config->get_bool("wiki_edit_user", false) && !$user->is_anonymous()) return true; - if($user->is_admin()) return true; + return false; } @@ -161,7 +169,7 @@ class Wiki extends SimpleExtension { ORDER BY revision DESC", array($title)); // fall back to wiki:default - if(is_null($row)) { + if(empty($row)) { $row = $database->db->GetRow(" SELECT * FROM wiki_pages @@ -169,17 +177,15 @@ class Wiki extends SimpleExtension { ORDER BY revision DESC", "wiki:default"); // fall further back to manual - if(is_null($row)) { + if(empty($row)) { $row = array( "id" => -1, "owner_ip" => "0.0.0.0", "date" => "", "revision" => 0, "locked" => false, - "body" => " - This is a default page for when a page is empty, - it can be edited by editing [[wiki:default]]. - ", + "body" => "This is a default page for when a page is empty, ". + "it can be edited by editing [[wiki:default]].", ); } @@ -189,7 +195,7 @@ class Wiki extends SimpleExtension { $row["owner_id"] = $config->get_int("anon_id", 0); } - assert(!is_null($row)); + assert(!empty($row)); return new WikiPage($row); } diff --git a/contrib/wiki/theme.php b/contrib/wiki/theme.php index 0aa3db39..23a6d511 100644 --- a/contrib/wiki/theme.php +++ b/contrib/wiki/theme.php @@ -17,6 +17,7 @@ class WikiTheme extends Themelet { $tfe = new TextFormattingEvent($nav_page->body); send_event($tfe); + // only the admin can edit the sidebar global $user; if($user->is_admin()) { $tfe->formatted .= "

(Edit)"; @@ -36,16 +37,6 @@ class WikiTheme extends Themelet { $page->add_block(new Block("Editor", $this->create_edit_html($wiki_page))); } - protected function can_edit(User $user, WikiPage $page) { - global $config; - - if(!is_null($page) && $page->is_locked() && !$user->is_admin()) return false; - if($config->get_bool("wiki_edit_anon", false) && $user->is_anonymous()) return true; - if($config->get_bool("wiki_edit_user", false) && !$user->is_anonymous()) return true; - if($user->is_admin()) return true; - return false; - } - protected function create_edit_html(WikiPage $page) { $h_title = html_escape($page->title); $u_title = url_escape($page->title); @@ -76,20 +67,23 @@ class WikiTheme extends Themelet { $tfe = new TextFormattingEvent($page->body); send_event($tfe); - $html = "

"; - $html .= $tfe->formatted; - $html .= "
"; - $html .= "
"; - - return $html; + return " +
+ $tfe->formatted +
+ +
+ "; } } ?>