From 1eefdb1c5b03e22f80fbcb5e49eeefcd5b2dd767 Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Sun, 30 Dec 2012 17:06:01 +0100 Subject: [PATCH] Extract the page rewriting code into a helper function --- action.php | 19 ++++--------------- helper.php | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 15 deletions(-) diff --git a/action.php b/action.php index 4786ff6..a80e8a2 100644 --- a/action.php +++ b/action.php @@ -35,21 +35,10 @@ function handle_read(Doku_Event $event, $param) { if ((isset($ACT) && (in_array($ACT, array('save', 'preview', 'recover', 'revert')))) || checklock($id) !== false) return; - $meta = p_get_metadata($id, 'plugin_pagemove', METADATA_DONT_RENDER); - if ($meta && isset($meta['moves'])) { - $stack[$id] = true; - $helper = $this->loadHelper('pagemove', true); - if (!is_null($helper)) { - $event->result = $helper->rewrite_content($event->result, $id, $meta['moves']); - } - $file = wikiFN($id, '', false); - if (is_writable($file)) { - saveWikiText($id,$event->result,$this->getLang('pm_linkchange')); - unset($meta['moves']); - p_set_metadata($id, array('plugin_pagemove' => $meta), false, true); - } else { // FIXME: print error here or fail silently? - msg('Error: Page '.hsc($id).' needs to be rewritten because of page renames but is not writable.', -1); - } + $helper = $this->loadHelper('pagemove', true); + if(!is_null($helper)) { + $stack[$id] = true; + $event->result = $helper->execute_rewrites($id, $event->result); unset($stack[$id]); } } diff --git a/helper.php b/helper.php index 1b4b0a3..fa85983 100644 --- a/helper.php +++ b/helper.php @@ -247,6 +247,32 @@ private function move_files($dir, $opts, $extregex) { } } + /** + * Rewrite the text of a page according to the recorded moves, the rewritten text is saved + * + * @param string $id The id of the page that shall be rewritten + * @param string|null $text Old content of the page. When null is given the content is loaded from disk. + * @return string The rewritten content + */ + public function execute_rewrites($id, $text = null) { + $meta = p_get_metadata($id, 'plugin_pagemove', METADATA_DONT_RENDER); + if($meta && isset($meta['moves'])) { + if(is_null($text)) $text = rawWiki($id); + + $text = $this->rewrite_content($text, $id, $meta['moves']); + $file = wikiFN($id, '', false); + if(is_writable($file)) { + saveWikiText($id, $text, $this->getLang('pm_linkchange')); + unset($meta['moves']); + p_set_metadata($id, array('plugin_pagemove' => $meta), false, true); + } else { // FIXME: print error here or fail silently? + msg('Error: Page '.hsc($id).' needs to be rewritten because of page renames but is not writable.', -1); + } + } + + return $text; + } + /** * Rewrite a text in order to fix the content after the given moves. *