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