Merge "Revert "Hide HHVM tag on Special:{Contributions,RecentChanges,...}""
[lhc/web/wiklou.git] / includes / MovePage.php
1 <?php
2
3 /**
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.
8 *
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.
13 *
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
18 *
19 * @file
20 */
21
22 /**
23 * Handles the backend logic of moving a page from one title
24 * to another.
25 *
26 * @since 1.24
27 */
28 class MovePage {
29
30 /**
31 * @var Title
32 */
33 protected $oldTitle;
34
35 /**
36 * @var Title
37 */
38 protected $newTitle;
39
40 public function __construct( Title $oldTitle, Title $newTitle ) {
41 $this->oldTitle = $oldTitle;
42 $this->newTitle = $newTitle;
43 }
44
45 public function checkPermissions( User $user, $reason ) {
46 $status = new Status();
47
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 )
53 );
54
55 // Convert into a Status object
56 if ( $errors ) {
57 foreach ( $errors as $error ) {
58 call_user_func_array( array( $status, 'fatal' ), $error );
59 }
60 }
61
62 if ( EditPage::matchSummarySpamRegex( $reason ) !== false ) {
63 // This is kind of lame, won't display nice
64 $status->fatal( 'spamprotectiontext' );
65 }
66
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).
70
71 if ( $this->newTitle->getArticleID() ) { # Target exists; check for validity
72 if ( !$this->isValidMoveTarget() ) {
73 $status->fatal( 'articleexists' );
74 }
75 } else {
76 $tp = $this->newTitle->getTitleProtection();
77 if ( $tp !== false ) {
78 if ( !$user->isAllowed( $tp['permission'] ) ) {
79 $status->fatal( 'cantmove-titleprotected' );
80 }
81 }
82 }
83
84 Hooks::run( 'MovePageCheckPermissions',
85 array( $this->oldTitle, $this->newTitle, $user, $reason, $status )
86 );
87
88 return $status;
89 }
90
91 /**
92 * Does various sanity checks that the move is
93 * valid. Only things based on the two titles
94 * should be checked here.
95 *
96 * @return Status
97 */
98 public function isValidMove() {
99 global $wgContentHandlerUseDB;
100 $status = new Status();
101
102 if ( $this->oldTitle->equals( $this->newTitle ) ) {
103 $status->fatal( 'selfmove' );
104 }
105 if ( !$this->oldTitle->isMovable() ) {
106 $status->fatal( 'immobile-source-namespace', $this->oldTitle->getNsText() );
107 }
108 if ( $this->newTitle->isExternal() ) {
109 $status->fatal( 'immobile-target-namespace-iw' );
110 }
111 if ( !$this->newTitle->isMovable() ) {
112 $status->fatal( 'immobile-target-namespace', $this->newTitle->getNsText() );
113 }
114
115 $oldid = $this->oldTitle->getArticleID();
116
117 if ( strlen( $this->newTitle->getDBkey() ) < 1 ) {
118 $status->fatal( 'articleexists' );
119 }
120 if (
121 ( $this->oldTitle->getDBkey() == '' ) ||
122 ( !$oldid ) ||
123 ( $this->newTitle->getDBkey() == '' )
124 ) {
125 $status->fatal( 'badarticleerror' );
126 }
127
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
132 $status->fatal(
133 'bad-target-model',
134 ContentHandler::getLocalizedName( $this->oldTitle->getContentModel() ),
135 ContentHandler::getLocalizedName( $this->newTitle->getContentModel() )
136 );
137 }
138
139 // Image-specific checks
140 if ( $this->oldTitle->inNamespace( NS_FILE ) ) {
141 $status->merge( $this->isValidFileMove() );
142 }
143
144 if ( $this->newTitle->inNamespace( NS_FILE ) && !$this->oldTitle->inNamespace( NS_FILE ) ) {
145 $status->fatal( 'nonfile-cannot-move-to-file' );
146 }
147
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 ) );
150
151 return $status;
152 }
153
154 /**
155 * Sanity checks for when a file is being moved
156 *
157 * @return Status
158 */
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' );
166 }
167 if ( !File::checkExtensionCompatibility( $file, $this->newTitle->getDBkey() ) ) {
168 $status->fatal( 'imagetypemismatch' );
169 }
170 }
171
172 if ( !$this->newTitle->inNamespace( NS_FILE ) ) {
173 $status->fatal( 'imagenocrossnamespace' );
174 }
175
176 return $status;
177 }
178
179 /**
180 * Checks if $this can be moved to a given Title
181 * - Selects for update, so don't call it unless you mean business
182 *
183 * @since 1.25
184 * @return bool
185 */
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" );
193 return false;
194 }
195 }
196 # Is it a redirect with no history?
197 if ( !$this->newTitle->isSingleRevRedirect() ) {
198 wfDebug( __METHOD__ . ": not a one-rev redirect\n" );
199 return false;
200 }
201 # Get the article text
202 $rev = Revision::newFromTitle( $this->newTitle, false, Revision::READ_LATEST );
203 if ( !is_object( $rev ) ) {
204 return false;
205 }
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;
210
211 if ( $redirTitle ) {
212 if ( $redirTitle->getPrefixedDBkey() !== $this->oldTitle->getPrefixedDBkey() &&
213 $redirTitle->getPrefixedDBkey() !== $this->newTitle->getPrefixedDBkey() ) {
214 wfDebug( __METHOD__ . ": redirect points to other page\n" );
215 return false;
216 } else {
217 return true;
218 }
219 } else {
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" );
223 return false;
224 }
225 }
226
227 /**
228 * @param User $user
229 * @param string $reason
230 * @param bool $createRedirect
231 * @return Status
232 */
233 public function move( User $user, $reason, $createRedirect ) {
234 global $wgCategoryCollation;
235
236 Hooks::run( 'TitleMove', array( $this->oldTitle, $this->newTitle, $user ) );
237
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() ) {
247 return $status;
248 }
249 }
250 // Clear RepoGroup process cache
251 RepoGroup::singleton()->clearCache( $this->oldTitle );
252 RepoGroup::singleton()->clearCache( $this->newTitle ); # clear false negative cache
253 }
254
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();
258
259 // Do the actual move
260 $this->moveToInternal( $user, $this->newTitle, $reason, $createRedirect );
261
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(
267 'categorylinks',
268 array( 'cl_sortkey_prefix', 'cl_to' ),
269 array( 'cl_from' => $pageid ),
270 __METHOD__
271 );
272 if ( $this->newTitle->getNamespace() == NS_CATEGORY ) {
273 $type = 'subcat';
274 } elseif ( $this->newTitle->getNamespace() == NS_FILE ) {
275 $type = 'file';
276 } else {
277 $type = 'page';
278 }
279 foreach ( $prefixes as $prefixRow ) {
280 $prefix = $prefixRow->cl_sortkey_prefix;
281 $catTo = $prefixRow->cl_to;
282 $dbw->update( 'categorylinks',
283 array(
284 'cl_sortkey' => Collation::singleton()->getSortKey(
285 $this->newTitle->getCategorySortkey( $prefix ) ),
286 'cl_collation' => $wgCategoryCollation,
287 'cl_type' => $type,
288 'cl_timestamp=cl_timestamp' ),
289 array(
290 'cl_from' => $pageid,
291 'cl_to' => $catTo ),
292 __METHOD__
293 );
294 }
295
296 $redirid = $this->oldTitle->getArticleID();
297
298 if ( $protected ) {
299 # Protect the redirect title as the title used to be...
300 $dbw->insertSelect( 'page_restrictions', 'page_restrictions',
301 array(
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'
308 ),
309 array( 'pr_page' => $pageid ),
310 __METHOD__,
311 array( 'IGNORE' )
312 );
313 # Update the protection log
314 $log = new LogPage( 'protect' );
315 $comment = wfMessage(
316 'prot_1movedto2',
317 $this->oldTitle->getPrefixedText(),
318 $this->newTitle->getPrefixedText()
319 )->inContentLanguage()->text();
320 if ( $reason ) {
321 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
322 }
323 // @todo FIXME: $params?
324 $logId = $log->addEntry(
325 'move_prot',
326 $this->newTitle,
327 $comment,
328 array( $this->oldTitle->getPrefixedText() ),
329 $user
330 );
331
332 // reread inserted pr_ids for log relation
333 $insertedPrIds = $dbw->select(
334 'page_restrictions',
335 'pr_id',
336 array( 'pr_page' => $redirid ),
337 __METHOD__
338 );
339 $logRelationsValues = array();
340 foreach ( $insertedPrIds as $prid ) {
341 $logRelationsValues[] = $prid->pr_id;
342 }
343 $log->addRelations( 'pr_id', $logRelationsValues, $logId );
344 }
345
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 ),
351 __METHOD__
352 );
353 $dbw->update( 'templatelinks',
354 array( 'tl_from_namespace' => $this->newTitle->getNamespace() ),
355 array( 'tl_from' => $pageid ),
356 __METHOD__
357 );
358 $dbw->update( 'imagelinks',
359 array( 'il_from_namespace' => $this->newTitle->getNamespace() ),
360 array( 'il_from' => $pageid ),
361 __METHOD__
362 );
363 }
364
365 # Update watchlists
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 );
372 }
373
374 $dbw->commit( __METHOD__ );
375
376 Hooks::run(
377 'TitleMoveComplete',
378 array( &$this->oldTitle, &$this->newTitle, &$user, $pageid, $redirid, $reason )
379 );
380 return Status::newGood();
381 }
382
383 /**
384 * Move page to a title which is either a redirect to the
385 * source page or nonexistent
386 *
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
394 */
395 private function moveToInternal( User $user, &$nt, $reason = '', $createRedirect = true ) {
396 global $wgContLang;
397
398 if ( $nt->exists() ) {
399 $moveOverRedirect = true;
400 $logType = 'move_redir';
401 } else {
402 $moveOverRedirect = false;
403 $logType = 'move';
404 }
405
406 if ( $createRedirect ) {
407 if ( $this->oldTitle->getNamespace() == NS_CATEGORY
408 && !wfMessage( 'category-move-redirect-override' )->inContentLanguage()->isDisabled()
409 ) {
410 $redirectContent = new WikitextContent(
411 wfMessage( 'category-move-redirect-override' )
412 ->params( $nt->getPrefixedText() )->inContentLanguage()->plain() );
413 } else {
414 $contentHandler = ContentHandler::getForTitle( $this->oldTitle );
415 $redirectContent = $contentHandler->makeRedirectContent( $nt,
416 wfMessage( 'move-redirect-text' )->inContentLanguage()->plain() );
417 }
418
419 // NOTE: If this page's content model does not support redirects, $redirectContent will be null.
420 } else {
421 $redirectContent = null;
422 }
423
424 // bug 57084: log_page should be the ID of the *moved* page
425 $oldid = $this->oldTitle->getArticleID();
426 $logTitle = clone $this->oldTitle;
427
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',
435 ) );
436
437 $formatter = LogFormatter::newFromEntry( $logEntry );
438 $formatter->setContext( RequestContext::newExtraneousContext( $this->oldTitle ) );
439 $comment = $formatter->getPlainActionText();
440 if ( $reason ) {
441 $comment .= wfMessage( 'colon-separator' )->inContentLanguage()->text() . $reason;
442 }
443 # Truncate for whole multibyte characters.
444 $comment = $wgContLang->truncate( $comment, 255 );
445
446 $dbw = wfGetDB( DB_MASTER );
447
448 $oldpage = WikiPage::factory( $this->oldTitle );
449 $oldcountable = $oldpage->isCountable();
450
451 $newpage = WikiPage::factory( $nt );
452
453 if ( $moveOverRedirect ) {
454 $newid = $nt->getArticleID();
455 $newcontent = $newpage->getContent();
456
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__ );
462
463 $newpage->doDeleteUpdates( $newid, $newcontent );
464 }
465
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__ );
470 }
471
472 $nullRevision->insertOn( $dbw );
473
474 # Change the name of the target page:
475 $dbw->update( 'page',
476 /* SET */ array(
477 'page_namespace' => $nt->getNamespace(),
478 'page_title' => $nt->getDBkey(),
479 ),
480 /* WHERE */ array( 'page_id' => $oldid ),
481 __METHOD__
482 );
483
484 // clean up the old title before reset article id - bug 45348
485 if ( !$redirectContent ) {
486 WikiPage::onArticleDelete( $this->oldTitle );
487 }
488
489 $this->oldTitle->resetArticleID( 0 ); // 0 == non existing
490 $nt->resetArticleID( $oldid );
491 $newpage->loadPageData( WikiPage::READ_LOCKING ); // bug 46397
492
493 $newpage->updateRevisionOn( $dbw, $nullRevision );
494
495 Hooks::run( 'NewRevisionFromEditComplete',
496 array( $newpage, $nullRevision, $nullRevision->getParentId(), $user ) );
497
498 $newpage->doEditUpdates( $nullRevision, $user,
499 array( 'changed' => false, 'moved' => true, 'oldcountable' => $oldcountable ) );
500
501 if ( !$moveOverRedirect ) {
502 WikiPage::onArticleCreate( $nt );
503 }
504
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
514 'page' => $newid,
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 );
521
522 Hooks::run( 'NewRevisionFromEditComplete',
523 array( $redirectArticle, $redirectRevision, false, $user ) );
524
525 $redirectArticle->doEditUpdates( $redirectRevision, $user, array( 'created' => true ) );
526 }
527 }
528
529 # Log the move
530 $logid = $logEntry->insert();
531 $logEntry->publish( $logid );
532 }
533 }