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 $file->load( File
::READ_LATEST
);
163 if ( $file->exists() ) {
164 if ( $this->newTitle
->getText() != wfStripIllegalFilenameChars( $this->newTitle
->getText() ) ) {
165 $status->fatal( 'imageinvalidfilename' );
167 if ( !File
::checkExtensionCompatibility( $file, $this->newTitle
->getDBkey() ) ) {
168 $status->fatal( 'imagetypemismatch' );
172 if ( !$this->newTitle
->inNamespace( NS_FILE
) ) {
173 $status->fatal( 'imagenocrossnamespace' );
180 * Checks if $this can be moved to a given Title
181 * - Selects for update, so don't call it unless you mean business
186 protected function isValidMoveTarget() {
187 # Is it an existing file?
188 if ( $this->newTitle
->inNamespace( NS_FILE
) ) {
189 $file = wfLocalFile( $this->newTitle
);
190 $file->load( File
::READ_LATEST
);
191 if ( $file->exists() ) {
192 wfDebug( __METHOD__
. ": file exists\n" );
196 # Is it a redirect with no history?
197 if ( !$this->newTitle
->isSingleRevRedirect() ) {
198 wfDebug( __METHOD__
. ": not a one-rev redirect\n" );
201 # Get the article text
202 $rev = Revision
::newFromTitle( $this->newTitle
, false, Revision
::READ_LATEST
);
203 if ( !is_object( $rev ) ) {
206 $content = $rev->getContent();
207 # Does the redirect point to the source?
208 # Or is it a broken self-redirect, usually caused by namespace collisions?
209 $redirTitle = $content ?
$content->getRedirectTarget() : null;
212 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle
->getPrefixedDBkey() &&
213 $redirTitle->getPrefixedDBkey() !== $this->newTitle
->getPrefixedDBkey() ) {
214 wfDebug( __METHOD__
. ": redirect points to other page\n" );
220 # Fail safe (not a redirect after all. strange.)
221 wfDebug( __METHOD__
. ": failsafe: database says " . $this->newTitle
->getPrefixedDBkey() .
222 " is a redirect, but it doesn't contain a valid redirect.\n" );
229 * @param string $reason
230 * @param bool $createRedirect
233 public function move( User
$user, $reason, $createRedirect ) {
234 global $wgCategoryCollation;
236 Hooks
::run( 'TitleMove', array( $this->oldTitle
, $this->newTitle
, $user ) );
238 // If it is a file, move it first.
239 // It is done before all other moving stuff is done because it's hard to revert.
240 $dbw = wfGetDB( DB_MASTER
);
241 if ( $this->oldTitle
->getNamespace() == NS_FILE
) {
242 $file = wfLocalFile( $this->oldTitle
);
243 $file->load( File
::READ_LATEST
);
244 if ( $file->exists() ) {
245 $status = $file->move( $this->newTitle
);
246 if ( !$status->isOk() ) {
250 // Clear RepoGroup process cache
251 RepoGroup
::singleton()->clearCache( $this->oldTitle
);
252 RepoGroup
::singleton()->clearCache( $this->newTitle
); # clear false negative cache
255 $dbw->begin( __METHOD__
); # If $file was a LocalFile, its transaction would have closed our own.
256 $pageid = $this->oldTitle
->getArticleID( Title
::GAID_FOR_UPDATE
);
257 $protected = $this->oldTitle
->isProtected();
259 // Do the actual move
260 $this->moveToInternal( $user, $this->newTitle
, $reason, $createRedirect );
262 // Refresh the sortkey for this row. Be careful to avoid resetting
263 // cl_timestamp, which may disturb time-based lists on some sites.
264 // @todo This block should be killed, it's duplicating code
265 // from LinksUpdate::getCategoryInsertions() and friends.
266 $prefixes = $dbw->select(
268 array( 'cl_sortkey_prefix', 'cl_to' ),
269 array( 'cl_from' => $pageid ),
272 if ( $this->newTitle
->getNamespace() == NS_CATEGORY
) {
274 } elseif ( $this->newTitle
->getNamespace() == NS_FILE
) {
279 foreach ( $prefixes as $prefixRow ) {
280 $prefix = $prefixRow->cl_sortkey_prefix
;
281 $catTo = $prefixRow->cl_to
;
282 $dbw->update( 'categorylinks',
284 'cl_sortkey' => Collation
::singleton()->getSortKey(
285 $this->newTitle
->getCategorySortkey( $prefix ) ),
286 'cl_collation' => $wgCategoryCollation,
288 'cl_timestamp=cl_timestamp' ),
290 'cl_from' => $pageid,
296 $redirid = $this->oldTitle
->getArticleID();
299 # Protect the redirect title as the title used to be...
300 $dbw->insertSelect( 'page_restrictions', 'page_restrictions',
302 'pr_page' => $redirid,
303 'pr_type' => 'pr_type',
304 'pr_level' => 'pr_level',
305 'pr_cascade' => 'pr_cascade',
306 'pr_user' => 'pr_user',
307 'pr_expiry' => 'pr_expiry'
309 array( 'pr_page' => $pageid ),
313 # Update the protection log
314 $log = new LogPage( 'protect' );
315 $comment = wfMessage(
317 $this->oldTitle
->getPrefixedText(),
318 $this->newTitle
->getPrefixedText()
319 )->inContentLanguage()->text();
321 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
323 // @todo FIXME: $params?
324 $logId = $log->addEntry(
328 array( $this->oldTitle
->getPrefixedText() ),
332 // reread inserted pr_ids for log relation
333 $insertedPrIds = $dbw->select(
336 array( 'pr_page' => $redirid ),
339 $logRelationsValues = array();
340 foreach ( $insertedPrIds as $prid ) {
341 $logRelationsValues[] = $prid->pr_id
;
343 $log->addRelations( 'pr_id', $logRelationsValues, $logId );
346 // Update *_from_namespace fields as needed
347 if ( $this->oldTitle
->getNamespace() != $this->newTitle
->getNamespace() ) {
348 $dbw->update( 'pagelinks',
349 array( 'pl_from_namespace' => $this->newTitle
->getNamespace() ),
350 array( 'pl_from' => $pageid ),
353 $dbw->update( 'templatelinks',
354 array( 'tl_from_namespace' => $this->newTitle
->getNamespace() ),
355 array( 'tl_from' => $pageid ),
358 $dbw->update( 'imagelinks',
359 array( 'il_from_namespace' => $this->newTitle
->getNamespace() ),
360 array( 'il_from' => $pageid ),
366 $oldtitle = $this->oldTitle
->getDBkey();
367 $newtitle = $this->newTitle
->getDBkey();
368 $oldsnamespace = MWNamespace
::getSubject( $this->oldTitle
->getNamespace() );
369 $newsnamespace = MWNamespace
::getSubject( $this->newTitle
->getNamespace() );
370 if ( $oldsnamespace != $newsnamespace ||
$oldtitle != $newtitle ) {
371 WatchedItem
::duplicateEntries( $this->oldTitle
, $this->newTitle
);
374 $dbw->commit( __METHOD__
);
378 array( &$this->oldTitle
, &$this->newTitle
, &$user, $pageid, $redirid, $reason )
380 return Status
::newGood();
384 * Move page to a title which is either a redirect to the
385 * source page or nonexistent
387 * @fixme This was basically directly moved from Title, it should be split into smaller functions
388 * @param User $user the User doing the move
389 * @param Title $nt The page to move to, which should be a redirect or nonexistent
390 * @param string $reason The reason for the move
391 * @param bool $createRedirect Whether to leave a redirect at the old title. Does not check
392 * if the user has the suppressredirect right
393 * @throws MWException
395 private function moveToInternal( User
$user, &$nt, $reason = '', $createRedirect = true ) {
398 if ( $nt->exists() ) {
399 $moveOverRedirect = true;
400 $logType = 'move_redir';
402 $moveOverRedirect = false;
406 if ( $createRedirect ) {
407 if ( $this->oldTitle
->getNamespace() == NS_CATEGORY
408 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
410 $redirectContent = new WikitextContent(
411 wfMessage( 'category-move-redirect-override' )
412 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
414 $contentHandler = ContentHandler
::getForTitle( $this->oldTitle
);
415 $redirectContent = $contentHandler->makeRedirectContent( $nt,
416 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() );
419 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
421 $redirectContent = null;
424 // bug 57084: log_page should be the ID of the *moved* page
425 $oldid = $this->oldTitle
->getArticleID();
426 $logTitle = clone $this->oldTitle
;
428 $logEntry = new ManualLogEntry( 'move', $logType );
429 $logEntry->setPerformer( $user );
430 $logEntry->setTarget( $logTitle );
431 $logEntry->setComment( $reason );
432 $logEntry->setParameters( array(
433 '4::target' => $nt->getPrefixedText(),
434 '5::noredir' => $redirectContent ?
'0': '1',
437 $formatter = LogFormatter
::newFromEntry( $logEntry );
438 $formatter->setContext( RequestContext
::newExtraneousContext( $this->oldTitle
) );
439 $comment = $formatter->getPlainActionText();
441 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
443 # Truncate for whole multibyte characters.
444 $comment = $wgContLang->truncate( $comment, 255 );
446 $dbw = wfGetDB( DB_MASTER
);
448 $oldpage = WikiPage
::factory( $this->oldTitle
);
449 $oldcountable = $oldpage->isCountable();
451 $newpage = WikiPage
::factory( $nt );
453 if ( $moveOverRedirect ) {
454 $newid = $nt->getArticleID();
455 $newcontent = $newpage->getContent();
457 # Delete the old redirect. We don't save it to history since
458 # by definition if we've got here it's rather uninteresting.
459 # We have to remove it so that the next step doesn't trigger
460 # a conflict on the unique namespace+title index...
461 $dbw->delete( 'page', array( 'page_id' => $newid ), __METHOD__
);
463 $newpage->doDeleteUpdates( $newid, $newcontent );
466 # Save a null revision in the page's history notifying of the move
467 $nullRevision = Revision
::newNullRevision( $dbw, $oldid, $comment, true, $user );
468 if ( !is_object( $nullRevision ) ) {
469 throw new MWException( 'No valid null revision produced in ' . __METHOD__
);
472 $nullRevision->insertOn( $dbw );
474 # Change the name of the target page:
475 $dbw->update( 'page',
477 'page_namespace' => $nt->getNamespace(),
478 'page_title' => $nt->getDBkey(),
480 /* WHERE */ array( 'page_id' => $oldid ),
484 // clean up the old title before reset article id - bug 45348
485 if ( !$redirectContent ) {
486 WikiPage
::onArticleDelete( $this->oldTitle
);
489 $this->oldTitle
->resetArticleID( 0 ); // 0 == non existing
490 $nt->resetArticleID( $oldid );
491 $newpage->loadPageData( WikiPage
::READ_LOCKING
); // bug 46397
493 $newpage->updateRevisionOn( $dbw, $nullRevision );
495 Hooks
::run( 'NewRevisionFromEditComplete',
496 array( $newpage, $nullRevision, $nullRevision->getParentId(), $user ) );
498 $newpage->doEditUpdates( $nullRevision, $user,
499 array( 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ) );
501 if ( !$moveOverRedirect ) {
502 WikiPage
::onArticleCreate( $nt );
505 # Recreate the redirect, this time in the other direction.
506 if ( $redirectContent ) {
507 $redirectArticle = WikiPage
::factory( $this->oldTitle
);
508 $redirectArticle->loadFromRow( false, WikiPage
::READ_LOCKING
); // bug 46397
509 $newid = $redirectArticle->insertOn( $dbw );
510 if ( $newid ) { // sanity
511 $this->oldTitle
->resetArticleID( $newid );
512 $redirectRevision = new Revision( array(
513 'title' => $this->oldTitle
, // for determining the default content model
515 'user_text' => $user->getName(),
516 'user' => $user->getId(),
517 'comment' => $comment,
518 'content' => $redirectContent ) );
519 $redirectRevision->insertOn( $dbw );
520 $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
522 Hooks
::run( 'NewRevisionFromEditComplete',
523 array( $redirectArticle, $redirectRevision, false, $user ) );
525 $redirectArticle->doEditUpdates( $redirectRevision, $user, array( 'created' => true ) );
530 $logid = $logEntry->insert();
531 $logEntry->publish( $logid );