Mass convert NULL -> null. Left strings and comments alone, obviously.
[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 $wgOut->addWikiMsg( 'movepagetext' );
135 $movepagebtn = wfMsg( 'movepagebtn' );
136 $submitVar = 'wpMove';
137 $confirm = false;
138 }
139
140 if ( !empty($err) && $err[0] == 'file-exists-sharedrepo' && $wgUser->isAllowed( 'reupload-shared' ) ) {
141 $wgOut->addWikiMsg( 'move-over-sharedrepo', $newTitle->getPrefixedText() );
142 $submitVar = 'wpMoveOverSharedFile';
143 $err = '';
144 }
145
146 $oldTalk = $this->oldTitle->getTalkPage();
147 $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
148
149 $dbr = wfGetDB( DB_SLAVE );
150 if ( $wgFixDoubleRedirects ) {
151 $hasRedirects = $dbr->selectField( 'redirect', '1',
152 array(
153 'rd_namespace' => $this->oldTitle->getNamespace(),
154 'rd_title' => $this->oldTitle->getDBkey(),
155 ) , __METHOD__ );
156 } else {
157 $hasRedirects = false;
158 }
159
160 if ( $considerTalk ) {
161 $wgOut->addWikiMsg( 'movepagetalktext' );
162 }
163
164 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
165 $token = htmlspecialchars( $wgUser->editToken() );
166
167 if ( !empty($err) ) {
168 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
169 if( $err[0] == 'hookaborted' ) {
170 $hookErr = $err[1];
171 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
172 $wgOut->addHTML( $errMsg );
173 } else {
174 $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
175 }
176 }
177
178 if ( $this->oldTitle->isProtected( 'move' ) ) {
179 # Is the title semi-protected?
180 if ( $this->oldTitle->isSemiProtected( 'move' ) ) {
181 $noticeMsg = 'semiprotectedpagemovewarning';
182 $classes[] = 'mw-textarea-sprotected';
183 } else {
184 # Then it must be protected based on static groups (regular)
185 $noticeMsg = 'protectedpagemovewarning';
186 $classes[] = 'mw-textarea-protected';
187 }
188 $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
189 $wgOut->addWikiMsg( $noticeMsg );
190 LogEventsList::showLogExtract( $wgOut, 'protect', $this->oldTitle->getPrefixedText(), '', array( 'lim' => 1 ) );
191 $wgOut->addHTML( "</div>\n" );
192 }
193
194 $wgOut->addHTML(
195 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
196 Xml::openElement( 'fieldset' ) .
197 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
198 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
199 "<tr>
200 <td class='mw-label'>" .
201 wfMsgHtml( 'movearticle' ) .
202 "</td>
203 <td class='mw-input'>
204 <strong>{$oldTitleLink}</strong>
205 </td>
206 </tr>
207 <tr>
208 <td class='mw-label'>" .
209 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
210 "</td>
211 <td class='mw-input'>" .
212 Xml::input( 'wpNewTitle', 40, $wgContLang->recodeForEdit( $newTitle->getPrefixedText() ), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
213 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
214 "</td>
215 </tr>
216 <tr>
217 <td class='mw-label'>" .
218 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
219 "</td>
220 <td class='mw-input'>" .
221 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
222 "</td>
223 </tr>"
224 );
225
226 if( $considerTalk ) {
227 $wgOut->addHTML( "
228 <tr>
229 <td></td>
230 <td class='mw-input'>" .
231 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
232 "</td>
233 </tr>"
234 );
235 }
236
237 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
238 $wgOut->addHTML( "
239 <tr>
240 <td></td>
241 <td class='mw-input' >" .
242 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
243 'wpLeaveRedirect', $this->leaveRedirect ) .
244 "</td>
245 </tr>"
246 );
247 }
248
249 if ( $hasRedirects ) {
250 $wgOut->addHTML( "
251 <tr>
252 <td></td>
253 <td class='mw-input' >" .
254 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
255 'wpFixRedirects', $this->fixRedirects ) .
256 "</td>
257 </tr>"
258 );
259 }
260
261 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
262 && $this->oldTitle->userCan( 'move-subpages' ) )
263 {
264 global $wgMaximumMovedPages, $wgLang;
265
266 $wgOut->addHTML( "
267 <tr>
268 <td></td>
269 <td class=\"mw-input\">" .
270 Xml::check(
271 'wpMovesubpages',
272 # Don't check the box if we only have talk subpages to
273 # move and we aren't moving the talk page.
274 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk),
275 array( 'id' => 'wpMovesubpages' )
276 ) . '&nbsp;' .
277 Xml::tags( 'label', array( 'for' => 'wpMovesubpages' ),
278 wfMsgExt(
279 ( $this->oldTitle->hasSubpages()
280 ? 'move-subpages'
281 : 'move-talk-subpages' ),
282 array( 'parseinline' ),
283 $wgLang->formatNum( $wgMaximumMovedPages ),
284 # $2 to allow use of PLURAL in message.
285 $wgMaximumMovedPages
286 )
287 ) .
288 "</td>
289 </tr>"
290 );
291 }
292
293 $watchChecked = $wgUser->isLoggedIn() && ($this->watch || $wgUser->getBoolOption( 'watchmoves' )
294 || $this->oldTitle->userIsWatching());
295 # Don't allow watching if user is not logged in
296 if( $wgUser->isLoggedIn() ) {
297 $wgOut->addHTML( "
298 <tr>
299 <td></td>
300 <td class='mw-input'>" .
301 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
302 "</td>
303 </tr>");
304 }
305
306 $wgOut->addHTML( "
307 {$confirm}
308 <tr>
309 <td>&nbsp;</td>
310 <td class='mw-submit'>" .
311 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
312 "</td>
313 </tr>" .
314 Xml::closeElement( 'table' ) .
315 Xml::hidden( 'wpEditToken', $token ) .
316 Xml::closeElement( 'fieldset' ) .
317 Xml::closeElement( 'form' ) .
318 "\n"
319 );
320
321 $this->showLogFragment( $this->oldTitle, $wgOut );
322 $this->showSubpages( $this->oldTitle, $wgOut );
323
324 }
325
326 function doSubmit() {
327 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
328 global $wgFixDoubleRedirects;
329
330 if ( $wgUser->pingLimiter( 'move' ) ) {
331 $wgOut->rateLimited();
332 return;
333 }
334
335 $ot = $this->oldTitle;
336 $nt = $this->newTitle;
337
338 # Delete to make way if requested
339 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
340 $article = new Article( $nt );
341
342 # Disallow deletions of big articles
343 $bigHistory = $article->isBigDeletion();
344 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
345 global $wgLang, $wgDeleteRevisionsLimit;
346 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
347 return;
348 }
349
350 // Delete an associated image if there is
351 $file = wfLocalFile( $nt );
352 if( $file->exists() ) {
353 $file->delete( wfMsgForContent( 'delete_and_move_reason' ), false );
354 }
355
356 // This may output an error message and exit
357 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
358 }
359
360 # don't allow moving to pages with # in
361 if ( !$nt || $nt->getFragment() != '' ) {
362 $this->showForm( 'badtitletext' );
363 return;
364 }
365
366 # Show a warning if the target file exists on a shared repo
367 if ( $nt->getNamespace() == NS_FILE
368 && !( $this->moveOverShared && $wgUser->isAllowed( 'reupload-shared' ) )
369 && !RepoGroup::singleton()->getLocalRepo()->findFile( $nt )
370 && wfFindFile( $nt ) )
371 {
372 $this->showForm( array('file-exists-sharedrepo') );
373 return;
374
375 }
376
377 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
378 $createRedirect = $this->leaveRedirect;
379 } else {
380 $createRedirect = true;
381 }
382
383 # Do the actual move.
384 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
385 if ( $error !== true ) {
386 # FIXME: show all the errors in a list, not just the first one
387 $this->showForm( reset( $error ) );
388 return;
389 }
390
391 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
392 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
393 }
394
395 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
396
397 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
398
399 $oldUrl = $ot->getFullUrl( 'redirect=no' );
400 $newUrl = $nt->getFullUrl();
401 $oldText = $ot->getPrefixedText();
402 $newText = $nt->getPrefixedText();
403 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
404 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
405
406 $msgName = $createRedirect ? 'movepage-moved-redirect' : 'movepage-moved-noredirect';
407 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
408 $wgOut->addWikiMsg( $msgName );
409
410 # Now we move extra pages we've been asked to move: subpages and talk
411 # pages. First, if the old page or the new page is a talk page, we
412 # can't move any talk pages: cancel that.
413 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
414 $this->moveTalk = false;
415 }
416
417 if( !$ot->userCan( 'move-subpages' ) ) {
418 $this->moveSubpages = false;
419 }
420
421 # Next make a list of id's. This might be marginally less efficient
422 # than a more direct method, but this is not a highly performance-cri-
423 # tical code path and readable code is more important here.
424 #
425 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
426 # 4 might get confused. If so, consider rewriting as a UNION.
427 #
428 # If the target namespace doesn't allow subpages, moving with subpages
429 # would mean that you couldn't move them back in one operation, which
430 # is bad. FIXME: A specific error message should be given in this
431 # case.
432
433 // FIXME: Use Title::moveSubpages() here
434 $dbr = wfGetDB( DB_MASTER );
435 if( $this->moveSubpages && (
436 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
437 $this->moveTalk &&
438 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
439 )
440 ) ) {
441 $conds = array(
442 'page_title' . $dbr->buildLike( $ot->getDBkey() . '/', $dbr->anyString() )
443 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
444 );
445 $conds['page_namespace'] = array();
446 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
447 $conds['page_namespace'] []= $ot->getNamespace();
448 }
449 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
450 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
451 }
452 } elseif( $this->moveTalk ) {
453 $conds = array(
454 'page_namespace' => $ot->getTalkPage()->getNamespace(),
455 'page_title' => $ot->getDBkey()
456 );
457 } else {
458 # Skip the query
459 $conds = null;
460 }
461
462 $extraPages = array();
463 if( !is_null( $conds ) ) {
464 $extraPages = TitleArray::newFromResult(
465 $dbr->select( 'page',
466 array( 'page_id', 'page_namespace', 'page_title' ),
467 $conds,
468 __METHOD__
469 )
470 );
471 }
472
473 $extraOutput = array();
474 $skin = $wgUser->getSkin();
475 $count = 1;
476 foreach( $extraPages as $oldSubpage ) {
477 if( $ot->equals( $oldSubpage ) ) {
478 # Already did this one.
479 continue;
480 }
481
482 $newPageName = preg_replace(
483 '#^'.preg_quote( $ot->getDBkey(), '#' ).'#',
484 str_replace( '\\', '\\\\', $nt->getDBkey() ), # bug 21234
485 $oldSubpage->getDBkey()
486 );
487 if( $oldSubpage->isTalkPage() ) {
488 $newNs = $nt->getTalkPage()->getNamespace();
489 } else {
490 $newNs = $nt->getSubjectPage()->getNamespace();
491 }
492 # Bug 14385: we need makeTitleSafe because the new page names may
493 # be longer than 255 characters.
494 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
495 if( !$newSubpage ) {
496 $oldLink = $skin->linkKnown( $oldSubpage );
497 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
498 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
499 continue;
500 }
501
502 # This was copy-pasted from Renameuser, bleh.
503 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
504 $link = $skin->linkKnown( $newSubpage );
505 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
506 } else {
507 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason, $createRedirect );
508 if( $success === true ) {
509 if ( $this->fixRedirects ) {
510 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
511 }
512 $oldLink = $skin->linkKnown(
513 $oldSubpage,
514 null,
515 array(),
516 array( 'redirect' => 'no' )
517 );
518 $newLink = $skin->linkKnown( $newSubpage );
519 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
520 ++$count;
521 if( $count >= $wgMaximumMovedPages ) {
522 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
523 break;
524 }
525 } else {
526 $oldLink = $skin->linkKnown( $oldSubpage );
527 $newLink = $skin->link( $newSubpage );
528 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
529 }
530 }
531
532 }
533
534 if( $extraOutput !== array() ) {
535 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
536 }
537
538 # Deal with watches (we don't watch subpages)
539 if( $this->watch && $wgUser->isLoggedIn() ) {
540 $wgUser->addWatch( $ot );
541 $wgUser->addWatch( $nt );
542 } else {
543 $wgUser->removeWatch( $ot );
544 $wgUser->removeWatch( $nt );
545 }
546 }
547
548 function showLogFragment( $title, &$out ) {
549 $out->addHTML( Xml::element( 'h2', null, LogPage::logName( 'move' ) ) );
550 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
551 }
552
553 function showSubpages( $title, $out ) {
554 global $wgUser, $wgLang;
555
556 if( !MWNamespace::hasSubpages( $title->getNamespace() ) )
557 return;
558
559 $subpages = $title->getSubpages();
560 $count = $subpages instanceof TitleArray ? $subpages->count() : 0;
561
562 $out->wrapWikiMsg( '== $1 ==', array( 'movesubpage', $count ) );
563
564 # No subpages.
565 if ( $count == 0 ) {
566 $out->addWikiMsg( 'movenosubpage' );
567 return;
568 }
569
570 $out->addWikiMsg( 'movesubpagetext', $wgLang->formatNum( $count ) );
571 $skin = $wgUser->getSkin();
572 $out->addHTML( "<ul>\n" );
573
574 foreach( $subpages as $subpage ) {
575 $link = $skin->link( $subpage );
576 $out->addHTML( "<li>$link</li>\n" );
577 }
578 $out->addHTML( "</ul>\n" );
579 }
580 }
581