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