From 8b568be5e2dfb889e1eeedbdca50b304864174f7 Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 18 Oct 2012 15:21:34 +0200 Subject: [PATCH] [Bug 41155] Record links on CSS/JS pages in the DB. 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 | 16 ++++++++++++++ includes/content/TextContent.php | 16 ++++++++++++-- tests/phpunit/includes/CssContentTest.php | 16 ++++++++++++-- .../includes/JavascriptContentTest.php | 16 ++++++++++++-- tests/phpunit/includes/TextContentTest.php | 22 +++++++++++++++++-- 5 files changed, 78 insertions(+), 8 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 05dc1d39ee..fdddfc9309 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 * diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php index b561b909e3..5e60b3c62f 100644 --- a/includes/content/TextContent.php +++ b/includes/content/TextContent.php @@ -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; } diff --git a/tests/phpunit/includes/CssContentTest.php b/tests/phpunit/includes/CssContentTest.php index 4f79dfc565..b6e8d293c5 100644 --- a/tests/phpunit/includes/CssContentTest.php +++ b/tests/phpunit/includes/CssContentTest.php @@ -15,8 +15,20 @@ class CssContentTest extends JavascriptContentTest { public function dataGetParserOutput() { return array( - array("MediaWiki:Test.css", null, "hello \n", - "
\nhello <world>\n\n
"), + array( + "MediaWiki:Test.css", + null, + "hello \n", + "
\nhello <world>\n\n
" ), + + array( + "MediaWiki:Test.css", + null, + "/* hello [[world]] */\n", + "
\n/* hello [[world]] */\n\n
", + array( 'Links' => array( // NOTE: assumes default settings for $wgTextModelsToParse + array( 'World' => 0 ) ) ) ), + // @todo: more...? ); } diff --git a/tests/phpunit/includes/JavascriptContentTest.php b/tests/phpunit/includes/JavascriptContentTest.php index b45caa2991..e10d7067b3 100644 --- a/tests/phpunit/includes/JavascriptContentTest.php +++ b/tests/phpunit/includes/JavascriptContentTest.php @@ -15,8 +15,20 @@ class JavascriptContentTest extends TextContentTest { public function dataGetParserOutput() { return array( - array("MediaWiki:Test.js", null, "hello \n", - "
\nhello <world>\n\n
"), + array( + "MediaWiki:Test.js", + null, + "hello \n", + "
\nhello <world>\n\n
" ), + + array( + "MediaWiki:Test.js", + null, + "hello(); // [[world]]\n", + "
\nhello(); // [[world]]\n\n
", + array( 'Links' => array( // NOTE: assumes default settings for $wgTextModelsToParse + array( 'World' => 0 ) ) ) ), + // @todo: more...? ); } diff --git a/tests/phpunit/includes/TextContentTest.php b/tests/phpunit/includes/TextContentTest.php index ee17a75a98..ebadfb56a8 100644 --- a/tests/phpunit/includes/TextContentTest.php +++ b/tests/phpunit/includes/TextContentTest.php @@ -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'' & stuff"), + array( + "TextContentTest_testGetParserOutput", + CONTENT_MODEL_TEXT, + "hello ''world'' & [[stuff]]\n", "hello ''world'' & [[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 } -- 2.20.1