Title: Don't create mSubpages member variable
[lhc/web/wiklou.git] / includes / Title.php
index a5bb9c6..0728065 100644 (file)
@@ -21,6 +21,9 @@
  *
  * @file
  */
+
+use Wikimedia\Rdbms\Database;
+use Wikimedia\Rdbms\IDatabase;
 use MediaWiki\Linker\LinkTarget;
 use MediaWiki\Interwiki\InterwikiLookup;
 use MediaWiki\MediaWikiServices;
@@ -1017,12 +1020,25 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Could this title have a corresponding talk page?
+        * Can this title have a corresponding talk page?
         *
-        * @return bool
+        * @deprecated since 1.30, use canHaveTalkPage() instead.
+        *
+        * @return bool True if this title either is a talk page or can have a talk page associated.
         */
        public function canTalk() {
-               return MWNamespace::canTalk( $this->mNamespace );
+               return $this->canHaveTalkPage();
+       }
+
+       /**
+        * Can this title have a corresponding talk page?
+        *
+        * @see MWNamespace::hasTalkNamespace
+        *
+        * @return bool True if this title either is a talk page or can have a talk page associated.
+        */
+       public function canHaveTalkPage() {
+               return MWNamespace::hasTalkNamespace( $this->mNamespace );
        }
 
        /**
@@ -1416,13 +1432,22 @@ class Title implements LinkTarget {
         * @return string The prefixed text
         */
        private function prefix( $name ) {
+               global $wgContLang;
+
                $p = '';
                if ( $this->isExternal() ) {
                        $p = $this->mInterwiki . ':';
                }
 
                if ( 0 != $this->mNamespace ) {
-                       $p .= $this->getNsText() . ':';
+                       $nsText = $this->getNsText();
+
+                       if ( $nsText === false ) {
+                               // See T165149. Awkward, but better than erroneously linking to the main namespace.
+                               $nsText = $wgContLang->getNsText( NS_SPECIAL ) . ":Badtitle/NS{$this->mNamespace}";
+                       }
+
+                       $p .= $nsText . ':';
                }
                return $p . $name;
        }
@@ -1681,6 +1706,33 @@ class Title implements LinkTarget {
                return $url;
        }
 
+       /**
+        * Get a url appropriate for making redirects based on an untrusted url arg
+        *
+        * This is basically the same as getFullUrl(), but in the case of external
+        * interwikis, we send the user to a landing page, to prevent possible
+        * phishing attacks and the like.
+        *
+        * @note Uses current protocol by default, since technically relative urls
+        *   aren't allowed in redirects per HTTP spec, so this is not suitable for
+        *   places where the url gets cached, as might pollute between
+        *   https and non-https users.
+        * @see self::getLocalURL for the arguments.
+        * @param array|string $query
+        * @param string $proto Protocol type to use in URL
+        * @return String. A url suitable to use in an HTTP location header.
+        */
+       public function getFullUrlForRedirect( $query = '', $proto = PROTO_CURRENT ) {
+               $target = $this;
+               if ( $this->isExternal() ) {
+                       $target = SpecialPage::getTitleFor(
+                               'GoToInterwiki',
+                               $this->getPrefixedDBKey()
+                       );
+               }
+               return $target->getFullUrl( $query, false, $proto );
+       }
+
        /**
         * Get a URL with no fragment or server name (relative URL) from a Title object.
         * If this page is generated with action=render, however,
@@ -2093,7 +2145,7 @@ class Title implements LinkTarget {
        private function checkSpecialsAndNSPermissions( $action, $user, $errors, $rigor, $short ) {
                # Only 'createaccount' can be performed on special pages,
                # which don't actually exist in the DB.
-               if ( NS_SPECIAL == $this->mNamespace && $action !== 'createaccount' ) {
+               if ( $this->isSpecialPage() && $action !== 'createaccount' ) {
                        $errors[] = [ 'ns-specialprotected' ];
                }
 
@@ -2122,8 +2174,7 @@ class Title implements LinkTarget {
        private function checkCSSandJSPermissions( $action, $user, $errors, $rigor, $short ) {
                # Protect css/js subpages of user pages
                # XXX: this might be better using restrictions
-               # XXX: right 'editusercssjs' is deprecated, for backward compatibility only
-               if ( $action != 'patrol' && !$user->isAllowed( 'editusercssjs' ) ) {
+               if ( $action != 'patrol' ) {
                        if ( preg_match( '/^' . preg_quote( $user->getName(), '/' ) . '\//', $this->mTextform ) ) {
                                if ( $this->isCssSubpage() && !$user->isAllowedAny( 'editmyusercss', 'editusercss' ) ) {
                                        $errors[] = [ 'mycustomcssprotected', $action ];
@@ -2287,6 +2338,17 @@ class Title implements LinkTarget {
                        ) {
                                $errors[] = [ 'delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ];
                        }
+               } elseif ( $action === 'undelete' ) {
+                       if ( count( $this->getUserPermissionsErrorsInternal( 'edit', $user, $rigor, true ) ) ) {
+                               // Undeleting implies editing
+                               $errors[] = [ 'undelete-cantedit' ];
+                       }
+                       if ( !$this->exists()
+                               && count( $this->getUserPermissionsErrorsInternal( 'create', $user, $rigor, true ) )
+                       ) {
+                               // Undeleting where nothing currently exists implies creating
+                               $errors[] = [ 'undelete-cantcreate' ];
+                       }
                }
                return $errors;
        }
@@ -3110,7 +3172,7 @@ class Title implements LinkTarget {
                if ( $limit > -1 ) {
                        $options['LIMIT'] = $limit;
                }
-               $this->mSubpages = TitleArray::newFromResult(
+               return TitleArray::newFromResult(
                        $dbr->select( 'page',
                                [ 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' ],
                                $conds,
@@ -3118,7 +3180,6 @@ class Title implements LinkTarget {
                                $options
                        )
                );
-               return $this->mSubpages;
        }
 
        /**
@@ -3386,7 +3447,7 @@ class Title implements LinkTarget {
                $this->mTextform = strtr( $this->mDbkeyform, '_', ' ' );
 
                # We already know that some pages won't be in the database!
-               if ( $this->isExternal() || $this->mNamespace == NS_SPECIAL ) {
+               if ( $this->isExternal() || $this->isSpecialPage() ) {
                        $this->mArticleID = 0;
                }
 
@@ -3673,8 +3734,8 @@ class Title implements LinkTarget {
         * @return array|bool True on success, getUserPermissionsErrors()-like array on failure
         */
        public function moveTo( &$nt, $auth = true, $reason = '', $createRedirect = true,
-               array $changeTags = [] ) {
-
+               array $changeTags = []
+       ) {
                global $wgUser;
                $err = $this->isValidMoveOperation( $nt, $auth, $reason );
                if ( is_array( $err ) ) {
@@ -3711,8 +3772,8 @@ class Title implements LinkTarget {
         *     no pages were moved
         */
        public function moveSubpages( $nt, $auth = true, $reason = '', $createRedirect = true,
-               array $changeTags = [] ) {
-
+               array $changeTags = []
+       ) {
                global $wgMaximumMovedPages;
                // Check permissions
                if ( !$this->userCan( 'move-subpages' ) ) {
@@ -3954,21 +4015,52 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Get the revision ID of the previous revision
-        *
+        * Get next/previous revision ID relative to another revision ID
         * @param int $revId Revision ID. Get the revision that was before this one.
         * @param int $flags Title::GAID_FOR_UPDATE
-        * @return int|bool Old revision ID, or false if none exists
-        */
-       public function getPreviousRevisionID( $revId, $flags = 0 ) {
-               $db = ( $flags & self::GAID_FOR_UPDATE ) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_REPLICA );
+        * @param string $dir 'next' or 'prev'
+        * @return int|bool New revision ID, or false if none exists
+        */
+       private function getRelativeRevisionID( $revId, $flags, $dir ) {
+               $revId = (int)$revId;
+               if ( $dir === 'next' ) {
+                       $op = '>';
+                       $sort = 'ASC';
+               } elseif ( $dir === 'prev' ) {
+                       $op = '<';
+                       $sort = 'DESC';
+               } else {
+                       throw new InvalidArgumentException( '$dir must be "next" or "prev"' );
+               }
+
+               if ( $flags & self::GAID_FOR_UPDATE ) {
+                       $db = wfGetDB( DB_MASTER );
+               } else {
+                       $db = wfGetDB( DB_REPLICA, 'contributions' );
+               }
+
+               // Intentionally not caring if the specified revision belongs to this
+               // page. We only care about the timestamp.
+               $ts = $db->selectField( 'revision', 'rev_timestamp', [ 'rev_id' => $revId ], __METHOD__ );
+               if ( $ts === false ) {
+                       $ts = $db->selectField( 'archive', 'ar_timestamp', [ 'ar_rev_id' => $revId ], __METHOD__ );
+                       if ( $ts === false ) {
+                               // Or should this throw an InvalidArgumentException or something?
+                               return false;
+                       }
+               }
+               $ts = $db->addQuotes( $ts );
+
                $revId = $db->selectField( 'revision', 'rev_id',
                        [
                                'rev_page' => $this->getArticleID( $flags ),
-                               'rev_id < ' . intval( $revId )
+                               "rev_timestamp $op $ts OR (rev_timestamp = $ts AND rev_id $op $revId)"
                        ],
                        __METHOD__,
-                       [ 'ORDER BY' => 'rev_id DESC' ]
+                       [
+                               'ORDER BY' => "rev_timestamp $sort, rev_id $sort",
+                               'IGNORE INDEX' => 'rev_timestamp', // Probably needed for T159319
+                       ]
                );
 
                if ( $revId === false ) {
@@ -3978,6 +4070,17 @@ class Title implements LinkTarget {
                }
        }
 
+       /**
+        * Get the revision ID of the previous revision
+        *
+        * @param int $revId Revision ID. Get the revision that was before this one.
+        * @param int $flags Title::GAID_FOR_UPDATE
+        * @return int|bool Old revision ID, or false if none exists
+        */
+       public function getPreviousRevisionID( $revId, $flags = 0 ) {
+               return $this->getRelativeRevisionID( $revId, $flags, 'prev' );
+       }
+
        /**
         * Get the revision ID of the next revision
         *
@@ -3986,21 +4089,7 @@ class Title implements LinkTarget {
         * @return int|bool Next revision ID, or false if none exists
         */
        public function getNextRevisionID( $revId, $flags = 0 ) {
-               $db = ( $flags & self::GAID_FOR_UPDATE ) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_REPLICA );
-               $revId = $db->selectField( 'revision', 'rev_id',
-                       [
-                               'rev_page' => $this->getArticleID( $flags ),
-                               'rev_id > ' . intval( $revId )
-                       ],
-                       __METHOD__,
-                       [ 'ORDER BY' => 'rev_id' ]
-               );
-
-               if ( $revId === false ) {
-                       return false;
-               } else {
-                       return intval( $revId );
-               }
+               return $this->getRelativeRevisionID( $revId, $flags, 'next' );
        }
 
        /**
@@ -4016,7 +4105,10 @@ class Title implements LinkTarget {
                        $row = $db->selectRow( 'revision', Revision::selectFields(),
                                [ 'rev_page' => $pageId ],
                                __METHOD__,
-                               [ 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 ]
+                               [
+                                       'ORDER BY' => 'rev_timestamp ASC, rev_id ASC',
+                                       'IGNORE INDEX' => 'rev_timestamp', // See T159319
+                               ]
                        );
                        if ( $row ) {
                                return new Revision( $row );
@@ -4596,7 +4688,7 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Whether the magic words __INDEX__ and __NOINDEX__ function for  this page.
+        * Whether the magic words __INDEX__ and __NOINDEX__ function for this page.
         *
         * @return bool
         */