Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

implement a save/load functionality. Instead of doing something cutes…

…y like saving to a wikipage in plain-text, there is a new DB table to store these records in. The UI needs lots of work but the functionality is there
  • Loading branch information...
commit b562b4603b11285690f3a31d258460b056d5912b 1 parent 8bed8e1
@Whiteknight authored
View
2  BookDesigner.i18n.php
@@ -45,6 +45,8 @@
'bookdesigner-optheaderstyle' => 'Headers:',
'bookdesigner-optautogenerate' => 'Autogenerate',
'bookdesigner-publishbutton' => 'Publish Book!',
+ 'bookdesigner-savebutton' => 'Save',
+ 'bookdesigner-loadbutton' => 'Load',
'bookdesigner-defaultheader' => <<<EOHEADER
<div style="border: 1px solid #AAAAAA; background-color: #F8F8F8; padding: 5px; margin: auto; width: 95%">
<center>
View
13 BookDesigner.parser.php
@@ -40,6 +40,8 @@ function getSectionHeadWikiText($name) {
}
function getPageHeadText($isroot) {
+ if ($this->options == null)
+ return "";
$text = $this->getHeaderTemplateTag() . "\n\n";
return $text;
}
@@ -118,6 +120,17 @@ function endPage() {
return $old;
}
+ function parseTitlePageOnly($text) {
+ $lines = explode("\n", $text);
+ for ($i = 0; $i < sizeof($lines); $i++) {
+ $line = $lines[$i];
+ if (preg_match("/<page name='([^']+)' children='(\d+)'>/", $line, $matches)) {
+ $name = $matches[1];
+ $this->startPage($name, $name, $matches[2]);
+ return;
+ }
+ }
+ }
# Home-brewed XML parser. I'm not familiar with any PHP XML libraries or
# utilities, and I don't know what features the MediaWiki server is going
View
8 BookDesigner.sql
@@ -0,0 +1,8 @@
+CREATE TABLE `bookdesigner_outlines` (
+ `id` int(10) unsigned NOT NULL auto_increment,
+ `user_id` int(10) unsigned NOT NULL,
+ `savedate` datetime NOT NULL,
+ `bookname` tinytext NOT NULL,
+ `outline` mediumtext NOT NULL,
+ PRIMARY KEY (`id`)
+) ENGINE=MyISAM DEFAULT CHARSET=latin1;
View
136 BookDesigner_body.php
@@ -50,45 +50,111 @@ function execute($par) {
}
$this->loadJSAndCSS();
-
$mode = "outline";
- $title = null;
+ $outlineid = 0;
if(isset($par)) {
$parts = explode('/', $par, 2);
$mode = $parts[0];
- #$title = $parts[1];
+ if (count($parts) > 1)
+ $outlineid = $parts[1];
}
if($wgRequest->wasPosted()) {
if ($mode == 'verify') {
$submit = $wgRequest->getVal("btnSubmit");
- if ($submit == $this->getMessage("publishbutton")) {
+ if ($submit == $this->GetMessage("publishbutton")) {
$this->options->getOptions();
$this->verifyPublishOutline();
- }
- }
- else if ($mode == 'publish') {
- $this->reallyPublishOutline();
- }
- else {
- $this->unknownModeError('post', $mode, $title);
+ } else if ($submit == $this->GetMessage("savebutton"))
+ $this->saveOutline();
}
+ else if ($mode == 'publish') $this->reallyPublishOutline();
+ else $this->unknownModeError('post', $mode, $title);
}
else {
# TODO: we've specified a book name, load that book into the outline
# $mode == 'outline' creates an empty outline with "title"
# $mode == 'preload' attempts to load an existing outline
if (!isset($mode) || $mode == "" || $mode == "outline" || $mode == "preload") {
- $this->displayMainOutline($mode, $title);
+ $this->displayMainOutline("");
return;
}
- else {
- $this->unknownModeError('show', $mode, $title);
- }
+ else if ($mode == 'loadoutline') $this->loadOutline($outlineid);
+ else if ($mode == 'deleteoutline') $this->deleteOutline($outlineid);
+ else $this->unknownModeError('show', $mode, $title);
+ }
+ }
+ function loadOutline($outlineid) {
+ $dbr = wfGetDB(DB_SLAVE);
+ $res = $dbr->select('bookdesigner_outlines', array("user_id", "outline"), 'id=' . $outlineid);
+ if ($dbr->numRows($res) == 1) {
+ $row = $dbr->fetchObject($res);
+ $this->displayMainOutline($row->outline);
+ }
+ else {
+ $text = <<<EOD
+<div>
+ Could not load outline.
+ <br />
+ <a href="{$wgScriptPath}/index.php?title=Special:BookDesigner">Back</a>
+</div>
+EOD;
+ $wgOut->addHTML($text);
}
}
+ function deleteOutline($outlineid) {
+ global $wgUser, $wgScriptPath, $wgOut;
+ $dbw = wfGetDB(DB_MASTER);
+ $res = $dbw->select('bookdesigner_outlines', 'user_id', 'id=' . $outlineid);
+ if ($dbw->numRows($res) == 1 && $dbw->fetchObject($res)->user_id == $wgUser->getId()) {
+ $dbw->delete('bookdesigner_outlines', array(
+ 'id' => $outlineid
+ ));
+ $text = <<<EOD
+<div>
+ BALEETED
+ <br />
+ <a href="{$wgScriptPath}/index.php?title=Special:BookDesigner">Back</a>
+</div>
+EOD;
+ $wgOut->addHTML($text);
+ } else {
+ $text = <<<EOD
+<div>
+ Not deleted
+ <br />
+ <a href="{$wgScriptPath}/index.php?title=Special:BookDesigner">Back</a>
+</div>
+EOD;
+ $wgOut->addHTML($text);
+ }
+ }
+
+ function saveOutline() {
+ global $wgUser, $wgOut, $wgRequest, $wgScriptPath;
+ $text = $wgRequest->getText('VBDHiddenTextArea');
+ $parser = new BookDesignerParser($this, null);
+ $parser->parseTitlePageOnly($text);
+ $this->titlepage = $parser->titlePage();
+ $dbw = wfGetDB(DB_MASTER);
+ $dbw->insert('bookdesigner_outlines', array(
+ 'user_id' => $wgUser->getId(),
+ 'savedate' => gmdate('Y-m-d H:i:s'),
+ 'bookname' => $this->titlepage->name(),
+ 'outline' => $text
+ ));
+ $text =<<<EOD
+<div>
+ Outline for {$this->titlepage->name()} saved.
+ <br />
+ <a href="{$wgScriptPath}/index.php?title=Special:BookDesigner">Back</a>
+</div>
+EOD;
+ $wgOut->addHTML($text);
+ }
+
function showauthenticationError() {
global $wgOut;
$text = <<<EOT
@@ -103,16 +169,12 @@ function showauthenticationError() {
$wgOut->addHTML($text);
}
- function unknownModeError($type, $mode, $title) {
+ function unknownModeError($type, $mode) {
global $wgOut;
- $title_extra = "";
- if (isset($title)) {
- $title_extra = "with arguments (" . $title . ")";
- }
$text = <<<EOD
<p>
<span style='color: red; font-weight: bold;'>Error:</span>
- Could not {$type} with mode {$mode} $title_extra
+ Could not {$type} with mode {$mode}
</p>
EOD;
@@ -273,7 +335,7 @@ function addCSSFile($file) {
}
}
- function displayMainOutline() {
+ function displayMainOutline($inittext) {
global $wgOut, $wgScriptPath;
# TODO: Have a hidden field somewhere that we can hold a list of
@@ -282,6 +344,7 @@ function displayMainOutline() {
<form action="{$wgScriptPath}/index.php?title=Special:BookDesigner/verify" method="POST">
<textarea name="VBDHiddenTextArea" id="VBDHiddenTextArea" style="display: none;">
+ {$inittext}
</textarea>
<div id="VBDWelcomeSpan">
{$this->GetMessage('welcome')}
@@ -310,11 +373,38 @@ function displayMainOutline() {
somewhere in userspace, and maybe a button somewhere to "load" an
existing outline. -->
<input type="submit" name="btnSubmit" value="{$this->GetMessage('publishbutton')}" />
- <br>
+ <div id="VBDOutlineManager">
+ <input type="submit" name="btnSubmit" value="{$this->GetMessage('savebutton')}" />
+EOD;
+ $wgOut->addHTML($text);
+ $this->getSavedOutlines();
+ $text = <<<EOD
+ </div>
</form>
EOD;
$wgOut->addHTML($text);
}
+
+ function getSavedOutlines() {
+ global $wgOut, $wgUser, $wgScriptPath;
+ $dbr = wfGetDB(DB_SLAVE);
+ $res = $dbr->select('bookdesigner_outlines', array('id', 'savedate', 'bookname'), 'user_id=' . $wgUser->getId());
+ while($row = $dbr->fetchObject($res)) {
+ $text = <<<EOD
+ <div class="WBVSavedOutlineEntry">
+ <b>{$row->bookname}</b>: {$row->savedate}
+ <a href="{$wgScriptPath}/index.php?title=Special:BookDesigner/loadoutline/{$row->id}">
+ Load
+ </a>
+ &mdash;
+ <a href="{$wgScriptPath}/index.php?title=Special:BookDesigner/deleteoutline/{$row->id}">
+ Delete
+ </a>
+ </div>
+EOD;
+ $wgOut->addHTML($text);
+ }
+ }
}
View
9 designer.css
@@ -62,3 +62,12 @@ div.VBDCreateIgnored {
background-color: #FFEEEE;
padding: 5px;
}
+
+div#VBDSaveLoadDiv {
+ margin-left: 5px;
+ margin-top: 2em;
+ margin-right: 37%;
+ border: 1px solid #AAAAAA;
+ background-color: #F8F8F8;
+ padding: 5px;
+}
View
56 designer.js
@@ -26,10 +26,13 @@ var vbd = {
// Set VBD to load on document load
addOnloadHook(function() {
vbd.version = document.getElementById("VBDVersion").value;
- vbd.pageTree = new BookPage(vbd.defName);
+ if (!vbd.parseExistingOutline())
+ vbd.pageTree = new BookPage(vbd.defName);
vbd.visual();
});
+
+
// Basic array management functions
vbd.CopyArray = function(array) {
return array.slice(0);
@@ -265,4 +268,55 @@ vbd.ToggleGUIWidget = function(pane, link) {
linkelem.innerHTML = (state == "none") ? "Hide" : "Show";
}
+vbd.parseExistingOutline = function() {
+ var text = document.getElementById('VBDHiddenTextArea').value;
+ if (text.indexOf('<') == -1)
+ return false;
+ text = "<?xml version='1.0' encoding='UTF-8' ?>\n" + text;
+ var xmlDoc = null;
+ if (window.DOMParser) {
+ var parser = new DOMParser();
+ xmlDoc = parser.parseFromString(text, "application/xml");
+ } else {
+ xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
+ xmlDoc.async = "false";
+ xmlDoc.loadXML(text);
+ }
+ if (xmlDoc == null)
+ return false;
+ var node = vbd.readNodeFromOutline(xmlDoc.documentElement);
+ if (node == null)
+ return false;
+ this.pageTree = node;
+ return true;
+}
+
+vbd.readNodeFromOutline = function (node) {
+ var n = node.getAttribute('name');
+ var page = new BookPage(n);
+ var children = node.getElementsByTagName("page");
+ for (var i = 0; i < children.length; i++) {
+ var kid = vbd.readNodeFromOutline(children[i]);
+ page.addSubpage(kid);
+ vbd.newpagecnt++;
+ }
+ children = node.getElementsByTagName('heading');
+ for (var i = 0; i < children.length; i++) {
+ var headname = children[i].getAttribute('name');
+ var head = new PageHeading(headname);
+ page.addHeading(head);
+ vbd.newheadcnt++;
+ vbd.getHeadingChildren(head, children[i]);
+ }
+ return page;
+}
+
+vbd.getHeadingChildren = function(heading, node) {
+ var children = node.getElementsByTagName("page");
+ for (var i = 0; i < children.length; i++) {
+ var page = vbd.readNodeFromOutline(children[i]);
+ heading.addSubpage(page);
+ }
+}
+
Please sign in to comment.
Something went wrong with that request. Please try again.