Skip to content

Commit

Permalink
Add new namespace move functionality including ajax
Browse files Browse the repository at this point in the history
Namespace moves are now split into junks of at maximum 10 pages
  • Loading branch information
michitux committed Mar 30, 2013
1 parent e1bff2e commit dbd7319
Show file tree
Hide file tree
Showing 6 changed files with 498 additions and 25 deletions.
40 changes: 40 additions & 0 deletions _test/namespace_move.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

// must be run within Dokuwiki
if (!defined('DOKU_INC')) die();

/**
* Test cases for namespace move functionality of the pagemove plugin
*/
class plugin_pagemove_namespace_move_test extends DokuWikiTest {

public function setUp() {
$this->pluginsEnabled[] = 'pagemove';
parent::setUp();
}

public function test_move_wiki_namespace() {
global $AUTH_ACL;

$AUTH_ACL[] = "wiki:*\t@ALL\t16";

idx_addPage('wiki:dokuwiki');
idx_addPage('wiki:syntax');

/** @var helper_plugin_pagemove $pagemove */
$pagemove = plugin_load('helper', 'pagemove');
$opts = array(
'ns' => 'wiki',
'newns' => 'foo',
'media' => true
);

$this->assertSame(3, $pagemove->start_namespace_move($opts));
$this->assertSame(1, $pagemove->continue_namespace_move());
$this->assertSame(0, $pagemove->continue_namespace_move());

$this->assertFileExists(wikiFN('foo:dokuwiki'));
$this->assertFileNotExists(wikiFN('wiki:syntax'));
$this->assertFileExists(mediaFN('foo:dokuwiki-128.png'));
}
}
68 changes: 68 additions & 0 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function register($controller) {
$controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache', array());
$controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'handle_index_version');
$controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'index_media_use');
$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax_call');
}

/**
Expand Down Expand Up @@ -151,4 +152,71 @@ private function get_media_references_from_instructions($instructions, &$media_r
}
}
}

/**
* Handle the plugin_pagemove_ns_continue ajax call
*
* @param Doku_Event $event The event that is handled
* @param array $params Optional parameters (unused)
*/
public function handle_ajax_call(Doku_Event $event, $params) {
if ($event->data == 'plugin_pagemove_ns_continue') {
$event->preventDefault();
$event->stopPropagation();

/** @var helper_plugin_pagemove $helper */
$helper = $this->loadHelper('pagemove', false);
$opts = $helper->get_namespace_move_opts();
$id = cleanID((string)$_POST['id']);
$skip = (int)$_POST['skip'];
if ($opts !== false) {
if ($skip) {
$helper->skip_namespace_move_item();
}
$remaining = $helper->continue_namespace_move();
$newid = $helper->getNewID($id, $opts['ns'], $opts['newns']);

$result = array();
$result['remaining'] = $remaining;
$result['pages'] = $opts['num_pages'];
$result['media'] = $opts['num_media'];
$result['redirect_url'] = wl($newid, '', true);
ob_start();
html_msgarea();
if ($remaining === false) {
$form = new Doku_Form(array('action' => wl($id), 'method' => 'post', 'class' => 'pagemove__nsform pagemove__nscontinue'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $id);
$form->addHidden('continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_tryagain')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($id), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $id);
$form->addHidden('abort_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_abort')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($id), 'method' => 'post', 'class' => 'pagemove__nsform pagemove__nsskip'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $id);
$form->addHidden('skip_continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_skip')));
$form->printForm();
ptln('<p>'.sprintf($this->getLang('pm_ns_move_error'), $opts['ns'], $opts['newns']).'</p>');
} else {
ptln('<p>'.sprintf($this->getLang('pm_ns_move_continued'), $opts['ns'], $opts['newns'], $remaining).'</p>');
}
$result['html'] = ob_get_clean();
} else {
$result = array();
$result['remaining'] = 0;
$result['pages'] = 0;
$result['media'] = 0;
$result['redirect_url'] = wl('', '', true);
$result['html'] = '';
}
$json = new JSON();
echo $json->encode($result);
}
}
}
146 changes: 127 additions & 19 deletions admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
class admin_plugin_pagemove extends DokuWiki_Admin_Plugin {

var $opts = array();
private $ns_opts = false;

/**
* Get the sort number that defines the position in the admin menu.
Expand Down Expand Up @@ -62,9 +63,71 @@ function getMenuText() {
* @author Gary Owen <[email protected]>
*/
function html() {
global $ID;
ptln('<!-- Pagemove Plugin start -->');
ptln( $this->locale_xhtml('pagemove') );
$this->printForm();
ptln('<div class="plugin__pagemove_forms">');
if ($this->ns_opts !== false && isset($this->ns_opts['started'])) {
ptln('<p>');
ptln(sprintf($this->getLang('pm_ns_move_started'), hsc($this->ns_opts['ns']), hsc($this->ns_opts['newns']), $this->ns_opts['num_pages'], $this->ns_opts['num_media']));
ptln('</p>');
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform pagemove__nscontinue'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_continue')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('abort_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_abort')));
$form->printForm();
} elseif ($this->ns_opts !== false && isset($this->ns_opts['remaining'])) {
if ($this->ns_opts['remaining'] === false) {
ptln('<p>');
ptln(sprintf($this->getLang('pm_ns_move_error'), $this->ns_opts['ns'], $this->ns_opts['newns']));
ptln('</p>');
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform pagemove__nscontinue'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_tryagain')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('abort_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_abort')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('skip_continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_skip')));
$form->printForm();
} else {
ptln('<p>');
ptln(sprintf($this->getLang('pm_ns_move_continued'), $this->ns_opts['ns'], $this->ns_opts['newns'], $this->ns_opts['remaining']));
ptln('</p>');

$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_continue')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('abort_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_abort')));
$form->printForm();
}
} else {
$this->printForm();
}
ptln('</div>');
ptln('<!-- Pagemove Plugin end -->');
}

Expand Down Expand Up @@ -93,16 +156,39 @@ function printForm() {
$form->endFieldset();
$form->printForm();

$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__form'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('move_type', 'namespace');
$form->startFieldset($this->getLang('pm_movens'));
$form->addElement(form_makeMenuField('targetns', $ns_select_data, $this->opts['targetns'], $this->getLang('pm_targetns'), '', 'block'));
$form->addElement(form_makeTextField('newnsname', $this->opts['newnsname'], $this->getLang('pm_newnsname'), '', 'block'));
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_submit')));
$form->endFieldset();
$form->printForm();
if ($this->ns_opts !== false && !isset($this->ns_opts['remaining'])) {
ptln('<fieldset>');
ptln('<legend>');
ptln($this->getLang('pm_movens'));
ptln('</legend>');
ptln('<p>');
ptln(sprintf($this->getLang('pm_ns_move_in_progress'), $this->ns_opts['num_pages'], $this->ns_opts['num_media'], ':'.hsc($this->ns_opts['ns']), ':'.hsc($this->ns_opts['newns'])));
ptln('</p>');
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform pagemove__nscontinue'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('continue_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_continue')));
$form->printForm();
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__nsform'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('abort_namespace_move', true);
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_ns_move_abort')));
$form->printForm();
ptln('</fieldset>');
} else {
$form = new Doku_Form(array('action' => wl($ID), 'method' => 'post', 'class' => 'pagemove__form'));
$form->addHidden('page', $this->getPluginName());
$form->addHidden('id', $ID);
$form->addHidden('move_type', 'namespace');
$form->startFieldset($this->getLang('pm_movens'));
$form->addElement(form_makeMenuField('targetns', $ns_select_data, $this->opts['targetns'], $this->getLang('pm_targetns'), '', 'block'));
$form->addElement(form_makeTextField('newnsname', $this->opts['newnsname'], $this->getLang('pm_newnsname'), '', 'block'));
$form->addElement(form_makeButton('submit', 'admin', $this->getLang('pm_submit')));
$form->endFieldset();
$form->printForm();
}
}


Expand Down Expand Up @@ -153,11 +239,35 @@ function handle() {
$this->opts['newnsname'] = '';
$this->opts['move_type'] = 'page';

/** @var helper_plugin_pagemove $helper */
$helper = $this->loadHelper('pagemove', true);
if (!$helper) return;

$this->ns_opts = $helper->get_namespace_move_opts();

// Only continue when the form was submitted
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
return;
}

if (isset($_POST['continue_namespace_move']) || isset($_POST['skip_continue_namespace_move'])) {
if (isset($_POST['skip_continue_namespace_move'])) {
$helper->skip_namespace_move_item();
}
$this->ns_opts['remaining'] = $helper->continue_namespace_move();
if ($this->ns_opts['remaining'] === 0) {
$ID = $helper->getNewID($INFO['id'], $this->opts['ns'], $this->opts['newns']);
$ACT = 'show';
}

return;
} elseif (isset($_POST['abort_namespace_move'])) {
$helper->abort_namespace_move();
$this->ns_opts = false;

return;
}

// Store the form data in the options and clean the submitted data.
if (isset($_POST['ns_for_page'])) $this->opts['ns_for_page'] = cleanID((string)$_POST['ns_for_page']);
if (isset($_POST['newns'])) $this->opts['newns'] = cleanID((string)$_POST['newns']);
Expand All @@ -166,22 +276,20 @@ function handle() {
if (isset($_POST['newnsname'])) $this->opts['newnsname'] = cleanID((string)$_POST['newnsname']);
if (isset($_POST['move_type'])) $this->opts['move_type'] = (string)$_POST['move_type'];

/** @var helper_plugin_pagemove $helper */
$helper = $this->loadHelper('pagemove', true);
if (!$helper) return;

// check the input for completeness
if( $this->opts['move_type'] == 'namespace' ) {
$this->opts['media'] = true; // FIXME: add checkbox later!

if ($this->opts['targetns'] == '') {
$this->opts['newns'] = $this->opts['newnsname'];
} else {
$this->opts['newns'] = $this->opts['targetns'].':'.$this->opts['newnsname'];
}

if ($helper->move_namespace($this->opts, true) &&
$helper->move_namespace($this->opts)) {
$ID = $helper->getNewID($INFO['id'], $this->opts['ns'], $this->opts['newns']);
$ACT = 'show';
$started = $helper->start_namespace_move($this->opts);
if ($started !== false) {
$this->ns_opts = $helper->get_namespace_move_opts();
$this->ns_opts['started'] = $started;
}
} else {
// check that the pagename is valid
Expand Down
Loading

0 comments on commit dbd7319

Please sign in to comment.