More globals and uninitialized variables fixes. Added WebRequest ($wgRequest)
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
1 <?php
2 include_once( "LinksUpdate.php" );
3
4 function wfSpecialMovepage()
5 {
6 global $wgUser, $wgOut, $wgRequest, $action;
7
8 if ( 0 == $wgUser->getID() or $wgUser->isBlocked() ) {
9 $wgOut->errorpage( "movenologin", "movenologintext" );
10 return;
11 }
12 if ( wfReadOnly() ) {
13 $wgOut->readOnlyPage();
14 return;
15 }
16
17 $f = new MovePageForm();
18
19 if ( "success" == $action ) { $f->showSuccess(); }
20 else if ( "submit" == $action && $wgRequest->wasPosted() ) { $f->doSubmit(); }
21 else { $f->showForm( "" ); }
22 }
23
24 class MovePageForm {
25 var $oldTitle, $newTitle; # Text input
26
27 var $ot, $nt; # Old, new Title objects
28 var $ons, $nns; # Namespaces
29 var $odt, $ndt; # Pagenames (dbkey form)
30 var $oft, $nft; # Full page titles (DBkey form)
31 var $ofx, $nfx; # Full page titles (Text form)
32 var $oldid, $newid; # "cur_id" field (yes, both from "cur")
33 var $talkmoved = 0;
34
35 function MovePageForm() {
36 global $wgRequest;
37 $this->oldTitle = $wgRequest->getText( 'wpOldTitle', $wgRequest->getVal( 'target' ) );
38 $this->newTitle = $wgRequest->getText( 'wpNewTitle' );
39 }
40
41 function showForm( $err )
42 {
43 global $wgOut, $wgUser, $wgLang;
44
45 $wgOut->setPagetitle( wfMsg( "movepage" ) );
46
47 if ( empty( $this->oldTitle ) ) {
48 $wgOut->errorpage( "notargettitle", "notargettext" );
49 return;
50 }
51
52 $encOldTitle = htmlspecialchars( $this->oldTitle );
53 $encNewTitle = htmlspecialchars( $this->newTitle );
54 $ot = Title::newFromURL( $this->oldTitle );
55 $ott = $ot->getPrefixedText();
56
57 $wgOut->addWikiText( wfMsg( "movepagetext" ) );
58 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
59 $wgOut->addWikiText( "\n\n" . wfMsg( "movepagetalktext" ) );
60 }
61
62 $ma = wfMsg( "movearticle" );
63 $newt = wfMsg( "newtitle" );
64 $mpb = wfMsg( "movepagebtn" );
65 $movetalk = wfMsg( "movetalk" );
66
67 $titleObj = Title::makeTitle( NS_SPECIAL, "Movepage" );
68 $action = $titleObj->escapeLocalURL( "action=submit" );
69
70 if ( "" != $err ) {
71 $wgOut->setSubtitle( wfMsg( "formerror" ) );
72 $wgOut->addHTML( "<p><font color='red' size='+1'>{$err}</font>\n" );
73 }
74 $wgOut->addHTML( "<p>
75 <form id=\"movepage\" method=\"post\" action=\"{$action}\">
76 <table border=0><tr>
77 <td align=right>{$ma}:</td>
78 <td align=left><strong>{$ott}</strong></td>
79 </tr><tr>
80 <td align=right>{$newt}:</td>
81 <td align=left>
82 <input type=text size=40 name=\"wpNewTitle\" value=\"{$encNewTitle}\">
83 <input type=hidden name=\"wpOldTitle\" value=\"{$encOldTitle}\">
84 </td>
85 </tr>" );
86
87 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
88 $wgOut->addHTML(
89 "<tr>
90 <td align=right>
91 <input type=checkbox name=\"wpMovetalk\" checked value=\"1\">
92 </td><td>{$movetalk}</td>
93 </tr>" );
94 }
95 $wgOut->addHTML(
96 "<tr>
97 <td>&nbsp;</td><td align=left>
98 <input type=submit name=\"wpMove\" value=\"{$mpb}\">
99 </td></tr></table>
100 </form>\n" );
101
102 }
103
104 function doSubmit()
105 {
106 global $wgOut, $wgUser, $wgLang;
107 global $wgDeferredUpdateList, $wgMessageCache;
108 global $wgUseSquid, $wgInternalServer;
109 $fname = "MovePageForm::doSubmit";
110
111 $this->ot = Title::newFromText( $this->oldTitle );
112 $this->nt = Title::newFromText( $this->newTitle );
113 if( !$this->ot or !$this->nt ) {
114 $this->showForm( wfMsg( "badtitletext" ) );
115 return;
116 }
117 $this->ons = $this->ot->getNamespace();
118 $this->nns = $this->nt->getNamespace();
119 $this->odt = wfStrencode( $this->ot->getDBkey() );
120 $this->ndt = wfStrencode( $this->nt->getDBkey() );
121 $this->oft = wfStrencode( $this->ot->getPrefixedDBkey() );
122 $this->nft = wfStrencode( $this->nt->getPrefixedDBkey() );
123 $this->ofx = $this->ot->getPrefixedText();
124 $this->nfx = $this->nt->getPrefixedText();
125
126 $this->oldid = $this->ot->getArticleID();
127 $this->newid = $this->nt->getArticleID();
128
129 if ( strlen( trim( $this->ndt ) ) < 1 ) {
130 $this->showForm( wfMsg( "articleexists" ) );
131 return;
132 }
133 if ( ( ! Namespace::isMovable( $this->ons ) ) ||
134 ( "" == $this->odt ) ||
135 ( "" != $this->ot->getInterwiki() ) ||
136 ( !$this->ot->userCanEdit() ) ||
137 ( !$this->oldid ) ||
138 ( ! Namespace::isMovable( $this->nns ) ) ||
139 ( "" == $this->ndt ) ||
140 ( "" != $this->nt->getInterwiki() ) ||
141 ( !$this->nt->userCanEdit() ) ||
142 ( $this->ons == NS_MEDIAWIKI && $wgMessageCache->isCacheable( $this->odt ) ) ) {
143 $this->showForm( wfMsg( "badarticleerror" ) );
144 return;
145 }
146 # The move is allowed only if (1) the target doesn't exist, or
147 # (2) the target is a redirect to the source, and has no history
148 # (so we can undo bad moves right after they're done).
149
150 if ( 0 != $this->newid ) { # Target exists; check for validity
151 if ( ! $this->isValidTarget() ) {
152 $this->showForm( wfMsg( "articleexists" ) );
153 return;
154 }
155 $this->moveOverExistingRedirect();
156 } else { # Target didn't exist, do normal move.
157 $this->moveToNewTitle();
158 }
159
160 $this->updateWatchlists();
161
162 $u = new SearchUpdate( $this->oldid, $this->nt->getPrefixedDBkey() );
163 $u->doUpdate();
164 $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
165 $u->doUpdate();
166
167 # Squid purging
168 if ( $wgUseSquid ) {
169 /* this needs to be done after LinksUpdate */
170 $urlArr = Array(
171 # purge new title
172 $wgInternalServer.$this->nt->getLocalURL(),
173 # purge old title
174 $wgInternalServer.$this->ot->getLocalURL(),
175 );
176 wfPurgeSquidServers($urlArr);
177 # purge pages linking to new title
178 $u = new SquidUpdate($this->nt);
179 array_push( $wgDeferredUpdateList, $u );
180 # purge pages linking to old title
181 $u = new SquidUpdate($this->ot);
182 array_push( $wgDeferredUpdateList, $u );
183
184
185 }
186
187 # Move talk page if (1) the checkbox says to, (2) the source
188 # and target namespaces are identical, (3) the namespaces are not
189 # themselves talk namespaces, and of course (4) it exists.
190
191 if ( ( 1 == $_REQUEST['wpMovetalk'] ) &&
192 ( ! Namespace::isTalk( $this->ons ) ) &&
193 ( $this->ons == $this->nns ) ) {
194
195 $this->ons = $this->nns = Namespace::getTalk( $this->ons );
196 $this->ot = Title::makeTitle( $this->ons, $this->ot->getDBkey() );
197 $this->nt = Title::makeTitle( $this->nns, $this->nt->getDBkey() );
198
199 # odt, ndt, ofx, nfx remain the same
200
201 $this->oft = wfStrencode( $this->ot->getPrefixedDBkey() );
202 $this->nft = wfStrencode( $this->nt->getPrefixedDBkey() );
203
204 $this->oldid = $this->ot->getArticleID();
205 $this->newid = $this->nt->getArticleID();
206
207 if ( 0 != $this->oldid ) {
208 if ( 0 != $this->newid ) {
209 if ( $this->isValidTarget() ) {
210 $this->moveOverExistingRedirect();
211 $this->talkmoved = 1;
212 } else {
213 $this->talkmoved = 'invalid';
214 }
215 } else {
216 $this->moveToNewTitle();
217 $this->talkmoved = 1;
218 }
219 $u = new SearchUpdate( $this->oldid, $this->nt->getPrefixedDBkey() );
220 $u->doUpdate();
221 $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
222 $u->doUpdate();
223
224 # Squid purging
225 if ( $wgUseSquid ) {
226 /* this needs to be done after LinksUpdate */
227 $urlArr = Array(
228 # purge new title
229 $nt->getInternalURL(),
230 # purge old title
231 $ot->getInternalURL(),
232 );
233 wfPurgeSquidServers($urlArr);
234 # purge pages linking to new title
235 $u = new SquidUpdate($this->nt);
236 array_push( $wgDeferredUpdateList, $u );
237 # purge pages linking to old title
238 $u = new SquidUpdate($this->ot);
239 array_push( $wgDeferredUpdateList, $u );
240
241
242 }
243 }
244 }
245 $titleObj = Title::makeTitle( NS_SPECIAL, "Movepage" );
246 $success = $titleObj->getFullURL(
247 "action=success&oldtitle=" . wfUrlencode( $this->ofx ) .
248 "&newtitle=" . wfUrlencode( $this->nfx ) .
249 "&talkmoved={$this->talkmoved}" );
250
251 $wgOut->redirect( $success );
252 }
253
254 function showSuccess()
255 {
256 global $wgOut, $wgUser;
257
258 $wgOut->setPagetitle( wfMsg( "movepage" ) );
259 $wgOut->setSubtitle( wfMsg( "pagemovedsub" ) );
260
261 $text = wfMsg( "pagemovedtext", $_REQUEST['oldtitle'], $_REQUEST['newtitle'] );
262 $wgOut->addWikiText( $text );
263
264 if ( 1 == $_REQUEST['talkmoved'] ) {
265 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagemoved" ) );
266 } elseif( 'invalid' == $_REQUEST['talkmoved'] ) {
267 $wgOut->addHTML( "\n<p><strong>" . wfMsg( "talkexists" ) . "</strong>" );
268 } else {
269 $ot = Title::newFromURL( $_REQUEST['oldtitle'] );
270 if ( ! Namespace::isTalk( $ot->getNamespace() ) ) {
271 $wgOut->addHTML( "\n<p>" . wfMsg( "talkpagenotmoved" ) );
272 }
273 }
274 }
275
276 # Is the the existing target title valid?
277
278 function isValidTarget()
279 {
280 $fname = "MovePageForm::isValidTarget";
281
282 $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
283 "WHERE cur_id={$this->newid}";
284 $res = wfQuery( $sql, DB_READ, $fname );
285 $obj = wfFetchObject( $res );
286
287 if ( 0 == $obj->cur_is_redirect ) { return false; }
288
289 if ( preg_match( "/\\[\\[\\s*([^\\]]*)]]/", $obj->cur_text, $m ) ) {
290 $rt = Title::newFromText( $m[1] );
291 if ( 0 != strcmp( wfStrencode( $rt->getPrefixedDBkey() ),
292 $this->oft ) ) {
293 return false;
294 }
295 }
296 $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
297 "AND old_title='{$this->ndt}'";
298 $res = wfQuery( $sql, DB_READ, $fname );
299 if ( 0 != wfNumRows( $res ) ) { return false; }
300
301 return true;
302 }
303
304 # Move page to title which is presently a redirect to the source
305 # page. Handling link tables here is tricky.
306
307 function moveOverExistingRedirect()
308 {
309 global $wgUser, $wgLinkCache;
310 $fname = "MovePageForm::moveOverExistingRedirect";
311 $mt = wfMsg( "movedto" );
312
313 # Change the name of the target page:
314 $now = wfTimestampNow();
315 $won = wfInvertTimestamp( $now );
316 $sql = "UPDATE cur SET cur_touched='{$now}'," .
317 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
318 "WHERE cur_id={$this->oldid}";
319 wfQuery( $sql, DB_WRITE, $fname );
320 $wgLinkCache->clearLink( $this->nft );
321
322 # Repurpose the old redirect. We don't save it to history since
323 # by definition if we've got here it's rather uninteresting.
324 $sql = "UPDATE cur SET cur_touched='{$now}',cur_timestamp='{$now}',inverse_timestamp='${won}'," .
325 "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
326 "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
327 "{$mt} \\\"{$this->nft}\\\"',cur_user='" . $wgUser->getID() .
328 "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
329 "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
330 "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
331 wfQuery( $sql, DB_WRITE, $fname );
332 $wgLinkCache->clearLink( $this->oft );
333
334 # Fix the redundant names for the past revisions of the target page.
335 # The redirect should have no old revisions.
336 $sql = "UPDATE old SET " .
337 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
338 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
339 wfQuery( $sql, DB_WRITE, $fname );
340
341 RecentChange::notifyMove( $now, $this->ot, $this->nt, $wgUser, $mt );
342
343 # The only link from here should be the old redirect
344
345 $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
346 wfQuery( $sql, DB_WRITE, $fname );
347
348 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
349 wfQuery( $sql, DB_WRITE, $fname );
350
351 # Swap links. Using MAXINT as a temp; if there's ever an article
352 # with id 4294967295, this will fail, but I think that's pretty safe
353
354 $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
355 wfQuery( $sql, DB_WRITE, $fname );
356
357 $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
358 wfQuery( $sql, DB_WRITE, $fname );
359
360 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
361 wfQuery( $sql, DB_WRITE, $fname );
362
363 # Note: the insert below must be after the updates above!
364
365 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
366 wfQuery( $sql, DB_WRITE, $fname );
367
368 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
369 wfQuery( $sql, DB_WRITE, $fname );
370 }
371
372 # Move page to non-existing title.
373
374 function moveToNewTitle()
375 {
376 global $wgUser, $wgLinkCache;
377 $fname = "MovePageForm::moveToNewTitle";
378 $mt = wfMsg( "movedto" );
379
380 $now = wfTimestampNow();
381 $won = wfInvertTimestamp( $now );
382 $sql = "UPDATE cur SET cur_touched='{$now}'," .
383 "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
384 "WHERE cur_id={$this->oldid}";
385 wfQuery( $sql, DB_WRITE, $fname );
386 $wgLinkCache->clearLink( $this->nft );
387
388 $comment = "{$mt} \"{$this->nft}\"";
389 $encComment = wfStrencode( $comment );
390 $common = "{$this->ons},'{$this->odt}'," .
391 "'$encComment','" .$wgUser->getID() . "','" .
392 wfStrencode( $wgUser->getName() ) ."','{$now}'";
393 $sql = "INSERT INTO cur (cur_namespace,cur_title," .
394 "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
395 "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
396 "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
397 wfQuery( $sql, DB_WRITE, $fname );
398 $this->newid = wfInsertId();
399 $wgLinkCache->clearLink( $this->oft );
400
401 $sql = "UPDATE old SET " .
402 "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
403 "old_namespace={$this->ons} AND old_title='{$this->odt}'";
404 wfQuery( $sql, DB_WRITE, $fname );
405
406 RecentChange::notifyMove( $now, $this->ot, $this->nt, $wgUser, $comment );
407 Article::onArticleCreate( $this->nt );
408
409 $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
410 wfQuery( $sql, DB_WRITE, $fname );
411
412 $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
413 wfQuery( $sql, DB_WRITE, $fname );
414
415 $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
416 wfQuery( $sql, DB_WRITE, $fname );
417
418 # Non-existent target may have had broken links to it; these must
419 # now be removed and made into good links.
420 $update = new LinksUpdate( $this->oldid, $this->nft );
421 $update->fixBrokenLinks();
422
423 $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
424 wfQuery( $sql, DB_WRITE, $fname );
425 }
426
427 function updateWatchlists()
428 {
429 $oldnamespace = $this->ons & ~1;
430 $newnamespace = $this->nns & ~1;
431 $oldtitle = $this->odt;
432 $newtitle = $this->ndt;
433
434 if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
435 return;
436
437 WatchedItem::duplicateEntries( $this->ot, $this->nt );
438 }
439
440 }
441 ?>