diff --git a/_test/mediaindex.test.php b/_test/mediaindex.test.php new file mode 100644 index 0000000..eded7bb --- /dev/null +++ b/_test/mediaindex.test.php @@ -0,0 +1,46 @@ + + */ +class plugin_pagemove_mediaindex_test extends DokuWikiTest { + + public function setUp() { + $this->pluginsEnabled[] = 'pagemove'; + parent::setUp(); + } + + public function test_internalmedia() { + saveWikiText('test:internalmedia_usage', '{{internalmedia.png}} {{..:internal media.png}}', 'Test initialization'); + idx_addPage('test:internalmedia_usage'); + + $query = array('test:internalmedia.png', 'internal_media.png'); + $this->assertEquals( array( + 'test:internalmedia.png' => array('test:internalmedia_usage'), + 'internal_media.png' => array('test:internalmedia_usage') + ), idx_get_indexer()->lookupKey('pagemove_media', $query)); + } + + public function test_media_in_links() { + saveWikiText('test:medialinks', '[[doku>wiki:dokuwiki|{{wiki:logo.png}}]] [[http://www.example.com|{{example.png?200x800}}]]', 'Test init'); + idx_addPage('test:medialinks'); + + $query = array('wiki:logo.png', 'test:example.png'); + $this->assertEquals(array( + 'wiki:logo.png' => array('test:medialinks'), + 'test:example.png' => array('test:medialinks') + ), idx_get_indexer()->lookupKey('pagemove_media', $query)); + } + + public function test_media_in_footnotes() { + saveWikiText('test:media_footnotes', '(({{footnote.png?20x50}} [[foonote|{{:footlink.png}}]]))', 'Test initialization'); + idx_addPage('test:media_footnotes'); + + $query = array('test:footnote.png', 'footlink.png'); + $this->assertEquals(array( + 'test:footnote.png' => array('test:media_footnotes'), + 'footlink.png' => array('test:media_footnotes') + ), idx_get_indexer()->lookupKey('pagemove_media', $query)); + } +} diff --git a/action.php b/action.php index 2071697..ebd7587 100644 --- a/action.php +++ b/action.php @@ -20,6 +20,8 @@ class action_plugin_pagemove extends DokuWiki_Action_Plugin { public function register($controller) { $controller->register_hook('IO_WIKIPAGE_READ', 'AFTER', $this, 'handle_read', array()); $controller->register_hook('PARSER_CACHE_USE', 'BEFORE', $this, 'handle_cache', array()); + $controller->register_hook('INDEXER_VERSION_GET', 'BEFORE', $this, 'handle_index_version'); + $controller->register_hook('INDEXER_PAGE_ADD', 'BEFORE', $this, 'index_media_use'); } /** @@ -90,4 +92,63 @@ function handle_cache(Doku_Event $event, $param) { } } } + + /** + * Add the pagemove version to theindex version + * + * @param Doku_Event $event The event object + * @param array $param Optional parameters (unused) + */ + public function handle_index_version(Doku_Event $event, $param) { + $event->data['plugin_pagemove'] = 0.1; + } + + /** + * Index media usage data + * + * @param Doku_Event $event The event object + * @param array $param Optional parameters (unused) + */ + public function index_media_use(Doku_Event $event, $param) { + $id = $event->data['page']; + $media_references = array(); + $instructions = p_cached_instructions(wikiFn($id), false, $id); + if (is_array($instructions)) { + $this->get_media_references_from_instructions($instructions, $media_references, $id); + } + $media_references = array_unique($media_references); + $event->data['metadata']['pagemove_media'] = $media_references; + } + + /** + * Helper function for getting all media references from an instruction array + * + * @param array $instructions The instructions to scan + * @param array $media_references The array of media references + * @param string $id The reference id for resolving media ids + */ + private function get_media_references_from_instructions($instructions, &$media_references, $id) { + foreach ($instructions as $ins) { + if ($ins[0] === 'internalmedia') { + $src = $ins[1][0]; + list($src,$hash) = explode('#',$src,2); + resolve_mediaid(getNS($id),$src, $exists); + $media_references[] = $src; + } elseif (in_array($ins[0], array('interwikilink', 'windowssharelink', 'externallink', 'emaillink', 'locallink', 'internallink'))) { + $img = $ins[1][1]; + if (is_array($img) && $img['type'] === 'internalmedia') { + list($src,$hash) = explode('#',$img['src'],2); + resolve_mediaid(getNS($id), $src, $exists); + $media_references[] = $src; + } + } elseif ($ins[0] === 'nest') { + // nested instructions + $this->get_media_references_from_instructions($ins[1][0], $media_references, $id); + } elseif ($ins[0] === 'plugin' && $ins[1][0] === 'variants_variants') { + // the variants plugin has two branches with nested instructions, both need to be rewritten + $this->get_media_references_from_instructions($ins[1][1][1], $media_references, $id); + $this->get_media_references_from_instructions($ins[1][1][2], $media_references, $id); + } + } + } } \ No newline at end of file