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