Fix warnings and enforce conventions in ContentHandler tests.
[lhc/web/wiklou.git] / includes / Title.php
index f2531a2..b1569e2 100644 (file)
@@ -387,6 +387,8 @@ class Title {
         * @deprecated since 1.21, use Content::getRedirectTarget instead.
         */
        public static function newFromRedirect( $text ) {
+               ContentHandler::deprecated( __METHOD__, '1.21' );
+
                $content = ContentHandler::makeContent( $text, null, CONTENT_MODEL_WIKITEXT );
                return $content->getRedirectTarget();
        }
@@ -402,6 +404,8 @@ class Title {
         * @deprecated since 1.21, use Content::getUltimateRedirectTarget instead.
         */
        public static function newFromRedirectRecurse( $text ) {
+               ContentHandler::deprecated( __METHOD__, '1.21' );
+
                $content = ContentHandler::makeContent( $text, null, CONTENT_MODEL_WIKITEXT );
                return $content->getUltimateRedirectTarget();
        }
@@ -417,6 +421,8 @@ class Title {
         * @deprecated since 1.21, use Content::getRedirectChain instead.
         */
        public static function newFromRedirectArray( $text ) {
+               ContentHandler::deprecated( __METHOD__, '1.21' );
+
                $content = ContentHandler::makeContent( $text, null, CONTENT_MODEL_WIKITEXT );
                return $content->getRedirectChain();
        }
@@ -691,7 +697,7 @@ class Title {
        /**
         * Convenience method for checking a title's content model name
         *
-        * @param int $id
+        * @param String $id The content model ID (use the CONTENT_MODEL_XXX constants).
         * @return Boolean true if $this->getContentModel() == $id
         */
        public function hasContentModel( $id ) {
@@ -1173,7 +1179,49 @@ class Title {
        }
 
        /**
-        * Get the base page name, i.e. the leftmost part before any slashes
+        * Get the root page name text without a namespace, i.e. the leftmost part before any slashes
+        *
+        * @par Example:
+        * @code
+        * Title::newFromText('User:Foo/Bar/Baz')->getRootText();
+        * # returns: 'Foo'
+        * @endcode
+        *
+        * @return String Root name
+        * @since 1.20
+        */
+       public function getRootText() {
+               if ( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
+                       return $this->getText();
+               }
+
+               return strtok( $this->getText(), '/' );
+       }
+
+       /**
+        * Get the root page name title, i.e. the leftmost part before any slashes
+        *
+        * @par Example:
+        * @code
+        * Title::newFromText('User:Foo/Bar/Baz')->getRootTitle();
+        * # returns: Title{User:Foo}
+        * @endcode
+        *
+        * @return Title Root title
+        * @since 1.20
+        */
+       public function getRootTitle() {
+               return Title::makeTitle( $this->getNamespace(), $this->getRootText() );
+       }
+
+       /**
+        * Get the base page name without a namespace, i.e. the part before the subpage name
+        *
+        * @par Example:
+        * @code
+        * Title::newFromText('User:Foo/Bar/Baz')->getBaseText();
+        * # returns: 'Foo/Bar'
+        * @endcode
         *
         * @return String Base name
         */
@@ -1190,9 +1238,31 @@ class Title {
                return implode( '/', $parts );
        }
 
+       /**
+        * Get the base page name title, i.e. the part before the subpage name
+        *
+        * @par Example:
+        * @code
+        * Title::newFromText('User:Foo/Bar/Baz')->getBaseTitle();
+        * # returns: Title{User:Foo/Bar}
+        * @endcode
+        *
+        * @return Title Base title
+        * @since 1.20
+        */
+       public function getBaseTitle() {
+               return Title::makeTitle( $this->getNamespace(), $this->getBaseText() );
+       }
+
        /**
         * Get the lowest-level subpage name, i.e. the rightmost part after any slashes
         *
+        * @par Example:
+        * @code
+        * Title::newFromText('User:Foo/Bar/Baz')->getSubpageText();
+        * # returns: "Baz"
+        * @endcode
+        *
         * @return String Subpage name
         */
        public function getSubpageText() {
@@ -1203,6 +1273,23 @@ class Title {
                return( $parts[count( $parts ) - 1] );
        }
 
+       /**
+        * Get the title for a subpage of the current page
+        *
+        * @par Example:
+        * @code
+        * Title::newFromText('User:Foo/Bar/Baz')->getSubpage("Asdf");
+        * # returns: Title{User:Foo/Bar/Baz/Asdf}
+        * @endcode
+        *
+        * @param $text String The subpage name to add to the title
+        * @return Title Subpage title
+        * @since 1.20
+        */
+       public function getSubpage( $text ) {
+               return Title::makeTitleSafe( $this->getNamespace(), $this->getText() . '/' . $text );
+       }
+
        /**
         * Get the HTML-escaped displayable text form.
         * Used for the title field in <a> tags.
@@ -1282,6 +1369,8 @@ class Title {
         *
         * @see self::getLocalURL
         * @see wfExpandUrl
+        * @param $query
+        * @param $query2 bool
         * @param $proto Protocol type to use in URL
         * @return String the URL
         */
@@ -1408,13 +1497,16 @@ class Title {
         *
         * See getLocalURL for the arguments.
         *
+        * @param $query
+        * @param $query2 bool
+        * @param $proto Protocol to use; setting this will cause a full URL to be used
         * @see self::getLocalURL
         * @return String the URL
         */
-       public function getLinkURL( $query = '', $query2 = false ) {
+       public function getLinkURL( $query = '', $query2 = false, $proto = PROTO_RELATIVE ) {
                wfProfileIn( __METHOD__ );
-               if ( $this->isExternal() ) {
-                       $ret = $this->getFullURL( $query, $query2 );
+               if ( $this->isExternal() || $proto !== PROTO_RELATIVE ) {
+                       $ret = $this->getFullURL( $query, $query2, $proto );
                } elseif ( $this->getPrefixedText() === '' && $this->getFragment() !== '' ) {
                        $ret = $this->getFragmentForURL();
                } else {
@@ -1653,15 +1745,8 @@ class Title {
 
                        if ( !$user->isAllowed( 'move' ) ) {
                                // 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 = User::groupHasPermission( 'user', 'move' );
+                               $autoconfirmedCanMove = User::groupHasPermission( 'autoconfirmed', 'move' );
                                if ( $user->isAnon() && ( $userCanMove || $autoconfirmedCanMove ) ) {
                                        // custom message if logged-in users without any special rights can move
                                        $errors[] = array( 'movenologintext' );
@@ -2002,13 +2087,13 @@ class Title {
         * @return Array list of errors
         */
        private function checkReadPermissions( $action, $user, $errors, $doExpensiveQueries, $short ) {
-               global $wgWhitelistRead, $wgGroupPermissions, $wgRevokePermissions;
+               global $wgWhitelistRead, $wgRevokePermissions;
                static $useShortcut = null;
 
                # Initialize the $useShortcut boolean, to determine if we can skip quite a bit of code below
                if ( is_null( $useShortcut ) ) {
                        $useShortcut = true;
-                       if ( empty( $wgGroupPermissions['*']['read'] ) ) {
+                       if ( !User::groupHasPermission( '*', 'read' ) ) {
                                # Not a public wiki, so no shortcut
                                $useShortcut = false;
                        } elseif ( !empty( $wgRevokePermissions ) ) {
@@ -2841,10 +2926,12 @@ class Title {
 
                $linkCache = LinkCache::singleton();
                $cached = $linkCache->getGoodLinkFieldObj( $this, 'redirect' );
-               if ( $cached === null ) { # check the assumption that the cache actually knows about this title
-                       # XXX: this does apparently happen, see https://bugzilla.wikimedia.org/show_bug.cgi?id=37209
-                       #      as a stop gap, perhaps log this, but don't throw an exception?
-                       throw new MWException( "LinkCache doesn't currently know about this title: " . $this->getPrefixedDBkey() );
+               if ( $cached === null ) {
+                       // TODO: check the assumption that the cache actually knows about this title
+                       // and handle this, such as get the title from the database.
+                       // See https://bugzilla.wikimedia.org/show_bug.cgi?id=37209
+                       wfDebug( "LinkCache doesn't currently know about this title: " . $this->getPrefixedDBkey() );
+                       wfDebug( wfBacktrace() );
                }
 
                $this->mRedirect = (bool)$cached;
@@ -2872,7 +2959,8 @@ class Title {
                if ( $cached === null ) { # check the assumption that the cache actually knows about this title
                        # XXX: this does apparently happen, see https://bugzilla.wikimedia.org/show_bug.cgi?id=37209
                        #      as a stop gap, perhaps log this, but don't throw an exception?
-                       throw new MWException( "LinkCache doesn't currently know about this title: " . $this->getPrefixedDBkey() );
+                       wfDebug( "LinkCache doesn't currently know about this title: " . $this->getPrefixedDBkey() );
+                       wfDebug( wfBacktrace() );
                }
 
                $this->mLength = intval( $cached );
@@ -3643,7 +3731,14 @@ class Title {
                        $logType = 'move';
                }
 
-               $redirectSuppressed = !$createRedirect;
+               if ( $createRedirect ) {
+                       $contentHandler = ContentHandler::getForTitle( $this );
+                       $redirectContent = $contentHandler->makeRedirectContent( $nt );
+
+                       // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
+               } else {
+                       $redirectContent = null;
+               }
 
                $logEntry = new ManualLogEntry( 'move', $logType );
                $logEntry->setPerformer( $wgUser );
@@ -3651,7 +3746,7 @@ class Title {
                $logEntry->setComment( $reason );
                $logEntry->setParameters( array(
                        '4::target' => $nt->getPrefixedText(),
-                       '5::noredir' => $redirectSuppressed ? '1': '0',
+                       '5::noredir' => $redirectContent ? '0': '1',
                ) );
 
                $formatter = LogFormatter::newFromEntry( $logEntry );
@@ -3686,7 +3781,8 @@ class Title {
                if ( !is_object( $nullRevision ) ) {
                        throw new MWException( 'No valid null revision produced in ' . __METHOD__ );
                }
-               $nullRevId = $nullRevision->insertOn( $dbw );
+
+               $nullRevision->insertOn( $dbw );
 
                # Change the name of the target page:
                $dbw->update( 'page',
@@ -3713,18 +3809,16 @@ class Title {
                }
 
                # Recreate the redirect, this time in the other direction.
-               if ( $redirectSuppressed ) {
+               if ( !$redirectContent ) {
                        WikiPage::onArticleDelete( $this );
                } else {
-                       $mwRedir = MagicWord::get( 'redirect' );
-                       $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
                        $redirectArticle = WikiPage::factory( $this );
                        $newid = $redirectArticle->insertOn( $dbw );
                        if ( $newid ) { // sanity
                                $redirectRevision = new Revision( array(
                                        'page'    => $newid,
                                        'comment' => $comment,
-                                       'text'    => $redirectText ) );
+                                       'content'    => $redirectContent ) );
                                $redirectRevision->insertOn( $dbw );
                                $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );