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