[Bug 41155] Record links on CSS/JS pages in the DB.
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 18 Oct 2012 13:21:34 +0000 (15:21 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Thu, 18 Oct 2012 18:21:04 +0000 (20:21 +0200)
ContentHandler removed wikitext parsing for CSS and JS pages.
However, people seem to rely on links and categories embedded
in script comments.

Change-Id: I0736f15878fbd3292e75854bf16f04df656ce363

includes/DefaultSettings.php
includes/content/TextContent.php
tests/phpunit/includes/CssContentTest.php
tests/phpunit/includes/JavascriptContentTest.php
tests/phpunit/includes/TextContentTest.php

index 05dc1d3..fdddfc9 100644 (file)
@@ -6324,6 +6324,22 @@ $wgContentHandlerTextFallback = 'ignore';
  */
 $wgContentHandlerUseDB = false;
 
+/**
+ * Determines which types of text are parsed as wikitext. This does not imply that these kinds
+ * of texts are also rendered as wikitext, it only means that links, magic words, etc will have
+ * the effect on the database they would have on a wikitext page.
+ *
+ * @todo: On the long run, it would be nice to put categories etc into a separate structure,
+ * or at least parse only the contents of comments in the scripts.
+ *
+ * @since 1.21
+ */
+$wgTextModelsToParse = array(
+       CONTENT_MODEL_WIKITEXT,    // Just for completeness, wikitext will always be parsed.
+       CONTENT_MODEL_JAVASCRIPT,  // Make categories etc work, people put them into comments.
+       CONTENT_MODEL_CSS,         // Make categories etc work, people put them into comments.
+);
+
 /**
  * Whether the user must enter their password to change their e-mail address
  *
index b561b90..5e60b3c 100644 (file)
@@ -164,7 +164,19 @@ class TextContent extends AbstractContent {
                $revId = null,
                ParserOptions $options = null, $generateHtml = true
        ) {
-               # Generic implementation, relying on $this->getHtml()
+               global $wgParser, $wgTextModelsToParse;
+
+               if ( !$options ) {
+                       //NOTE: use canonical options per default to produce cacheable output
+                       $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
+               }
+
+               if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
+                       // parse just to get links etc into the database
+                       $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
+               } else {
+                       $po = new ParserOutput();
+               }
 
                if ( $generateHtml ) {
                        $html = $this->getHtml();
@@ -172,7 +184,7 @@ class TextContent extends AbstractContent {
                        $html = '';
                }
 
-               $po = new ParserOutput( $html );
+               $po->setText( $html );
                return $po;
        }
 
index 4f79dfc..b6e8d29 100644 (file)
@@ -15,8 +15,20 @@ class CssContentTest extends JavascriptContentTest {
 
        public function dataGetParserOutput() {
                return array(
-                       array("MediaWiki:Test.css", null, "hello <world>\n",
-                               "<pre class=\"mw-code mw-css\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"),
+                       array(
+                               "MediaWiki:Test.css",
+                               null,
+                               "hello <world>\n",
+                               "<pre class=\"mw-code mw-css\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>" ),
+
+                       array(
+                               "MediaWiki:Test.css",
+                               null,
+                               "/* hello [[world]] */\n",
+                               "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n/* hello [[world]] */\n\n</pre>",
+                               array( 'Links' => array( // NOTE: assumes default settings for $wgTextModelsToParse
+                                       array( 'World' => 0 ) ) ) ),
+
                        // @todo: more...?
                );
        }
index b45caa2..e10d706 100644 (file)
@@ -15,8 +15,20 @@ class JavascriptContentTest extends TextContentTest {
 
        public function dataGetParserOutput() {
                return array(
-                       array("MediaWiki:Test.js", null, "hello <world>\n",
-                                       "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>"),
+                       array(
+                               "MediaWiki:Test.js",
+                               null,
+                               "hello <world>\n",
+                               "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello &lt;world&gt;\n\n</pre>" ),
+
+                       array(
+                               "MediaWiki:Test.js",
+                               null,
+                               "hello(); // [[world]]\n",
+                               "<pre class=\"mw-code mw-js\" dir=\"ltr\">\nhello(); // [[world]]\n\n</pre>",
+                               array( 'Links' => array( // NOTE: assumes default settings for $wgTextModelsToParse
+                                                       array( 'World' => 0 ) ) ) ),
+
                        // @todo: more...?
                );
        }
index ee17a75..ebadfb5 100644 (file)
@@ -27,7 +27,11 @@ class TextContentTest extends MediaWikiTestCase {
 
        public function dataGetParserOutput() {
                return array(
-                       array("TextContentTest_testGetParserOutput", CONTENT_MODEL_TEXT, "hello ''world'' & stuff\n", "hello ''world'' &amp; stuff"),
+                       array(
+                               "TextContentTest_testGetParserOutput",
+                               CONTENT_MODEL_TEXT,
+                               "hello ''world'' & [[stuff]]\n", "hello ''world'' &amp; [[stuff]]",
+                               array( 'Links' => array() ) ),
                        // @todo: more...?
                );
        }
@@ -35,7 +39,7 @@ class TextContentTest extends MediaWikiTestCase {
        /**
         * @dataProvider dataGetParserOutput
         */
-       public function testGetParserOutput( $title, $model, $text, $expectedHtml ) {
+       public function testGetParserOutput( $title, $model, $text, $expectedHtml, $expectedFields = null ) {
                $title = Title::newFromText( $title );
                $content = ContentHandler::makeContent( $text, $title, $model );
 
@@ -45,6 +49,20 @@ class TextContentTest extends MediaWikiTestCase {
                $html = preg_replace( '#<!--.*?-->#sm', '', $html ); // strip comments
 
                $this->assertEquals( $expectedHtml, trim( $html ) );
+
+               if ( $expectedFields ) {
+                       foreach ( $expectedFields as $field => $exp ) {
+                               $f = 'get' . ucfirst( $field );
+                               $v = call_user_func( array( $po, $f ) );
+
+                               if ( is_array( $exp ) ) {
+                                       $this->assertArrayEquals( $exp, $v );
+                               } else {
+                                       $this->assertEquals( $exp, $v );
+                               }
+                       }
+               }
+
                // @todo: assert more properties
        }