SPQRobin has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/137033

Change subject: Improve page language functions
......................................................................

Improve page language functions

First patch; work in progress

Change-Id: I0ad0e4f47cf7624067f89384f67bfe76c57bd820
---
M includes/Title.php
M includes/content/ContentHandler.php
M includes/content/CssContentHandler.php
M includes/content/JavaScriptContentHandler.php
4 files changed, 126 insertions(+), 78 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/33/137033/1

diff --git a/includes/Title.php b/includes/Title.php
index d8f6b75..88ea65e 100644
--- a/includes/Title.php
+++ b/includes/Title.php
@@ -150,6 +150,8 @@
        /** @var bool The (string) language code of the page's language and 
content code. */
        private $mPageLanguage = false;
 
+       private $mDBPageLanguage = null;
+
        /** @var TitleValue A corresponding TitleValue object */
        private $mTitleValue = null;
        // @}
@@ -438,6 +440,9 @@
                                $this->mContentModel = strval( 
$row->page_content_model );
                        } else {
                                $this->mContentModel = false; # initialized 
lazily in getContentModel()
+                       }
+                       if ( isset( $row->page_lang ) ) {
+                               $this->mDBPageLanguage = 
(string)$row->page_lang;
                        }
                } else { // page not found
                        $this->mArticleID = 0;
@@ -4958,6 +4963,71 @@
        }
 
        /**
+        * @since 1.24
+        * @return array 'pagelanguage', 'pageviewlanguage', 'usedb'
+        */
+       public function getPageLanguageSettings() {
+               global $wgContLang, $wgLang, $wgLanguageCode;
+               wfProfileIn( __METHOD__ );
+               $settings = array(
+                       'pagelanguage' => $wgContLang,
+                       'pageviewlanguage' => 'pagelanguage',
+                       'usedb' => false, // whether it is changeable on-wiki 
using the DB
+               );
+
+               if( $this->isSpecialPage() ) {
+                       // special pages are in the user language
+                       $settings['pagelanguage'] = 
$settings['pageviewlanguage'] = $wgLang;
+                       return $settings;
+                       wfProfileOut( __METHOD__ );
+               }
+
+               $contentHandler = ContentHandler::getForTitle( $this );
+
+               // backwards compatibility for ContentHandlers that are not yet 
updated
+               if( !method_exists( $contentHandler, 'getPageLanguageSettings' 
) ) {
+                       $settings['pagelanguage'] = 
$contentHandler->getPageLanguage();
+                       $settings['pageviewlanguage'] = 
$contentHandler->getPageViewLanguage();
+               } else {
+                       $settings = $contentHandler->getPageLanguageSettings( 
$settings, $this );
+               }
+
+               if ( !$this->mPageLanguage || $this->mPageLanguage[1] !== 
$wgLanguageCode ) {
+                       // NOTE: ContentHandler::getPageLanguage() may need to 
load the
+                       // content to determine the page language!
+                       // Checking $wgLanguageCode hasn't changed for the 
benefit of unit
+                       // tests.
+                       $contentHandler = ContentHandler::getForTitle( $this );
+                       $langObj = wfGetLangObj( $settings['pagelanguage'] );
+                       $this->mPageLanguage = array( $langObj->getCode(), 
$wgLanguageCode );
+               } else {
+                       $settings['pagelanguage'] = wfGetLangObj( 
$this->mPageLanguage[0] );
+               }
+
+               if( $this->mDBPageLanguage ) {
+                       // if the page content lang is defined on-wiki, use that
+                       $settings['pagelanguage'] = wfGetLangObj( 
$this->mDBPageLanguage );
+               }
+
+               if( $settings['pageviewlanguage'] == 'pagelanguage' ) {
+                       // if not explicitly set, it should probably assume
+                       // it is a normal text/string that can be converted
+                       // when the user chooses a variant.
+                       // Then the content displayed is actually
+                       // in a language whose code is the variant code.
+                       $variant = 
$settings['pagelanguage']->getPreferredVariant();
+                       if ( $settings['pagelanguage']->getCode() !== $variant 
) {
+                               $settings['pageviewlanguage'] = 
Language::factory( $variant );
+                       } else {
+                               $settings['pageviewlanguage'] = 
$settings['pagelanguage'];
+                       }
+               }
+               wfProfileOut( __METHOD__ );
+
+               return $settings;
+       }
+
+       /**
         * Get the language in which the content of this page is written in
         * wikitext. Defaults to $wgContLang, but in certain cases it can be
         * e.g. $wgLang (such as special pages, which are in the user language).
@@ -4966,29 +5036,8 @@
         * @return Language
         */
        public function getPageLanguage() {
-               global $wgLang, $wgLanguageCode;
-               wfProfileIn( __METHOD__ );
-               if ( $this->isSpecialPage() ) {
-                       // special pages are in the user language
-                       wfProfileOut( __METHOD__ );
-                       return $wgLang;
-               }
-
-               if ( !$this->mPageLanguage || $this->mPageLanguage[1] !== 
$wgLanguageCode ) {
-                       // Note that this may depend on user settings, so the 
cache should
-                       // be only per-request.
-                       // NOTE: ContentHandler::getPageLanguage() may need to 
load the
-                       // content to determine the page language!
-                       // Checking $wgLanguageCode hasn't changed for the 
benefit of unit
-                       // tests.
-                       $contentHandler = ContentHandler::getForTitle( $this );
-                       $langObj = wfGetLangObj( 
$contentHandler->getPageLanguage( $this ) );
-                       $this->mPageLanguage = array( $langObj->getCode(), 
$wgLanguageCode );
-               } else {
-                       $langObj = wfGetLangObj( $this->mPageLanguage[0] );
-               }
-               wfProfileOut( __METHOD__ );
-               return $langObj;
+               $settings = $this->getPageLanguageSettings();
+               return $settings['pagelanguage'];
        }
 
        /**
@@ -5000,25 +5049,8 @@
         * @return Language
         */
        public function getPageViewLanguage() {
-               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 = $wgLang->getPreferredVariant();
-                       if ( $wgLang->getCode() !== $variant ) {
-                               return Language::factory( $variant );
-                       }
-
-                       return $wgLang;
-               }
-
-               // @note Can't be cached persistently, 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;
+               $settings = $this->getPageLanguageSettings();
+               return $settings['pageviewlanguage'];
        }
 
        /**
diff --git a/includes/content/ContentHandler.php 
b/includes/content/ContentHandler.php
index b5b4d13..506ed9c 100644
--- a/includes/content/ContentHandler.php
+++ b/includes/content/ContentHandler.php
@@ -643,7 +643,44 @@
         * 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.24
+        *
+        * @param array $settings The settings array from 
Title::getPageLanguageSettings().
+        * @param Title $title The page to determine the language for.
+        * @param Content $content The page's content, if you have it handy, to 
avoid reloading it.
+        *
+        * @return Language The page's language
+        */
+       public function getPageLanguageSettings( $settings, Title $title, 
$content = null ) {
+               global $wgLang;
+               if ( $title->getNamespace() == NS_MEDIAWIKI ) {
+                       // Parse mediawiki messages with correct target language
+                       list( /* $unused */, $lang ) = 
MessageCache::singleton()->figureMessage( $title->getText() );
+                       $settings['pagelanguage'] = wfGetLangObj( $lang );
+               } else {
+                       // it's a normal wikitext page, let users set the 
language on-wiki
+                       $settings['usedb'] = true;
+               }
+
+               wfRunHooks( 'PageContentLanguage', array( $title, 
&$settings['pagelanguage'], $wgLang ) );
+
+               return $settings;
+       }
+
+       /**
+        * Get the language in which the content of the given page is written.
+        *
+        * This default implementation just returns $wgContLang (except for 
pages
+        * in the MediaWiki namespace)
+        *
+        * Note that the pages language is not cacheable, since it may in some
+        * cases depend 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.21
+        * @deprecated 1.24
         *
         * @param Title $title The page to determine the language for.
         * @param Content $content The page's content, if you have it handy, to 
avoid reloading it.
@@ -679,6 +716,7 @@
         * that is, this method may load the content in order to determine the 
language.
         *
         * @since 1.21
+        * @deprecated 1.24
         *
         * @param Title $title The page to determine the language for.
         * @param Content $content The page's content, if you have it handy, to 
avoid reloading it.
diff --git a/includes/content/CssContentHandler.php 
b/includes/content/CssContentHandler.php
index 85059a8..7217777 100644
--- a/includes/content/CssContentHandler.php
+++ b/includes/content/CssContentHandler.php
@@ -62,29 +62,18 @@
        /**
         * Returns the english language, because CSS is english, and should be 
handled as such.
         *
+        * @param array $settings
         * @param Title $title
         * @param Content $content
         *
-        * @return Language wfGetLangObj( 'en' )
+        * @return array
         *
-        * @see ContentHandler::getPageLanguage()
+        * @see ContentHandler::getPageLanguageSettings()
         */
-       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.
-        *
-        * @param Title $title
-        * @param Content $content
-        *
-        * @return Language wfGetLangObj( 'en' )
-        *
-        * @see ContentHandler::getPageViewLanguage()
-        */
-       public function getPageViewLanguage( Title $title, Content $content = 
null ) {
-               return wfGetLangObj( 'en' );
+       public function getPageLanguageSettings( $settings, Title $title, 
Content $content = null ) {
+               $settings['pagelanguage'] = wfGetLangObj( 'en' );
+               $settings['pageviewlanguage'] = wfGetLangObj( 'en' );
+               return $settings;
        }
 
 }
diff --git a/includes/content/JavaScriptContentHandler.php 
b/includes/content/JavaScriptContentHandler.php
index 2e98976..7a94023 100644
--- a/includes/content/JavaScriptContentHandler.php
+++ b/includes/content/JavaScriptContentHandler.php
@@ -62,29 +62,18 @@
        /**
         * Returns the english language, because JS is english, and should be 
handled as such.
         *
+        * @param array $settings
         * @param Title $title
         * @param Content $content
         *
-        * @return Language wfGetLangObj( 'en' )
+        * @return array
         *
-        * @see ContentHandler::getPageLanguage()
+        * @see ContentHandler::getPageLanguageSettings()
         */
-       public function getPageLanguage( Title $title, Content $content = null 
) {
-               return wfGetLangObj( 'en' );
-       }
-
-       /**
-        * Returns the english language, because JS is english, and should be 
handled as such.
-        *
-        * @param Title $title
-        * @param Content $content
-        *
-        * @return Language wfGetLangObj( 'en' )
-        *
-        * @see ContentHandler::getPageViewLanguage()
-        */
-       public function getPageViewLanguage( Title $title, Content $content = 
null ) {
-               return wfGetLangObj( 'en' );
+       public function getPageLanguageSettings( $settings, Title $title, 
Content $content = null ) {
+               $settings['pagelanguage'] = wfGetLangObj( 'en' );
+               $settings['pageviewlanguage'] = wfGetLangObj( 'en' );
+               return $settings;
        }
 
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/137033
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I0ad0e4f47cf7624067f89384f67bfe76c57bd820
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: SPQRobin <robinp.1...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to