Skip to content

Address scenarios with close page-media relations #279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 55 additions & 8 deletions action/rename.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ public function addsvgbutton(Doku_Event $event) {

/**
* Rename a single page
*
* This creates a plan and executes it right away. If the user selected to move media with the page,
* all media files used in the original page that are located in the same namespace are moved with the page
* to the new namespace.
*
*/
public function handle_ajax(Doku_Event $event) {
if($event->data != 'plugin_move_rename') return;
Expand All @@ -105,22 +110,64 @@ public function handle_ajax(Doku_Event $event) {

$src = cleanID($INPUT->str('id'));
$dst = cleanID($INPUT->str('newid'));

/** @var helper_plugin_move_op $MoveOperator */
$MoveOperator = plugin_load('helper', 'move_op');
$doMedia = $INPUT->bool('media');

header('Content-Type: application/json');

if($this->renameOkay($src) && $MoveOperator->movePage($src, $dst)) {
// all went well, redirect
if(!$this->renameOkay($src)) {
echo json_encode(['error' => $this->getLang('cantrename')]);
return;
}

if(!$dst || $dst == $src) {
echo json_encode(['error' => $this->getLang('nodst')]);
return;
}

/** @var helper_plugin_move_plan $plan */
$plan = plugin_load('helper', 'move_plan');
if($plan->isCommited()) {
echo json_encode(['error' => $this->getLang('cantrename')]);
return;
}
$plan->setOption('autorewrite', true);
$plan->addPageMove($src, $dst); // add the page move to the plan

if($doMedia) { // move media with the page?
$srcNS = getNS($src);
$dstNS = getNS($dst);
$srcNSLen = strlen($srcNS);
// we don't do this for root namespace or if namespace hasn't changed
if ($srcNS != '' && $srcNS != $dstNS) {
$media = p_get_metadata($src, 'relation media');
if (is_array($media)) {
foreach ($media as $file => $exists) {
if(!$exists) continue;
$mediaNS = getNS($file);
if ($mediaNS == $srcNS) {
$plan->addMediaMove($file, $dstNS . substr($file, $srcNSLen));
}
}
}
}
}

try {
// commit and execute the plan
$plan->commit();
do {
$next = $plan->nextStep();
if ($next === false) throw new \Exception('Move plan failed');
} while ($next > 0);
echo json_encode(array('redirect_url' => wl($dst, '', true, '&')));
} else {
} catch (\Exception $e) {
// error should be in $MSG
if(isset($MSG[0])) {
$error = $MSG[0]; // first error
} else {
$error = $this->getLang('cantrename');
$error = $this->getLang('cantrename') . ' ' . $e->getMessage();
}
echo json_encode(array('error' => $error));
echo json_encode(['error' => $error]);
}
}

Expand Down
11 changes: 4 additions & 7 deletions action/tree.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,10 @@ public function handle_ajax_call(Doku_Event $event, $params) {
$type = admin_plugin_move_tree::TYPE_PAGES;
}

$data = $plugin->tree($type, $ns, $ns);
header('Content-Type: application/json');

echo html_buildlist(
$data, 'tree_list',
array($plugin, 'html_list'),
array($plugin, 'html_li')
);
$data = $plugin->tree($type, $ns, $ns);
echo json_encode($data);
}

}
}
13 changes: 11 additions & 2 deletions admin/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,16 +105,25 @@ protected function createPlanFromInput() {
$this->plan->setOption('autorewrite', $INPUT->bool('autorewrite'));

if($ID && $INPUT->has('dst')) {
// input came from form
$dst = trim($INPUT->str('dst'));
if($dst == '') {
msg($this->getLang('nodst'), -1);
return false;
}

// input came from form
if($INPUT->str('class') == 'namespace') {
$src = getNS($ID);
} else {
$src = $ID;
}

if($dst == $src) {
msg(sprintf($this->getLang('notchanged'), $src), -1);
return false;
}

if($INPUT->str('class') == 'namespace') {
if($INPUT->str('type') == 'both') {
$this->plan->addPageNamespaceMove($src, $dst);
$this->plan->addMediaNamespaceMove($src, $dst);
Expand All @@ -124,7 +133,7 @@ protected function createPlanFromInput() {
$this->plan->addMediaNamespaceMove($src, $dst);
}
} else {
$this->plan->addPageMove($ID, $INPUT->str('dst'));
$this->plan->addPageMove($src, $INPUT->str('dst'));
}
$this->plan->commit();
return true;
Expand Down
Loading