Use canonical parser options per default
authordaniel <daniel.kinzler@wikimedia.de>
Thu, 4 Oct 2012 11:00:05 +0000 (13:00 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Thu, 4 Oct 2012 11:00:05 +0000 (13:00 +0200)
Change-Id: I2cbfaab2e15d9a1c1061f9ea31c7a8ebbeffdbd0

includes/WikiPage.php
includes/content/ContentHandler.php
includes/content/WikitextContent.php

index 5d7bb7c..c6fced0 100644 (file)
@@ -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;
        }
 
index bf1349a..63c26a8 100644 (file)
@@ -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().
index 8e72889..5de09b6 100644 (file)
@@ -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 );