From: daniel Date: Tue, 21 Aug 2012 15:08:49 +0000 (+0200) Subject: Add Content::matchMagicWord X-Git-Tag: 1.31.0-rc.0~22097^2^2~50^2 X-Git-Url: https://git.cyclocoop.org/%242?a=commitdiff_plain;h=ac8150cd6ff073f93991564992a0e44641bc957e;p=lhc%2Fweb%2Fwiklou.git Add Content::matchMagicWord Allows Content objects to control how magic worlds are applied to it. Useful mainly when magic words are used outside the parser. Change-Id: I9711f580f8beadfb9f93a706f87c97c7cf667f6c --- diff --git a/includes/Content.php b/includes/Content.php index 54d8110cdb..38d2e275b3 100644 --- a/includes/Content.php +++ b/includes/Content.php @@ -432,6 +432,15 @@ interface Content { public function getDeletionUpdates( Title $title, ParserOutput $parserOutput = null ); + /** + * Returns true if this Content object matches the given magic word. + * + * @param MagicWord $word the magic word to match + * + * @return bool whether this Content object matches the given magic word. + */ + public function matchMagicWord( MagicWord $word ); + # TODO: handle ImagePage and CategoryPage # TODO: make sure we cover lucene search / wikisearch. # TODO: make sure ReplaceTemplates still works @@ -730,9 +739,7 @@ abstract class AbstractContent implements Content { } /** - * Returns a list of updates to perform when this content is deleted. - * The necessary updates may be taken from the Content object, or depend on - * the current state of the database. + * @see Content::getDeletionUpdates() * * @since WD.1 * @@ -751,6 +758,19 @@ abstract class AbstractContent implements Content { new LinksDeletionUpdate( $title ), ); } + + /** + * @see Content::matchMagicWord() + * + * This default implementation always returns false. Subclasses may override this to supply matching logic. + * + * @param MagicWord $word + * + * @return bool + */ + public function matchMagicWord( MagicWord $word ) { + return false; + } } /** @@ -930,7 +950,6 @@ abstract class TextContent extends AbstractContent { # TODO: make Highlighter interface, use highlighter here, if available return htmlspecialchars( $this->getNativeData() ); } - } /** @@ -1182,7 +1201,18 @@ class WikitextContent extends TextContent { ); } - + /** + * @see Content::matchMagicWord() + * + * This implementation calls $word->match() on the this TextContent object's text. + * + * @param MagicWord $word + * + * @return bool whether this Content object matches the given magic word. + */ + public function matchMagicWord( MagicWord $word ) { + return $word->match( $this->getNativeData() ); + } } /** diff --git a/includes/job/DoubleRedirectJob.php b/includes/job/DoubleRedirectJob.php index 6a81b27754..7acc4f28c4 100644 --- a/includes/job/DoubleRedirectJob.php +++ b/includes/job/DoubleRedirectJob.php @@ -102,9 +102,8 @@ class DoubleRedirectJob extends Job { } # Check for a suppression tag (used e.g. in periodically archived discussions) - $text = ContentHandler::getContentText( $content ); $mw = MagicWord::get( 'staticredirect' ); - if ( $mw->match( $text ) ) { #FIXME: add support for this to ContentHandler/Content + if ( $content->matchMagicWord( $mw ) ) { wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" ); return true; } @@ -125,6 +124,8 @@ class DoubleRedirectJob extends Job { $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(), $currentDest->getFragment() ); + $text = ContentHandler::getContentText( $content ); #FIXME: get rid of this! + # Fix the text # Remember that redirect pages can have categories, templates, etc., # so the regex has to be fairly general diff --git a/tests/phpunit/includes/JavascriptContentTest.php b/tests/phpunit/includes/JavascriptContentTest.php index cb40e764d9..84cde8cfbb 100644 --- a/tests/phpunit/includes/JavascriptContentTest.php +++ b/tests/phpunit/includes/JavascriptContentTest.php @@ -214,6 +214,13 @@ class JavascriptContentTest extends WikitextContentTest { ); } + public function testMatchMagicWord( ) { + $mw = MagicWord::get( "staticredirect" ); + + $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" ); + $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word, since it's not wikitext" ); + } + # ================================================================================================================= public function testGetModel() { diff --git a/tests/phpunit/includes/WikitextContentTest.php b/tests/phpunit/includes/WikitextContentTest.php index b5b2fb52a3..602a9a2c75 100644 --- a/tests/phpunit/includes/WikitextContentTest.php +++ b/tests/phpunit/includes/WikitextContentTest.php @@ -419,6 +419,16 @@ just a test" $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() ); } + public function testMatchMagicWord( ) { + $mw = MagicWord::get( "staticredirect" ); + + $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" ); + $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" ); + + $content = $this->newContent( "#REDIRECT [[FOO]]" ); + $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" ); + } + # ================================================================================================================= public function testGetModel() {