Bug 3835: add a cancel link on special:movepage
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Constructor
10 */
11 function wfSpecialMovepage( $par = null ) {
12 global $wgUser, $wgOut, $wgRequest, $action, $wgOnlySysopMayMove;
13
14 # check rights. We don't want newbies to move pages to prevents possible attack
15 if ( !$wgUser->isAllowed( 'move' ) or $wgUser->isBlocked() or ($wgOnlySysopMayMove and $wgUser->isNewbie())) {
16 $wgOut->errorpage( "movenologin", "movenologintext" );
17 return;
18 }
19 # We don't move protected pages
20 if ( wfReadOnly() ) {
21 $wgOut->readOnlyPage();
22 return;
23 }
24
25 $f = new MovePageForm( $par );
26
27 if ( 'success' == $action ) {
28 $f->showSuccess();
29 } else if ( 'submit' == $action && $wgRequest->wasPosted()
30 && $wgUser->matchEditToken( $wgRequest->getVal( 'wpEditToken' ) ) ) {
31 $f->doSubmit();
32 } else {
33 $f->showForm( '' );
34 }
35 }
36
37 /**
38 *
39 * @package MediaWiki
40 * @subpackage SpecialPage
41 */
42 class MovePageForm {
43 var $oldTitle, $newTitle, $reason; # Text input
44 var $moveTalk, $deleteAndMove;
45
46 function MovePageForm( $par ) {
47 global $wgRequest;
48 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
49 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $target );
50 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
51 $this->reason = $wgRequest->getText( 'wpReason' );
52 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
53 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' ) && $wgRequest->getBool( 'wpConfirm' );
54 }
55
56 function showForm( $err ) {
57 global $wgOut, $wgUser;
58
59 $wgOut->setPagetitle( wfMsg( 'movepage' ) );
60
61 $ot = Title::newFromURL( $this->oldTitle );
62 if( is_null( $ot ) ) {
63 $wgOut->errorpage( 'notargettitle', 'notargettext' );
64 return;
65 }
66 $oldTitle = $ot->getPrefixedText();
67
68 $encOldTitle = htmlspecialchars( $oldTitle );
69 if( $this->newTitle == '' ) {
70 # Show the current title as a default
71 # when the form is first opened.
72 $encNewTitle = $encOldTitle;
73 } else {
74 if( $err == '' ) {
75 $nt = Title::newFromURL( $this->newTitle );
76 if( $nt ) {
77 # If a title was supplied, probably from the move log revert
78 # link, check for validity. We can then show some diagnostic
79 # information and save a click.
80 $newerr = $ot->isValidMoveOperation( $nt );
81 if( is_string( $newerr ) ) {
82 $err = $newerr;
83 }
84 }
85 }
86 $encNewTitle = htmlspecialchars( $this->newTitle );
87 }
88 $encReason = htmlspecialchars( $this->reason );
89
90 if ( $err == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
91 $wgOut->addWikiText( wfMsg( 'delete_and_move_text', $encNewTitle ) );
92 $movepagebtn = wfMsgHtml( 'delete_and_move' );
93 $confirmText = wfMsgHtml( 'delete_and_move_confirm' );
94 $submitVar = 'wpDeleteAndMove';
95 $confirm = "
96 <tr>
97 <td align='right'>
98 <input type='checkbox' name='wpConfirm' id='wpConfirm' value=\"true\" />
99 </td>
100 <td align='left'><label for='wpConfirm'>{$confirmText}</label></td>
101 </tr>";
102 $err = '';
103 } else {
104 $wgOut->addWikiText( wfMsg( 'movepagetext' ) );
105 $movepagebtn = wfMsgHtml( 'movepagebtn' );
106 $submitVar = 'wpMove';
107 $confirm = false;
108 }
109
110 if ( !$ot->isTalkPage() ) {
111 $wgOut->addWikiText( wfMsg( 'movepagetalktext' ) );
112 }
113
114 $movearticle = wfMsgHtml( 'movearticle' );
115 $newtitle = wfMsgHtml( 'newtitle' );
116 $movetalk = wfMsgHtml( 'movetalk' );
117 $movereason = wfMsgHtml( 'movereason' );
118
119 $sk =& $wgUser->getSkin();
120 $cancel = $sk->makeKnownLink( $oldTitle, wfMsg('cancel') );
121
122 $titleObj = Title::makeTitle( NS_SPECIAL, 'Movepage' );
123 $action = $titleObj->escapeLocalURL( 'action=submit' );
124 $token = htmlspecialchars( $wgUser->editToken() );
125
126 if ( $err != '' ) {
127 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
128 $wgOut->addWikiText( '<p class="error">' . wfMsg($err) . "</p>\n" );
129 }
130
131 $moveTalkChecked = $this->moveTalk ? ' checked="checked"' : '';
132
133 $wgOut->addHTML( "
134 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
135 <table border='0'>
136 <tr>
137 <td align='right'>{$movearticle}:</td>
138 <td align='left'><strong>{$oldTitle}</strong></td>
139 </tr>
140 <tr>
141 <td align='right'><label for='wpNewTitle'>{$newtitle}:</label></td>
142 <td align='left'>
143 <input type='text' size='40' name='wpNewTitle' id='wpNewTitle' value=\"{$encNewTitle}\" />
144 <input type='hidden' name=\"wpOldTitle\" value=\"{$encOldTitle}\" />
145 </td>
146 </tr>
147 <tr>
148 <td align='right' valign='top'><br /><label for='wpReason'>{$movereason}:</label></td>
149 <td align='left' valign='top'><br />
150 <textarea cols='60' rows='2' name='wpReason' id='wpReason'>{$encReason}</textarea>
151 </td>
152 </tr>" );
153
154 if ( ! $ot->isTalkPage() ) {
155 $wgOut->addHTML( "
156 <tr>
157 <td align='right'>
158 <input type='checkbox' id=\"wpMovetalk\" name=\"wpMovetalk\"{$moveTalkChecked} value=\"1\" />
159 </td>
160 <td><label for=\"wpMovetalk\">{$movetalk}</label></td>
161 </tr>" );
162 }
163 $wgOut->addHTML( "
164 {$confirm}
165 <tr>
166 <td>&nbsp;</td>
167 <td align='left'>
168 <input type='submit' name=\"{$submitVar}\" value=\"{$movepagebtn}\" />
169 {$cancel}
170 </td>
171 </tr>
172 </table>
173 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
174 </form>\n" );
175
176 }
177
178 function doSubmit() {
179 global $wgOut, $wgUser, $wgRequest;
180 $fname = "MovePageForm::doSubmit";
181
182 if ( $wgUser->pingLimiter( 'move' ) ) {
183 $wgOut->rateLimited();
184 return;
185 }
186
187 # Variables beginning with 'o' for old article 'n' for new article
188
189 $ot = Title::newFromText( $this->oldTitle );
190 $nt = Title::newFromText( $this->newTitle );
191
192 # Delete to make way if requested
193 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
194 $article = new Article( $nt );
195 // This may output an error message and exit
196 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
197 }
198
199 # don't allow moving to pages with # in
200 if ( !$nt || $nt->getFragment() != '' ) {
201 $this->showForm( 'badtitletext' );
202 return;
203 }
204
205 $error = $ot->moveTo( $nt, true, $this->reason );
206 if ( $error !== true ) {
207 $this->showForm( $error );
208 return;
209 }
210
211 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ot , &$nt ) ) ;
212
213 # Move talk page if
214 # (1) the checkbox says to,
215 # (2) the namespaces are not themselves talk namespaces, and of course
216 # (3) it exists.
217 if ( ( $wgRequest->getVal('wpMovetalk') == 1 ) &&
218 !$ot->isTalkPage() &&
219 !$nt->isTalkPage() ) {
220
221 $ott = $ot->getTalkPage();
222 $ntt = $nt->getTalkPage();
223
224 # Attempt the move
225 $error = $ott->moveTo( $ntt, true, $this->reason );
226 if ( $error === true ) {
227 $talkmoved = 1;
228 wfRunHooks( 'SpecialMovepageAfterMove', array( &$this , &$ott , &$ntt ) ) ;
229 } else {
230 $talkmoved = $error;
231 }
232 } else {
233 # Stay silent on the subject of talk.
234 $talkmoved = '';
235 }
236
237 # Give back result to user.
238 $titleObj = Title::makeTitle( NS_SPECIAL, 'Movepage' );
239 $success = $titleObj->getFullURL(
240 'action=success&oldtitle=' . wfUrlencode( $ot->getPrefixedText() ) .
241 '&newtitle=' . wfUrlencode( $nt->getPrefixedText() ) .
242 '&talkmoved='.$talkmoved );
243
244 $wgOut->redirect( $success );
245 }
246
247 function showSuccess() {
248 global $wgOut, $wgRequest, $wgRawHtml;
249
250 $wgOut->setPagetitle( wfMsg( 'movepage' ) );
251 $wgOut->setSubtitle( wfMsg( 'pagemovedsub' ) );
252 $oldtitle = $wgRequest->getVal('oldtitle');
253 $newtitle = $wgRequest->getVal('newtitle');
254 $talkmoved = $wgRequest->getVal('talkmoved');
255
256 $text = wfMsg( 'pagemovedtext', $oldtitle, $newtitle );
257
258 # Temporarily disable raw html wikitext option out of XSS paranoia
259 $marchingantofdoom = $wgRawHtml;
260 $wgRawHtml = false;
261 $wgOut->addWikiText( $text );
262 $wgRawHtml = $marchingantofdoom;
263
264 if ( $talkmoved == 1 ) {
265 $wgOut->addWikiText( wfMsg( 'talkpagemoved' ) );
266 } elseif( 'articleexists' == $talkmoved ) {
267 $wgOut->addWikiText( wfMsg( 'talkexists' ) );
268 } else {
269 $ot = Title::newFromURL( $oldtitle );
270 if ( ! $ot->isTalkPage() ) {
271 $wgOut->addWikiText( wfMsg( 'talkpagenotmoved', wfMsg( $talkmoved ) ) );
272 }
273 }
274 }
275 }
276 ?>