Fix error display on invalid destination title on GET, e.g. with URL /wiki/Special...
[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 $oldTitleText = $wgRequest->getText( 'wpOldTitle', $target );
21 $newTitleText = $wgRequest->getText( 'wpNewTitle' );
22
23 $oldTitle = Title::newFromText( $oldTitleText );
24 $newTitle = Title::newFromText( $newTitleText );
25
26 if( is_null( $oldTitle ) ) {
27 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
28 return;
29 }
30 if( !$oldTitle->exists() ) {
31 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
32 return;
33 }
34
35 # Check rights
36 $permErrors = $oldTitle->getUserPermissionsErrors( 'move', $wgUser );
37 if( !empty( $permErrors ) ) {
38 $wgOut->showPermissionsErrorPage( $permErrors );
39 return;
40 }
41
42 $form = new MovePageForm( $oldTitle, $newTitle );
43
44 if ( 'submit' == $action && $wgRequest->wasPosted()
45 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
46 $form->doSubmit();
47 } else {
48 $form->showForm( '' );
49 }
50 }
51
52 /**
53 * HTML form for Special:Movepage
54 * @ingroup SpecialPage
55 */
56 class MovePageForm {
57 var $oldTitle, $newTitle; # Objects
58 var $reason; # Text input
59 var $moveTalk, $deleteAndMove, $moveSubpages, $fixRedirects, $leaveRedirect; # Checks
60
61 private $watch = false;
62
63 function __construct( $oldTitle, $newTitle ) {
64 global $wgRequest;
65 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
66 $this->oldTitle = $oldTitle;
67 $this->newTitle = $newTitle;
68 $this->reason = $wgRequest->getText( 'wpReason' );
69 if ( $wgRequest->wasPosted() ) {
70 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
71 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', false );
72 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', false );
73 } else {
74 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
75 $this->fixRedirects = $wgRequest->getBool( 'wpFixRedirects', true );
76 $this->leaveRedirect = $wgRequest->getBool( 'wpLeaveRedirect', true );
77 }
78 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
79 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
80 $this->watch = $wgRequest->getCheck( 'wpWatch' );
81 }
82
83 function showForm( $err ) {
84 global $wgOut, $wgUser, $wgFixDoubleRedirects;
85
86 $skin = $wgUser->getSkin();
87
88 $oldTitleLink = $skin->makeLinkObj( $this->oldTitle );
89
90 $wgOut->setPagetitle( wfMsg( 'move-page', $this->oldTitle->getPrefixedText() ) );
91 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
92
93 $newTitle = $this->newTitle;
94
95 if( !$newTitle ) {
96 # Show the current title as a default
97 # when the form is first opened.
98 $newTitle = $this->oldTitle;
99 }
100 else {
101 if( empty($err) ) {
102 # If a title was supplied, probably from the move log revert
103 # link, check for validity. We can then show some diagnostic
104 # information and save a click.
105 $newerr = $this->oldTitle->isValidMoveOperation( $newTitle );
106 if( $newerr ) {
107 $err = $newerr[0];
108 }
109 }
110 }
111
112 if ( !empty($err) && $err[0] == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
113 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle->getPrefixedText() );
114 $movepagebtn = wfMsg( 'delete_and_move' );
115 $submitVar = 'wpDeleteAndMove';
116 $confirm = "
117 <tr>
118 <td></td>
119 <td class='mw-input'>" .
120 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
121 "</td>
122 </tr>";
123 $err = '';
124 } else {
125 $wgOut->addWikiMsg( 'movepagetext' );
126 $movepagebtn = wfMsg( 'movepagebtn' );
127 $submitVar = 'wpMove';
128 $confirm = false;
129 }
130
131 $oldTalk = $this->oldTitle->getTalkPage();
132 $considerTalk = ( !$this->oldTitle->isTalkPage() && $oldTalk->exists() );
133
134 $dbr = wfGetDB( DB_SLAVE );
135 if ( $wgFixDoubleRedirects ) {
136 $hasRedirects = $dbr->selectField( 'redirect', '1',
137 array(
138 'rd_namespace' => $this->oldTitle->getNamespace(),
139 'rd_title' => $this->oldTitle->getDBkey(),
140 ) , __METHOD__ );
141 } else {
142 $hasRedirects = false;
143 }
144
145 if ( $considerTalk ) {
146 $wgOut->addWikiMsg( 'movepagetalktext' );
147 }
148
149 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
150 $token = htmlspecialchars( $wgUser->editToken() );
151
152 if ( !empty($err) ) {
153 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
154 if( $err[0] == 'hookaborted' ) {
155 $hookErr = $err[1];
156 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
157 $wgOut->addHTML( $errMsg );
158 } else {
159 $wgOut->wrapWikiMsg( '<p><strong class="error">$1</strong></p>', $err );
160 }
161 }
162
163 $wgOut->addHTML(
164 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
165 Xml::openElement( 'fieldset' ) .
166 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
167 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
168 "<tr>
169 <td class='mw-label'>" .
170 wfMsgHtml( 'movearticle' ) .
171 "</td>
172 <td class='mw-input'>
173 <strong>{$oldTitleLink}</strong>
174 </td>
175 </tr>
176 <tr>
177 <td class='mw-label'>" .
178 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
179 "</td>
180 <td class='mw-input'>" .
181 Xml::input( 'wpNewTitle', 40, $newTitle->getPrefixedText(), array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
182 Xml::hidden( 'wpOldTitle', $this->oldTitle->getPrefixedText() ) .
183 "</td>
184 </tr>
185 <tr>
186 <td class='mw-label'>" .
187 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
188 "</td>
189 <td class='mw-input'>" .
190 Xml::tags( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ), htmlspecialchars( $this->reason ) ) .
191 "</td>
192 </tr>"
193 );
194
195 if( $considerTalk ) {
196 $wgOut->addHTML( "
197 <tr>
198 <td></td>
199 <td class='mw-input'>" .
200 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $this->moveTalk ) .
201 "</td>
202 </tr>"
203 );
204 }
205
206 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
207 $wgOut->addHTML( "
208 <tr>
209 <td></td>
210 <td class='mw-input' >" .
211 Xml::checkLabel( wfMsg( 'move-leave-redirect' ), 'wpLeaveRedirect',
212 'wpLeaveRedirect', $this->leaveRedirect ) .
213 "</td>
214 </tr>"
215 );
216 }
217
218 if ( $hasRedirects ) {
219 $wgOut->addHTML( "
220 <tr>
221 <td></td>
222 <td class='mw-input' >" .
223 Xml::checkLabel( wfMsg( 'fix-double-redirects' ), 'wpFixRedirects',
224 'wpFixRedirects', $this->fixRedirects ) .
225 "</td>
226 </tr>"
227 );
228 }
229
230 if( ($this->oldTitle->hasSubpages() || $this->oldTitle->getTalkPage()->hasSubpages())
231 && $this->oldTitle->userCan( 'move-subpages' ) ) {
232 $wgOut->addHTML( "
233 <tr>
234 <td></td>
235 <td class=\"mw-input\">" .
236 Xml::checkLabel( wfMsg(
237 $this->oldTitle->hasSubpages()
238 ? 'move-subpages'
239 : 'move-talk-subpages'
240 ),
241 'wpMovesubpages', 'wpMovesubpages',
242 # Don't check the box if we only have talk subpages to
243 # move and we aren't moving the talk page.
244 $this->moveSubpages && ($this->oldTitle->hasSubpages() || $this->moveTalk)
245 ) .
246 "</td>
247 </tr>"
248 );
249 }
250
251 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' )
252 || $this->oldTitle->userIsWatching();
253 $wgOut->addHTML( "
254 <tr>
255 <td></td>
256 <td class='mw-input'>" .
257 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
258 "</td>
259 </tr>
260 {$confirm}
261 <tr>
262 <td>&nbsp;</td>
263 <td class='mw-submit'>" .
264 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
265 "</td>
266 </tr>" .
267 Xml::closeElement( 'table' ) .
268 Xml::hidden( 'wpEditToken', $token ) .
269 Xml::closeElement( 'fieldset' ) .
270 Xml::closeElement( 'form' ) .
271 "\n"
272 );
273
274 $this->showLogFragment( $this->oldTitle, $wgOut );
275
276 }
277
278 function doSubmit() {
279 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
280 global $wgFixDoubleRedirects;
281
282 if ( $wgUser->pingLimiter( 'move' ) ) {
283 $wgOut->rateLimited();
284 return;
285 }
286
287 $ot = $this->oldTitle;
288 $nt = $this->newTitle;
289
290 # Delete to make way if requested
291 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
292 $article = new Article( $nt );
293
294 # Disallow deletions of big articles
295 $bigHistory = $article->isBigDeletion();
296 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
297 global $wgLang, $wgDeleteRevisionsLimit;
298 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
299 return;
300 }
301
302 // This may output an error message and exit
303 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
304 }
305
306 # don't allow moving to pages with # in
307 if ( !$nt || $nt->getFragment() != '' ) {
308 $this->showForm( 'badtitletext' );
309 return;
310 }
311
312 if ( $wgUser->isAllowed( 'suppressredirect' ) ) {
313 $createRedirect = $this->leaveRedirect;
314 } else {
315 $createRedirect = true;
316 }
317
318 $error = $ot->moveTo( $nt, true, $this->reason, $createRedirect );
319 if ( $error !== true ) {
320 call_user_func_array( array($this, 'showForm'), $error );
321 return;
322 }
323
324 if ( $wgFixDoubleRedirects && $this->fixRedirects ) {
325 DoubleRedirectJob::fixRedirects( 'move', $ot, $nt );
326 }
327
328 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
329
330 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
331
332 $oldUrl = $ot->getFullUrl( 'redirect=no' );
333 $newUrl = $nt->getFullUrl();
334 $oldText = $ot->getPrefixedText();
335 $newText = $nt->getPrefixedText();
336 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
337 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
338
339 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
340
341 # Now we move extra pages we've been asked to move: subpages and talk
342 # pages. First, if the old page or the new page is a talk page, we
343 # can't move any talk pages: cancel that.
344 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
345 $this->moveTalk = false;
346 }
347
348 if( !$ot->userCan( 'move-subpages' ) ) {
349 $this->moveSubpages = false;
350 }
351
352 # Next make a list of id's. This might be marginally less efficient
353 # than a more direct method, but this is not a highly performance-cri-
354 # tical code path and readable code is more important here.
355 #
356 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
357 # 4 might get confused. If so, consider rewriting as a UNION.
358 #
359 # If the target namespace doesn't allow subpages, moving with subpages
360 # would mean that you couldn't move them back in one operation, which
361 # is bad. FIXME: A specific error message should be given in this
362 # case.
363 $dbr = wfGetDB( DB_MASTER );
364 if( $this->moveSubpages && (
365 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
366 $this->moveTalk &&
367 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
368 )
369 ) ) {
370 $conds = array(
371 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
372 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
373 );
374 $conds['page_namespace'] = array();
375 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
376 $conds['page_namespace'] []= $ot->getNamespace();
377 }
378 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
379 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
380 }
381 } elseif( $this->moveTalk ) {
382 $conds = array(
383 'page_namespace' => $ot->getTalkPage()->getNamespace(),
384 'page_title' => $ot->getDBKey()
385 );
386 } else {
387 # Skip the query
388 $conds = null;
389 }
390
391 $extraPages = array();
392 if( !is_null( $conds ) ) {
393 $extraPages = TitleArray::newFromResult(
394 $dbr->select( 'page',
395 array( 'page_id', 'page_namespace', 'page_title' ),
396 $conds,
397 __METHOD__
398 )
399 );
400 }
401
402 $extraOutput = array();
403 $skin = $wgUser->getSkin();
404 $count = 1;
405 foreach( $extraPages as $oldSubpage ) {
406 if( $oldSubpage->getArticleId() == $ot->getArticleId() ) {
407 # Already did this one.
408 continue;
409 }
410
411 $newPageName = preg_replace(
412 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
413 $nt->getDBKey(),
414 $oldSubpage->getDBKey()
415 );
416 if( $oldSubpage->isTalkPage() ) {
417 $newNs = $nt->getTalkPage()->getNamespace();
418 } else {
419 $newNs = $nt->getSubjectPage()->getNamespace();
420 }
421 # Bug 14385: we need makeTitleSafe because the new page names may
422 # be longer than 255 characters.
423 $newSubpage = Title::makeTitleSafe( $newNs, $newPageName );
424 if( !$newSubpage ) {
425 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
426 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink,
427 htmlspecialchars(Title::makeName( $newNs, $newPageName )));
428 continue;
429 }
430
431 # This was copy-pasted from Renameuser, bleh.
432 if ( $newSubpage->exists() && !$oldSubpage->isValidMoveTarget( $newSubpage ) ) {
433 $link = $skin->makeKnownLinkObj( $newSubpage );
434 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
435 } else {
436 $success = $oldSubpage->moveTo( $newSubpage, true, $this->reason );
437 if( $success === true ) {
438 if ( $this->fixRedirects ) {
439 DoubleRedirectJob::fixRedirects( 'move', $oldSubpage, $newSubpage );
440 }
441 $oldLink = $skin->makeKnownLinkObj( $oldSubpage, '', 'redirect=no' );
442 $newLink = $skin->makeKnownLinkObj( $newSubpage );
443 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
444 } else {
445 $oldLink = $skin->makeKnownLinkObj( $oldSubpage );
446 $newLink = $skin->makeLinkObj( $newSubpage );
447 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
448 }
449 }
450
451 ++$count;
452 if( $count >= $wgMaximumMovedPages ) {
453 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
454 break;
455 }
456 }
457
458 if( $extraOutput !== array() ) {
459 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
460 }
461
462 # Deal with watches (we don't watch subpages)
463 if( $this->watch ) {
464 $wgUser->addWatch( $ot );
465 $wgUser->addWatch( $nt );
466 } else {
467 $wgUser->removeWatch( $ot );
468 $wgUser->removeWatch( $nt );
469 }
470 }
471
472 function showLogFragment( $title, &$out ) {
473 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
474 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
475 }
476
477 }