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