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