Skip to content

Commit

Permalink
optionally move media with page rename #222
Browse files Browse the repository at this point in the history
A new checkbox allows to move referenced page media along with a page
rename.

This happens only if

* the checkbox is ticked
* the page actually moves to a new namespace
* the page was not in the root namespace
* the referenced media is in the same namespace as the page
  • Loading branch information
splitbrain committed Jul 8, 2024
1 parent 25f3554 commit 64e370e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
60 changes: 50 additions & 10 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,24 +110,59 @@ public function handle_ajax(Doku_Event $event) {

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

/** @var helper_plugin_move_op $MoveOperator */
$MoveOperator = plugin_load('helper', 'move_op');
header('Content-Type: application/json');

$JSON = new JSON();
if(!$this->renameOkay($src)) {
echo json_encode(['error' => $this->getLang('cantrename')]);
return;
}

header('Content-Type: application/json');
/** @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));
}
}
}
}
}

if($this->renameOkay($src) && $MoveOperator->movePage($src, $dst)) {
// all went well, redirect
echo $JSON->encode(array('redirect_url' => wl($dst, '', true, '&')));
} else {
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, '&')));
} 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
1 change: 1 addition & 0 deletions lang/en/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
$lang['js']['rename'] = 'Rename';
$lang['js']['cancel'] = 'Cancel';
$lang['js']['newname'] = 'New name:';
$lang['js']['rename_media'] = 'Move referenced media files along with the page';
$lang['js']['inprogress'] = 'renaming page and adjusting links...';
$lang['js']['complete'] = 'Move operation finished.';

Expand Down
10 changes: 8 additions & 2 deletions script/rename.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
'<label>' + LANG.plugins.move.newname + '<br>' +
'<input type="text" name="id" style="width:100%">' +
'</label>' +
'<label>' +
'<input type="checkbox" name="media" value="1"> ' + LANG.plugins.move.rename_media +
'</label>' +
'</form>' +
'</div>'
);
Expand All @@ -26,6 +29,8 @@
const newid = $dialog.find('input[name=id]').val();
if (!newid) return false;

const doMedia = $dialog.find('input[name=media]').is(':checked');

// remove buttons and show throbber
$dialog.html(
'<img src="' + DOKU_BASE + 'lib/images/throbber.gif" /> ' +
Expand All @@ -39,12 +44,13 @@
{
call: 'plugin_move_rename',
id: JSINFO.id,
newid: newid
newid: newid,
media: doMedia ? 1 : 0,
},
// redirect or display error
function (result) {
if (result.error) {
$dialog.html(result.error.msg);
$dialog.html(result.error);
} else {
window.location.href = result.redirect_url;
}
Expand Down

0 comments on commit 64e370e

Please sign in to comment.