From b4f6d8fdd2906ad8a9f941b4e3dbfebd934207b1 Mon Sep 17 00:00:00 2001 From: Michael Hamann Date: Fri, 29 Mar 2013 18:22:55 +0100 Subject: [PATCH] Add media reference adaption support for images inside other links --- _test/mediamove.test.php | 36 ++++++++++++++++++++++++++++++++++++ helper.php | 31 +++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 _test/mediamove.test.php diff --git a/_test/mediamove.test.php b/_test/mediamove.test.php new file mode 100644 index 0000000..1d044f3 --- /dev/null +++ b/_test/mediamove.test.php @@ -0,0 +1,36 @@ +pluginsEnabled[] = 'pagemove'; + parent::setUp(); + } + + public function test_movePageWithRelativeMedia() { + global $ID; + + $ID = 'mediareltest:foo'; + saveWikiText($ID, + '{{ myimage.png}} [[:start|{{ testimage.png?200x800 }}]] [[bar|{{testimage.gif?400x200}}]] +[[doku>wiki:dokuwiki|{{wiki:logo.png}}]] [[http://www.example.com|{{testimage.jpg}}]] +[[doku>wiki:foo|{{foo.gif?200x3000}}]]', 'Test setup'); + idx_addPage($ID); + + $opts = array(); + $opts['ns'] = getNS($ID); + $opts['name'] = noNS($ID); + $opts['newns'] = ''; + $opts['newname'] = 'foo'; + /** @var helper_plugin_pagemove $pagemove */ + $pagemove = plugin_load('helper', 'pagemove'); + $pagemove->move_page($opts); + + $this->assertEquals('{{ mediareltest:myimage.png}} [[:start|{{ mediareltest:testimage.png?200x800 }}]] [[mediareltest:bar|{{mediareltest:testimage.gif?400x200}}]] +[[doku>wiki:dokuwiki|{{wiki:logo.png}}]] [[http://www.example.com|{{mediareltest:testimage.jpg}}]] +[[doku>wiki:foo|{{mediareltest:foo.gif?200x3000}}]]', rawWiki('foo')); + } +} diff --git a/helper.php b/helper.php index 56da3a9..a9960ca 100644 --- a/helper.php +++ b/helper.php @@ -583,6 +583,14 @@ public function internallink($match, $state, $pos) { $link = explode('|',$link,2); if ( !isset($link[1]) ) { $link[1] = NULL; + } else if ( preg_match('/^\{\{[^\}]+\}\}$/',$link[1]) ) { + // If the title is an image, rewrite it + $old_title = $link[1]; + $link[1] = $this->rewrite_media($link[1]); + // do a simple replace of the first match so really only the id is changed and not e.g. the alignment + $oldpos = strpos($match, $old_title); + $oldlen = strlen($old_title); + $match = substr_replace($match, $link[1], $oldpos, $oldlen); } $link[0] = trim($link[0]); @@ -657,21 +665,28 @@ public function internallink($match, $state, $pos) { * @return bool If parsing should be continued */ public function media($match, $state, $pos) { + $this->calls .= $this->rewrite_media($match); + return true; + } + + /** + * Rewrite a media syntax + * + * @param string $match The text match of the media syntax + * @return string The rewritten syntax + */ + protected function rewrite_media($match) { $p = Doku_Handler_Parse_Media($match); - if ($p['type'] == 'internalmedia') { + if ($p['type'] == 'internalmedia') { // else: external media $new_src = $this->adaptRelativeId($p['src']); - if ($new_src == $p['src']) { - $this->calls .= $match; - } else { + if ($new_src !== $p['src']) { // do a simple replace of the first match so really only the id is changed and not e.g. the alignment $srcpos = strpos($match, $p['src']); $srclen = strlen($p['src']); - $this->calls .= substr_replace($match, $new_src, $srcpos, $srclen); + return substr_replace($match, $new_src, $srcpos, $srclen); } - } else { // external media - $this->calls .= $match; } - return true; + return $match; } /**