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( 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 # The move is allowed only if (1) the target doesn't exist, or
68 # (2) the target is a redirect to the source, and has no history
69 # (so we can undo bad moves right after they're done).
71 if ( $this->newTitle
->getArticleID() ) { # Target exists; check for validity
72 if ( !$this->isValidMoveTarget() ) {
73 $status->fatal( 'articleexists' );
76 $tp = $this->newTitle
->getTitleProtection();
77 if ( $tp !== false ) {
78 if ( !$user->isAllowed( $tp['permission'] ) ) {
79 $status->fatal( 'cantmove-titleprotected' );
84 Hooks
::run( 'MovePageCheckPermissions',
85 array( $this->oldTitle
, $this->newTitle
, $user, $reason, $status )
92 * Does various sanity checks that the move is
93 * valid. Only things based on the two titles
94 * should be checked here.
98 public function isValidMove() {
99 global $wgContentHandlerUseDB;
100 $status = new Status();
102 if ( $this->oldTitle
->equals( $this->newTitle
) ) {
103 $status->fatal( 'selfmove' );
105 if ( !$this->oldTitle
->isMovable() ) {
106 $status->fatal( 'immobile-source-namespace', $this->oldTitle
->getNsText() );
108 if ( $this->newTitle
->isExternal() ) {
109 $status->fatal( 'immobile-target-namespace-iw' );
111 if ( !$this->newTitle
->isMovable() ) {
112 $status->fatal( 'immobile-target-namespace', $this->newTitle
->getNsText() );
115 $oldid = $this->oldTitle
->getArticleID();
117 if ( strlen( $this->newTitle
->getDBkey() ) < 1 ) {
118 $status->fatal( 'articleexists' );
121 ( $this->oldTitle
->getDBkey() == '' ) ||
123 ( $this->newTitle
->getDBkey() == '' )
125 $status->fatal( 'badarticleerror' );
128 // Content model checks
129 if ( !$wgContentHandlerUseDB &&
130 $this->oldTitle
->getContentModel() !== $this->newTitle
->getContentModel() ) {
131 // can't move a page if that would change the page's content model
134 ContentHandler
::getLocalizedName( $this->oldTitle
->getContentModel() ),
135 ContentHandler
::getLocalizedName( $this->newTitle
->getContentModel() )
139 // Image-specific checks
140 if ( $this->oldTitle
->inNamespace( NS_FILE
) ) {
141 $status->merge( $this->isValidFileMove() );
144 if ( $this->newTitle
->inNamespace( NS_FILE
) && !$this->oldTitle
->inNamespace( NS_FILE
) ) {
145 $status->fatal( 'nonfile-cannot-move-to-file' );
148 // Hook for extensions to say a title can't be moved for technical reasons
149 Hooks
::run( 'MovePageIsValidMove', array( $this->oldTitle
, $this->newTitle
, $status ) );
155 * Sanity checks for when a file is being moved
159 protected function isValidFileMove() {
160 $status = new Status();
161 $file = wfLocalFile( $this->oldTitle
);
162 if ( $file->exists() ) {
163 if ( $this->newTitle
->getText() != wfStripIllegalFilenameChars( $this->newTitle
->getText() ) ) {
164 $status->fatal( 'imageinvalidfilename' );
166 if ( !File
::checkExtensionCompatibility( $file, $this->newTitle
->getDBkey() ) ) {
167 $status->fatal( 'imagetypemismatch' );
171 if ( !$this->newTitle
->inNamespace( NS_FILE
) ) {
172 $status->fatal( 'imagenocrossnamespace' );
179 * Checks if $this can be moved to a given Title
180 * - Selects for update, so don't call it unless you mean business
185 protected function isValidMoveTarget() {
186 # Is it an existing file?
187 if ( $this->newTitle
->inNamespace( NS_FILE
) ) {
188 $file = wfLocalFile( $this->newTitle
);
189 if ( $file->exists() ) {
190 wfDebug( __METHOD__
. ": file exists\n" );
194 # Is it a redirect with no history?
195 if ( !$this->newTitle
->isSingleRevRedirect() ) {
196 wfDebug( __METHOD__
. ": not a one-rev redirect\n" );
199 # Get the article text
200 $rev = Revision
::newFromTitle( $this->newTitle
, false, Revision
::READ_LATEST
);
201 if ( !is_object( $rev ) ) {
204 $content = $rev->getContent();
205 # Does the redirect point to the source?
206 # Or is it a broken self-redirect, usually caused by namespace collisions?
207 $redirTitle = $content ?
$content->getRedirectTarget() : null;
210 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle
->getPrefixedDBkey() &&
211 $redirTitle->getPrefixedDBkey() !== $this->newTitle
->getPrefixedDBkey() ) {
212 wfDebug( __METHOD__
. ": redirect points to other page\n" );
218 # Fail safe (not a redirect after all. strange.)
219 wfDebug( __METHOD__
. ": failsafe: database says " . $this->newTitle
->getPrefixedDBkey() .
220 " is a redirect, but it doesn't contain a valid redirect.\n" );
227 * @param string $reason
228 * @param bool $createRedirect
231 public function move( User
$user, $reason, $createRedirect ) {
232 global $wgCategoryCollation;
234 Hooks
::run( 'TitleMove', array( $this->oldTitle
, $this->newTitle
, $user ) );
236 // If it is a file, move it first.
237 // It is done before all other moving stuff is done because it's hard to revert.
238 $dbw = wfGetDB( DB_MASTER
);
239 if ( $this->oldTitle
->getNamespace() == NS_FILE
) {
240 $file = wfLocalFile( $this->oldTitle
);
241 if ( $file->exists() ) {
242 $status = $file->move( $this->newTitle
);
243 if ( !$status->isOk() ) {
247 // Clear RepoGroup process cache
248 RepoGroup
::singleton()->clearCache( $this->oldTitle
);
249 RepoGroup
::singleton()->clearCache( $this->newTitle
); # clear false negative cache
252 $dbw->begin( __METHOD__
); # If $file was a LocalFile, its transaction would have closed our own.
253 $pageid = $this->oldTitle
->getArticleID( Title
::GAID_FOR_UPDATE
);
254 $protected = $this->oldTitle
->isProtected();
256 // Do the actual move
257 $this->moveToInternal( $user, $this->newTitle
, $reason, $createRedirect );
259 // Refresh the sortkey for this row. Be careful to avoid resetting
260 // cl_timestamp, which may disturb time-based lists on some sites.
261 // @todo This block should be killed, it's duplicating code
262 // from LinksUpdate::getCategoryInsertions() and friends.
263 $prefixes = $dbw->select(
265 array( 'cl_sortkey_prefix', 'cl_to' ),
266 array( 'cl_from' => $pageid ),
269 if ( $this->newTitle
->getNamespace() == NS_CATEGORY
) {
271 } elseif ( $this->newTitle
->getNamespace() == NS_FILE
) {
276 foreach ( $prefixes as $prefixRow ) {
277 $prefix = $prefixRow->cl_sortkey_prefix
;
278 $catTo = $prefixRow->cl_to
;
279 $dbw->update( 'categorylinks',
281 'cl_sortkey' => Collation
::singleton()->getSortKey(
282 $this->newTitle
->getCategorySortkey( $prefix ) ),
283 'cl_collation' => $wgCategoryCollation,
285 'cl_timestamp=cl_timestamp' ),
287 'cl_from' => $pageid,
293 $redirid = $this->oldTitle
->getArticleID();
296 # Protect the redirect title as the title used to be...
297 $dbw->insertSelect( 'page_restrictions', 'page_restrictions',
299 'pr_page' => $redirid,
300 'pr_type' => 'pr_type',
301 'pr_level' => 'pr_level',
302 'pr_cascade' => 'pr_cascade',
303 'pr_user' => 'pr_user',
304 'pr_expiry' => 'pr_expiry'
306 array( 'pr_page' => $pageid ),
310 # Update the protection log
311 $log = new LogPage( 'protect' );
312 $comment = wfMessage(
314 $this->oldTitle
->getPrefixedText(),
315 $this->newTitle
->getPrefixedText()
316 )->inContentLanguage()->text();
318 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
320 // @todo FIXME: $params?
321 $logId = $log->addEntry(
325 array( $this->oldTitle
->getPrefixedText() ),
329 // reread inserted pr_ids for log relation
330 $insertedPrIds = $dbw->select(
333 array( 'pr_page' => $redirid ),
336 $logRelationsValues = array();
337 foreach ( $insertedPrIds as $prid ) {
338 $logRelationsValues[] = $prid->pr_id
;
340 $log->addRelations( 'pr_id', $logRelationsValues, $logId );
343 // Update *_from_namespace fields as needed
344 if ( $this->oldTitle
->getNamespace() != $this->newTitle
->getNamespace() ) {
345 $dbw->update( 'pagelinks',
346 array( 'pl_from_namespace' => $this->newTitle
->getNamespace() ),
347 array( 'pl_from' => $pageid ),
350 $dbw->update( 'templatelinks',
351 array( 'tl_from_namespace' => $this->newTitle
->getNamespace() ),
352 array( 'tl_from' => $pageid ),
355 $dbw->update( 'imagelinks',
356 array( 'il_from_namespace' => $this->newTitle
->getNamespace() ),
357 array( 'il_from' => $pageid ),
363 $oldtitle = $this->oldTitle
->getDBkey();
364 $newtitle = $this->newTitle
->getDBkey();
365 $oldsnamespace = MWNamespace
::getSubject( $this->oldTitle
->getNamespace() );
366 $newsnamespace = MWNamespace
::getSubject( $this->newTitle
->getNamespace() );
367 if ( $oldsnamespace != $newsnamespace ||
$oldtitle != $newtitle ) {
368 WatchedItem
::duplicateEntries( $this->oldTitle
, $this->newTitle
);
371 $dbw->commit( __METHOD__
);
375 array( &$this->oldTitle
, &$this->newTitle
, &$user, $pageid, $redirid, $reason )
377 return Status
::newGood();
381 * Move page to a title which is either a redirect to the
382 * source page or nonexistent
384 * @fixme This was basically directly moved from Title, it should be split into smaller functions
385 * @param User $user the User doing the move
386 * @param Title $nt The page to move to, which should be a redirect or nonexistent
387 * @param string $reason The reason for the move
388 * @param bool $createRedirect Whether to leave a redirect at the old title. Does not check
389 * if the user has the suppressredirect right
390 * @throws MWException
392 private function moveToInternal( User
$user, &$nt, $reason = '', $createRedirect = true ) {
395 if ( $nt->exists() ) {
396 $moveOverRedirect = true;
397 $logType = 'move_redir';
399 $moveOverRedirect = false;
403 if ( $createRedirect ) {
404 if ( $this->oldTitle
->getNamespace() == NS_CATEGORY
405 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
407 $redirectContent = new WikitextContent(
408 wfMessage( 'category-move-redirect-override' )
409 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
411 $contentHandler = ContentHandler
::getForTitle( $this->oldTitle
);
412 $redirectContent = $contentHandler->makeRedirectContent( $nt,
413 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() );
416 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
418 $redirectContent = null;
421 // bug 57084: log_page should be the ID of the *moved* page
422 $oldid = $this->oldTitle
->getArticleID();
423 $logTitle = clone $this->oldTitle
;
425 $logEntry = new ManualLogEntry( 'move', $logType );
426 $logEntry->setPerformer( $user );
427 $logEntry->setTarget( $logTitle );
428 $logEntry->setComment( $reason );
429 $logEntry->setParameters( array(
430 '4::target' => $nt->getPrefixedText(),
431 '5::noredir' => $redirectContent ?
'0': '1',
434 $formatter = LogFormatter
::newFromEntry( $logEntry );
435 $formatter->setContext( RequestContext
::newExtraneousContext( $this->oldTitle
) );
436 $comment = $formatter->getPlainActionText();
438 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
440 # Truncate for whole multibyte characters.
441 $comment = $wgContLang->truncate( $comment, 255 );
443 $dbw = wfGetDB( DB_MASTER
);
445 $oldpage = WikiPage
::factory( $this->oldTitle
);
446 $oldcountable = $oldpage->isCountable();
448 $newpage = WikiPage
::factory( $nt );
450 if ( $moveOverRedirect ) {
451 $newid = $nt->getArticleID();
452 $newcontent = $newpage->getContent();
454 # Delete the old redirect. We don't save it to history since
455 # by definition if we've got here it's rather uninteresting.
456 # We have to remove it so that the next step doesn't trigger
457 # a conflict on the unique namespace+title index...
458 $dbw->delete( 'page', array( 'page_id' => $newid ), __METHOD__
);
460 $newpage->doDeleteUpdates( $newid, $newcontent );
463 # Save a null revision in the page's history notifying of the move
464 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true, $user );
465 if ( !is_object( $nullRevision ) ) {
466 throw new MWException( 'No valid null revision produced in ' . __METHOD__
);
469 $nullRevision->insertOn( $dbw );
471 # Change the name of the target page:
472 $dbw->update( 'page',
474 'page_namespace' => $nt->getNamespace(),
475 'page_title' => $nt->getDBkey(),
477 /* WHERE */ array( 'page_id' => $oldid ),
481 // clean up the old title before reset article id - bug 45348
482 if ( !$redirectContent ) {
483 WikiPage
::onArticleDelete( $this->oldTitle
);
486 $this->oldTitle
->resetArticleID( 0 ); // 0 == non existing
487 $nt->resetArticleID( $oldid );
488 $newpage->loadPageData( WikiPage
::READ_LOCKING
); // bug 46397
490 $newpage->updateRevisionOn( $dbw, $nullRevision );
492 Hooks
::run( 'NewRevisionFromEditComplete',
493 array( $newpage, $nullRevision, $nullRevision->getParentId(), $user ) );
495 $newpage->doEditUpdates( $nullRevision, $user,
496 array( 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ) );
498 if ( !$moveOverRedirect ) {
499 WikiPage
::onArticleCreate( $nt );
502 # Recreate the redirect, this time in the other direction.
503 if ( $redirectContent ) {
504 $redirectArticle = WikiPage
::factory( $this->oldTitle
);
505 $redirectArticle->loadFromRow( false, WikiPage
::READ_LOCKING
); // bug 46397
506 $newid = $redirectArticle->insertOn( $dbw );
507 if ( $newid ) { // sanity
508 $this->oldTitle
->resetArticleID( $newid );
509 $redirectRevision = new Revision( array(
510 'title' => $this->oldTitle
, // for determining the default content model
512 'user_text' => $user->getName(),
513 'user' => $user->getId(),
514 'comment' => $comment,
515 'content' => $redirectContent ) );
516 $redirectRevision->insertOn( $dbw );
517 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
519 Hooks
::run( 'NewRevisionFromEditComplete',
520 array( $redirectArticle, $redirectRevision, false, $user ) );
522 $redirectArticle->doEditUpdates( $redirectRevision, $user, array( 'created' => true ) );
527 $logid = $logEntry->insert();
528 $logEntry->publish( $logid );