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