a bunch of wiki fixes

This commit is contained in:
Shish 2009-07-28 04:49:24 +01:00
parent 2a36504b4f
commit d02c803d60
2 changed files with 48 additions and 48 deletions

View file

@ -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);
}

View file

@ -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 .= "<p>(<a href='".make_link("wiki/wiki:sidebar", "edit=on")."'>Edit</a>)";
@ -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 = "<div class='wiki-page'>";
$html .= $tfe->formatted;
$html .= "<hr>";
$html .= "<p class='wiki-footer'>Revision {$page->revision} by ".
"<a href='".make_link("user/{$owner->name}")."'>{$owner->name}</a> at {$page->date} ";
global $user;
if($this->can_edit($user, $page)) {
$html .= "[<a href='".make_link("wiki/{$page->title}", "edit=on")."'>edit</a>] ";
}
$edit = Wiki::can_edit($user, $page) ?
"[<a href='".make_link("wiki/{$page->title}", "edit=on")."'>edit</a>] " :
"";
$html .= "</p></div>";
return $html;
return "
<div class='wiki-page'>
$tfe->formatted
<hr>
<p class='wiki-footer'>
Revision {$page->revision}
by <a href='".make_link("user/{$owner->name}")."'>{$owner->name}</a>
at {$page->date}
$edit
</p>
</div>
";
}
}
?>