wiki updates

This commit is contained in:
Shish 2009-08-03 11:04:43 +01:00
parent 63be20458d
commit 057ed92e90

View file

@ -16,6 +16,9 @@ class WikiUpdateEvent extends Event {
} }
} }
class WikiUpdateException extends SCoreException {
}
class WikiPage { class WikiPage {
var $id; var $id;
var $owner_id; var $owner_id;
@ -50,7 +53,29 @@ class WikiPage {
class Wiki extends SimpleExtension { class Wiki extends SimpleExtension {
public function onInitExt($event) { public function onInitExt($event) {
$this->setup(); global $database;
global $config;
if($config->get_int("ext_wiki_version", 0) < 1) {
$database->create_table("wiki_pages", "
id SCORE_AIPK,
owner_id INTEGER NOT NULL,
owner_ip SCORE_INET NOT NULL,
date DATETIME DEFAULT NULL,
title VARCHAR(255) NOT NULL,
revision INTEGER NOT NULL DEFAULT 1,
locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
body TEXT NOT NULL,
UNIQUE (title, revision),
FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE
");
$config->set_int("ext_wiki_version", 2);
}
if($config->get_int("ext_wiki_version") < 2) {
$database->Execute("ALTER TABLE wiki_pages ADD COLUMN
locked ENUM('Y', 'N') DEFAULT 'N' NOT NULL AFTER REVISION");
$config->set_int("ext_wiki_version", 2);
}
} }
public function onPageRequest($event) { public function onPageRequest($event) {
@ -116,7 +141,17 @@ class Wiki extends SimpleExtension {
} }
public function onWikiUpdate($event) { public function onWikiUpdate($event) {
$this->set_page($event->user, $event->wikipage); global $database;
$wpage = $event->wikipage;
try {
$row = $database->Execute("
INSERT INTO wiki_pages(owner_id, owner_ip, date, title, revision, locked, body)
VALUES (?, ?, now(), ?, ?, ?, ?)", array($event->user->id, $_SERVER['REMOTE_ADDR'],
$wpage->title, $wpage->rev, $wpage->locked?'Y':'N', $wpage->body));
}
catch(Exception $e) {
throw new WikiUpdateException("Somebody else edited that page at the same time :-(");
}
} }
public function onSetupBuilding($event) { public function onSetupBuilding($event) {
@ -147,32 +182,6 @@ class Wiki extends SimpleExtension {
return false; return false;
} }
private function setup() {
global $database;
global $config;
if($config->get_int("ext_wiki_version", 0) < 1) {
$database->create_table("wiki_pages", "
id SCORE_AIPK,
owner_id INTEGER NOT NULL,
owner_ip SCORE_INET NOT NULL,
date DATETIME DEFAULT NULL,
title VARCHAR(255) NOT NULL,
revision INTEGER NOT NULL DEFAULT 1,
locked SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
body TEXT NOT NULL,
UNIQUE (title, revision),
FOREIGN KEY (owner_id) REFERENCES users(id) ON DELETE CASCADE
");
$config->set_int("ext_wiki_version", 2);
}
if($config->get_int("ext_wiki_version") < 2) {
$database->Execute("ALTER TABLE wiki_pages ADD COLUMN
locked ENUM('Y', 'N') DEFAULT 'N' NOT NULL AFTER REVISION");
$config->set_int("ext_wiki_version", 2);
}
}
private function get_page($title, $revision=-1) { private function get_page($title, $revision=-1) {
global $database; global $database;
// first try and get the actual page // first try and get the actual page
@ -213,14 +222,5 @@ class Wiki extends SimpleExtension {
return new WikiPage($row); return new WikiPage($row);
} }
private function set_page(User $user, WikiPage $wpage) {
global $database;
// FIXME: deal with collisions
$row = $database->Execute("
INSERT INTO wiki_pages(owner_id, owner_ip, date, title, revision, locked, body)
VALUES (?, ?, now(), ?, ?, ?, ?)", array($user->id, $_SERVER['REMOTE_ADDR'],
$wpage->title, $wpage->rev, $wpage->locked?'Y':'N', $wpage->body));
}
} }
?> ?>