Remove most named character references from output
[lhc/web/wiklou.git] / includes / specials / SpecialMovepage.php
1 <?php
2 /**
3 * @file
4 * @ingroup SpecialPage
5 */
6
7 /**
8 * Constructor
9 */
10 function wfSpecialMovepage( $par = null ) {
11 global $wgUser, $wgOut, $wgRequest, $action;
12
13 # Check for database lock
14 if ( wfReadOnly() ) {
15 $wgOut->readOnlyPage();
16 return;
17 }
18
19 $target = isset( $par ) ? $par : $wgRequest->getVal( 'target' );
20
21 // Yes, the use of getVal() and getText() is wanted, see bug 20365
22 $oldTitleText = $wgRequest->getVal( 'wpOldTitle', $target );
23 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
24
25 $oldTitle = Title::newFromText( $oldTitleText );
26 $newTitle = Title::newFromText( $newTitleText );
27
28 if( is_null( $oldTitle ) ) {
29 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
30 return;
31 }
32 if( !$oldTitle->exists() ) {
33 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
34 return;
35 }
36
37 # Check rights
38 $permErrors = $oldTitle->getUserPermissionsErrors( 'move', $wgUser );
39 if( !empty( $permErrors ) ) {
40 $wgOut->showPermissionsErrorPage( $permErrors );
41 return;
42 }
43
44 $form = new MovePageForm( $oldTitle, $newTitle );
45
46 if ( 'submit' == $action && $wgRequest->wasPosted()
47 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
48 $form->doSubmit();
49 } else {
50 $form->showForm( '' );
51 }
52 }
53
54 /**
55 * HTML form for Special:Movepage
56 * @ingroup SpecialPage
57 */
58 class MovePageForm {
59 var $oldTitle, $newTitle; # Objects
60 var $reason; # Text input
61 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect, $moveOverShared; # Checks
62
63 private $watch = false;
64
65 function __construct( $oldTitle, $newTitle ) {
66 global $wgRequest, $wgUser;
67 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
68 $this->oldTitle = $oldTitle;
69 $this->newTitle = $newTitle;
70 $this->reason = $wgRequest->getText( 'wpReason' );
71 if ( $wgRequest->wasPosted() ) {
72 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
73 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', false );
74 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', false );
75 } else {
76 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
77 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', true );
78 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', true );
79 }
80 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
81 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
82 $this->moveOverShared = $wgRequest->getBool( 'wpMoveOverSharedFile', false );
83 $this->watch = $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn();
84 }
85
86 /**
87 * Show the form
88 * @param mixed $err Error message. May either be a string message name or
89 * array message name and parameters, like the second argument to
90 * OutputPage::wrapWikiMsg().
91 */
92 function showForm( $err ) {
93 global $wgOut, $wgUser, $wgContLang, $wgFixDoubleRedirects;
94
95 $skin = $wgUser->getSkin();
96
97 $oldTitleLink = $skin->link( $this->oldTitle );
98
99 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
100 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
101
102 $newTitle = $this->newTitle;
103
104 if( !$newTitle ) {
105 # Show the current title as a default
106 # when the form is first opened.
107 $newTitle = $this->oldTitle;
108 }
109 else {
110 if( empty($err) ) {
111 # If a title was supplied, probably from the move log revert
112 # link, check for validity. We can then show some diagnostic
113 # information and save a click.
114 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
115 if( $newerr ) {
116 $err = $newerr[0];
117 }
118 }
119 }
120
121 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
122 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
123 $movepagebtn = wfMsg( 'delete_and_move' );
124 $submitVar = 'wpDeleteAndMove';
125 $confirm = "
126 <tr>
127 <td></td>
128 <td class='mw-input'>" .
129 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
130 "</td>
131 </tr>";
132 $err = '';
133 } else {
134 if ($this->oldTitle->getNamespace() == NS_USER && !$this->oldTitle->isSubpage() ) {
135 $wgOut->wrapWikiMsg( "<div class=\"error mw-moveuserpage-warning\">\n$1\n</div>", 'moveuserpage-warning' );
136 }
137 $wgOut->addWikiMsg( 'movepagetext' );
138 $movepagebtn = wfMsg( 'movepagebtn' );
139 $submitVar = 'wpMove';
140 $confirm = false;
141 }
142
143 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
144 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
145 $submitVar = 'wpMoveOverSharedFile';
146 $err = '';
147 }
148
149 $oldTalk = $this->oldTitle->getTalkPage();
150 $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
151
152 $dbr = wfGetDB( DB_SLAVE );
153 if ( $wgFixDoubleRedirects ) {
154 $hasRedirects = $dbr->selectField( 'redirect', '1',
155 array(
156 'rd_namespace' => $this->oldTitle->getNamespace(),
157 'rd_title' => $this->oldTitle->getDBkey(),
158 ) , __METHOD__ );
159 } else {
160 $hasRedirects = false;
161 }
162
163 if ( $considerTalk ) {
164 $wgOut->addWikiMsg( 'movepagetalktext' );
165 }
166
167 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
168 $token = htmlspecialchars( $wgUser->editToken() );
169
170 if ( !empty($err) ) {
171 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
172 if( $err[0] == 'hookaborted' ) {
173 $hookErr = $err[1];
174 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
175 $wgOut->addHTML( $errMsg );
176 } else {
177 $wgOut->wrapWikiMsg( "<p><strong class=\"error\">\n$1\n</strong></p>", $err );
178 }
179 }
180
181 if ( $this->oldTitle->isProtected( 'move' ) ) {
182 # Is the title semi-protected?
183 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
184 $noticeMsg = 'semiprotectedpagemovewarning';
185 $classes[] = 'mw-textarea-sprotected';
186 } else {
187 # Then it must be protected based on static groups (regular)
188 $noticeMsg = 'protectedpagemovewarning';
189 $classes[] = 'mw-textarea-protected';
190 }
191 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
192 $wgOut->addWikiMsg( $noticeMsg );
193 LogEventsList::showLogExtract( $wgOut, 'protect', $this->oldTitle->getPrefixedText(), '', array( 'lim' => 1 ) );
194 $wgOut->addHTML( "</div>\n" );
195 }
196
197 $wgOut->addHTML(
198 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
199 Xml::openElement( 'fieldset' ) .
200 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
201 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
202 "<tr>
203 <td class='mw-label'>" .
204 wfMsgHtml( 'movearticle' ) .
205 "</td>
206 <td class='mw-input'>
207 <strong>{$oldTitleLink}</strong>
208 </td>
209 </tr>
210 <tr>
211 <td class='mw-label'>" .
212 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
213 "</td>
214 <td class='mw-input'>" .
215 Xml::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
216 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
217 "</td>
218 </tr>
219 <tr>
220 <td class='mw-label'>" .
221 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
222 "</td>
223 <td class='mw-input'>" .
224 Html::element( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2,
225 'maxlength' => 200 ), $this->reason ) .
226 "</td>
227 </tr>"
228 );
229
230 if( $considerTalk ) {
231 $wgOut->addHTML( "
232 <tr>
233 <td></td>
234 <td class='mw-input'>" .
235 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
236 "</td>
237 </tr>"
238 );
239 }
240
241 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
242 $wgOut->addHTML( "
243 <tr>
244 <td></td>
245 <td class='mw-input' >" .
246 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
247 'wpLeaveRedirect', $this->leaveRedirect ) .
248 "</td>
249 </tr>"
250 );
251 }
252
253 if ( $hasRedirects ) {
254 $wgOut->addHTML( "
255 <tr>
256 <td></td>
257 <td class='mw-input' >" .
258 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
259 'wpFixRedirects', $this->fixRedirects ) .
260 "</td>
261 </tr>"
262 );
263 }
264
265 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
266 && $this->oldTitle->userCan( 'move-subpages' ) )
267 {
268 global $wgMaximumMovedPages, $wgLang;
269
270 $wgOut->addHTML( "
271 <tr>
272 <td></td>
273 <td class=\"mw-input\">" .
274 Xml::check(
275 'wpMovesubpages',
276 # Don't check the box if we only have talk subpages to
277 # move and we aren't moving the talk page.
278 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk),
279 array( 'id' => 'wpMovesubpages' )
280 ) . '&#160;' .
281 Xml::tags( 'label', array( 'for' => 'wpMovesubpages' ),
282 wfMsgExt(
283 ( $this->oldTitle->hasSubpages()
284 ? 'move-subpages'
285 : 'move-talk-subpages' ),
286 array( 'parseinline' ),
287 $wgLang->formatNum( $wgMaximumMovedPages ),
288 # $2 to allow use of PLURAL in message.
289 $wgMaximumMovedPages
290 )
291 ) .
292 "</td>
293 </tr>"
294 );
295 }
296
297 $watchChecked = $wgUser->isLoggedIn() && ($this->watch || $wgUser->getBoolOption( 'watchmoves' )
298 || $this->oldTitle->userIsWatching());
299 # Don't allow watching if user is not logged in
300 if( $wgUser->isLoggedIn() ) {
301 $wgOut->addHTML( "
302 <tr>
303 <td></td>
304 <td class='mw-input'>" .
305 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
306 "</td>
307 </tr>");
308 }
309
310 $wgOut->addHTML( "
311 {$confirm}
312 <tr>
313 <td>&#160;</td>
314 <td class='mw-submit'>" .
315 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
316 "</td>
317 </tr>" .
318 Xml::closeElement( 'table' ) .
319 Xml::hidden( 'wpEditToken', $token ) .
320 Xml::closeElement( 'fieldset' ) .
321 Xml::closeElement( 'form' ) .
322 "\n"
323 );
324
325 $this->showLogFragment( $this->oldTitle, $wgOut );
326 $this->showSubpages( $this->oldTitle, $wgOut );
327
328 }
329
330 function doSubmit() {
331 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
332 global $wgFixDoubleRedirects;
333
334 if ( $wgUser->pingLimiter( 'move' ) ) {
335 $wgOut->rateLimited();
336 return;
337 }
338
339 $ot = $this->oldTitle;
340 $nt = $this->newTitle;
341
342 # Delete to make way if requested
343 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
344 $article = new Article( $nt );
345
346 # Disallow deletions of big articles
347 $bigHistory = $article->isBigDeletion();
348 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
349 global $wgLang, $wgDeleteRevisionsLimit;
350 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
351 return;
352 }
353
354 // Delete an associated image if there is
355 $file = wfLocalFile( $nt );
356 if( $file->exists() ) {
357 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
358 }
359
360 // This may output an error message and exit
361 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
362 }
363
364 # don't allow moving to pages with # in
365 if ( !$nt || $nt->getFragment() != '' ) {
366 $this->showForm( 'badtitletext' );
367 return;
368 }
369
370 # Show a warning if the target file exists on a shared repo
371 if ( $nt->getNamespace() == NS_FILE
372 && !( $this->moveOverShared && $wgUser->isAllowed( 'reupload-shared' ) )
373 && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
374 && wfFindFile( $nt ) )
375 {
376 $this->showForm( array('file-exists-sharedrepo') );
377 return;
378
379 }
380
381 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
382 $createRedirect = $this->leaveRedirect;
383 } else {
384 $createRedirect = true;
385 }
386
387 # Do the actual move.
388 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
389 if ( $error !== true ) {
390 # FIXME: show all the errors in a list, not just the first one
391 $this->showForm( reset( $error ) );
392 return;
393 }
394
395 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
396 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
397 }
398
399 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
400
401 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
402
403 $oldUrl = $ot->getFullUrl( 'redirect=no' );
404 $newUrl = $nt->getFullUrl();
405 $oldText = $ot->getPrefixedText();
406 $newText = $nt->getPrefixedText();
407 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
408 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
409
410 $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
411 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
412 $wgOut->addWikiMsg( $msgName );
413
414 # Now we move extra pages we've been asked to move: subpages and talk
415 # pages. First, if the old page or the new page is a talk page, we
416 # can't move any talk pages: cancel that.
417 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
418 $this->moveTalk = false;
419 }
420
421 if( !$ot->userCan( 'move-subpages' ) ) {
422 $this->moveSubpages = false;
423 }
424
425 # Next make a list of id's. This might be marginally less efficient
426 # than a more direct method, but this is not a highly performance-cri-
427 # tical code path and readable code is more important here.
428 #
429 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
430 # 4 might get confused. If so, consider rewriting as a UNION.
431 #
432 # If the target namespace doesn't allow subpages, moving with subpages
433 # would mean that you couldn't move them back in one operation, which
434 # is bad. FIXME: A specific error message should be given in this
435 # case.
436
437 // FIXME: Use Title::moveSubpages() here
438 $dbr = wfGetDB( DB_MASTER );
439 if( $this->moveSubpages && (
440 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
441 $this->moveTalk &&
442 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
443 )
444 ) ) {
445 $conds = array(
446 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
447 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
448 );
449 $conds['page_namespace'] = array();
450 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
451 $conds['page_namespace'] []= $ot->getNamespace();
452 }
453 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
454 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
455 }
456 } elseif( $this->moveTalk ) {
457 $conds = array(
458 'page_namespace' => $ot->getTalkPage()->getNamespace(),
459 'page_title' => $ot->getDBkey()
460 );
461 } else {
462 # Skip the query
463 $conds = null;
464 }
465
466 $extraPages = array();
467 if( !is_null( $conds ) ) {
468 $extraPages = TitleArray::newFromResult(
469 $dbr->select( 'page',
470 array( 'page_id', 'page_namespace', 'page_title' ),
471 $conds,
472 __METHOD__
473 )
474 );
475 }
476
477 $extraOutput = array();
478 $skin = $wgUser->getSkin();
479 $count = 1;
480 foreach( $extraPages as $oldSubpage ) {
481 if( $ot->equals( $oldSubpage ) ) {
482 # Already did this one.
483 continue;
484 }
485
486 $newPageName = preg_replace(
487 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
488 StringUtils::escapeRegexReplacement( $nt->getDBkey() ), # bug 21234
489 $oldSubpage->getDBkey()
490 );
491 if( $oldSubpage->isTalkPage() ) {
492 $newNs = $nt->getTalkPage()->getNamespace();
493 } else {
494 $newNs = $nt->getSubjectPage()->getNamespace();
495 }
496 # Bug 14385: we need makeTitleSafe because the new page names may
497 # be longer than 255 characters.
498 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
499 if( !$newSubpage ) {
500 $oldLink = $skin->linkKnown( $oldSubpage );
501 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
502 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
503 continue;
504 }
505
506 # This was copy-pasted from Renameuser, bleh.
507 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
508 $link = $skin->linkKnown( $newSubpage );
509 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
510 } else {
511 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
512 if( $success === true ) {
513 if ( $this->fixRedirects ) {
514 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
515 }
516 $oldLink = $skin->linkKnown(
517 $oldSubpage,
518 null,
519 array(),
520 array( 'redirect' => 'no' )
521 );
522 $newLink = $skin->linkKnown( $newSubpage );
523 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
524 ++$count;
525 if( $count >= $wgMaximumMovedPages ) {
526 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
527 break;
528 }
529 } else {
530 $oldLink = $skin->linkKnown( $oldSubpage );
531 $newLink = $skin->link( $newSubpage );
532 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
533 }
534 }
535
536 }
537
538 if( $extraOutput !== array() ) {
539 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
540 }
541
542 # Deal with watches (we don't watch subpages)
543 if( $this->watch && $wgUser->isLoggedIn() ) {
544 $wgUser->addWatch( $ot );
545 $wgUser->addWatch( $nt );
546 } else {
547 $wgUser->removeWatch( $ot );
548 $wgUser->removeWatch( $nt );
549 }
550
551 # Re-clear the file redirect cache, which may have been polluted by
552 # parsing in messages above. See CR r56745.
553 # FIXME: needs a more robust solution inside FileRepo.
554 if( $ot->getNamespace() == NS_FILE ) {
555 RepoGroup::singleton()->getLocalRepo()->invalidateImageRedirect( $ot );
556 }
557 }
558
559 function showLogFragment( $title, &$out ) {
560 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'move' ) ) );
561 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
562 }
563
564 function showSubpages( $title, $out ) {
565 global $wgUser, $wgLang;
566
567 if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
568 return;
569
570 $subpages = $title->getSubpages();
571 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
572
573 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
574
575 # No subpages.
576 if ( $count == 0 ) {
577 $out->addWikiMsg( 'movenosubpage' );
578 return;
579 }
580
581 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
582 $skin = $wgUser->getSkin();
583 $out->addHTML( "<ul>\n" );
584
585 foreach( $subpages as $subpage ) {
586 $link = $skin->link( $subpage );
587 $out->addHTML( "<li>$link</li>\n" );
588 }
589 $out->addHTML( "</ul>\n" );
590 }
591 }
592