Skip to content

Commit

Permalink
Add new link rewriting code, some code cleanup
Browse files Browse the repository at this point in the history
This includes:
- test cases adapted for PHPUnit
- use metadata search index for finding backlinks
- new code for rewriting links, media references and plugin syntax
- backlinks aren't corrected immediately anymore but instead a queue is
  filled with all moves that need to be considered
  • Loading branch information
michitux committed Nov 1, 2012
1 parent 8f3fcd1 commit 4e15b85
Show file tree
Hide file tree
Showing 5 changed files with 578 additions and 504 deletions.
10 changes: 0 additions & 10 deletions _test/pagemove.group.php

This file was deleted.

107 changes: 55 additions & 52 deletions _test/pagemove.test.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
<?php
require_once(DOKU_INC.'_test/lib/unittest.php');

require_once(DOKU_INC.'inc/init.php');
require_once(DOKU_INC.'inc/plugin.php');
require_once(DOKU_INC.'inc/common.php');
require_once(DOKU_INC.'lib/plugins/pagemove/admin.php');

class PagemovePageTest extends Doku_UnitTestCase {
/**
* Test cases for the pagemove plugin
*/
class PagemovePageTest extends DokuWikiTest {

var $movedToId = '';
var $movedId = 'parent_ns:current_ns:test_page';
var $parentBacklinkingId = 'parent_ns:some_page';
var $currentNsBacklinkingId = 'parent_ns:current_ns:some_page';
var $otherBacklinkingId = 'level0:level1:other_backlinking_page';
var $subNsPage = 'parent_ns:current_ns:sub_ns:some_page';
/** @var admin_plugin_pagemove $pagemove */
private $pagemove = NULL;

// @todo Move page to an ID which already exists
// @todo Check backlinks of a sub-namespace page (moving same, up, down, different)

function setUp() {
$this->pluginsEnabled[] = 'pagemove';
global $ID;
global $INFO;

Expand All @@ -41,6 +42,9 @@ function setUp() {
saveWikiText($this->movedId, $text, $summary);
$INFO = pageinfo();

$references = array_keys(p_get_metadata($this->movedId, 'relation references', METADATA_RENDER_UNLIMITED));
idx_get_indexer()->addMetaKeys($this->movedId, 'relation_references', $references);

$text = <<<EOT
[[$this->movedId|$this->movedId]]
[[:$this->movedId|:$this->movedId]]
Expand All @@ -66,6 +70,8 @@ function setUp() {
[[/start|/start]]
EOT;
saveWikiText($this->parentBacklinkingId, $text, $summary);
$references = array_keys(p_get_metadata($this->parentBacklinkingId, 'relation references', METADATA_RENDER_UNLIMITED));
idx_get_indexer()->addMetaKeys($this->parentBacklinkingId, 'relation_references', $references);

$text = <<<EOT
[[$this->movedId|$this->movedId]]
Expand Down Expand Up @@ -96,6 +102,8 @@ function setUp() {
[[/start|/start]]
EOT;
saveWikiText($this->currentNsBacklinkingId, $text, $summary);
$references = array_keys(p_get_metadata($this->currentNsBacklinkingId, 'relation references', METADATA_RENDER_UNLIMITED));
idx_get_indexer()->addMetaKeys($this->currentNsBacklinkingId, 'relation_references', $references);

$text = <<<EOT
[[$this->movedId|$this->movedId]]
Expand All @@ -121,6 +129,8 @@ function setUp() {
[[/start|/start]]
EOT;
saveWikiText($this->otherBacklinkingId, $text, $summary);
$references = array_keys(p_get_metadata($this->otherBacklinkingId, 'relation references', METADATA_RENDER_UNLIMITED));
idx_get_indexer()->addMetaKeys($this->otherBacklinkingId, 'relation_references', $references);

$text = <<<EOT
[[$this->movedId|$this->movedId]]
Expand Down Expand Up @@ -150,8 +160,11 @@ function setUp() {
[[/start|/start]]
EOT;
saveWikiText($this->subNsPage, $text, $summary);
$references = array_keys(p_get_metadata($this->subNsPage, 'relation references', METADATA_RENDER_UNLIMITED));
idx_get_indexer()->addMetaKeys($this->subNsPage, 'relation_references', $references);

$this->pagemove = new admin_plugin_pagemove();
parent::setUp();
}

# function testPagemove() {
Expand Down Expand Up @@ -181,18 +194,18 @@ function test_move_page_in_same_ns() {
$expectedContent = <<<EOT
[[start|start]]
[[parallel_page|parallel_page]]
[[start|.:]]
[[start|..current_ns:]]
[[start|..:current_ns:]]
[[parent_ns:parallel_ns:start|..parallel_ns:]]
[[parent_ns:parallel_ns:start|..:parallel_ns:]]
[[.:|.:]]
[[..current_ns:|..current_ns:]]
[[..:current_ns:|..:current_ns:]]
[[..parallel_ns:|..parallel_ns:]]
[[..:parallel_ns:|..:parallel_ns:]]
[[..:..:|..:..:]]
[[parent_ns:start|..:..:parent_ns:]]
[[..:..:parent_ns:|..:..:parent_ns:]]
[[parent_ns:new_page|parent_ns:new_page]]
[[parent_ns:new_page|parent_ns/new_page]]
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->parentBacklinkingId);
$expectedContent = <<<EOT
Expand All @@ -219,7 +232,7 @@ function test_move_page_in_same_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->currentNsBacklinkingId);
$expectedContent = <<<EOT
Expand Down Expand Up @@ -250,7 +263,7 @@ function test_move_page_in_same_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->otherBacklinkingId);
$expectedContent = <<<EOT
Expand All @@ -276,7 +289,7 @@ function test_move_page_in_same_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);
}


Expand All @@ -299,18 +312,18 @@ function test_move_page_to_parallel_ns() {
$expectedContent = <<<EOT
[[parent_ns:current_ns:start|start]]
[[parent_ns:current_ns:parallel_page|parallel_page]]
[[parent_ns:current_ns:start|.:]]
[[parent_ns:current_ns:start|..current_ns:]]
[[parent_ns:current_ns:start|..:current_ns:]]
[[start|..parallel_ns:]]
[[start|..:parallel_ns:]]
[[parent_ns:current_ns:|.:]]
[[..current_ns:|..current_ns:]]
[[..:current_ns:|..:current_ns:]]
[[..parallel_ns:|..parallel_ns:]]
[[..:parallel_ns:|..:parallel_ns:]]
[[..:..:|..:..:]]
[[parent_ns:start|..:..:parent_ns:]]
[[..:..:parent_ns:|..:..:parent_ns:]]
[[parent_ns:new_page|parent_ns:new_page]]
[[parent_ns:new_page|parent_ns/new_page]]
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->parentBacklinkingId);
$expectedContent = <<<EOT
Expand All @@ -337,7 +350,7 @@ function test_move_page_to_parallel_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->currentNsBacklinkingId);
$expectedContent = <<<EOT
Expand Down Expand Up @@ -368,7 +381,7 @@ function test_move_page_to_parallel_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->otherBacklinkingId);
$expectedContent = <<<EOT
Expand All @@ -394,7 +407,7 @@ function test_move_page_to_parallel_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);
}


Expand All @@ -418,18 +431,18 @@ function test_move_page_to_parent_ns() {
$expectedContent = <<<EOT
[[parent_ns:current_ns:start|start]]
[[parent_ns:current_ns:parallel_page|parallel_page]]
[[parent_ns:current_ns:start|.:]]
[[parent_ns:current_ns:start|..current_ns:]]
[[parent_ns:current_ns:start|..:current_ns:]]
[[parent_ns:parallel_ns:start|..parallel_ns:]]
[[parent_ns:parallel_ns:start|..:parallel_ns:]]
[[..:..:|..:..:]]
[[parent_ns:current_ns:|.:]]
[[parent_ns:current_ns:|..current_ns:]]
[[parent_ns:current_ns:|..:current_ns:]]
[[parent_ns:parallel_ns:|..parallel_ns:]]
[[parent_ns:parallel_ns:|..:parallel_ns:]]
[[:|..:..:]]
[[start|..:..:parent_ns:]]
[[new_page|parent_ns:new_page]]
[[new_page|parent_ns/new_page]]
[[parent_ns:new_page|parent_ns:new_page]]
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

// page is moved to same NS as backlinking page (parent_ns)
$newContent = rawWiki($this->parentBacklinkingId);
Expand Down Expand Up @@ -457,7 +470,7 @@ function test_move_page_to_parent_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->currentNsBacklinkingId);
$expectedContent = <<<EOT
Expand Down Expand Up @@ -488,7 +501,7 @@ function test_move_page_to_parent_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);

$newContent = rawWiki($this->otherBacklinkingId);
$expectedContent = <<<EOT
Expand All @@ -514,14 +527,14 @@ function test_move_page_to_parent_ns() {
[[parent_ns/new_page|parent_ns/new_page]]
[[/start|/start]]
EOT;
$this->assertEqual($expectedContent, $newContent);
$this->assertEquals($expectedContent, $newContent);
}


function test_move_ns_in_same_ns() {
global $ID;

$newNamespace = 'new_ns';
$newPagename = '';

$opts = array();
$opts['page_ns'] = 'ns';
Expand All @@ -533,15 +546,5 @@ function test_move_ns_in_same_ns() {

}

function tearDown() {
saveWikiText($this->movedId, '', 'removed');
saveWikiText($this->movedToId, '', 'removed');
saveWikiText($this->parentBacklinkingId, '', 'removed');
saveWikiText($this->currentNsBacklinkingId, '', 'removed');
saveWikiText($this->otherBacklinkingId, '', 'removed');
saveWikiText($this->subNsPage, '', 'removed');
}

}

?>
72 changes: 72 additions & 0 deletions action.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Action part of the pagemove plugin
*
* @author Michael Hamann <[email protected]>
*/
class action_plugin_pagemove extends DokuWiki_Action_Plugin {
/**
* Register event handlers.
*
* @param Doku_Event_Handler $controller The plugin controller
*/
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());
}

/**
* Rewrite pages when they are read and they need to be updated.
*
* @param Doku_Event $event The event object
* @param mixed $param Optional parameters (not used)
*/
function handle_read(Doku_Event $event, $param) {
static $stack = array();
// handle only reads of the current revision
if ($event->data[3]) return;

$id = $event->data[2];
if ($event->data[1]) $id = $event->data[1].':'.$id;
if (isset($stack[$id])) 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);
}
unset($stack[$id]);
}
}

/**
* Handle the cache events, it looks if a page needs to be rewritten so it can expire the cache of the page
*
* @param Doku_Event $event The even object
* @param mixed $param Optional parameters (not used)
*/
function handle_cache(Doku_Event $event, $param) {
/** @var $cache cache_parser */
$cache = $event->data;
$id = $cache->page;
if ($id) {
$meta = p_get_metadata($id, 'plugin_pagemove', METADATA_DONT_RENDER);
if ($meta && isset($meta['moves'])) {
$file = wikiFN($id, '', false);
if (is_writable($file))
$cache->depends['purge'] = 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);
}
}
}
}
Loading

0 comments on commit 4e15b85

Please sign in to comment.