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