Merge "Cache page content language in Title object"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 7 Nov 2013 01:43:00 +0000 (01:43 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 7 Nov 2013 01:43:00 +0000 (01:43 +0000)
1  2 
includes/Title.php

diff --combined includes/Title.php
@@@ -86,6 -86,7 +86,7 @@@ class Title 
        var $mRedirect = null;            // /< Is the article at this title a redirect?
        var $mNotificationTimestamp = array(); // /< Associative array of user ID -> timestamp/false
        var $mHasSubpage;                 // /< Whether a page has any subpages
+       private $mPageLanguage = false;   // /< The (string) language code of the page's language and content code.
        // @}
  
        /**
        }
  
        /**
 -       * Is this page "semi-protected" - the *only* protection is autoconfirm?
 +       * Is this page "semi-protected" - the *only* protection levels are listed
 +       * in $wgSemiprotectedRestrictionLevels?
         *
         * @param string $action Action to check (default: edit)
         * @return Bool
         */
        public function isSemiProtected( $action = 'edit' ) {
 -              if ( $this->exists() ) {
 -                      $restrictions = $this->getRestrictions( $action );
 -                      if ( count( $restrictions ) > 0 ) {
 -                              foreach ( $restrictions as $restriction ) {
 -                                      if ( strtolower( $restriction ) != 'editsemiprotected' &&
 -                                              strtolower( $restriction ) != 'autoconfirmed' // BC
 -                                      ) {
 -                                              return false;
 -                                      }
 -                              }
 -                      } else {
 -                              # Not protected
 -                              return false;
 -                      }
 -                      return true;
 -              } else {
 -                      # If it doesn't exist, it can't be protected
 +              global $wgSemiprotectedRestrictionLevels;
 +
 +              $restrictions = $this->getRestrictions( $action );
 +              $semi = $wgSemiprotectedRestrictionLevels;
 +              if ( !$restrictions || !$semi ) {
 +                      // Not protected, or all protection is full protection
                        return false;
                }
 +
 +              // Remap autoconfirmed to editsemiprotected for BC
 +              foreach ( array_keys( $semi, 'autoconfirmed' ) as $key ) {
 +                      $semi[$key] = 'editsemiprotected';
 +              }
 +              foreach ( array_keys( $restrictions, 'autoconfirmed' ) as $key ) {
 +                      $restrictions[$key] = 'editsemiprotected';
 +              }
 +
 +              return !array_diff( $restrictions, $semi );
        }
  
        /**
                $this->mLatestID = false;
                $this->mContentModel = false;
                $this->mEstimateRevisions = null;
+               $this->mPageLanguage = false;
        }
  
        /**
                        __METHOD__
                );
  
 -              $this->resetArticleID( 0 );
 +              // clean up the old title before reset article id - bug 45348
 +              if ( !$redirectContent ) {
 +                      WikiPage::onArticleDelete( $this );
 +              }
 +
 +              $this->resetArticleID( 0 ); // 0 == non existing
                $nt->resetArticleID( $oldid );
                $newpage->loadPageData( WikiPage::READ_LOCKING ); // bug 46397
  
                }
  
                # Recreate the redirect, this time in the other direction.
 -              if ( !$redirectContent ) {
 -                      WikiPage::onArticleDelete( $this );
 -              } else {
 +              if ( $redirectContent ) {
                        $redirectArticle = WikiPage::factory( $this );
                        $redirectArticle->loadFromRow( false, WikiPage::READ_LOCKING ); // bug 46397
                        $newid = $redirectArticle->insertOn( $dbw );
                        if ( $newid ) { // sanity
 +                              $this->resetArticleID( $newid );
                                $redirectRevision = new Revision( array(
                                        'title' => $this, // for determining the default content model
                                        'page' => $newid,
         * @return Language
         */
        public function getPageLanguage() {
-               global $wgLang;
+               global $wgLang, $wgLanguageCode;
+               wfProfileIn( __METHOD__ );
                if ( $this->isSpecialPage() ) {
                        // special pages are in the user language
+                       wfProfileOut( __METHOD__ );
                        return $wgLang;
                }
  
-               //TODO: use the LinkCache to cache this! 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!
-               $contentHandler = ContentHandler::getForTitle( $this );
-               $pageLang = $contentHandler->getPageLanguage( $this );
-               return wfGetLangObj( $pageLang );
+               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;
        }
  
        /**