* Add form to RCLinked and add to sp:specialpages
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
1 <?php
2 /**
3 *
4 * @addtogroup 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 ( 'success' == $action ) {
34 $f->showSuccess();
35 } else if ( 'submit' == $action && $wgRequest->wasPosted()
36 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
37 $f->doSubmit();
38 } else {
39 $f->showForm( '' );
40 }
41 }
42
43 /**
44 * HTML form for Special:Movepage
45 * @addtogroup SpecialPage
46 */
47 class MovePageForm {
48 var $oldTitle, $newTitle, $reason; # Text input
49 var $moveTalk, $deleteAndMove;
50
51 private $watch = false;
52
53 function MovePageForm( $par ) {
54 global $wgRequest;
55 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
56 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $target );
57 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
58 $this->reason = $wgRequest->getText( 'wpReason' );
59 if ( $wgRequest->wasPosted() ) {
60 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', false );
61 } else {
62 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
63 }
64 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
65 $this->watch = $wgRequest->getCheck( 'wpWatch' );
66 }
67
68 function showForm( $err, $hookErr = '' ) {
69 global $wgOut, $wgUser, $wgContLang;
70
71 $ot = Title::newFromURL( $this->oldTitle );
72 if( is_null( $ot ) ) {
73 $wgOut->showErrorPage( 'notargettitle', 'notargettext' );
74 return;
75 }
76
77 $start = $wgContLang->isRTL() ? 'right' : 'left';
78 $end = $wgContLang->isRTL() ? 'left' : 'right';
79 $sk = $wgUser->getSkin();
80
81 $oldTitleLink = $sk->makeLinkObj( $ot );
82 $oldTitle = $ot->getPrefixedText();
83
84 $wgOut->setPagetitle( wfMsg( 'move-page', $oldTitle ) );
85 $wgOut->setSubtitle( wfMsg( 'move-page-backlink', $oldTitleLink ) );
86
87 if( $this->newTitle == '' ) {
88 # Show the current title as a default
89 # when the form is first opened.
90 $newTitle = $oldTitle;
91 } else {
92 if( $err == '' ) {
93 $nt = Title::newFromURL( $this->newTitle );
94 if( $nt ) {
95 # If a title was supplied, probably from the move log revert
96 # link, check for validity. We can then show some diagnostic
97 # information and save a click.
98 $newerr = $ot->isValidMoveOperation( $nt );
99 if( is_string( $newerr ) ) {
100 $err = $newerr;
101 }
102 }
103 }
104 $newTitle = $this->newTitle;
105 }
106
107 if ( $err == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
108 $wgOut->addWikiMsg( 'delete_and_move_text', $newTitle );
109 $movepagebtn = wfMsg( 'delete_and_move' );
110 $submitVar = 'wpDeleteAndMove';
111 $confirm = "
112 <tr>
113 <td></td>
114 <td>" .
115 Xml::checkLabel( wfMsg( 'delete_and_move_confirm' ), 'wpConfirm', 'wpConfirm' ) .
116 "</td>
117 </tr>";
118 $err = '';
119 } else {
120 $wgOut->addWikiMsg( 'movepagetext' );
121 $movepagebtn = wfMsg( 'movepagebtn' );
122 $submitVar = 'wpMove';
123 $confirm = false;
124 }
125
126 $oldTalk = $ot->getTalkPage();
127 $considerTalk = ( !$ot->isTalkPage() && $oldTalk->exists() );
128
129 if ( $considerTalk ) {
130 $wgOut->addWikiMsg( 'movepagetalktext' );
131 }
132
133 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
134 $token = htmlspecialchars( $wgUser->editToken() );
135
136 if ( $err != '' ) {
137 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
138 $errMsg = "";
139 if( $err == 'hookaborted' ) {
140 $errMsg = "<p><strong class=\"error\">$hookErr</strong></p>\n";
141 } else if (is_array($err)) {
142 $errMsg = '<p><strong class="error">' . call_user_func_array( 'wfMsgWikiHtml', $err ) . "</strong></p>\n";
143 } else {
144 $errMsg = '<p><strong class="error">' . wfMsgWikiHtml( $err ) . "</strong></p>\n";
145 }
146 $wgOut->addHTML( $errMsg );
147 }
148
149 $moveTalkChecked = $this->moveTalk ? ' checked="checked"' : '';
150
151 $wgOut->addHTML(
152 Xml::openElement( 'form', array( 'method' => 'post', 'action' => $titleObj->getLocalURL( 'action=submit' ), 'id' => 'movepage' ) ) .
153 Xml::openElement( 'fieldset' ) .
154 Xml::element( 'legend', null, wfMsg( 'move-page-legend' ) ) .
155 Xml::openElement( 'table', array( 'border' => '0', 'id' => 'mw-movepage-table' ) ) .
156 "<tr>
157 <td align='$end'>" .
158 wfMsgHtml( 'movearticle' ) .
159 "</td>
160 <td align='$start'>
161 <strong>{$oldTitleLink}</strong>
162 </td>
163 </tr>
164 <tr>
165 <td align='$end'>" .
166 Xml::label( wfMsg( 'newtitle' ), 'wpNewTitle' ) .
167 "</td>
168 <td align='$start'>" .
169 Xml::input( 'wpNewTitle', 40, $newTitle, array( 'type' => 'text', 'id' => 'wpNewTitle' ) ) .
170 Xml::hidden( 'wpOldTitle', $oldTitle ) .
171 "</td>
172 </tr>
173 <tr>
174 <td align='$end' valign='top'><br />" .
175 Xml::label( wfMsg( 'movereason' ), 'wpReason' ) .
176 "</td>
177 <td align='$start' valign='top'><br />" .
178 Xml::openElement( 'textarea', array( 'name' => 'wpReason', 'id' => 'wpReason', 'cols' => 60, 'rows' => 2 ) ) .
179 htmlspecialchars( $this->reason ) .
180 Xml::closeElement( 'textarea' ) .
181 "</td>
182 </tr>"
183 );
184
185 if ( $considerTalk ) {
186 $wgOut->addHTML( "
187 <tr>
188 <td></td>
189 <td>" .
190 Xml::checkLabel( wfMsg( 'movetalk' ), 'wpMovetalk', 'wpMovetalk', $moveTalkChecked ) .
191 "</td>
192 </tr>"
193 );
194 }
195
196 $watchChecked = $this->watch || $wgUser->getBoolOption( 'watchmoves' ) || $ot->userIsWatching();
197 $wgOut->addHTML( "
198 <tr>
199 <td></td>
200 <td>" .
201 Xml::checkLabel( wfMsg( 'move-watch' ), 'wpWatch', 'watch', $watchChecked ) .
202 "</td>
203 </tr>
204 {$confirm}
205 <tr>
206 <td>&nbsp;</td>
207 <td align='$start'>" .
208 Xml::submitButton( $movepagebtn, array( 'name' => $submitVar ) ) .
209 "</td>
210 </tr>" .
211 Xml::closeElement( 'table' ) .
212 Xml::hidden( 'wpEditToken', $token ) .
213 Xml::closeElement( 'fieldset' ) .
214 Xml::closeElement( 'form' ) .
215 "\n"
216 );
217
218 $this->showLogFragment( $ot, $wgOut );
219
220 }
221
222 function doSubmit() {
223 global $wgOut, $wgUser, $wgRequest;
224
225 if ( $wgUser->pingLimiter( 'move' ) ) {
226 $wgOut->rateLimited();
227 return;
228 }
229
230 # Variables beginning with 'o' for old article 'n' for new article
231
232 $ot = Title::newFromText( $this->oldTitle );
233 $nt = Title::newFromText( $this->newTitle );
234
235 # Delete to make way if requested
236 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
237 $article = new Article( $nt );
238
239 # Disallow deletions of big articles
240 $bigHistory = $article->isBigDeletion();
241 if( $bigHistory && !$nt->userCan( 'bigdelete' ) ) {
242 global $wgLang, $wgDeleteRevisionsLimit;
243 $this->showForm( array('delete-toobig', $wgLang->formatNum( $wgDeleteRevisionsLimit ) ) );
244 return;
245 }
246
247 // This may output an error message and exit
248 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
249 }
250
251 # don't allow moving to pages with # in
252 if ( !$nt || $nt->getFragment() != '' ) {
253 $this->showForm( 'badtitletext' );
254 return;
255 }
256
257 $hookErr = null;
258 if( !wfRunHooks( 'AbortMove', array( $ot, $nt, $wgUser, &$hookErr ) ) ) {
259 $this->showForm( 'hookaborted', $hookErr );
260 return;
261 }
262
263 $error = $ot->moveTo( $nt, true, $this->reason );
264 if ( $error !== true ) {
265 $this->showForm( $error );
266 return;
267 }
268
269 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
270
271 # Move the talk page if relevant, if it exists, and if we've been told to
272 $ott = $ot->getTalkPage();
273 if( $ott->exists() ) {
274 if( $this->moveTalk && !$ot->isTalkPage() && !$nt->isTalkPage() ) {
275 $ntt = $nt->getTalkPage();
276
277 # Attempt the move
278 $error = $ott->moveTo( $ntt, true, $this->reason );
279 if ( $error === true ) {
280 $talkmoved = 1;
281 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ott , &$ntt ) );
282 } else {
283 $talkmoved = $error;
284 }
285 } else {
286 # Stay silent on the subject of talk.
287 $talkmoved = '';
288 }
289 } else {
290 $talkmoved = 'notalkpage';
291 }
292
293 # Deal with watches
294 if( $this->watch ) {
295 $wgUser->addWatch( $ot );
296 $wgUser->addWatch( $nt );
297 } else {
298 $wgUser->removeWatch( $ot );
299 $wgUser->removeWatch( $nt );
300 }
301
302 # Give back result to user.
303 $titleObj = SpecialPage::getTitleFor( 'Movepage' );
304 $success = $titleObj->getFullURL(
305 'action=success&oldtitle=' . wfUrlencode( $ot->getPrefixedText() ) .
306 '&newtitle=' . wfUrlencode( $nt->getPrefixedText() ) .
307 '&talkmoved='.$talkmoved );
308
309 $wgOut->redirect( $success );
310 }
311
312 function showSuccess() {
313 global $wgOut, $wgRequest, $wgUser;
314
315 $old = Title::newFromText( $wgRequest->getVal( 'oldtitle' ) );
316 $new = Title::newFromText( $wgRequest->getVal( 'newtitle' ) );
317
318 if( is_null( $old ) || is_null( $new ) ) {
319 throw new ErrorPageError( 'badtitle', 'badtitletext' );
320 }
321
322 $wgOut->setPagetitle( wfMsg( 'pagemovedsub' ) );
323
324 $talkmoved = $wgRequest->getVal( 'talkmoved' );
325 $oldUrl = $old->getFullUrl( 'redirect=no' );
326 $newUrl = $new->getFullUrl();
327 $oldText = $old->getPrefixedText();
328 $newText = $new->getPrefixedText();
329 $oldLink = "<span class='plainlinks'>[$oldUrl $oldText]</span>";
330 $newLink = "<span class='plainlinks'>[$newUrl $newText]</span>";
331
332 $s = wfMsgNoTrans( 'movepage-moved', $oldLink, $newLink, $oldText, $newText );
333
334 if ( $talkmoved == 1 ) {
335 $s .= "\n\n" . wfMsgNoTrans( 'talkpagemoved' );
336 } elseif( 'articleexists' == $talkmoved ) {
337 $s .= "\n\n" . wfMsgNoTrans( 'talkexists' );
338 } else {
339 if( !$old->isTalkPage() && $talkmoved != 'notalkpage' ) {
340 $s .= "\n\n" . wfMsgNoTrans( 'talkpagenotmoved', wfMsgNoTrans( $talkmoved ) );
341 }
342 }
343 $wgOut->addWikiText( $s );
344 }
345
346 function showLogFragment( $title, &$out ) {
347 $out->addHTML( Xml::element( 'h2', NULL, LogPage::logName( 'move' ) ) );
348 LogEventsList::showLogExtract( $out, 'move', $title->getPrefixedText() );
349 }
350
351 }