r35258 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r35257‎ | r35258 | r35259 >
Date:22:00, 23 May 2008
Author:simetrical
Status:old
Tags:
Comment:
Code simplification (-205 bytes :P):
* Add MWNamespace::hasSubpages() and use that instead of $wgNamespacesWithSubpages everywhere
* Put early returns first, and don't else { } the rest of the code
Modified paths:
  • /trunk/phase3/includes/Namespace.php (modified) (history)
  • /trunk/phase3/includes/Parser.php (modified) (history)
  • /trunk/phase3/includes/Skin.php (modified) (history)
  • /trunk/phase3/includes/SpecialMovepage.php (modified) (history)
  • /trunk/phase3/includes/Title.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuerySiteinfo.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Parser.php
@@ -1865,8 +1865,7 @@
18661866 */
18671867 function areSubpagesAllowed() {
18681868 # Some namespaces don't allow subpages
1869 - global $wgNamespacesWithSubpages;
1870 - return !empty($wgNamespacesWithSubpages[$this->mTitle->getNamespace()]);
 1869+ return MWNamespace::hasSubpages( $this->mTitle->getNamespace() );
18711870 }
18721871
18731872 /**
Index: trunk/phase3/includes/SpecialMovepage.php
@@ -65,7 +65,7 @@
6666 }
6767
6868 function showForm( $err, $hookErr = '' ) {
69 - global $wgOut, $wgUser, $wgNamespacesWithSubpages;
 69+ global $wgOut, $wgUser;
7070
7171 $ot = Title::newFromURL( $this->oldTitle );
7272 if( is_null( $ot ) ) {
@@ -237,8 +237,7 @@
238238 }
239239
240240 function doSubmit() {
241 - global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang,
242 - $wgNamespacesWithSubpages;
 241+ global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
243242
244243 if ( $wgUser->pingLimiter( 'move' ) ) {
245244 $wgOut->rateLimited();
@@ -317,9 +316,9 @@
318317 # case.
319318 $dbr = wfGetDB( DB_SLAVE );
320319 if( $this->moveSubpages && (
321 - !empty($wgNamespacesWithSubpages[$nt->getNamespace()]) || (
 320+ MWNamespace::hasSubpages( $nt->getNamespace() ) || (
322321 $this->moveTalk &&
323 - !empty( $wgNamespacesWithSubpages[$nt->getTalkPage()->getNamespace()] )
 322+ MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
324323 )
325324 ) ) {
326325 $conds = array(
@@ -327,15 +326,15 @@
328327 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
329328 );
330329 $conds['page_namespace'] = array();
331 - if( !empty( $wgNamespacesWithSubpages[$nt->getNamespace()] ) ) {
 330+ if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
332331 $conds['page_namespace'] []= $ot->getNamespace();
333332 }
334 - if( $this->moveTalk && !empty( $wgNamespacesWithSubpages[$nt->getTalkPage()->getNamespace()] ) ) {
 333+ if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
335334 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
336335 }
337336 } elseif( $this->moveTalk ) {
338337 $conds = array(
339 - 'page_namespace' => MWNamespace::getTalk($ot->getNamespace()),
 338+ 'page_namespace' => $ot->getTalkPage()->getNamespace(),
340339 'page_title' => $ot->getDBKey()
341340 );
342341 } else {
Index: trunk/phase3/includes/api/ApiQuerySiteinfo.php
@@ -104,7 +104,7 @@
105105 }
106106
107107 protected function appendNamespaces($property) {
108 - global $wgContLang, $wgNamespacesWithSubpages;
 108+ global $wgContLang;
109109
110110 $data = array ();
111111 foreach ($wgContLang->getFormattedNamespaces() as $ns => $title) {
@@ -112,7 +112,7 @@
113113 'id' => $ns
114114 );
115115 ApiResult :: setContent($data[$ns], $title);
116 - if(!empty($wgNamespacesWithSubpages[$ns]))
 116+ if( MWNamespace::hasSubpages( $ns ) )
117117 $data[$ns]['subpages'] = '';
118118 }
119119
Index: trunk/phase3/includes/Title.php
@@ -697,16 +697,15 @@
698698 * @return string Base name
699699 */
700700 public function getBaseText() {
701 - global $wgNamespacesWithSubpages;
702 - if( !empty( $wgNamespacesWithSubpages[$this->mNamespace] ) ) {
703 - $parts = explode( '/', $this->getText() );
704 - # Don't discard the real title if there's no subpage involved
705 - if( count( $parts ) > 1 )
706 - unset( $parts[ count( $parts ) - 1 ] );
707 - return implode( '/', $parts );
708 - } else {
 701+ if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
709702 return $this->getText();
710703 }
 704+
 705+ $parts = explode( '/', $this->getText() );
 706+ # Don't discard the real title if there's no subpage involved
 707+ if( count( $parts ) > 1 )
 708+ unset( $parts[ count( $parts ) - 1 ] );
 709+ return implode( '/', $parts );
711710 }
712711
713712 /**
@@ -714,13 +713,11 @@
715714 * @return string Subpage name
716715 */
717716 public function getSubpageText() {
718 - global $wgNamespacesWithSubpages;
719 - if( !empty( $wgNamespacesWithSubpages[ $this->mNamespace ] ) ) {
720 - $parts = explode( '/', $this->mTextform );
721 - return( $parts[ count( $parts ) - 1 ] );
722 - } else {
 717+ if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
723718 return( $this->mTextform );
724719 }
 720+ $parts = explode( '/', $this->mTextform );
 721+ return( $parts[ count( $parts ) - 1 ] );
725722 }
726723
727724 /**
@@ -1494,13 +1491,9 @@
14951492 * @return bool
14961493 */
14971494 public function isSubpage() {
1498 - global $wgNamespacesWithSubpages;
1499 -
1500 - if( !empty( $wgNamespacesWithSubpages[ $this->mNamespace ] ) ) {
1501 - return strpos( $this->getText(), '/' ) !== false;
1502 - } else {
1503 - return false;
1504 - }
 1495+ return MWNamespace::hasSubpages( $this->mNamespace )
 1496+ ? strpos( $this->getText(), '/' ) !== false;
 1497+ : false;
15051498 }
15061499
15071500 /**
@@ -1508,9 +1501,7 @@
15091502 * @return bool
15101503 */
15111504 public function hasSubpages() {
1512 - global $wgNamespacesWithSubpages;
1513 -
1514 - if( empty( $wgNamespacesWithSubpages[$this->mNamespace] ) ) {
 1505+ if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
15151506 # Duh
15161507 return false;
15171508 }
Index: trunk/phase3/includes/Namespace.php
@@ -146,8 +146,8 @@
147147 }
148148
149149 /**
150 - * Does this namespace contain content, for the purposes
151 - * of calculating statistics, etc?
 150+ * Does this namespace contain content, for the purposes of calculating
 151+ * statistics, etc?
152152 *
153153 * @param $index Int: index to check
154154 * @return bool
@@ -167,4 +167,15 @@
168168 return $index >= NS_MAIN;
169169 }
170170
 171+ /**
 172+ * Does the namespace allow subpages?
 173+ *
 174+ * @param $index int Index to check
 175+ * @return bool
 176+ */
 177+ public static function hasSubpages( $index ) {
 178+ global $wgNamespacesWithSubpages;
 179+ return !empty( $wgNamespacesWithSubpages[$index] );
 180+ }
 181+
171182 }
Index: trunk/phase3/includes/Skin.php
@@ -882,8 +882,8 @@
883883 if(!wfRunHooks('SkinSubPageSubtitle', array(&$subpages)))
884884 return $subpages;
885885
886 - global $wgOut, $wgTitle, $wgNamespacesWithSubpages;
887 - if($wgOut->isArticle() && !empty($wgNamespacesWithSubpages[$wgTitle->getNamespace()])) {
 886+ global $wgOut, $wgTitle;
 887+ if($wgOut->isArticle() && MWNamespace::hasSubpages( $wgTitle->getNamespace() )) {
888888 $ptext=$wgTitle->getPrefixedText();
889889 if(preg_match('/\//',$ptext)) {
890890 $links = explode('/',$ptext);

Status & tagging log