4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
23 * Handles the backend logic of moving a page from one title
40 public function __construct( Title
$oldTitle, Title
$newTitle ) {
41 $this->oldTitle
= $oldTitle;
42 $this->newTitle
= $newTitle;
45 public function checkPermissions( User
$user, $reason ) {
46 $status = new Status();
48 $errors = wfMergeErrorArrays(
49 $this->oldTitle
->getUserPermissionsErrors( 'move', $user ),
50 $this->oldTitle
->getUserPermissionsErrors( 'edit', $user ),
51 $this->newTitle
->getUserPermissionsErrors( 'move-target', $user ),
52 $this->newTitle
->getUserPermissionsErrors( 'edit', $user )
55 // Convert into a Status object
57 foreach ( $errors as $error ) {
58 call_user_func_array( [ $status, 'fatal' ], $error );
62 if ( EditPage
::matchSummarySpamRegex( $reason ) !== false ) {
63 // This is kind of lame, won't display nice
64 $status->fatal( 'spamprotectiontext' );
67 $tp = $this->newTitle
->getTitleProtection();
68 if ( $tp !== false && !$user->isAllowed( $tp['permission'] ) ) {
69 $status->fatal( 'cantmove-titleprotected' );
72 Hooks
::run( 'MovePageCheckPermissions',
73 [ $this->oldTitle
, $this->newTitle
, $user, $reason, $status ]
80 * Does various sanity checks that the move is
81 * valid. Only things based on the two titles
82 * should be checked here.
86 public function isValidMove() {
87 global $wgContentHandlerUseDB;
88 $status = new Status();
90 if ( $this->oldTitle
->equals( $this->newTitle
) ) {
91 $status->fatal( 'selfmove' );
93 if ( !$this->oldTitle
->isMovable() ) {
94 $status->fatal( 'immobile-source-namespace', $this->oldTitle
->getNsText() );
96 if ( $this->newTitle
->isExternal() ) {
97 $status->fatal( 'immobile-target-namespace-iw' );
99 if ( !$this->newTitle
->isMovable() ) {
100 $status->fatal( 'immobile-target-namespace', $this->newTitle
->getNsText() );
103 $oldid = $this->oldTitle
->getArticleID();
105 if ( strlen( $this->newTitle
->getDBkey() ) < 1 ) {
106 $status->fatal( 'articleexists' );
109 ( $this->oldTitle
->getDBkey() == '' ) ||
111 ( $this->newTitle
->getDBkey() == '' )
113 $status->fatal( 'badarticleerror' );
116 # The move is allowed only if (1) the target doesn't exist, or
117 # (2) the target is a redirect to the source, and has no history
118 # (so we can undo bad moves right after they're done).
119 if ( $this->newTitle
->getArticleID() && !$this->isValidMoveTarget() ) {
120 $status->fatal( 'articleexists' );
123 // Content model checks
124 if ( !$wgContentHandlerUseDB &&
125 $this->oldTitle
->getContentModel() !== $this->newTitle
->getContentModel() ) {
126 // can't move a page if that would change the page's content model
129 ContentHandler
::getLocalizedName( $this->oldTitle
->getContentModel() ),
130 ContentHandler
::getLocalizedName( $this->newTitle
->getContentModel() )
134 // Image-specific checks
135 if ( $this->oldTitle
->inNamespace( NS_FILE
) ) {
136 $status->merge( $this->isValidFileMove() );
139 if ( $this->newTitle
->inNamespace( NS_FILE
) && !$this->oldTitle
->inNamespace( NS_FILE
) ) {
140 $status->fatal( 'nonfile-cannot-move-to-file' );
143 // Hook for extensions to say a title can't be moved for technical reasons
144 Hooks
::run( 'MovePageIsValidMove', [ $this->oldTitle
, $this->newTitle
, $status ] );
150 * Sanity checks for when a file is being moved
154 protected function isValidFileMove() {
155 $status = new Status();
156 $file = wfLocalFile( $this->oldTitle
);
157 $file->load( File
::READ_LATEST
);
158 if ( $file->exists() ) {
159 if ( $this->newTitle
->getText() != wfStripIllegalFilenameChars( $this->newTitle
->getText() ) ) {
160 $status->fatal( 'imageinvalidfilename' );
162 if ( !File
::checkExtensionCompatibility( $file, $this->newTitle
->getDBkey() ) ) {
163 $status->fatal( 'imagetypemismatch' );
167 if ( !$this->newTitle
->inNamespace( NS_FILE
) ) {
168 $status->fatal( 'imagenocrossnamespace' );
175 * Checks if $this can be moved to a given Title
176 * - Selects for update, so don't call it unless you mean business
181 protected function isValidMoveTarget() {
182 # Is it an existing file?
183 if ( $this->newTitle
->inNamespace( NS_FILE
) ) {
184 $file = wfLocalFile( $this->newTitle
);
185 $file->load( File
::READ_LATEST
);
186 if ( $file->exists() ) {
187 wfDebug( __METHOD__
. ": file exists\n" );
191 # Is it a redirect with no history?
192 if ( !$this->newTitle
->isSingleRevRedirect() ) {
193 wfDebug( __METHOD__
. ": not a one-rev redirect\n" );
196 # Get the article text
197 $rev = Revision
::newFromTitle( $this->newTitle
, false, Revision
::READ_LATEST
);
198 if ( !is_object( $rev ) ) {
201 $content = $rev->getContent();
202 # Does the redirect point to the source?
203 # Or is it a broken self-redirect, usually caused by namespace collisions?
204 $redirTitle = $content ?
$content->getRedirectTarget() : null;
207 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle
->getPrefixedDBkey() &&
208 $redirTitle->getPrefixedDBkey() !== $this->newTitle
->getPrefixedDBkey() ) {
209 wfDebug( __METHOD__
. ": redirect points to other page\n" );
215 # Fail safe (not a redirect after all. strange.)
216 wfDebug( __METHOD__
. ": failsafe: database says " . $this->newTitle
->getPrefixedDBkey() .
217 " is a redirect, but it doesn't contain a valid redirect.\n" );
224 * @param string $reason
225 * @param bool $createRedirect
228 public function move( User
$user, $reason, $createRedirect ) {
229 global $wgCategoryCollation;
231 Hooks
::run( 'TitleMove', [ $this->oldTitle
, $this->newTitle
, $user ] );
233 // If it is a file, move it first.
234 // It is done before all other moving stuff is done because it's hard to revert.
235 $dbw = wfGetDB( DB_MASTER
);
236 if ( $this->oldTitle
->getNamespace() == NS_FILE
) {
237 $file = wfLocalFile( $this->oldTitle
);
238 $file->load( File
::READ_LATEST
);
239 if ( $file->exists() ) {
240 $status = $file->move( $this->newTitle
);
241 if ( !$status->isOk() ) {
245 // Clear RepoGroup process cache
246 RepoGroup
::singleton()->clearCache( $this->oldTitle
);
247 RepoGroup
::singleton()->clearCache( $this->newTitle
); # clear false negative cache
250 $dbw->startAtomic( __METHOD__
);
252 Hooks
::run( 'TitleMoveStarting', [ $this->oldTitle
, $this->newTitle
, $user ] );
254 $pageid = $this->oldTitle
->getArticleID( Title
::GAID_FOR_UPDATE
);
255 $protected = $this->oldTitle
->isProtected();
257 // Do the actual move
258 $nullRevision = $this->moveToInternal( $user, $this->newTitle
, $reason, $createRedirect );
260 // Refresh the sortkey for this row. Be careful to avoid resetting
261 // cl_timestamp, which may disturb time-based lists on some sites.
262 // @todo This block should be killed, it's duplicating code
263 // from LinksUpdate::getCategoryInsertions() and friends.
264 $prefixes = $dbw->select(
266 [ 'cl_sortkey_prefix', 'cl_to' ],
267 [ 'cl_from' => $pageid ],
270 if ( $this->newTitle
->getNamespace() == NS_CATEGORY
) {
272 } elseif ( $this->newTitle
->getNamespace() == NS_FILE
) {
277 foreach ( $prefixes as $prefixRow ) {
278 $prefix = $prefixRow->cl_sortkey_prefix
;
279 $catTo = $prefixRow->cl_to
;
280 $dbw->update( 'categorylinks',
282 'cl_sortkey' => Collation
::singleton()->getSortKey(
283 $this->newTitle
->getCategorySortkey( $prefix ) ),
284 'cl_collation' => $wgCategoryCollation,
286 'cl_timestamp=cl_timestamp' ],
288 'cl_from' => $pageid,
294 $redirid = $this->oldTitle
->getArticleID();
297 # Protect the redirect title as the title used to be...
298 $dbw->insertSelect( 'page_restrictions', 'page_restrictions',
300 'pr_page' => $redirid,
301 'pr_type' => 'pr_type',
302 'pr_level' => 'pr_level',
303 'pr_cascade' => 'pr_cascade',
304 'pr_user' => 'pr_user',
305 'pr_expiry' => 'pr_expiry'
307 [ 'pr_page' => $pageid ],
312 // Build comment for log
313 $comment = wfMessage(
315 $this->oldTitle
->getPrefixedText(),
316 $this->newTitle
->getPrefixedText()
317 )->inContentLanguage()->text();
319 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
322 // reread inserted pr_ids for log relation
323 $insertedPrIds = $dbw->select(
326 [ 'pr_page' => $redirid ],
329 $logRelationsValues = [];
330 foreach ( $insertedPrIds as $prid ) {
331 $logRelationsValues[] = $prid->pr_id
;
334 // Update the protection log
335 $logEntry = new ManualLogEntry( 'protect', 'move_prot' );
336 $logEntry->setTarget( $this->newTitle
);
337 $logEntry->setComment( $comment );
338 $logEntry->setPerformer( $user );
339 $logEntry->setParameters( [
340 '4::oldtitle' => $this->oldTitle
->getPrefixedText(),
342 $logEntry->setRelations( [ 'pr_id' => $logRelationsValues ] );
343 $logId = $logEntry->insert();
344 $logEntry->publish( $logId );
347 // Update *_from_namespace fields as needed
348 if ( $this->oldTitle
->getNamespace() != $this->newTitle
->getNamespace() ) {
349 $dbw->update( 'pagelinks',
350 [ 'pl_from_namespace' => $this->newTitle
->getNamespace() ],
351 [ 'pl_from' => $pageid ],
354 $dbw->update( 'templatelinks',
355 [ 'tl_from_namespace' => $this->newTitle
->getNamespace() ],
356 [ 'tl_from' => $pageid ],
359 $dbw->update( 'imagelinks',
360 [ 'il_from_namespace' => $this->newTitle
->getNamespace() ],
361 [ 'il_from' => $pageid ],
367 $oldtitle = $this->oldTitle
->getDBkey();
368 $newtitle = $this->newTitle
->getDBkey();
369 $oldsnamespace = MWNamespace
::getSubject( $this->oldTitle
->getNamespace() );
370 $newsnamespace = MWNamespace
::getSubject( $this->newTitle
->getNamespace() );
371 if ( $oldsnamespace != $newsnamespace ||
$oldtitle != $newtitle ) {
372 WatchedItem
::duplicateEntries( $this->oldTitle
, $this->newTitle
);
376 'TitleMoveCompleting',
377 [ $this->oldTitle
, $this->newTitle
,
378 $user, $pageid, $redirid, $reason, $nullRevision ]
381 $dbw->endAtomic( __METHOD__
);
392 $dbw->onTransactionIdle( function () use ( $params, $dbw ) {
393 // Keep each single hook handler atomic
394 $dbw->setFlag( DBO_TRX
); // flag is automatically reset by DB layer
395 Hooks
::run( 'TitleMoveComplete', $params );
398 return Status
::newGood();
402 * Move page to a title which is either a redirect to the
403 * source page or nonexistent
405 * @fixme This was basically directly moved from Title, it should be split into smaller functions
406 * @param User $user the User doing the move
407 * @param Title $nt The page to move to, which should be a redirect or nonexistent
408 * @param string $reason The reason for the move
409 * @param bool $createRedirect Whether to leave a redirect at the old title. Does not check
410 * if the user has the suppressredirect right
411 * @return Revision the revision created by the move
412 * @throws MWException
414 private function moveToInternal( User
$user, &$nt, $reason = '', $createRedirect = true ) {
417 if ( $nt->exists() ) {
418 $moveOverRedirect = true;
419 $logType = 'move_redir';
421 $moveOverRedirect = false;
425 if ( $createRedirect ) {
426 if ( $this->oldTitle
->getNamespace() == NS_CATEGORY
427 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
429 $redirectContent = new WikitextContent(
430 wfMessage( 'category-move-redirect-override' )
431 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
433 $contentHandler = ContentHandler
::getForTitle( $this->oldTitle
);
434 $redirectContent = $contentHandler->makeRedirectContent( $nt,
435 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() );
438 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
440 $redirectContent = null;
443 // Figure out whether the content model is no longer the default
444 $oldDefault = ContentHandler
::getDefaultModelFor( $this->oldTitle
);
445 $contentModel = $this->oldTitle
->getContentModel();
446 $newDefault = ContentHandler
::getDefaultModelFor( $nt );
447 $defaultContentModelChanging = ( $oldDefault !== $newDefault
448 && $oldDefault === $contentModel );
450 // bug 57084: log_page should be the ID of the *moved* page
451 $oldid = $this->oldTitle
->getArticleID();
452 $logTitle = clone $this->oldTitle
;
454 $logEntry = new ManualLogEntry( 'move', $logType );
455 $logEntry->setPerformer( $user );
456 $logEntry->setTarget( $logTitle );
457 $logEntry->setComment( $reason );
458 $logEntry->setParameters( [
459 '4::target' => $nt->getPrefixedText(),
460 '5::noredir' => $redirectContent ?
'0': '1',
463 $formatter = LogFormatter
::newFromEntry( $logEntry );
464 $formatter->setContext( RequestContext
::newExtraneousContext( $this->oldTitle
) );
465 $comment = $formatter->getPlainActionText();
467 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
469 # Truncate for whole multibyte characters.
470 $comment = $wgContLang->truncate( $comment, 255 );
472 $dbw = wfGetDB( DB_MASTER
);
474 $oldpage = WikiPage
::factory( $this->oldTitle
);
475 $oldcountable = $oldpage->isCountable();
477 $newpage = WikiPage
::factory( $nt );
479 if ( $moveOverRedirect ) {
480 $newid = $nt->getArticleID();
481 $newcontent = $newpage->getContent();
483 # Delete the old redirect. We don't save it to history since
484 # by definition if we've got here it's rather uninteresting.
485 # We have to remove it so that the next step doesn't trigger
486 # a conflict on the unique namespace+title index...
487 $dbw->delete( 'page', [ 'page_id' => $newid ], __METHOD__
);
489 $newpage->doDeleteUpdates( $newid, $newcontent );
492 # Save a null revision in the page's history notifying of the move
493 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true, $user );
494 if ( !is_object( $nullRevision ) ) {
495 throw new MWException( 'No valid null revision produced in ' . __METHOD__
);
498 $nullRevision->insertOn( $dbw );
500 # Change the name of the target page:
501 $dbw->update( 'page',
503 'page_namespace' => $nt->getNamespace(),
504 'page_title' => $nt->getDBkey(),
506 /* WHERE */ [ 'page_id' => $oldid ],
510 // clean up the old title before reset article id - bug 45348
511 if ( !$redirectContent ) {
512 WikiPage
::onArticleDelete( $this->oldTitle
);
515 $this->oldTitle
->resetArticleID( 0 ); // 0 == non existing
516 $nt->resetArticleID( $oldid );
517 $newpage->loadPageData( WikiPage
::READ_LOCKING
); // bug 46397
519 $newpage->updateRevisionOn( $dbw, $nullRevision );
521 Hooks
::run( 'NewRevisionFromEditComplete',
522 [ $newpage, $nullRevision, $nullRevision->getParentId(), $user ] );
524 $newpage->doEditUpdates( $nullRevision, $user,
525 [ 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ] );
527 // If the default content model changes, we need to populate rev_content_model
528 if ( $defaultContentModelChanging ) {
531 [ 'rev_content_model' => $contentModel ],
532 [ 'rev_page' => $nt->getArticleID(), 'rev_content_model IS NULL' ],
537 if ( !$moveOverRedirect ) {
538 WikiPage
::onArticleCreate( $nt );
541 # Recreate the redirect, this time in the other direction.
542 if ( $redirectContent ) {
543 $redirectArticle = WikiPage
::factory( $this->oldTitle
);
544 $redirectArticle->loadFromRow( false, WikiPage
::READ_LOCKING
); // bug 46397
545 $newid = $redirectArticle->insertOn( $dbw );
546 if ( $newid ) { // sanity
547 $this->oldTitle
->resetArticleID( $newid );
548 $redirectRevision = new Revision( [
549 'title' => $this->oldTitle
, // for determining the default content model
551 'user_text' => $user->getName(),
552 'user' => $user->getId(),
553 'comment' => $comment,
554 'content' => $redirectContent ] );
555 $redirectRevision->insertOn( $dbw );
556 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
558 Hooks
::run( 'NewRevisionFromEditComplete',
559 [ $redirectArticle, $redirectRevision, false, $user ] );
561 $redirectArticle->doEditUpdates( $redirectRevision, $user, [ 'created' => true ] );
566 $logid = $logEntry->insert();
567 $logEntry->publish( $logid );
569 return $nullRevision;