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