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