From: daniel Date: Fri, 5 Oct 2012 14:50:32 +0000 (+0200) Subject: Use ContentHandler as a factory for recirects. X-Git-Tag: 1.31.0-rc.0~22097^2^2~5^2 X-Git-Url: https://git.cyclocoop.org/%7B%24www_url%7Dadmin/compta/exercices/journal.php?a=commitdiff_plain;h=01e1b1a8e806ebd65b1ee157b0e555fe1a0641ef;p=lhc%2Fweb%2Fwiklou.git Use ContentHandler as a factory for recirects. Redirects should not be constructed as wikitext, since other content models may use other mechanisms to represent redirects. Change-Id: Id85c3b3ada1924628e4e51757573d233e998f920 --- diff --git a/includes/Title.php b/includes/Title.php index affffc3c68..8f18a79973 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -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 ); diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index 91da3e8bc0..39b0d25125 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -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. * diff --git a/includes/specials/SpecialMergeHistory.php b/includes/specials/SpecialMergeHistory.php index 1f057499d9..bc9a3d90f8 100644 --- a/includes/specials/SpecialMergeHistory.php +++ b/includes/specials/SpecialMergeHistory.php @@ -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 }