Add bug and comment for r35609: * (bug 13434) Show a warning when hash identical...
[lhc/web/wiklou.git] / includes / 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 rights
14 if ( !$wgUser->isAllowed( 'move' ) ) {
15 $wgOut->showPermissionsErrorPage( array( $wgUser->isAnon() ? 'movenologintext' : 'movenotallowed' ) );
16 return;
17 }
18
19 # Don't allow blocked users to move pages
20 if ( $wgUser->isBlocked() ) {
21 $wgOut->blockedPage();
22 return;
23 }
24
25 # Check for database lock
26 if ( wfReadOnly() ) {
27 $wgOut->readOnlyPage();
28 return;
29 }
30
31 $f = new MovePageForm( $par );
32
33 if ( 'submit' == $action && $wgRequest->wasPosted()
34 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
35 $f->doSubmit();
36 } else {
37 $f->showForm( '' );
38 }
39 }
40
41 /**
42 * HTML form for Special:Movepage
43 * @ingroup SpecialPage
44 */
45 class MovePageForm {
46 var $oldTitle, $newTitle, $reason; # Text input
47 var $moveTalk, $deleteAndMove, $moveSubpages;
48
49 private $watch = false;
50
51 function MovePageForm( $par ) {
52 global $wgRequest;
53 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
54 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $target );
55 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
56 $this->reason = $wgRequest->getText( 'wpReason' );
57 if ( $wgRequest->wasPosted() ) {
58 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
59 } else {
60 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
61 }
62 $this->moveSubpages = $wgRequest->getBool( 'wpMovesubpages', false );
63 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
64 $this->watch = $wgRequest->getCheck( 'wpWatch' );
65 }
66
67 function showForm( $err, $hookErr = '' ) {
68 global $wgOut, $wgUser;
69
70 $ot = Title::newFromURL( $this->oldTitle );
71 if( is_null( $ot ) ) {
72 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
73 return;
74 }
75 if( !$ot->exists() ) {
76 $wgOut->showErrorPage( 'nopagetitle', 'nopagetext' );
77 return;
78 }
79
80 $sk = $wgUser->getSkin();
81
82 $oldTitleLink = $sk->makeLinkObj( $ot );
83 $oldTitle = $ot->getPrefixedText();
84
85 $wgOut->setPagetitle( wfMsg( 'move-page', $oldTitle ) );
86 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
87
88 if( $this->newTitle == '' ) {
89 # Show the current title as a default
90 # when the form is first opened.
91 $newTitle = $oldTitle;
92 } else {
93 if( $err == '' ) {
94 $nt = Title::newFromURL( $this->newTitle );
95 if( $nt ) {
96 # If a title was supplied, probably from the move log revert
97 # link, check for validity. We can then show some diagnostic
98 # information and save a click.
99 $newerr = $ot->isValidMoveOperation( $nt );
100 if( is_string( $newerr ) ) {
101 $err = $newerr;
102 }
103 }
104 }
105 $newTitle = $this->newTitle;
106 }
107
108 if ( $err == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
109 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle );
110 $movepagebtn = wfMsg( 'delete_and_move' );
111 $submitVar = 'wpDeleteAndMove';
112 $confirm = "
113 <tr>
114 <td></td>
115 <td class='mw-input'>" .
116 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
117 "</td>
118 </tr>";
119 $err = '';
120 } else {
121 $wgOut->addWikiMsg( 'movepagetext' );
122 $movepagebtn = wfMsg( 'movepagebtn' );
123 $submitVar = 'wpMove';
124 $confirm = false;
125 }
126
127 $oldTalk = $ot->getTalkPage();
128 $considerTalk = ( !$ot->isTalkPage() && $oldTalk->exists() );
129
130 if ( $considerTalk ) {
131 $wgOut->addWikiMsg( 'movepagetalktext' );
132 }
133
134 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
135 $token = htmlspecialchars( $wgUser->editToken() );
136
137 if ( $err != '' ) {
138 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
139 $errMsg = "";
140 if( $err == 'hookaborted' ) {
141 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
142 } else if (is_array($err)) {
143 $errMsg = '<p><strong class="error">' . call_user_func_array( 'wfMsgWikiHtml', $err ) . "</strong></p>\n";
144 } else {
145 $errMsg = '<p><strong class="error">' . wfMsgWikiHtml( $err ) . "</strong></p>\n";
146 }
147 $wgOut->addHTML( $errMsg );
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 $wgOut->addHTML( "
195 <tr>
196 <td></td>
197 <td class=\"mw-input\">" .
198 Xml::checkLabel( wfMsgHtml(
199 $ot->hasSubpages()
200 ? 'move-subpages'
201 : 'move-talk-subpages'
202 ),
203 'wpMovesubpages', 'wpMovesubpages',
204 # Don't check the box if we only have talk subpages to
205 # move and we aren't moving the talk page.
206 $this->moveSubpages && ($ot->hasSubpages() || $this->moveTalk)
207 ) .
208 "</td>
209 </tr>"
210 );
211 }
212
213 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' ) || $ot->userIsWatching();
214 $wgOut->addHTML( "
215 <tr>
216 <td></td>
217 <td class='mw-input'>" .
218 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
219 "</td>
220 </tr>
221 {$confirm}
222 <tr>
223 <td>&nbsp;</td>
224 <td class='mw-submit'>" .
225 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
226 "</td>
227 </tr>" .
228 Xml::closeElement( 'table' ) .
229 Xml::hidden( 'wpEditToken', $token ) .
230 Xml::closeElement( 'fieldset' ) .
231 Xml::closeElement( 'form' ) .
232 "\n"
233 );
234
235 $this->showLogFragment( $ot, $wgOut );
236
237 }
238
239 function doSubmit() {
240 global $wgOut, $wgUser, $wgRequest, $wgMaximumMovedPages, $wgLang;
241
242 if ( $wgUser->pingLimiter( 'move' ) ) {
243 $wgOut->rateLimited();
244 return;
245 }
246
247 # Variables beginning with 'o' for old article 'n' for new article
248
249 $ot = Title::newFromText( $this->oldTitle );
250 $nt = Title::newFromText( $this->newTitle );
251
252 # Delete to make way if requested
253 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
254 $article = new Article( $nt );
255
256 # Disallow deletions of big articles
257 $bigHistory = $article->isBigDeletion();
258 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
259 global $wgLang, $wgDeleteRevisionsLimit;
260 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
261 return;
262 }
263
264 // This may output an error message and exit
265 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
266 }
267
268 # don't allow moving to pages with # in
269 if ( !$nt || $nt->getFragment() != '' ) {
270 $this->showForm( 'badtitletext' );
271 return;
272 }
273
274 $error = $ot->moveTo( $nt, true, $this->reason );
275 if ( $error !== true ) {
276 # FIXME: showForm() should handle multiple errors
277 call_user_func_array(array($this, 'showForm'), $error[0]);
278 return;
279 }
280
281 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
282
283 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
284
285 $oldUrl = $ot->getFullUrl( 'redirect=no' );
286 $newUrl = $nt->getFullUrl();
287 $oldText = $ot->getPrefixedText();
288 $newText = $nt->getPrefixedText();
289 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
290 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
291
292 $wgOut->addWikiMsg( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
293
294 # Now we move extra pages we've been asked to move: subpages and talk
295 # pages. First, if the old page or the new page is a talk page, we
296 # can't move any talk pages: cancel that.
297 if( $ot->isTalkPage() || $nt->isTalkPage() ) {
298 $this->moveTalk = false;
299 }
300
301 # Next make a list of id's. This might be marginally less efficient
302 # than a more direct method, but this is not a highly performance-cri-
303 # tical code path and readable code is more important here.
304 #
305 # Note: this query works nicely on MySQL 5, but the optimizer in MySQL
306 # 4 might get confused. If so, consider rewriting as a UNION.
307 #
308 # If the target namespace doesn't allow subpages, moving with subpages
309 # would mean that you couldn't move them back in one operation, which
310 # is bad. FIXME: A specific error message should be given in this
311 # case.
312 $dbr = wfGetDB( DB_SLAVE );
313 if( $this->moveSubpages && (
314 MWNamespace::hasSubpages( $nt->getNamespace() ) || (
315 $this->moveTalk &&
316 MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() )
317 )
318 ) ) {
319 $conds = array(
320 'page_title LIKE '.$dbr->addQuotes( $dbr->escapeLike( $ot->getDBkey() ) . '/%' )
321 .' OR page_title = ' . $dbr->addQuotes( $ot->getDBkey() )
322 );
323 $conds['page_namespace'] = array();
324 if( MWNamespace::hasSubpages( $nt->getNamespace() ) ) {
325 $conds['page_namespace'] []= $ot->getNamespace();
326 }
327 if( $this->moveTalk && MWNamespace::hasSubpages( $nt->getTalkPage()->getNamespace() ) ) {
328 $conds['page_namespace'] []= $ot->getTalkPage()->getNamespace();
329 }
330 } elseif( $this->moveTalk ) {
331 $conds = array(
332 'page_namespace' => $ot->getTalkPage()->getNamespace(),
333 'page_title' => $ot->getDBKey()
334 );
335 } else {
336 # Skip the query
337 $conds = null;
338 }
339
340 $extrapages = array();
341 if( !is_null( $conds ) ) {
342 $extrapages = $dbr->select( 'page',
343 array( 'page_id', 'page_namespace', 'page_title' ),
344 $conds,
345 __METHOD__
346 );
347 }
348
349 $extraOutput = array();
350 $skin = $wgUser->getSkin();
351 $count = 1;
352 foreach( $extrapages as $row ) {
353 if( $row->page_id == $ot->getArticleId() ) {
354 # Already did this one.
355 continue;
356 }
357
358 $oldPage = Title::newFromRow( $row );
359 $newPageName = preg_replace(
360 '#^'.preg_quote( $ot->getDBKey(), '#' ).'#',
361 $nt->getDBKey(),
362 $oldPage->getDBKey()
363 );
364 # The following line is an atrocious hack. Kill it with fire.
365 $newNs = $nt->getNamespace() + ($oldPage->getNamespace() & 1);
366 $newPage = Title::makeTitle( $newNs, $newPageName );
367
368 # This was copy-pasted from Renameuser, bleh.
369 if ( $newPage->exists() && !$oldPage->isValidMoveTarget( $newPage ) ) {
370 $link = $skin->makeKnownLinkObj( $newPage );
371 $extraOutput []= wfMsgHtml( 'movepage-page-exists', $link );
372 } else {
373 $success = $oldPage->moveTo( $newPage, true, $this->reason );
374 if( $success === true ) {
375 $oldLink = $skin->makeKnownLinkObj( $oldPage, '', 'redirect=no' );
376 $newLink = $skin->makeKnownLinkObj( $newPage );
377 $extraOutput []= wfMsgHtml( 'movepage-page-moved', $oldLink, $newLink );
378 } else {
379 $oldLink = $skin->makeKnownLinkObj( $oldPage );
380 $newLink = $skin->makeLinkObj( $newPage );
381 $extraOutput []= wfMsgHtml( 'movepage-page-unmoved', $oldLink, $newLink );
382 }
383 }
384
385 ++$count;
386 if( $count >= $wgMaximumMovedPages ) {
387 $extraOutput []= wfMsgExt( 'movepage-max-pages', array( 'parsemag', 'escape' ), $wgLang->formatNum( $wgMaximumMovedPages ) );
388 break;
389 }
390 }
391
392 if( $extraOutput !== array() ) {
393 $wgOut->addHTML( "<ul>\n<li>" . implode( "</li>\n<li>", $extraOutput ) . "</li>\n</ul>" );
394 }
395
396 # Deal with watches (we don't watch subpages)
397 if( $this->watch ) {
398 $wgUser->addWatch( $ot );
399 $wgUser->addWatch( $nt );
400 } else {
401 $wgUser->removeWatch( $ot );
402 $wgUser->removeWatch( $nt );
403 }
404 }
405
406 function showLogFragment( $title, &$out ) {
407 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
408 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
409 }
410
411 }