Use ContentHandler as a factory for recirects.
authordaniel <daniel.kinzler@wikimedia.de>
Fri, 5 Oct 2012 14:50:32 +0000 (16:50 +0200)
committerdaniel <daniel.kinzler@wikimedia.de>
Fri, 5 Oct 2012 14:50:32 +0000 (16:50 +0200)
Redirects should not be constructed as wikitext, since other content models
may use other mechanisms to represent redirects.

Change-Id: Id85c3b3ada1924628e4e51757573d233e998f920

includes/Title.php
includes/content/ContentHandler.php
includes/specials/SpecialMergeHistory.php

index affffc3..8f18a79 100644 (file)
@@ -3634,7 +3634,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 +3649,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 +3711,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 );
 
index 91da3e8..39b0d25 100644 (file)
@@ -411,6 +411,23 @@ abstract class ContentHandler {
         */
        public abstract function makeEmptyContent();
 
+       /**
+        * Creates a new Content object that acts as a redirect to the given page,
+        * or null of redirects are not supported by this content model.
+        *
+        * This default implementation always returns null. Subclasses supporting redirects
+        * must override this method.
+        *
+        * @since 1.21
+        *
+        * @param Title $destination the page to redirect to.
+        *
+        * @return Content
+        */
+       public function makeRedirectContent( Title $destination ) {
+               return null;
+       }
+
        /**
         * Returns the model id that identifies the content model this
         * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
@@ -1073,10 +1090,32 @@ class WikitextContentHandler extends TextContentHandler {
                return new WikitextContent( $text );
        }
 
+       /**
+        * @see ContentHandler::makeEmptyContent
+        *
+        * @return Content
+        */
        public function makeEmptyContent() {
                return new WikitextContent( '' );
        }
 
+
+       /**
+        * Returns a WikitextContent object representing a redirect to the given destination page.
+        *
+        * @see ContentHandler::makeRedirectContent
+        *
+        * @param Title $destination the page to redirect to.
+        *
+        * @return Content
+        */
+       public function makeRedirectContent( Title $destination ) {
+               $mwRedir = MagicWord::get( 'redirect' );
+               $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destination->getPrefixedText() . "]]\n";
+
+               return new WikitextContent( $redirectText );
+       }
+
        /**
         * Returns true because wikitext supports sections.
         *
index 1f05749..bc9a3d9 100644 (file)
@@ -373,26 +373,32 @@ class SpecialMergeHistory extends SpecialPage {
                                        $destTitle->getPrefixedText()
                                )->inContentLanguage()->text();
                        }
-                       $mwRedir = MagicWord::get( 'redirect' );
-                       $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destTitle->getPrefixedText() . "]]\n";
-                       $redirectPage = WikiPage::factory( $targetTitle );
-                       $redirectRevision = new Revision( array(
-                               'page'    => $this->mTargetID,
-                               'comment' => $comment,
-                               'text'    => $redirectText ) );
-                       $redirectRevision->insertOn( $dbw );
-                       $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
-
-                       # 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' => $this->mDestID ), __METHOD__ );
-                       $dbw->insert( 'pagelinks',
-                               array(
-                                       'pl_from'      => $this->mDestID,
-                                       'pl_namespace' => $destTitle->getNamespace(),
-                                       'pl_title'     => $destTitle->getDBkey() ),
-                               __METHOD__
-                       );
+
+                       $contentHandler = ContentHandler::getForTitle( $targetTitle );
+                       $redirectContent = $contentHandler->makeRedirectContent( $destTitle );
+
+                       if ( $redirectContent ) {
+                               $redirectPage = WikiPage::factory( $targetTitle );
+                               $redirectRevision = new Revision( array(
+                                       'page'    => $this->mTargetID,
+                                       'comment' => $comment,
+                                       'content' => $redirectContent ) );
+                               $redirectRevision->insertOn( $dbw );
+                               $redirectPage->updateRevisionOn( $dbw, $redirectRevision );
+
+                               # 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' => $this->mDestID ), __METHOD__ );
+                               $dbw->insert( 'pagelinks',
+                                       array(
+                                               'pl_from'      => $this->mDestID,
+                                               'pl_namespace' => $destTitle->getNamespace(),
+                                               'pl_title'     => $destTitle->getDBkey() ),
+                                       __METHOD__
+                               );
+                       } else {
+                               // would be nice to show a warning if we couldn't create a redirect
+                       }
                } else {
                        $targetTitle->invalidateCache(); // update histories
                }