From 64e370eb7e8064ba13451de082bd64a81c90d26a Mon Sep 17 00:00:00 2001 From: Andreas Gohr Date: Mon, 8 Jul 2024 13:11:50 +0200 Subject: [PATCH] optionally move media with page rename #222 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 --- action/rename.php | 60 +++++++++++++++++++++++++++++++++++++++-------- lang/en/lang.php | 1 + script/rename.js | 10 ++++++-- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/action/rename.php b/action/rename.php index ca7cb0d..814767b 100644 --- a/action/rename.php +++ b/action/rename.php @@ -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; @@ -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]); } } diff --git a/lang/en/lang.php b/lang/en/lang.php index 48a81d6..202ab3b 100644 --- a/lang/en/lang.php +++ b/lang/en/lang.php @@ -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.'; diff --git a/script/rename.js b/script/rename.js index 03a8e5f..cf4d916 100644 --- a/script/rename.js +++ b/script/rename.js @@ -14,6 +14,9 @@ '' + + '' + '' + '' ); @@ -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( ' ' + @@ -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; }