Skip to content

Commit

Permalink
Send smw:parseraftertidy event (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Mar 9, 2019
1 parent 6a3f6bc commit ad2fa30
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Hooks.php
Expand Up @@ -3,6 +3,7 @@
namespace SMW\ApprovedRevs;

use SMW\ApplicationFactory;
use Onoi\Cache\Cache;

/**
* @license GNU GPL v2+
Expand All @@ -17,6 +18,11 @@ class Hooks {
*/
private $handlers = [];

/**
* @var Cache
*/
private $cache;

/**
* @since 1.0
*
Expand All @@ -26,6 +32,15 @@ public function __construct( $config = [] ) {
$this->registerHandlers( $config );
}

/**
* @since 1.0
*
* @param Cache $cache
*/
public function setCache( Cache $cache ) {
$this->cache = $cache;
}

/**
* @since 1.0
*/
Expand Down Expand Up @@ -187,8 +202,64 @@ public function onUpdateDataBefore( $store, $semanticData ) {
return true;
}

/**
* @see ??
*
* @since 1.0
*
* @param ParserOutput $output
* @param Title $title
* @param integer $rev_id
* @param string $content
*/
public function onApprovedRevsRevisionApproved( $output, $title, $rev_id, $content ) {

$ttl = 60 * 60; // 1hr

if ( $this->cache === null ) {
$this->cache = ApplicationFactory::getInstance()->getCache();
}

// Send an event to ParserAfterTidy and allow it to pass the preliminary
// test even in cases where the content doesn't contain any SMW related
// annotations. It is to ensure that when an agent switches to a blank
// version (no SMW related annotations or categories) the update is carried
// out and the store is able to remove any remaining annotations.
$key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() );
$this->cache->save( $key, $rev_id, $ttl );

return true;
}

/**
* @see ??
*
* @since 1.0
*
* @param Parser $parser
* @param Title $title
* @param integer $timestamp
* @param string $sha1
*/
public function onApprovedRevsFileRevisionApproved( $parser, $title, $timestamp, $sha1 ) {

$ttl = 60 * 60; // 1hr

if ( $this->cache === null ) {
$this->cache = ApplicationFactory::getInstance()->getCache();
}

// @see onApprovedRevsRevisionApproved for the same reason
$key = smwfCacheKey( 'smw:parseraftertidy', $title->getPrefixedDBKey() );
$this->cache->save( $key, $sha1, $ttl );

return true;
}

private function registerHandlers( $config ) {
$this->handlers = [
'ApprovedRevsRevisionApproved' => [ $this, 'onApprovedRevsRevisionApproved' ],
'ApprovedRevsFileRevisionApproved' => [ $this, 'onApprovedRevsFileRevisionApproved' ],
'SMW::LinksUpdate::ApprovedUpdate' => [ $this, 'onSkipUpdate' ],
'SMW::DataUpdater::SkipUpdate' => [ $this, 'onSkipUpdate' ],
'SMW::Parser::ChangeRevision' => [ $this, 'onChangeRevision' ],
Expand Down
67 changes: 67 additions & 0 deletions tests/phpunit/Unit/HooksTest.php
Expand Up @@ -31,6 +31,9 @@ public function testRegister() {
$instance->deregister();
$instance->register();

$this->callOnApprovedRevsRevisionApproved( $instance );
$this->callOnApprovedRevsFileRevisionApproved( $instance );

$this->callOnSMWLinksUpdateApprovedUpdate( $instance );
$this->callOnSMWDataUpdaterSkipUpdate( $instance );
$this->callOnSMWParserChangeRevision( $instance );
Expand All @@ -39,6 +42,70 @@ public function testRegister() {
$this->callOnSMWStoreUpdateDataBefore( $instance );
}

public function callOnApprovedRevsRevisionApproved( $instance ) {

$handler = 'ApprovedRevsRevisionApproved';

$title = $this->getMockBuilder( '\Title' )
->disableOriginalConstructor()
->getMock();

$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
->disableOriginalConstructor()
->getMock();

$cache->expects( $this->once() )
->method( 'save' )
->with( $this->stringContains( 'smw:parseraftertidy' ) );

$instance->setCache( $cache );

$this->assertTrue(
$instance->isRegistered( $handler )
);

$output = '';
$rev_id = 42;
$content = '';

$this->assertThatHookIsExcutable(
$instance->getHandlers( $handler ),
[ $output, $title, $rev_id, $content ]
);
}

public function callOnApprovedRevsFileRevisionApproved( $instance ) {

$handler = 'ApprovedRevsFileRevisionApproved';

$title = $this->getMockBuilder( '\Title' )
->disableOriginalConstructor()
->getMock();

$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
->disableOriginalConstructor()
->getMock();

$cache->expects( $this->once() )
->method( 'save' )
->with( $this->stringContains( 'smw:parseraftertidy' ) );

$instance->setCache( $cache );

$this->assertTrue(
$instance->isRegistered( $handler )
);

$parser = '';
$timestamp = 42;
$sha1 = '1001';

$this->assertThatHookIsExcutable(
$instance->getHandlers( $handler ),
[ $parser, $title, $timestamp, $sha1 ]
);
}

public function callOnSMWLinksUpdateApprovedUpdate( $instance ) {

$handler = 'SMW::LinksUpdate::ApprovedUpdate';
Expand Down

0 comments on commit ad2fa30

Please sign in to comment.