r79524 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r79523‎ | r79524 | r79525 >
Date:20:32, 3 January 2011
Author:vyznev
Status:ok (Comments)
Tags:brion 
Comment:
merge the nearly identical Title::moveToNewTitle() and Title::moveOverExistingRedirect() to a single Title::moveToInternal() method
Modified paths:
  • /trunk/phase3/includes/Title.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/Title.php
@@ -3100,17 +3100,14 @@
31013101
31023102 $pageid = $this->getArticleID();
31033103 $protected = $this->isProtected();
3104 - if ( $nt->exists() ) {
3105 - $err = $this->moveOverExistingRedirect( $nt, $reason, $createRedirect );
3106 - $pageCountChange = ( $createRedirect ? 0 : -1 );
3107 - } else { # Target didn't exist, do normal move.
3108 - $err = $this->moveToNewTitle( $nt, $reason, $createRedirect );
3109 - $pageCountChange = ( $createRedirect ? 1 : 0 );
3110 - }
 3104+ $pageCountChange = ( $createRedirect ? 1 : 0 ) - ( $nt->exists() ? 1 : 0 );
31113105
 3106+ // Do the actual move
 3107+ $err = $this->moveToInternal( $nt, $reason, $createRedirect );
31123108 if ( is_array( $err ) ) {
31133109 return $err;
31143110 }
 3111+
31153112 $redirid = $this->getArticleID();
31163113
31173114 // Refresh the sortkey for this row. Be careful to avoid resetting
@@ -3210,64 +3207,73 @@
32113208 }
32123209
32133210 /**
3214 - * Move page to a title which is at present a redirect to the
3215 - * source page
 3211+ * Move page to a title which is either a redirect to the
 3212+ * source page or nonexistent
32163213 *
3217 - * @param $nt \type{Title} the page to move to, which should currently
3218 - * be a redirect
 3214+ * @param $nt \type{Title} the page to move to, which should
 3215+ * be a redirect or nonexistent
32193216 * @param $reason \type{\string} The reason for the move
3220 - * @param $createRedirect \type{\bool} Whether to leave a redirect at the old title.
3221 - * Ignored if the user doesn't have the suppressredirect right
 3217+ * @param $createRedirect \type{\bool} Whether to leave a
 3218+ * redirect at the old title. Ignored if the user doesn't
 3219+ * have the suppressredirect right
32223220 */
3223 - private function moveOverExistingRedirect( &$nt, $reason = '', $createRedirect = true ) {
3224 - global $wgUseSquid, $wgUser, $wgContLang;
 3221+ private function moveToInternal( &$nt, $reason = '', $createRedirect = true ) {
 3222+ global $wgUser, $wgContLang;
32253223
3226 - $comment = wfMsgForContent( '1movedto2_redir', $this->getPrefixedText(), $nt->getPrefixedText() );
 3224+ $moveOverRedirect = $nt->exists();
32273225
 3226+ $commentMsg = ( $moveOverRedirect ? '1movedto2_redir' : '1movedto2' );
 3227+ $comment = wfMsgForContent( $commentMsg, $this->getPrefixedText(), $nt->getPrefixedText() );
 3228+
32283229 if ( $reason ) {
32293230 $comment .= wfMsgForContent( 'colon-separator' ) . $reason;
32303231 }
32313232 # Truncate for whole multibyte characters. +5 bytes for ellipsis
32323233 $comment = $wgContLang->truncate( $comment, 250 );
32333234
3234 - $now = wfTimestampNow();
3235 - $newid = $nt->getArticleID();
32363235 $oldid = $this->getArticleID();
32373236 $latest = $this->getLatestRevID();
32383237
32393238 $dbw = wfGetDB( DB_MASTER );
32403239
3241 - $rcts = $dbw->timestamp( $nt->getEarliestRevTime() );
3242 - $newns = $nt->getNamespace();
3243 - $newdbk = $nt->getDBkey();
 3240+ if ( $moveOverRedirect ) {
 3241+ $rcts = $dbw->timestamp( $nt->getEarliestRevTime() );
32443242
3245 - # Delete the old redirect. We don't save it to history since
3246 - # by definition if we've got here it's rather uninteresting.
3247 - # We have to remove it so that the next step doesn't trigger
3248 - # a conflict on the unique namespace+title index...
3249 - $dbw->delete( 'page', array( 'page_id' => $newid ), __METHOD__ );
3250 - if ( !$dbw->cascadingDeletes() ) {
3251 - $dbw->delete( 'revision', array( 'rev_page' => $newid ), __METHOD__ );
3252 - global $wgUseTrackbacks;
3253 - if ( $wgUseTrackbacks ) {
3254 - $dbw->delete( 'trackbacks', array( 'tb_page' => $newid ), __METHOD__ );
 3243+ $newid = $nt->getArticleID();
 3244+ $newns = $nt->getNamespace();
 3245+ $newdbk = $nt->getDBkey();
 3246+
 3247+ # Delete the old redirect. We don't save it to history since
 3248+ # by definition if we've got here it's rather uninteresting.
 3249+ # We have to remove it so that the next step doesn't trigger
 3250+ # a conflict on the unique namespace+title index...
 3251+ $dbw->delete( 'page', array( 'page_id' => $newid ), __METHOD__ );
 3252+ if ( !$dbw->cascadingDeletes() ) {
 3253+ $dbw->delete( 'revision', array( 'rev_page' => $newid ), __METHOD__ );
 3254+ global $wgUseTrackbacks;
 3255+ if ( $wgUseTrackbacks ) {
 3256+ $dbw->delete( 'trackbacks', array( 'tb_page' => $newid ), __METHOD__ );
 3257+ }
 3258+ $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), __METHOD__ );
 3259+ $dbw->delete( 'imagelinks', array( 'il_from' => $newid ), __METHOD__ );
 3260+ $dbw->delete( 'categorylinks', array( 'cl_from' => $newid ), __METHOD__ );
 3261+ $dbw->delete( 'templatelinks', array( 'tl_from' => $newid ), __METHOD__ );
 3262+ $dbw->delete( 'externallinks', array( 'el_from' => $newid ), __METHOD__ );
 3263+ $dbw->delete( 'langlinks', array( 'll_from' => $newid ), __METHOD__ );
 3264+ $dbw->delete( 'redirect', array( 'rd_from' => $newid ), __METHOD__ );
32553265 }
3256 - $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), __METHOD__ );
3257 - $dbw->delete( 'imagelinks', array( 'il_from' => $newid ), __METHOD__ );
3258 - $dbw->delete( 'categorylinks', array( 'cl_from' => $newid ), __METHOD__ );
3259 - $dbw->delete( 'templatelinks', array( 'tl_from' => $newid ), __METHOD__ );
3260 - $dbw->delete( 'externallinks', array( 'el_from' => $newid ), __METHOD__ );
3261 - $dbw->delete( 'langlinks', array( 'll_from' => $newid ), __METHOD__ );
3262 - $dbw->delete( 'redirect', array( 'rd_from' => $newid ), __METHOD__ );
 3266+ // If the target page was recently created, it may have an entry in recentchanges still
 3267+ $dbw->delete( 'recentchanges',
 3268+ array( 'rc_timestamp' => $rcts, 'rc_namespace' => $newns, 'rc_title' => $newdbk, 'rc_new' => 1 ),
 3269+ __METHOD__
 3270+ );
32633271 }
3264 - // If the redirect was recently created, it may have an entry in recentchanges still
3265 - $dbw->delete( 'recentchanges',
3266 - array( 'rc_timestamp' => $rcts, 'rc_namespace' => $newns, 'rc_title' => $newdbk, 'rc_new' => 1 ),
3267 - __METHOD__
3268 - );
32693272
32703273 # Save a null revision in the page's history notifying of the move
32713274 $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
 3275+ if ( !is_object( $nullRevision ) ) {
 3276+ throw new MWException( 'No valid null revision produced in ' . __METHOD__ );
 3277+ }
32723278 $nullRevId = $nullRevision->insertOn( $dbw );
32733279
32743280 $article = new Article( $this );
@@ -3276,7 +3282,7 @@
32773283 # Change the name of the target page:
32783284 $dbw->update( 'page',
32793285 /* SET */ array(
3280 - 'page_touched' => $dbw->timestamp( $now ),
 3286+ 'page_touched' => $dbw->timestamp(),
32813287 'page_namespace' => $nt->getNamespace(),
32823288 'page_title' => $nt->getDBkey(),
32833289 'page_latest' => $nullRevId,
@@ -3318,103 +3324,17 @@
33193325
33203326 # Log the move
33213327 $log = new LogPage( 'move' );
3322 - $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText(), 2 => $redirectSuppressed ) );
 3328+ $logType = ( $moveOverRedirect ? 'move_redir' : 'move' );
 3329+ $log->addEntry( $logType, $this, $reason, array( 1 => $nt->getPrefixedText(), 2 => $redirectSuppressed ) );
33233330
3324 - # Purge squid
3325 - if ( $wgUseSquid ) {
3326 - $urls = array_merge( $nt->getSquidURLs(), $this->getSquidURLs() );
3327 - $u = new SquidUpdate( $urls );
3328 - $u->doUpdate();
3329 - }
3330 -
3331 - }
3332 -
3333 - /**
3334 - * Move page to non-existing title.
3335 - *
3336 - * @param $nt \type{Title} the new Title
3337 - * @param $reason \type{\string} The reason for the move
3338 - * @param $createRedirect \type{\bool} Whether to create a redirect from the old title to the new title
3339 - * Ignored if the user doesn't have the suppressredirect right
3340 - */
3341 - private function moveToNewTitle( &$nt, $reason = '', $createRedirect = true ) {
3342 - global $wgUser, $wgContLang;
3343 -
3344 - $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
3345 - if ( $reason ) {
3346 - $comment .= wfMsgExt( 'colon-separator',
3347 - array( 'escapenoentities', 'content' ) );
3348 - $comment .= $reason;
3349 - }
3350 - # Truncate for whole multibyte characters. +5 bytes for ellipsis
3351 - $comment = $wgContLang->truncate( $comment, 250 );
3352 -
3353 - $oldid = $this->getArticleID();
3354 - $latest = $this->getLatestRevId();
3355 -
3356 - $dbw = wfGetDB( DB_MASTER );
3357 - $now = $dbw->timestamp();
3358 -
3359 - # Save a null revision in the page's history notifying of the move
3360 - $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
3361 - if ( !is_object( $nullRevision ) ) {
3362 - throw new MWException( 'No valid null revision produced in ' . __METHOD__ );
3363 - }
3364 - $nullRevId = $nullRevision->insertOn( $dbw );
3365 -
3366 - $article = new Article( $this );
3367 - wfRunHooks( 'NewRevisionFromEditComplete', array( $article, $nullRevision, $latest, $wgUser ) );
3368 -
3369 - # Rename page entry
3370 - $dbw->update( 'page',
3371 - /* SET */ array(
3372 - 'page_touched' => $now,
3373 - 'page_namespace' => $nt->getNamespace(),
3374 - 'page_title' => $nt->getDBkey(),
3375 - 'page_latest' => $nullRevId,
3376 - ),
3377 - /* WHERE */ array( 'page_id' => $oldid ),
3378 - __METHOD__
3379 - );
3380 - $nt->resetArticleID( $oldid );
3381 -
3382 - if ( $createRedirect || !$wgUser->isAllowed( 'suppressredirect' ) ) {
3383 - # Insert redirect
3384 - $mwRedir = MagicWord::get( 'redirect' );
3385 - $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
3386 - $redirectArticle = new Article( $this );
3387 - $newid = $redirectArticle->insertOn( $dbw );
3388 - $redirectRevision = new Revision( array(
3389 - 'page' => $newid,
3390 - 'comment' => $comment,
3391 - 'text' => $redirectText ) );
3392 - $redirectRevision->insertOn( $dbw );
3393 - $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
3394 -
3395 - wfRunHooks( 'NewRevisionFromEditComplete', array( $redirectArticle, $redirectRevision, false, $wgUser ) );
3396 -
3397 - # Record the just-created redirect's linking to the page
3398 - $dbw->insert( 'pagelinks',
3399 - array(
3400 - 'pl_from' => $newid,
3401 - 'pl_namespace' => $nt->getNamespace(),
3402 - 'pl_title' => $nt->getDBkey() ),
3403 - __METHOD__ );
3404 - $redirectSuppressed = false;
 3331+ # Purge caches for old and new titles
 3332+ if ( $moveOverRedirect ) {
 3333+ # A simple purge is enough when moving over a redirect
 3334+ $nt->purgeSquid();
34053335 } else {
3406 - $this->resetArticleID( 0 );
3407 - $redirectSuppressed = true;
 3336+ # Purge caches as per article creation, including any pages that link to this title
 3337+ Article::onArticleCreate( $nt );
34083338 }
3409 -
3410 - # Log the move
3411 - $log = new LogPage( 'move' );
3412 - $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText(), 2 => $redirectSuppressed ) );
3413 -
3414 - # Purge caches as per article creation
3415 - Article::onArticleCreate( $nt );
3416 -
3417 - # Purge old title from squid
3418 - # The new title, and links to the new title, are purged in Article::onArticleCreate()
34193339 $this->purgeSquid();
34203340 }
34213341

Comments

#Comment by Aaron Schulz (talk | contribs)   22:26, 14 June 2011

Looks OK...

BTW, brion and I could figure out this (pre-existing) line on IRC:

# It should have no other outgoing links...
$dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), __METHOD__ );

Status & tagging log