Follow-up r93258, r93266, r93266: Move the defines to Defines.php
[lhc/web/wiklou.git] / includes / Title.php
index 5dccdf9..ca0838a 100644 (file)
@@ -772,7 +772,7 @@ class Title {
        }
 
        /**
-        * Get the base name, i.e. the leftmost parts before the /
+        * Get the base page name, i.e. the leftmost part before any slashes
         *
         * @return String Base name
         */
@@ -790,7 +790,7 @@ class Title {
        }
 
        /**
-        * Get the lowest-level subpage name, i.e. the rightmost part after /
+        * Get the lowest-level subpage name, i.e. the rightmost part after any slashes
         *
         * @return String Subpage name
         */
@@ -1167,17 +1167,12 @@ class Title {
         * Determines if $user is unable to edit this page because it has been protected
         * by $wgNamespaceProtection.
         *
-        * @param $user User object, $wgUser will be used if not passed
+        * @param $user User object to check permissions
         * @return Bool
         */
-       public function isNamespaceProtected( User $user = null ) {
+       public function isNamespaceProtected( User $user ) {
                global $wgNamespaceProtection;
 
-               if ( $user === null ) {
-                       global $wgUser;
-                       $user = $wgUser;
-               }
-
                if ( isset( $wgNamespaceProtection[$this->mNamespace] ) ) {
                        foreach ( (array)$wgNamespaceProtection[$this->mNamespace] as $right ) {
                                if ( $right != '' && !$user->isAllowed( $right ) ) {
@@ -1239,34 +1234,33 @@ class Title {
         * @return Array list of errors
         */
        private function checkQuickPermissions( $action, $user, $errors, $doExpensiveQueries, $short ) {
+               $ns = $this->getNamespace();
+               
                if ( $action == 'create' ) {
-                       if ( ( $this->isTalkPage() && !$user->isAllowed( 'createtalk' ) ) ||
-                                ( !$this->isTalkPage() && !$user->isAllowed( 'createpage' ) ) ) {
+                       if ( ( $this->isTalkPage() && !$user->isAllowed( 'createtalk', $ns ) ) ||
+                                ( !$this->isTalkPage() && !$user->isAllowed( 'createpage', $ns ) ) ) {
                                $errors[] = $user->isAnon() ? array( 'nocreatetext' ) : array( 'nocreate-loggedin' );
                        }
                } elseif ( $action == 'move' ) {
-                       if ( !$user->isAllowed( 'move-rootuserpages' )
-                                       && $this->mNamespace == NS_USER && !$this->isSubpage() ) {
+                       if ( !$user->isAllowed( 'move-rootuserpages', $ns )
+                                       && $ns == NS_USER && !$this->isSubpage() ) {
                                // Show user page-specific message only if the user can move other pages
                                $errors[] = array( 'cant-move-user-page' );
                        }
 
                        // Check if user is allowed to move files if it's a file
-                       if ( $this->mNamespace == NS_FILE && !$user->isAllowed( 'movefile' ) ) {
+                       if ( $ns == NS_FILE && !$user->isAllowed( 'movefile', $ns ) ) {
                                $errors[] = array( 'movenotallowedfile' );
                        }
 
-                       if ( !$user->isAllowed( 'move' ) ) {
+                       if ( !$user->isAllowed( 'move', $ns) ) {
                                // User can't move anything
-                               global $wgGroupPermissions;
-                               $userCanMove = false;
-                               if ( isset( $wgGroupPermissions['user']['move'] ) ) {
-                                       $userCanMove = $wgGroupPermissions['user']['move'];
-                               }
-                               $autoconfirmedCanMove = false;
-                               if ( isset( $wgGroupPermissions['autoconfirmed']['move'] ) ) {
-                                       $autoconfirmedCanMove = $wgGroupPermissions['autoconfirmed']['move'];
-                               }
+
+                               $userCanMove = in_array( 'move', User::getGroupPermissions( 
+                                       array( 'user' ), $ns ), true );
+                               $autoconfirmedCanMove = in_array( 'move', User::getGroupPermissions( 
+                                       array( 'autoconfirmed' ), $ns ), true );
+
                                if ( $user->isAnon() && ( $userCanMove || $autoconfirmedCanMove ) ) {
                                        // custom message if logged-in users without any special rights can move
                                        $errors[] = array( 'movenologintext' );
@@ -1275,20 +1269,20 @@ class Title {
                                }
                        }
                } elseif ( $action == 'move-target' ) {
-                       if ( !$user->isAllowed( 'move' ) ) {
+                       if ( !$user->isAllowed( 'move', $ns ) ) {
                                // User can't move anything
                                $errors[] = array( 'movenotallowed' );
-                       } elseif ( !$user->isAllowed( 'move-rootuserpages' )
-                                       && $this->mNamespace == NS_USER && !$this->isSubpage() ) {
+                       } elseif ( !$user->isAllowed( 'move-rootuserpages', $ns )
+                                       && $ns == 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 ) ) {
+               } elseif ( !$user->isAllowed( $action, $ns ) ) {
                        // We avoid expensive display logic for quickUserCan's and such
                        $groups = false;
                        if ( !$short ) {
                                $groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
-                                       User::getGroupsWithPermission( $action ) );
+                                       User::getGroupsWithPermission( $action, $ns ) );
                        }
 
                        if ( $groups ) {
@@ -1406,8 +1400,6 @@ class Title {
        private function checkCSSandJSPermissions( $action, $user, $errors, $doExpensiveQueries, $short ) {
                # 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->userCanEditCssSubpage()
-               #      and $this->userCanEditJsSubpage() from working
                # XXX: right 'editusercssjs' is deprecated, for backward compatibility only
                if ( $action != 'patrol' && !$user->isAllowed( 'editusercssjs' )
                                && !preg_match( '/^' . preg_quote( $user->getName(), '/' ) . '\//', $this->mTextform ) ) {
@@ -1440,9 +1432,9 @@ class Title {
                        if ( $right == 'sysop' ) {
                                $right = 'protect';
                        }
-                       if ( $right != '' && !$user->isAllowed( $right ) ) {
+                       if ( $right != '' && !$user->isAllowed( $right, $this->mNamespace ) ) {
                                // Users with 'editprotected' permission can edit protected pages
-                               if ( $action == 'edit' && $user->isAllowed( 'editprotected' ) ) {
+                               if ( $action == 'edit' && $user->isAllowed( 'editprotected', $this->mNamespace ) ) {
                                        // Users with 'editprotected' permission cannot edit protected pages
                                        // with cascading option turned on.
                                        if ( $this->mCascadeRestriction ) {
@@ -1483,7 +1475,7 @@ class Title {
                        if ( isset( $restrictions[$action] ) ) {
                                foreach ( $restrictions[$action] as $right ) {
                                        $right = ( $right == 'sysop' ) ? 'protect' : $right;
-                                       if ( $right != '' && !$user->isAllowed( $right ) ) {
+                                       if ( $right != '' && !$user->isAllowed( $right, $this->mNamespace ) ) {
                                                $pages = '';
                                                foreach ( $cascadingSources as $page )
                                                        $pages .= '* [[:' . $page->getPrefixedText() . "]]\n";
@@ -1519,7 +1511,9 @@ class Title {
                                if( $title_protection['pt_create_perm'] == 'sysop' ) {
                                        $title_protection['pt_create_perm'] = 'protect'; // B/C
                                }
-                               if( $title_protection['pt_create_perm'] == '' || !$user->isAllowed( $title_protection['pt_create_perm'] ) ) {
+                               if( $title_protection['pt_create_perm'] == '' || 
+                                               !$user->isAllowed( $title_protection['pt_create_perm'], 
+                                               $this->mNamespace ) ) {
                                        $errors[] = array( 'titleprotected', User::whoIs( $title_protection['pt_user'] ), $title_protection['pt_reason'] );
                                }
                        }
@@ -1955,8 +1949,10 @@ class Title {
         * @return Bool
         */
        public function isCssOrJsPage() {
-               return $this->mNamespace == NS_MEDIAWIKI
+               $retval = $this->mNamespace == NS_MEDIAWIKI
                        && preg_match( '!\.(?:css|js)$!u', $this->mTextform ) > 0;
+               wfRunHooks( 'TitleIsCssOrJsPage', array( $this, &$retval ) );
+               return $retval;
        }
 
        /**
@@ -2010,11 +2006,12 @@ class Title {
         * Protect css subpages of user pages: can $wgUser edit
         * this page?
         *
+        * @deprecated in 1.19; will be removed in 1.20. Use getUserPermissionsErrors() instead.
         * @return Bool
-        * @todo XXX: this might be better using restrictions
         */
        public function userCanEditCssSubpage() {
                global $wgUser;
+               wfDeprecated( __METHOD__ );
                return ( ( $wgUser->isAllowedAll( 'editusercssjs', 'editusercss' ) )
                        || preg_match( '/^' . preg_quote( $wgUser->getName(), '/' ) . '\//', $this->mTextform ) );
        }
@@ -2023,11 +2020,12 @@ class Title {
         * Protect js subpages of user pages: can $wgUser edit
         * this page?
         *
+        * @deprecated in 1.19; will be removed in 1.20. Use getUserPermissionsErrors() instead.
         * @return Bool
-        * @todo XXX: this might be better using restrictions
         */
        public function userCanEditJsSubpage() {
                global $wgUser;
+               wfDeprecated( __METHOD__ );
                return ( ( $wgUser->isAllowedAll( 'editusercssjs', 'edituserjs' ) )
                           || preg_match( '/^' . preg_quote( $wgUser->getName(), '/' ) . '\//', $this->mTextform ) );
        }
@@ -2355,7 +2353,7 @@ class Title {
                        $conditions = array( 'ar_namespace' => $this->getNamespace(), 'ar_title' => $this->getDBkey() );
 
                        if( !$includeSuppressed ) {
-                               $suppressedTextBits = REVISION::DELETED_TEXT | REVISION::DELETED_RESTRICTED;
+                               $suppressedTextBits = Revision::DELETED_TEXT | Revision::DELETED_RESTRICTED;
                                $conditions[] = $dbr->bitAnd('ar_deleted', $suppressedTextBits ) .
                                ' != ' . $suppressedTextBits;
                        }
@@ -2367,7 +2365,7 @@ class Title {
                        if ( $this->getNamespace() == NS_FILE ) {
                                $fconditions = array( 'fa_name' => $this->getDBkey() );
                                if( !$includeSuppressed ) {
-                                       $suppressedTextBits = FILE::DELETED_FILE | FILE::DELETED_RESTRICTED;
+                                       $suppressedTextBits = File::DELETED_FILE | File::DELETED_RESTRICTED;
                                        $fconditions[] = $dbr->bitAnd('fa_deleted', $suppressedTextBits ) .
                                        ' != ' . $suppressedTextBits;
                                }
@@ -3236,6 +3234,7 @@ class Title {
                if ( $u ) {
                        $u->doUpdate();
                }
+
                # Update message cache for interface messages
                if ( $this->getNamespace() == NS_MEDIAWIKI ) {
                        # @bug 17860: old article can be deleted, if this the case,
@@ -3328,10 +3327,11 @@ class Title {
                }
                $nullRevId = $nullRevision->insertOn( $dbw );
 
+               $now = wfTimestampNow();
                # Change the name of the target page:
                $dbw->update( 'page',
                        /* SET */ array(
-                               'page_touched'   => $dbw->timestamp(),
+                               'page_touched'   => $dbw->timestamp( $now ),
                                'page_namespace' => $nt->getNamespace(),
                                'page_title'     => $nt->getDBkey(),
                                'page_latest'    => $nullRevId,
@@ -3343,6 +3343,7 @@ class Title {
 
                $article = new Article( $nt );
                wfRunHooks( 'NewRevisionFromEditComplete', array( $article, $nullRevision, $latest, $wgUser ) );
+               $article->setCachedLastEditTime( $now );
 
                # Recreate the redirect, this time in the other direction.
                if ( $createRedirect || !$wgUser->isAllowed( 'suppressredirect' ) ) {