introducing ContentHandler::getPageViewLanguage
authordaniel <daniel.kinzler@wikimedia.de>
Wed, 5 Sep 2012 16:27:32 +0000 (18:27 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Wed, 5 Sep 2012 16:27:32 +0000 (18:27 +0200)
Change-Id: I5b7aabaab48b30a9b6620a0cb4272bc5e348c014

includes/ContentHandler.php
includes/Title.php

index 03e430b..6140824 100644 (file)
@@ -549,10 +549,11 @@ abstract class ContentHandler {
        /**
         * Get the language in which the content of the given page is written.
         *
-        * This default implementation returns $wgContLang->getCode().
+        * This default implementation just returns $wgContLang (except for pages in the MediaWiki namespace)
         *
         * Note that a page's language must be permanent and cacheable, that is, it must not depend
-        * on user preferences, request parameters or session state.
+        * on user preferences, request parameters or session state. The only exception is pages in the
+        * MediaWiki namespace.
         *
         * Also note that the page language may or may not depend on the actual content of the page,
         * that is, this method may load the content in order to determine the language.
@@ -566,9 +567,51 @@ abstract class ContentHandler {
         */
        public function getPageLanguage( Title $title, Content $content = null ) {
                global $wgContLang;
+
+               if ( $title->getNamespace() == NS_MEDIAWIKI ) {
+                       // Parse mediawiki messages with correct target language
+                       list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $title->getText() );
+                       return wfGetLangObj( $lang );
+               }
+
                return $wgContLang;
        }
 
+       /**
+        * Get the language in which the content of this page is written when
+        * viewed by user. Defaults to $this->getPageLanguage(), but if the user
+        * specified a preferred variant, the variant will be used.
+        *
+        * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
+        * the user specified a preferred variant.
+        *
+        * Note that the pages view language is not cacheable, since it depends on user settings.
+        *
+        * Also note that the page language may or may not depend on the actual content of the page,
+        * that is, this method may load the content in order to determine the language.
+        *
+        * @since 1.WD
+        *
+        * @param Title        $title the page to determine the language for.
+        * @param Content|null $content the page's content, if you have it handy, to avoid reloading it.
+        *
+        * @return Language the page's language code for viewing
+        */
+       public function getPageViewLanguage( Title $title, Content $content = null ) {
+               $pageLang = $this->getPageLanguage( $title, $content );
+
+               if ( $title->getNamespace() !== NS_MEDIAWIKI ) {
+                       // If the user chooses a variant, the content is actually
+                       // in a language whose code is the variant code.
+                       $variant = $pageLang->getPreferredVariant();
+                       if ( $pageLang->getCode() !== $variant ) {
+                               $pageLang = Language::factory( $variant );
+                       }
+               }
+
+               return $pageLang;
+       }
+
        /**
         * Returns the name of the diff engine to use.
         *
@@ -1022,6 +1065,17 @@ class JavaScriptContentHandler extends TextContentHandler {
        public function getPageLanguage( Title $title, Content $content = null ) {
                return wfGetLangObj( 'en' );
        }
+
+       /**
+        * Returns the english language, because CSS is english, and should be handled as such.
+        *
+        * @return Language wfGetLangObj( 'en' )
+        *
+        * @see ContentHandler::getPageViewLanguage()
+        */
+       public function getPageViewLanguage( Title $title, Content $content = null ) {
+               return wfGetLangObj( 'en' );
+       }
 }
 
 /**
@@ -1053,4 +1107,15 @@ class CssContentHandler extends TextContentHandler {
        public function getPageLanguage( Title $title, Content $content = null ) {
                return wfGetLangObj( 'en' );
        }
+
+       /**
+        * Returns the english language, because CSS is english, and should be handled as such.
+        *
+        * @return Language wfGetLangObj( 'en' )
+        *
+        * @see ContentHandler::getPageViewLanguage()
+        */
+       public function getPageViewLanguage( Title $title, Content $content = null ) {
+               return wfGetLangObj( 'en' );
+       }
 }
index 7918b5c..15757b0 100644 (file)
@@ -4588,10 +4588,6 @@ class Title {
                if ( $this->isSpecialPage() ) {
                        // special pages are in the user language
                        return $wgLang;
-               } elseif ( $this->getNamespace() == NS_MEDIAWIKI ) {
-                       // Parse mediawiki messages with correct target language
-                       list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $this->getText() );
-                       return wfGetLangObj( $lang );
                }
 
                //TODO: use the LinkCache to cache this!
@@ -4613,19 +4609,24 @@ class Title {
         * @return Language
         */
        public function getPageViewLanguage() {
-               $pageLang = $this->getPageLanguage();
-               // If this is nothing special (so the content is converted when viewed)
-               if ( !$this->isSpecialPage()
-                       && !$this->isCssOrJsPage() && !$this->isCssJsSubpage()
-                       && $this->getNamespace() !== NS_MEDIAWIKI
-               ) {
+               global $wgLang;
+
+               if ( $this->isSpecialPage() ) {
                        // If the user chooses a variant, the content is actually
                        // in a language whose code is the variant code.
-                       $variant = $pageLang->getPreferredVariant();
-                       if ( $pageLang->getCode() !== $variant ) {
-                               $pageLang = Language::factory( $variant );
+                       $variant = $wgLang->getPreferredVariant();
+                       if ( $wgLang->getCode() !== $variant ) {
+                               return Language::factory( $variant );
                        }
+
+                       return $wgLang;
                }
+
+               //NOTE: can't be cached, depends on user settings
+               //NOTE: ContentHandler::getPageViewLanguage() may need to load the content to determine the page language!
+               $contentHandler = ContentHandler::getForTitle( $this );
+               $pageLang = $contentHandler->getPageViewLanguage( $this );
+
                return $pageLang;
        }
 }