Redo r47313 in a more readable way (probably more efficient too) by not using all...
[lhc/web/wiklou.git] / includes / Title.php
index 845c3a5..a204754 100644 (file)
@@ -554,7 +554,7 @@ class Title {
         * @return \type{\string} Namespace text
         */
        public function getNsText() {
-               global $wgContLang, $wgCanonicalNamespaceNames;
+               global $wgCanonicalNamespaceNames;
 
                if ( '' != $this->mInterwiki ) {
                        // This probably shouldn't even happen. ohh man, oh yuck.
@@ -567,6 +567,35 @@ class Title {
                                return $wgCanonicalNamespaceNames[$this->mNamespace];
                        }
                }
+
+               return $this->getNsTextInternal( $this->mNamespace );
+       }
+
+       function getNsTextInternal( $namespace ) {
+               global $wgContLang, $wgRequest, $wgTitle, $wgSlowGenderAliases;
+               if( $namespace === NS_USER || $namespace === NS_USER_TALK ) {
+                       static $gender = null;
+
+                       $name = $this->getBaseText();
+                       if( !isset($gender[$name] ) ) {
+                               $gender[$name] = User::getDefaultOption( 'gender' );
+
+                               // wgTitle may not be defined
+                               $mytitle = isset( $wgTitle ) ? $wgTitle : Title::newFromText( $wgRequest->getVal( 'title' ) );
+
+                               // Check stuff
+                               if ( $wgSlowGenderAliases ||
+                                    // Needs to be checked always to produce desired
+                                    // effect when viewing user pages
+                                    ( $mytitle && $name === $mytitle->getBaseText() ) ) {
+
+                                       $user = User::newFromName( $name );
+                                       if ( $user ) $gender[$name] = $user->getOption( 'gender' );
+                               }
+                       }
+
+                       return $wgContLang->getGenderNsText( $this->mNamespace, $gender[$name] );
+               }
                return $wgContLang->getNsText( $this->mNamespace );
        }
        /**
@@ -581,16 +610,14 @@ class Title {
         * @return \type{\string} Namespace text
         */
        public function getSubjectNsText() {
-               global $wgContLang;
-               return $wgContLang->getNsText( MWNamespace::getSubject( $this->mNamespace ) );
+               return $this->getNsTextInternal( MWNamespace::getSubject( $this->mNamespace ) );
        }
        /**
         * Get the namespace text of the talk page
         * @return \type{\string} Namespace text
         */
        public function getTalkNsText() {
-               global $wgContLang;
-               return( $wgContLang->getNsText( MWNamespace::getTalk( $this->mNamespace ) ) );
+               return $this->getNsTextInternal( MWNamespace::getTalk( $this->mNamespace ) );
        }
        /**
         * Could this title have a corresponding talk page?
@@ -1190,6 +1217,16 @@ class Title {
                        {
                                $errors[] = $user->isAnon() ? array ('nocreatetext') : array ('nocreate-loggedin');
                        }
+               } elseif( $action == 'move-target' ) {
+                       if( !$user->isAllowed( 'move' ) ) {
+                               // User can't move anything
+                               $errors[] = $user->isAnon() ? array ( 'movenologintext' ) : array ('movenotallowed');
+                       } elseif( !$user->isAllowed( 'move-rootuserpages' )
+                               && $this->getNamespace() == NS_USER && !$this->isSubpage() )
+                       {
+                               // Show user page-specific message only if the user can move other pages
+                               $errors[] = array( 'cant-move-to-user-page' );
+                       }
                } elseif( !$user->isAllowed( $action ) ) {
                        $return = null;
                        $groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
@@ -1247,12 +1284,14 @@ class Title {
                        return $errors;
                }
                
-               // TODO: document
+               # Only 'createaccount' and 'execute' can be performed on
+               # special pages, which don't actually exist in the DB.
                $specialOKActions = array( 'createaccount', 'execute' );
                if( NS_SPECIAL == $this->mNamespace && !in_array( $action, $specialOKActions) ) {
                        $errors[] = array('ns-specialprotected');
                }
 
+               # Check $wgNamespaceProtection for restricted namespaces
                if( $this->isNamespaceProtected() ) {
                        $ns = $this->getNamespace() == NS_MAIN ?
                                wfMsg( 'nstab-main' ) : $this->getNsText();
@@ -1260,7 +1299,7 @@ class Title {
                                array('protectedinterface') : array( 'namespaceprotected',  $ns );
                }
 
-               # protect css/js subpages of user pages
+               # Protect css/js subpages of user pages
                # XXX: this might be better using restrictions
                # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
                if( $this->isCssJsSubpage() && !$user->isAllowed('editusercssjs')
@@ -1269,6 +1308,32 @@ class Title {
                        $errors[] = array('customcssjsprotected');
                }
 
+               # Check against page_restrictions table requirements on this
+               # page. The user must possess all required rights for this action.
+               foreach( $this->getRestrictions($action) as $right ) {
+                       // Backwards compatibility, rewrite sysop -> protect
+                       if( $right == 'sysop' ) {
+                               $right = 'protect';
+                       }
+                       if( '' != $right && !$user->isAllowed( $right ) ) {
+                               // Users with 'editprotected' permission can edit protected pages
+                               if( $action=='edit' && $user->isAllowed( 'editprotected' ) ) {
+                                       // Users with 'editprotected' permission cannot edit protected pages
+                                       // with cascading option turned on.
+                                       if( $this->mCascadeRestriction ) {
+                                               $errors[] = array( 'protectedpagetext', $right );
+                                       }
+                               } else {
+                                       $errors[] = array( 'protectedpagetext', $right );
+                               }
+                       }
+               }
+               # Short-circuit point
+               if( $short && count($errors) > 0 ) {
+                       wfProfileOut( __METHOD__ );
+                       return $errors;
+               }
+               
                if( $doExpensiveQueries && !$this->isCssJsSubpage() ) {
                        # We /could/ use the protection level on the source page, but it's fairly ugly
                        #  as we have to establish a precedence hierarchy for pages included by multiple
@@ -1297,30 +1362,6 @@ class Title {
                        return $errors;
                }
 
-               foreach( $this->getRestrictions($action) as $right ) {
-                       // Backwards compatibility, rewrite sysop -> protect
-                       if( $right == 'sysop' ) {
-                               $right = 'protect';
-                       }
-                       if( '' != $right && !$user->isAllowed( $right ) ) {
-                               // Users with 'editprotected' permission can edit protected pages
-                               if( $action=='edit' && $user->isAllowed( 'editprotected' ) ) {
-                                       // Users with 'editprotected' permission cannot edit protected pages
-                                       // with cascading option turned on.
-                                       if( $this->mCascadeRestriction ) {
-                                               $errors[] = array( 'protectedpagetext', $right );
-                                       }
-                               } else {
-                                       $errors[] = array( 'protectedpagetext', $right );
-                               }
-                       }
-               }
-               # Short-circuit point
-               if( $short && count($errors) > 0 ) {
-                       wfProfileOut( __METHOD__ );
-                       return $errors;
-               }
-
                if( $action == 'protect' ) {
                        if( $this->getUserPermissionsErrors('edit', $user) != array() ) {
                                $errors[] = array( 'protect-cantedit' ); // If they can't edit, they shouldn't protect.
@@ -1349,15 +1390,6 @@ class Title {
                                $errors[] = array( 'immobile-page' );
                        }
                } elseif( $action == 'move-target' ) {
-                       if( !$user->isAllowed( 'move' ) ) {
-                               // User can't move anything
-                               $errors[] = $user->isAnon() ? array ( 'movenologintext' ) : array ('movenotallowed');
-                       } elseif( !$user->isAllowed( 'move-rootuserpages' ) 
-                               && $this->getNamespace() == NS_USER && !$this->isSubpage() ) 
-                       {
-                               // Show user page-specific message only if the user can move other pages
-                               $errors[] = array( 'cant-move-to-user-page' );
-                       }
                        if( !MWNamespace::isMovable( $this->getNamespace() ) ) {
                                $errors[] = array( 'immobile-target-namespace', $this->getNsText() );
                        } elseif( !$this->isMovable() ) {
@@ -1608,10 +1640,36 @@ class Title {
                }
 
                $db = wfGetDB( DB_SLAVE );
-               return $this->mHasSubpages = (bool)$db->selectField( 'page', '1',
-                       "page_namespace = {$this->mNamespace} AND page_title LIKE '"
-                       . $db->escapeLike( $this->mDbkeyform ) . "/%'",
-                       __METHOD__
+               $subpages = $this->getSubpages( 1 );
+               if( $subpages instanceof TitleArray )
+                       return $this->mHasSubpages = (bool)$subpages->count();
+               return $this->mHasSubpages = false;
+       }
+       
+       /**
+        * Get all subpages of this page.
+        * @param $limit Maximum number of subpages to fetch; -1 for no limit
+        * @return mixed TitleArray, or empty array if this page's namespace
+        *  doesn't allow subpages
+        */
+       public function getSubpages($limit = -1) {
+               if( !MWNamespace::hasSubpages( $this->getNamespace() ) )
+                       return array();
+
+               $dbr = wfGetDB( DB_SLAVE );
+               $conds['page_namespace'] = $this->getNamespace();
+               $conds[] = 'page_title LIKE ' . $dbr->addQuotes(
+                               $dbr->escapeLike( $this->getDBkey() ) . '/%' );
+               $options = array();
+               if( $limit > -1 )
+                       $options['LIMIT'] = $limit;
+               return $this->mSubpages = TitleArray::newFromResult(
+                       $dbr->select( 'page',
+                               array( 'page_id', 'page_namespace', 'page_title' ),
+                               $conds,
+                               __METHOD__,
+                               $options
+                       )
                );
        }
 
@@ -2934,6 +2992,67 @@ class Title {
                
        }
        
+       /**
+        * Move this page's subpages to be subpages of $nt
+        * @param $nt Title Move target
+        * @param $auth bool Whether $wgUser's permissions should be checked
+        * @param $reason string The reason for the move
+        * @param $createRedirect bool Whether to create redirects from the old subpages to the new ones
+        *  Ignored if the user doesn't have the 'suppressredirect' right
+        * @return mixed array with old page titles as keys, and strings (new page titles) or
+        *  arrays (errors) as values, or an error array with numeric indices if no pages were moved
+        */
+       public function moveSubpages( $nt, $auth = true, $reason = '', $createRedirect = true ) {
+               global $wgUser, $wgMaximumMovedPages;
+               // Check permissions
+               if( !$this->userCan( 'move-subpages' ) )
+                       return array( 'cant-move-subpages' );
+               // Do the source and target namespaces support subpages?
+               if( !MWNamespace::hasSubpages( $this->getNamespace() ) )
+                       return array( 'namespace-nosubpages',
+                               MWNamespace::getCanonicalName( $this->getNamespace() ) );
+               if( !MWNamespace::hasSubpages( $nt->getNamespace() ) )
+                       return array( 'namespace-nosubpages',
+                               MWNamespace::getCanonicalName( $nt->getNamespace() ) );
+
+               $subpages = $this->getSubpages($wgMaximumMovedPages + 1);
+               $retval = array();
+               $count = 0;
+               foreach( $subpages as $oldSubpage ) {
+                       $count++;
+                       if( $count > $wgMaximumMovedPages ) {
+                               $retval[$oldSubpage->getPrefixedTitle()] =
+                                               array( 'movepage-max-pages',
+                                                       $wgMaximumMovedPages );
+                               break;
+                       }
+
+                       if( $oldSubpage->getArticleId() == $this->getArticleId() )
+                               // When moving a page to a subpage of itself,
+                               // don't move it twice
+                               continue;
+                       $newPageName = preg_replace(
+                                       '#^'.preg_quote( $this->getDBKey(), '#' ).'#',
+                                       $nt->getDBKey(), $oldSubpage->getDBKey() );
+                       if( $oldSubpage->isTalkPage() ) {
+                               $newNs = $nt->getTalkPage()->getNamespace();
+                       } else {
+                               $newNs = $nt->getSubjectPage()->getNamespace();
+                       }
+                       # Bug 14385: we need makeTitleSafe because the new page names may
+                       # be longer than 255 characters.
+                       $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
+
+                       $success = $oldSubpage->moveTo( $newSubpage, $auth, $reason, $createRedirect );
+                       if( $success === true ) {
+                               $retval[$oldSubpage->getPrefixedText()] = $newSubpage->getPrefixedText();
+                       } else {
+                               $retval[$oldSubpage->getPrefixedText()] = $success;
+                       }
+               }
+               return $retval;
+       }
+       
        /**
         * Checks if this page is just a one-rev redirect.
         * Adds lock, so don't use just for light purposes.