Add Content::matchMagicWord
authordaniel <daniel.kinzler@wikimedia.de>
Tue, 21 Aug 2012 15:08:49 +0000 (17:08 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Tue, 21 Aug 2012 16:10:29 +0000 (18:10 +0200)
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

includes/Content.php
includes/job/DoubleRedirectJob.php
tests/phpunit/includes/JavascriptContentTest.php
tests/phpunit/includes/WikitextContentTest.php

index 54d8110..38d2e27 100644 (file)
@@ -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() );
+       }
 }
 
 /**
index 6a81b27..7acc4f2 100644 (file)
@@ -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
index cb40e76..84cde8c 100644 (file)
@@ -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() {
index b5b2fb5..602a9a2 100644 (file)
@@ -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() {