Skip to content

Commit

Permalink
Add index for media usage (will be used for media moves later)
Browse files Browse the repository at this point in the history
  • Loading branch information
michitux committed Mar 29, 2013
1 parent b4f6d8f commit 38b4334
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
46 changes: 46 additions & 0 deletions _test/mediaindex.test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
/**
* Test cases for the media usage index
*
* @author Michael Hamann <[email protected]>
*/
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));
}
}
61 changes: 61 additions & 0 deletions action.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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);
}
}
}
}

0 comments on commit 38b4334

Please sign in to comment.