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