Skip to content

Commit

Permalink
ParserAfterTidy, listen to external event (#3770)
Browse files Browse the repository at this point in the history
  • Loading branch information
mwjames committed Mar 9, 2019
1 parent d530f11 commit a734f62
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 44 deletions.
4 changes: 3 additions & 1 deletion src/MediaWiki/Hooks.php
Expand Up @@ -275,7 +275,9 @@ public function onParserAfterTidy( &$parser, &$text ) {
$applicationFactory = ApplicationFactory::getInstance();

$parserAfterTidy = new ParserAfterTidy(
$parser
$parser,
$applicationFactory->getNamespaceExaminer(),
$applicationFactory->getCache()
);

$parserAfterTidy->isCommandLineMode(
Expand Down
9 changes: 5 additions & 4 deletions src/MediaWiki/Hooks/ArticlePurge.php
Expand Up @@ -26,6 +26,8 @@ class ArticlePurge {

use EventDispatcherAwareTrait;

const CACHE_NAMESPACE = 'smw:arc';

/**
* @since 1.9
*
Expand All @@ -36,16 +38,15 @@ public function process( WikiPage &$wikiPage ) {
$applicationFactory = ApplicationFactory::getInstance();

$title = $wikiPage->getTitle();
$pageId = $title->getArticleID();
$articleID = $title->getArticleID();

$settings = $applicationFactory->getSettings();

$cache = $applicationFactory->getCache();
$cacheFactory = $applicationFactory->newCacheFactory();

if ( $pageId > 0 ) {
if ( $articleID > 0 ) {
$cache->save(
$cacheFactory->getPurgeCacheKey( $pageId ),
smwfCacheKey( self::CACHE_NAMESPACE, $articleID ),
$settings->get( 'smwgAutoRefreshOnPurge' )
);
}
Expand Down
36 changes: 28 additions & 8 deletions src/MediaWiki/Hooks/ParserAfterTidy.php
Expand Up @@ -7,6 +7,8 @@
use SMW\MediaWiki\MediaWiki;
use SMW\ParserData;
use SMW\SemanticData;
use Onoi\Cache\Cache;
use SMW\NamespaceExaminer;

/**
* Hook: ParserAfterTidy to add some final processing to the
Expand All @@ -21,6 +23,8 @@
*/
class ParserAfterTidy extends HookHandler {

const CACHE_NAMESPACE = 'smw:parseraftertidy';

/**
* @var Parser
*/
Expand All @@ -31,6 +35,11 @@ class ParserAfterTidy extends HookHandler {
*/
private $namespaceExaminer;

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

/**
* @var boolean
*/
Expand All @@ -45,10 +54,13 @@ class ParserAfterTidy extends HookHandler {
* @since 1.9
*
* @param Parser $parser
* @param NamespaceExaminer $NamespaceExaminer
* @param Cache $cache
*/
public function __construct( Parser &$parser ) {
public function __construct( Parser &$parser, NamespaceExaminer $namespaceExaminer, Cache $cache ) {
$this->parser = $parser;
$this->namespaceExaminer = ApplicationFactory::getInstance()->getNamespaceExaminer();
$this->namespaceExaminer = $namespaceExaminer;
$this->cache = $cache;
}

/**
Expand Down Expand Up @@ -136,6 +148,15 @@ private function canPerformUpdate() {
return true;
}

// Allow an external event to trigger a processing,if set so that
// the an update can happen when for example as part of a programtic
// purge request even when no text (or annotations) are available
$key = smwfCacheKey( self::CACHE_NAMESPACE, $title->getPrefixedDBKey() );

if( $this->cache->fetch( $key ) !== false ) {
return true;
}

return false;
}

Expand Down Expand Up @@ -230,15 +251,14 @@ private function addPropertyAnnotations( $propertyAnnotatorFactory, $semanticDat
*/
private function checkPurgeRequest( $parserData ) {

$cache = ApplicationFactory::getInstance()->getCache();
$start = microtime( true );
$title = $this->parser->getTitle();

$key = ApplicationFactory::getInstance()->getCacheFactory()->getPurgeCacheKey(
$this->parser->getTitle()->getArticleID()
);
$key = smwfCacheKey( ArticlePurge::CACHE_NAMESPACE, $title->getArticleID() );

if( $cache->contains( $key ) && $cache->fetch( $key ) ) {
$cache->delete( $key );
if( $this->cache->contains( $key ) && $this->cache->fetch( $key ) ) {
$this->cache->delete( $key );
$this->cache->delete( smwfCacheKey( self::CACHE_NAMESPACE, $title->getPrefixedDBKey() ) );

// Avoid a Parser::lock for when a PurgeRequest remains intact
// during an update process while being executed from the cmdLine
Expand Down
12 changes: 11 additions & 1 deletion tests/phpunit/Unit/CacheFactoryTest.php
Expand Up @@ -60,7 +60,7 @@ public function testGetPurgeCacheKey() {
->disableOriginalConstructor()
->getMock();

$title->expects( $this->once() )
$title->expects( $this->any() )
->method( 'getArticleID' )
->will( $this->returnValue( 42 ) );

Expand All @@ -70,6 +70,16 @@ public function testGetPurgeCacheKey() {
'string',
$instance->getPurgeCacheKey( $title )
);

$this->assertSame(
smwfCacheKey( 'smw:arc', 42 ),
$instance->getPurgeCacheKey( $title )
);

$this->assertSame(
smwfCacheKey( \SMW\MediaWiki\Hooks\ArticlePurge::CACHE_NAMESPACE, 42 ),
$instance->getPurgeCacheKey( $title )
);
}

public function testCanConstructCacheOptions() {
Expand Down
131 changes: 101 additions & 30 deletions tests/phpunit/Unit/MediaWiki/Hooks/ParserAfterTidyTest.php
Expand Up @@ -25,6 +25,9 @@ class ParserAfterTidyTest extends \PHPUnit_Framework_TestCase {
private $parserFactory;
private $spyLogger;
private $testEnvironment;
private $parser;
private $namespaceExaminer;
private $cache;

protected function setUp() {
parent::setUp();
Expand All @@ -49,6 +52,18 @@ protected function setUp() {
$this->testEnvironment->registerObject( 'Store', $store );

$this->applicationFactory = ApplicationFactory::getInstance();

$this->parser = $this->getMockBuilder( '\Parser' )
->disableOriginalConstructor()
->getMock();

$this->namespaceExaminer = $this->getMockBuilder( '\SMW\NamespaceExaminer' )
->disableOriginalConstructor()
->getMock();

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

protected function tearDown() {
Expand All @@ -58,26 +73,23 @@ protected function tearDown() {

public function testCanConstruct() {

$parser = $this->getMockBuilder( 'Parser' )
->disableOriginalConstructor()
->getMock();

$this->assertInstanceOf(
'\SMW\MediaWiki\Hooks\ParserAfterTidy',
new ParserAfterTidy( $parser )
ParserAfterTidy::class,
new ParserAfterTidy( $this->parser, $this->namespaceExaminer, $this->cache )
);
}

public function testIsReadOnly() {

$parser = $this->getMockBuilder( 'Parser' )
->disableOriginalConstructor()
->getMock();

$parser->expects( $this->never() )
$this->parser->expects( $this->never() )
->method( 'getTitle' );

$instance = new ParserAfterTidy( $parser );
$instance = new ParserAfterTidy(
$this->parser,
$this->namespaceExaminer,
$this->cache
);

$instance->isReadOnly( true );

$text = '';
Expand All @@ -86,16 +98,10 @@ public function testIsReadOnly() {

public function testNotEnabledNamespace() {

$namespaceExaminer = $this->getMockBuilder( '\SMW\NamespaceExaminer' )
->disableOriginalConstructor()
->getMock();

$namespaceExaminer->expects( $this->once() )
$this->namespaceExaminer->expects( $this->once() )
->method( 'isSemanticEnabled' )
->will( $this->returnValue( false ) );

$this->testEnvironment->registerObject( 'NamespaceExaminer', $namespaceExaminer );

$title = MockTitle::buildMock( __METHOD__ );

$title->expects( $this->atLeastOnce() )
Expand All @@ -111,15 +117,15 @@ public function testNotEnabledNamespace() {
$title->expects( $this->never() )
->method( 'isSpecialPage' );

$parser = $this->getMockBuilder( 'Parser' )
->disableOriginalConstructor()
->getMock();

$parser->expects( $this->any() )
$this->parser->expects( $this->any() )
->method( 'getTitle' )
->will( $this->returnValue( $title ) );

$instance = new ParserAfterTidy( $parser );
$instance = new ParserAfterTidy(
$this->parser,
$this->namespaceExaminer,
$this->cache
);

$text = '';
$instance->process( $text );
Expand All @@ -129,7 +135,7 @@ private function newMockCache( $id, $containsStatus, $fetchStatus ) {

$key = $this->applicationFactory->newCacheFactory()->getPurgeCacheKey( $id );

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

Expand All @@ -151,6 +157,10 @@ private function newMockCache( $id, $containsStatus, $fetchStatus ) {
*/
public function testProcess( $parameters ) {

$this->namespaceExaminer->expects( $this->once() )
->method( 'isSemanticEnabled' )
->will( $this->returnValue( true ) );

$this->testEnvironment->registerObject( 'Store', $parameters['store'] );

$wikiPage = $this->getMockBuilder( '\WikiPage' )
Expand Down Expand Up @@ -181,8 +191,6 @@ public function testProcess( $parameters ) {
$parameters['cache-fetch']
);

$this->testEnvironment->registerObject( 'Cache', $cache );

$parser = $this->parserFactory->newFromTitle( $parameters['title'] );

$parser->getOutput()->setProperty(
Expand All @@ -197,15 +205,74 @@ public function testProcess( $parameters ) {

$text = '';

$instance = new ParserAfterTidy( $parser );
$instance = new ParserAfterTidy(
$parser,
$this->namespaceExaminer,
$cache
);

$this->assertTrue(
$instance->process( $text )
);
}

public function testCanPerformOnExternalEvent() {

$parserOptions = $this->getMockBuilder( '\ParserOptions' )
->disableOriginalConstructor()
->getMock();

$parserOutput = $this->getMockBuilder( '\ParserOutput' )
->disableOriginalConstructor()
->getMock();

$parserOutput->expects( $this->any() )
->method( 'getCategoryLinks' )
->will( $this->returnValue( [] ) );

$parserOutput->expects( $this->any() )
->method( 'getImages' )
->will( $this->returnValue( [] ) );

$this->cache->expects( $this->any() )
->method( 'fetch' )
->with( $this->stringContains( "smw:parseraftertidy" ) )
->will( $this->returnValue( true ) );

$this->namespaceExaminer->expects( $this->once() )
->method( 'isSemanticEnabled' )
->will( $this->returnValue( true ) );

$text = '';
$title = Title::newFromText( __METHOD__ );

$this->parser->expects( $this->any() )
->method( 'getTitle' )
->will( $this->returnValue( $title ) );

$this->parser->expects( $this->any() )
->method( 'getOptions' )
->will( $this->returnValue( $parserOptions ) );

$this->parser->expects( $this->any() )
->method( 'getOutput' )
->will( $this->returnValue( $parserOutput ) );

$instance = new ParserAfterTidy(
$this->parser,
$this->namespaceExaminer,
$this->cache
);

$instance->process( $text );
}

public function testSemanticDataParserOuputUpdateIntegration() {

$this->namespaceExaminer->expects( $this->once() )
->method( 'isSemanticEnabled' )
->will( $this->returnValue( true ) );

$settings = [
'smwgMainCacheType' => 'hash',
'smwgEnableUpdateJobs' => false,
Expand All @@ -224,7 +291,11 @@ public function testSemanticDataParserOuputUpdateIntegration() {
$parser->getOutput()->addCategory( 'Bar', 'Bar' );
$parser->getOutput()->setProperty( 'smw-semanticdata-status', true );

$instance = new ParserAfterTidy( $parser );
$instance = new ParserAfterTidy(
$parser,
$this->namespaceExaminer,
$this->cache
);

$this->assertTrue(
$instance->process( $text )
Expand Down

0 comments on commit a734f62

Please sign in to comment.