From fe27182d64e4a5d6d3f61d81c0b03909533e5d8b Mon Sep 17 00:00:00 2001 From: daniel Date: Thu, 4 Oct 2012 13:00:05 +0200 Subject: [PATCH] Use canonical parser options per default Change-Id: I2cbfaab2e15d9a1c1061f9ea31c7a8ebbeffdbd0 --- includes/WikiPage.php | 17 ++++---------- includes/content/ContentHandler.php | 35 ++++++++++++++++++++++++++++ includes/content/WikitextContent.php | 4 ++-- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/includes/WikiPage.php b/includes/WikiPage.php index 5d7bb7c19c..c6fced0b24 100644 --- a/includes/WikiPage.php +++ b/includes/WikiPage.php @@ -1894,6 +1894,8 @@ class WikiPage extends Page implements IDBAccessObject { /** * Get parser options suitable for rendering the primary article wikitext * + * @see ContentHandler::makeParserOptions + * * @param IContextSource|User|string $context One of the following: * - IContextSource: Use the User and the Language of the provided * context @@ -1904,24 +1906,13 @@ class WikiPage extends Page implements IDBAccessObject { * @return ParserOptions */ public function makeParserOptions( $context ) { - global $wgContLang; - - if ( $context instanceof IContextSource ) { - $options = ParserOptions::newFromContext( $context ); - } elseif ( $context instanceof User ) { // settings per user (even anons) - $options = ParserOptions::newFromUser( $context ); - } else { // canonical settings - $options = ParserOptions::newFromUserAndLang( new User, $wgContLang ); - } + $options = $this->getContentHandler()->makeParserOptions( $context ); if ( $this->getTitle()->isConversionTable() ) { - //@todo: ConversionTable should become a separate content model. + //@todo: ConversionTable should become a separate content model, so we don't need special cases like this one. $options->disableContentConversion(); } - $options->enableLimitReport(); // show inclusion/loop reports - $options->setTidy( true ); // fix bad HTML - return $options; } diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index bf1349adc8..63c26a855e 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -873,6 +873,41 @@ abstract class ContentHandler { return $undone_content; } + /** + * Get parser options suitable for rendering the primary article wikitext + * + * @param IContextSource|User|string $context One of the following: + * - IContextSource: Use the User and the Language of the provided + * context + * - User: Use the provided User object and $wgLang for the language, + * so use an IContextSource object if possible. + * - 'canonical': Canonical options (anonymous user with default + * preferences and content language). + * + * @param IContextSource|User|string $context + * + * @throws MWException + * @return ParserOptions + */ + public function makeParserOptions( $context ) { + global $wgContLang; + + if ( $context instanceof IContextSource ) { + $options = ParserOptions::newFromContext( $context ); + } elseif ( $context instanceof User ) { // settings per user (even anons) + $options = ParserOptions::newFromUser( $context ); + } elseif ( $context === 'canonical' ) { // canonical settings + $options = ParserOptions::newFromUserAndLang( new User, $wgContLang ); + } else { + throw new MWException( "Bad context for parser options: $context" ); + } + + $options->enableLimitReport(); // show inclusion/loop reports + $options->setTidy( true ); // fix bad HTML + + return $options; + } + /** * Returns true for content models that support caching using the * ParserCache mechanism. See WikiPage::isParserCacheUser(). diff --git a/includes/content/WikitextContent.php b/includes/content/WikitextContent.php index 8e72889917..5de09b65fc 100644 --- a/includes/content/WikitextContent.php +++ b/includes/content/WikitextContent.php @@ -237,7 +237,6 @@ class WikitextContent extends TextContent { return $truncatedtext; } - /** * Returns a ParserOutput object resulting from parsing the content's text * using $wgParser. @@ -260,7 +259,8 @@ class WikitextContent extends TextContent { global $wgParser; if ( !$options ) { - $options = new ParserOptions(); + //NOTE: use canonical options per default to produce cacheable output + $options = $this->getContentHandler()->makeParserOptions( 'canonical' ); } $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId ); -- 2.20.1