Move page tweaks:
[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() {
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();
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() {
52 global $wgRequest;
53 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $wgRequest->getVal( 'target' ) );
54 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
55 $this->reason = $wgRequest->getText( 'wpReason' );
56 $this->moveTalk = $wgRequest->getBool( 'wpMovetalk', true );
57 $this->deleteAndMove = $wgRequest->getBool( 'wpDeleteAndMove' );
58 }
59
60 function showForm( $err ) {
61 global $wgOut, $wgUser, $wgLang;
62
63 $wgOut->setPagetitle( wfMsg( 'movepage' ) );
64
65 if ( $this->oldTitle == '' ) {
66 $wgOut->errorpage( 'notargettitle', 'notargettext' );
67 return;
68 }
69
70 $ot = Title::newFromURL( $this->oldTitle );
71 $oldTitle = $ot->getPrefixedText();
72
73 $encOldTitle = htmlspecialchars( $this->oldTitle );
74 if( $this->newTitle == '' ) {
75 # Show the current title as a default
76 # when the form is first opened.
77 $encNewTitle = $oldTitle;
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 $err = $ot->isValidMoveOperation( $nt );
86 }
87 }
88 $encNewTitle = htmlspecialchars( $this->newTitle );
89 }
90 $encReason = htmlspecialchars( $this->reason );
91
92 if ( $err == 'articleexists' && $wgUser->isAllowed( 'delete' ) ) {
93 $wgOut->addWikiText( wfMsg( 'delete_and_move_text', $encNewTitle ) );
94 $movepagebtn = wfMsg( 'delete_and_move' );
95 $submitVar = 'wpDeleteAndMove';
96 $err = '';
97 } else {
98 $wgOut->addWikiText( wfMsg( 'movepagetext' ) );
99 $movepagebtn = wfMsg( 'movepagebtn' );
100 $submitVar = 'wpMove';
101 }
102
103 if ( !$ot->isTalkPage() ) {
104 $wgOut->addWikiText( wfMsg( 'movepagetalktext' ) );
105 }
106
107 $movearticle = wfMsg( 'movearticle' );
108 $newtitle = wfMsg( 'newtitle' );
109 $movetalk = wfMsg( 'movetalk' );
110 $movereason = wfMsg( 'movereason' );
111
112 $titleObj = Title::makeTitle( NS_SPECIAL, 'Movepage' );
113 $action = $titleObj->escapeLocalURL( 'action=submit' );
114 $token = htmlspecialchars( $wgUser->editToken() );
115
116 if ( $err != '' ) {
117 $wgOut->setSubtitle( wfMsg( 'formerror' ) );
118 $wgOut->addHTML( '<p class="error">' . wfMsg($err) . "</p>\n" );
119 }
120
121 $moveTalkChecked = $this->moveTalk ? ' checked="checked"' : '';
122
123 $wgOut->addHTML( "
124 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
125 <table border='0'>
126 <tr>
127 <td align='right'>{$movearticle}:</td>
128 <td align='left'><strong>{$oldTitle}</strong></td>
129 </tr>
130 <tr>
131 <td align='right'>{$newtitle}:</td>
132 <td align='left'>
133 <input type='text' size='40' name=\"wpNewTitle\" value=\"{$encNewTitle}\" />
134 <input type='hidden' name=\"wpOldTitle\" value=\"{$encOldTitle}\" />
135 </td>
136 </tr>
137 <tr>
138 <td align='right'>{$movereason}:</td>
139 <td align='left'>
140 <input type='text' size=40 name=\"wpReason\" value=\"{$encReason}\" />
141 </td>
142 </tr>" );
143
144 if ( ! $ot->isTalkPage() ) {
145 $wgOut->addHTML( "
146 <tr>
147 <td align='right'>
148 <input type='checkbox' name=\"wpMovetalk\"{$moveTalkChecked} value=\"1\" />
149 </td>
150 <td>{$movetalk}</td>
151 </tr>" );
152 }
153 $wgOut->addHTML( "
154 <tr>
155 <td>&nbsp;</td>
156 <td align='left'>
157 <input type='submit' name=\"{$submitVar}\" value=\"{$movepagebtn}\" />
158 </td>
159 </tr>
160 </table>
161 <input type='hidden' name='wpEditToken' value=\"{$token}\" />
162 </form>\n" );
163
164 }
165
166 function doSubmit() {
167 global $wgOut, $wgUser, $wgLang;
168 global $wgDeferredUpdateList, $wgMessageCache;
169 global $wgUseSquid, $wgRequest;
170 $fname = "MovePageForm::doSubmit";
171
172 # Variables beginning with 'o' for old article 'n' for new article
173
174 $ot = Title::newFromText( $this->oldTitle );
175 $nt = Title::newFromText( $this->newTitle );
176
177 # Delete to make way if requested
178 if ( $wgUser->isAllowed( 'delete' ) && $this->deleteAndMove ) {
179 $article = new Article( $nt );
180 // This may output an error message and exit
181 $article->doDelete( wfMsgForContent( 'delete_and_move_reason' ) );
182 }
183
184 # don't allow moving to pages with # in
185 if ( !$nt || $nt->getFragment() != '' ) {
186 $this->showForm( 'badtitletext' );
187 return;
188 }
189
190 $error = $ot->moveTo( $nt, true, $this->reason );
191 if ( $error !== true ) {
192 $this->showForm( $error );
193 return;
194 }
195
196 # Update counters if the article got moved into or out of NS_MAIN namespace
197 $ons = $ot->getNamespace();
198 $nns = $nt->getNamespace();
199
200 # moved out of article namespace?
201 if ( $ons == NS_MAIN and $nns != NS_MAIN ) {
202 $u = new SiteStatsUpdate( 0, 1, -1); # not viewed, edited, removing
203 }
204 # moved into article namespace?
205 elseif ( $ons != NS_MAIN and $nns == NS_MAIN ) {
206 $u = new SiteStatsUpdate( 0, 1, +1 ); # not viewed, edited, adding
207 } else {
208 $u = false;
209 }
210 if ( $u !== false ) {
211 # save it for later update
212 array_push( $wgDeferredUpdateList, $u );
213 unset($u);
214 }
215
216 # Move talk page if
217 # (1) the checkbox says to,
218 # (2) the namespaces are not themselves talk namespaces, and of course
219 # (3) it exists.
220 if ( ( $wgRequest->getVal('wpMovetalk') == 1 ) &&
221 ( ! Namespace::isTalk( $ons ) ) &&
222 ( ! Namespace::isTalk( $nns ) ) ) {
223
224 # get old talk page namespace
225 $ons = Namespace::getTalk( $ons );
226 # get new talk page namespace
227 $nns = Namespace::getTalk( $nns );
228
229 # make talk page title objects
230 $ott = Title::makeTitle( $ons, $ot->getDBkey() );
231 $ntt = Title::makeTitle( $nns, $nt->getDBkey() );
232
233 # Attempt the move
234 $error = $ott->moveTo( $ntt, true, $this->reason );
235 if ( $error === true ) {
236 $talkmoved = 1;
237 } else {
238 $talkmoved = $error;
239 }
240 }
241
242 # Give back result to user.
243 $titleObj = Title::makeTitle( NS_SPECIAL, 'Movepage' );
244 $success = $titleObj->getFullURL(
245 'action=success&oldtitle=' . wfUrlencode( $ot->getPrefixedText() ) .
246 '&newtitle=' . wfUrlencode( $nt->getPrefixedText() ) .
247 '&talkmoved='.$talkmoved );
248
249 $wgOut->redirect( $success );
250 }
251
252 function showSuccess() {
253 global $wgOut, $wgRequest, $wgRawHtml;
254
255 $wgOut->setPagetitle( wfMsg( 'movepage' ) );
256 $wgOut->setSubtitle( wfMsg( 'pagemovedsub' ) );
257 $oldtitle = $wgRequest->getVal('oldtitle');
258 $newtitle = $wgRequest->getVal('newtitle');
259 $talkmoved = $wgRequest->getVal('talkmoved');
260
261 $text = wfMsg( 'pagemovedtext', $oldtitle, $newtitle );
262
263 # Temporarily disable raw html wikitext option out of XSS paranoia
264 $marchingantofdoom = $wgRawHtml;
265 $wgRawHtml = false;
266 $wgOut->addWikiText( $text );
267 $wgRawHtml = $marchingantofdoom;
268
269 if ( $talkmoved == 1 ) {
270 $wgOut->addHTML( "\n<p>" . wfMsg( 'talkpagemoved' ) . "</p>\n" );
271 } elseif( 'articleexists' == $talkmoved ) {
272 $wgOut->addHTML( "\n<p><strong>" . wfMsg( 'talkexists' ) . "</strong></p>\n" );
273 } else {
274 $ot = Title::newFromURL( $oldtitle );
275 if ( ! $ot->isTalkPage() ) {
276 $wgOut->addHTML( "\n<p>" . wfMsg( 'talkpagenotmoved', wfMsg( $talkmoved ) ) . "</p>\n" );
277 }
278 }
279 }
280 }
281 ?>