This is giving me a syntax error. It looks gross this way, but I can't think of any...
[lhc/web/wiklou.git] / includes / Title.php
index 63ec737..af720ee 100644 (file)
@@ -931,7 +931,7 @@ class Title {
        /**
         * Does the title correspond to a protected article?
         * @param string $what the action the page is protected from,
-        *      by default checks move and edit
+        * by default checks move and edit
         * @return boolean
         */
        public function isProtected( $action = '' ) {
@@ -980,7 +980,7 @@ class Title {
                return $this->mWatched;
        }
 
-       /**
+       /**
         * Can $wgUser perform $action on this page?
         * This skips potentially expensive cascading permission checks.
         *
@@ -999,7 +999,7 @@ class Title {
        /**
         * Determines if $wgUser is unable to edit this page because it has been protected
         * by $wgNamespaceProtection.
-        * 
+        *
         * @return boolean
         */
        public function isNamespaceProtected() {
@@ -1013,7 +1013,7 @@ class Title {
                return false;
        }
 
-       /**
+       /**
         * Can $wgUser perform $action on this page?
         * @param string $action action that permission needs to be checked for
         * @param bool $doExpensiveQueries Set this to false to avoid doing unnecessary queries.
@@ -1024,12 +1024,12 @@ class Title {
                return ( $this->getUserPermissionsErrorsInternal( $action, $wgUser, $doExpensiveQueries ) === array());
        }
 
-        /**
+       /**
         * Can $user perform $action on this page?
         * @param string $action action that permission needs to be checked for
         * @param bool $doExpensiveQueries Set this to false to avoid doing unnecessary queries.
         * @return array Array of arrays of the arguments to wfMsg to explain permissions problems.
-       */
+        */
        public function getUserPermissionsErrors( $action, $user, $doExpensiveQueries = true ) {
                $errors = $this->getUserPermissionsErrorsInternal( $action, $user, $doExpensiveQueries );
 
@@ -1056,6 +1056,9 @@ class Title {
 
                        $id = $user->blockedBy();
                        $reason = $user->blockedFor();
+                       if( $reason == '' ) {
+                               $reason = wfMsg( 'blockednoreason' );
+                       }
                        $ip = wfGetIP();
 
                        if ( is_numeric( $id ) ) {
@@ -1109,10 +1112,22 @@ class Title {
 
                $errors = array();
 
+               // Use getUserPermissionsErrors instead
                if ( !wfRunHooks( 'userCan', array( &$this, &$user, $action, &$result ) ) ) {
                        return $result ? array() : array( array( 'badaccess-group0' ) );
                }
 
+               if (!wfRunHooks( 'getUserPermissionsErrors', array( &$this, &$user, $action, &$result ) ) ) {
+                       if ($result != array() && is_array($result) && !is_array($result[0]))
+                               $errors[] = $result; # A single array representing an error
+                       else if (is_array($result) && is_array($result[0]))
+                               $errors = array_merge( $errors, $result ); # A nested array representing multiple errors
+                       else if ($result != '' && $result != null && $result !== true && $result !== false)
+                               $errors[] = array($result); # A string representing a message-id
+                       else if ($result === false )
+                               $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
+               }
+
                if( NS_SPECIAL == $this->mNamespace ) {
                        $errors[] = array('ns-specialprotected');
                }
@@ -1135,7 +1150,7 @@ class Title {
                # 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('editinterface')
+                       && !$user->isAllowed('editusercssjs')
                        && !preg_match('/^'.preg_quote($user->getName(), '/').'\//', $this->mTextform) ) {
                        $errors[] = array('customcssjsprotected');
                }
@@ -1169,7 +1184,7 @@ class Title {
                                $right = 'protect';
                        }
                        if( '' != $right && !$user->isAllowed( $right ) ) {
-                               $errors[] = array( 'protectedpagetext' );
+                               $errors[] = array( 'protectedpagetext', $right );
                        }
                }
 
@@ -1189,7 +1204,6 @@ class Title {
                                $groupName = User::getGroupName( $key );
                                $groupPage = User::getGroupPage( $key );
                                if( $groupPage ) {
-                                   $skin = $user->getSkin();
                                    $groups[] = '[['.$groupPage->getPrefixedText().'|'.$groupName.']]';
                                } else {
                                    $groups[] = $groupName;
@@ -1278,19 +1292,26 @@ class Title {
                        if( $this->isSpecial( 'Userlogin' ) || $this->isSpecial( 'Resetpass' ) ) {
                                return true;
                        }
+
+                       /**
+                        * Bail out if there isn't whitelist
+                        */
+                       if( !is_array($wgWhitelistRead) ) {
+                               return false;
+                       }
                        
                        /**
                         * Check for explicit whitelisting
                         */
                        $name = $this->getPrefixedText();
-                       if( $wgWhitelistRead && in_array( $name, $wgWhitelistRead, true ) )
+                       if( in_array( $name, $wgWhitelistRead, true ) )
                                return true;
                        
                        /**
                         * Old settings might have the title prefixed with
                         * a colon for main-namespace pages
                         */
-                       if( $wgWhitelistRead && $this->getNamespace() == NS_MAIN ) {
+                       if( $this->getNamespace() == NS_MAIN ) {
                                if( in_array( ':' . $name, $wgWhitelistRead ) )
                                        return true;
                        }
@@ -1300,8 +1321,13 @@ class Title {
                         * and check again
                         */
                        if( $this->getNamespace() == NS_SPECIAL ) {
-                               $name = $this->getText();
+                               $name = $this->getDBKey();
                                list( $name, /* $subpage */) = SpecialPage::resolveAliasWithSubpage( $name );
+                               if ( $name === false ) {
+                                       # Invalid special page, but we show standard login required message
+                                       return false;
+                               }
+
                                $pure = SpecialPage::getTitleFor( $name )->getPrefixedText();
                                if( in_array( $pure, $wgWhitelistRead, true ) )
                                        return true;
@@ -1394,7 +1420,7 @@ class Title {
         */
        public function userCanEditCssJsSubpage() {
                global $wgUser;
-               return ( $wgUser->isAllowed('editinterface') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
+               return ( $wgUser->isAllowed('editusercssjs') or preg_match('/^'.preg_quote($wgUser->getName(), '/').'\//', $this->mTextform) );
        }
 
        /**
@@ -2027,10 +2053,16 @@ class Title {
        /**
         * Get an array of Title objects referring to non-existent articles linked from this page
         *
+        * @todo check if needed (used only in SpecialBrokenRedirects.php, and should use redirect table in this case)
         * @param string $options may be FOR UPDATE
         * @return array the Title objects
         */
        public function getBrokenLinksFrom( $options = '' ) {
+               if ( $this->getArticleId() == 0 ) {
+                       # All links from article ID 0 are false positives
+                       return array();
+               }
+
                if ( $options ) {
                        $db = wfGetDB( DB_MASTER );
                } else {
@@ -2160,9 +2192,11 @@ class Title {
         * @param Title &$nt the new title
         * @param bool $auth indicates whether $wgUser's permissions
         *      should be checked
+        * @param string $reason The reason for the move
+        * @param bool $createRedirect Whether to create a redirect from the old title to the new title
         * @return mixed true on success, message name on failure
         */
-       public function moveTo( &$nt, $auth = true, $reason = '' ) {
+       public function moveTo( &$nt, $auth = true, $reason = '', $createRedirect = true ) {
                $err = $this->isValidMoveOperation( $nt, $auth );
                if( is_string( $err ) ) {
                        return $err;
@@ -2170,11 +2204,11 @@ class Title {
 
                $pageid = $this->getArticleID();
                if( $nt->exists() ) {
-                       $this->moveOverExistingRedirect( $nt, $reason );
-                       $pageCountChange = 0;
+                       $this->moveOverExistingRedirect( $nt, $reason, $createRedirect );
+                       $pageCountChange = ($createRedirect ? 0 : -1);
                } else { # Target didn't exist, do normal move.
-                       $this->moveToNewTitle( $nt, $reason );
-                       $pageCountChange = 1;
+                       $this->moveToNewTitle( $nt, $reason, $createRedirect );
+                       $pageCountChange = ($createRedirect ? 1 : 0);
                }
                $redirid = $this->getArticleID();
 
@@ -2233,8 +2267,10 @@ class Title {
         *
         * @param Title &$nt the page to move to, which should currently
         *      be a redirect
+        * @param string $reason The reason for the move
+        * @param bool $createRedirect Whether to leave a redirect at the old title
         */
-       private function moveOverExistingRedirect( &$nt, $reason = '' ) {
+       private function moveOverExistingRedirect( &$nt, $reason = '', $createRedirect = true ) {
                global $wgUseSquid;
                $fname = 'Title::moveOverExistingRedirect';
                $comment = wfMsgForContent( '1movedto2_redir', $this->getPrefixedText(), $nt->getPrefixedText() );
@@ -2273,32 +2309,35 @@ class Title {
                $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
                # Recreate the redirect, this time in the other direction.
-               $mwRedir = MagicWord::get( 'redirect' );
-               $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
-               $redirectArticle = new Article( $this );
-               $newid = $redirectArticle->insertOn( $dbw );
-               $redirectRevision = new Revision( array(
-                       'page'    => $newid,
-                       'comment' => $comment,
-                       'text'    => $redirectText ) );
-               $redirectRevision->insertOn( $dbw );
-               $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-               $linkCache->clearLink( $this->getPrefixedDBkey() );
-
+               if($createRedirect)
+               {
+                       $mwRedir = MagicWord::get( 'redirect' );
+                       $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
+                       $redirectArticle = new Article( $this );
+                       $newid = $redirectArticle->insertOn( $dbw );
+                       $redirectRevision = new Revision( array(
+                               'page'    => $newid,
+                               'comment' => $comment,
+                               'text'    => $redirectText ) );
+                       $redirectRevision->insertOn( $dbw );
+                       $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
+                       $linkCache->clearLink( $this->getPrefixedDBkey() );
+
+                       # Now, we record the link from the redirect to the new title.
+                       # It should have no other outgoing links...
+                       $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
+                       $dbw->insert( 'pagelinks',
+                               array(
+                                       'pl_from'      => $newid,
+                                       'pl_namespace' => $nt->getNamespace(),
+                                       'pl_title'     => $nt->getDbKey() ),
+                               $fname );
+               }
+               
                # Log the move
                $log = new LogPage( 'move' );
                $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
 
-               # Now, we record the link from the redirect to the new title.
-               # It should have no other outgoing links...
-               $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), $fname );
-               $dbw->insert( 'pagelinks',
-                       array(
-                               'pl_from'      => $newid,
-                               'pl_namespace' => $nt->getNamespace(),
-                               'pl_title'     => $nt->getDbKey() ),
-                       $fname );
-
                # Purge squid
                if ( $wgUseSquid ) {
                        $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
@@ -2310,8 +2349,10 @@ class Title {
        /**
         * Move page to non-existing title.
         * @param Title &$nt the new Title
+        * @param string $reason The reason for the move
+        * @param bool $createRedirect Whether to create a redirect from the old title to the new title
         */
-       private function moveToNewTitle( &$nt, $reason = '' ) {
+       private function moveToNewTitle( &$nt, $reason = '', $createRedirect = true ) {
                global $wgUseSquid;
                $fname = 'MovePageForm::moveToNewTitle';
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
@@ -2343,18 +2384,28 @@ class Title {
 
                $linkCache->clearLink( $nt->getPrefixedDBkey() );
 
-               # Insert redirect
-               $mwRedir = MagicWord::get( 'redirect' );
-               $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
-               $redirectArticle = new Article( $this );
-               $newid = $redirectArticle->insertOn( $dbw );
-               $redirectRevision = new Revision( array(
-                       'page'    => $newid,
-                       'comment' => $comment,
-                       'text'    => $redirectText ) );
-               $redirectRevision->insertOn( $dbw );
-               $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-               $linkCache->clearLink( $this->getPrefixedDBkey() );
+               if($createRedirect)
+               {
+                       # Insert redirect
+                       $mwRedir = MagicWord::get( 'redirect' );
+                       $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
+                       $redirectArticle = new Article( $this );
+                       $newid = $redirectArticle->insertOn( $dbw );
+                       $redirectRevision = new Revision( array(
+                               'page'    => $newid,
+                               'comment' => $comment,
+                               'text'    => $redirectText ) );
+                       $redirectRevision->insertOn( $dbw );
+                       $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
+                       $linkCache->clearLink( $this->getPrefixedDBkey() );
+                       # Record the just-created redirect's linking to the page
+                       $dbw->insert( 'pagelinks',
+                               array(
+                                       'pl_from'      => $newid,
+                                       'pl_namespace' => $nt->getNamespace(),
+                                       'pl_title'     => $nt->getDBkey() ),
+                               $fname );
+               }
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -2363,14 +2414,6 @@ class Title {
                # Purge caches as per article creation
                Article::onArticleCreate( $nt );
 
-               # Record the just-created redirect's linking to the page
-               $dbw->insert( 'pagelinks',
-                       array(
-                               'pl_from'      => $newid,
-                               'pl_namespace' => $nt->getNamespace(),
-                               'pl_title'     => $nt->getDBkey() ),
-                       $fname );
-
                # Purge old title from squid
                # The new title, and links to the new title, are purged in Article::onArticleCreate()
                $this->purgeSquid();
@@ -2469,7 +2512,7 @@ class Title {
                                $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
                        $dbr->freeResult ( $res ) ;
                } else {
-                       $data = '';
+                       $data = array();
                }
                return $data;
        }