Skip to content

Commit

Permalink
Extract the page rewriting code into a helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
michitux committed Dec 30, 2012
1 parent 38d04ae commit 1eefdb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
19 changes: 4 additions & 15 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
Expand Down
26 changes: 26 additions & 0 deletions helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down

0 comments on commit 1eefdb1

Please sign in to comment.