Code simplification (-205 bytes :P):
authorAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 23 May 2008 22:00:14 +0000 (22:00 +0000)
committerAryeh Gregor <simetrical@users.mediawiki.org>
Fri, 23 May 2008 22:00:14 +0000 (22:00 +0000)
* Add MWNamespace::hasSubpages() and use that instead of $wgNamespacesWithSubpages everywhere
* Put early returns first, and don't else { } the rest of the code

includes/Namespace.php
includes/Parser.php
includes/Skin.php
includes/SpecialMovepage.php
includes/Title.php
includes/api/ApiQuerySiteinfo.php

index 6b468c8..7c7b7de 100644 (file)
@@ -146,8 +146,8 @@ class MWNamespace {
         }
 
        /**
-        * Does this namespace contain content, for the purposes
-        * of calculating statistics, etc?
+        * Does this namespace contain content, for the purposes of calculating
+        * statistics, etc?
         *
         * @param $index Int: index to check
         * @return bool
@@ -167,4 +167,15 @@ class MWNamespace {
                return $index >= NS_MAIN;
        }
 
+       /**
+        * Does the namespace allow subpages?
+        *
+        * @param $index int Index to check
+        * @return bool
+        */
+       public static function hasSubpages( $index ) {
+               global $wgNamespacesWithSubpages;
+               return !empty( $wgNamespacesWithSubpages[$index] );
+       }
+
 }
index 27b0177..26848e2 100644 (file)
@@ -1865,8 +1865,7 @@ class Parser
         */
        function areSubpagesAllowed() {
                # Some namespaces don't allow subpages
-               global $wgNamespacesWithSubpages;
-               return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
+               return MWNamespace::hasSubpages( $this->mTitle->getNamespace() );
        }
 
        /**
index a1a3a7d..e2e9a32 100644 (file)
@@ -882,8 +882,8 @@ END;
                if(!wfRunHooks('SkinSubPageSubtitle', array(&$subpages)))
                        return $subpages;
 
-               global $wgOut, $wgTitle, $wgNamespacesWithSubpages;
-               if($wgOut->isArticle() && !empty($wgNamespacesWithSubpages[$wgTitle->getNamespace()])) {
+               global $wgOut, $wgTitle;
+               if($wgOut->isArticle() && MWNamespace::hasSubpages( $wgTitle->getNamespace() )) {
                        $ptext=$wgTitle->getPrefixedText();
                        if(preg_match('/\//',$ptext)) {
                                $links = explode('/',$ptext);
index 9207113..94ab79b 100644 (file)
@@ -65,7 +65,7 @@ class MovePageForm {
        }
 
        function showForm( $err, $hookErr = '' ) {
-               global $wgOut, $wgUser, $wgNamespacesWithSubpages;
+               global $wgOut, $wgUser;
 
                $ot = Title::newFromURL( $this->oldTitle );
                if( is_null( $ot ) ) {
@@ -237,8 +237,7 @@ class MovePageForm {
        }
 
        function doSubmit() {
-               global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang,
-               $wgNamespacesWithSubpages;
+               global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
 
                if ( $wgUser->pingLimiter( 'move' ) ) {
                        $wgOut->rateLimited();
@@ -317,9 +316,9 @@ class MovePageForm {
                # case.
                $dbr = wfGetDB( DB_SLAVE );
                if( $this->moveSubpages && (
-                       !empty($wgNamespacesWithSubpages[$nt->getNamespace()]) || (
+                       MWNamespace::hasSubpages( $nt->getNamespace() ) || (
                                $this->moveTalk &&
-                               !empty( $wgNamespacesWithSubpages[$nt->getTalkPage()->getNamespace()] )
+                               MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
                        )
                ) ) {
                        $conds = array(
@@ -327,15 +326,15 @@ class MovePageForm {
                                        .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
                        );
                        $conds['page_namespace'] = array();
-                       if( !empty( $wgNamespacesWithSubpages[$nt->getNamespace()] ) ) {
+                       if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
                                $conds['page_namespace'] []= $ot->getNamespace();
                        }
-                       if( $this->moveTalk && !empty( $wgNamespacesWithSubpages[$nt->getTalkPage()->getNamespace()] ) ) {
+                       if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
                                $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
                        }
                } elseif( $this->moveTalk ) {
                        $conds = array(
-                               'page_namespace' => MWNamespace::getTalk($ot->getNamespace()),
+                               'page_namespace' => $ot->getTalkPage()->getNamespace(),
                                'page_title' => $ot->getDBKey()
                        );
                } else {
index 4df4a7b..b8cf9bb 100644 (file)
@@ -697,16 +697,15 @@ class Title {
         * @return string Base name
         */
        public function getBaseText() {
-               global $wgNamespacesWithSubpages;
-               if( !empty( $wgNamespacesWithSubpages[$this->mNamespace] ) ) {
-                       $parts = explode( '/', $this->getText() );
-                       # Don't discard the real title if there's no subpage involved
-                       if( count( $parts ) > 1 )
-                               unset( $parts[ count( $parts ) - 1 ] );
-                       return implode( '/', $parts );
-               } else {
+               if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
                        return $this->getText();
                }
+
+               $parts = explode( '/', $this->getText() );
+               # Don't discard the real title if there's no subpage involved
+               if( count( $parts ) > 1 )
+                       unset( $parts[ count( $parts ) - 1 ] );
+               return implode( '/', $parts );
        }
 
        /**
@@ -714,13 +713,11 @@ class Title {
         * @return string Subpage name
         */
        public function getSubpageText() {
-               global $wgNamespacesWithSubpages;
-               if( !empty( $wgNamespacesWithSubpages[ $this->mNamespace ] ) ) {
-                       $parts = explode( '/', $this->mTextform );
-                       return( $parts[ count( $parts ) - 1 ] );
-               } else {
+               if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
                        return( $this->mTextform );
                }
+               $parts = explode( '/', $this->mTextform );
+               return( $parts[ count( $parts ) - 1 ] );
        }
 
        /**
@@ -1494,13 +1491,9 @@ class Title {
         * @return bool
         */
        public function isSubpage() {
-               global $wgNamespacesWithSubpages;
-
-               if( !empty( $wgNamespacesWithSubpages[ $this->mNamespace ] ) ) {
-                       return strpos( $this->getText(), '/' ) !== false;
-               } else {
-                       return false;
-               }
+               return MWNamespace::hasSubpages( $this->mNamespace )
+                       ? strpos( $this->getText(), '/' ) !== false;
+                       : false;
        }
 
        /**
@@ -1508,9 +1501,7 @@ class Title {
         * @return bool
         */
        public function hasSubpages() {
-               global $wgNamespacesWithSubpages;
-
-               if( empty( $wgNamespacesWithSubpages[$this->mNamespace] ) ) {
+               if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
                        # Duh
                        return false;
                }
index 510e1a2..bf1cbe4 100644 (file)
@@ -104,7 +104,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
        }
 
        protected function appendNamespaces($property) {
-               global $wgContLang, $wgNamespacesWithSubpages;
+               global $wgContLang;
 
                $data = array ();
                foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
@@ -112,7 +112,7 @@ class ApiQuerySiteinfo extends ApiQueryBase {
                                'id' => $ns
                        );
                        ApiResult :: setContent($data[$ns], $title);
-                       if(!empty($wgNamespacesWithSubpages[$ns]))
+                       if( MWNamespace::hasSubpages( $ns ) )
                                $data[$ns]['subpages'] = '';
                }