merge latest master into Wikidata branch
[lhc/web/wiklou.git] / includes / Title.php
index affffc3..731fc14 100644 (file)
@@ -1173,7 +1173,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 +1232,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 +1267,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.
@@ -1408,13 +1489,14 @@ class Title {
         *
         * See getLocalURL for the arguments.
         *
+        * @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 {
@@ -3375,7 +3457,7 @@ class Title {
         * @return Mixed True on success, getUserPermissionsErrors()-like array on failure
         */
        public function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
-               global $wgUser;
+               global $wgUser, $wgContentHandlerUseDB;
 
                $errors = array();
                if ( !$nt ) {
@@ -3408,6 +3490,15 @@ class Title {
                        $errors[] = array( 'badarticleerror' );
                }
 
+               // Content model checks
+               if ( !$wgContentHandlerUseDB &&
+                               $this->getContentModel() !== $nt->getContentModel() ) {
+                       // can't move a page if that would change the page's content model
+                       $errors[] = array( 'bad-target-model',
+                                                       ContentHandler::getLocalizedName( $this->getContentModel() ),
+                                                       ContentHandler::getLocalizedName( $nt->getContentModel() ) );
+               }
+
                // Image-specific checks
                if ( $this->getNamespace() == NS_FILE ) {
                        $errors = array_merge( $errors, $this->validateFileMoveOperation( $nt ) );
@@ -3634,7 +3725,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 );
@@ -3642,7 +3740,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 );
@@ -3704,18 +3802,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 );