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