Fix for compatibility with short_open_tag = Off
[lhc/web/wiklou.git] / includes / SpecialMovepage.php
index 2a67b95..3a0b32d 100644 (file)
@@ -1,4 +1,5 @@
-<?
+<?php
+include_once( "LinksUpdate.php" );
 
 function wfSpecialMovepage()
 {
@@ -100,11 +101,16 @@ class MovePageForm {
        {
                global $wgOut, $wgUser, $wgLang;
                global $wpNewTitle, $wpOldTitle, $wpMovetalk, $target;
-               global $wgDeferredUpdateList;
+               global $wgDeferredUpdateList, $wgMessageCache;
+               global  $wgUseSquid, $wgInternalServer;
                $fname = "MovePageForm::doSubmit";
 
                $this->ot = Title::newFromText( $wpOldTitle );
                $this->nt = Title::newFromText( $wpNewTitle );
+               if( !$this->ot or !$this->nt ) {
+                       $this->showForm( wfMsg( "badtitletext" ) );
+                       return;
+               }
                $this->ons = $this->ot->getNamespace();
                $this->nns = $this->nt->getNamespace();
                $this->odt = wfStrencode( $this->ot->getDBkey() );
@@ -117,16 +123,20 @@ class MovePageForm {
                $this->oldid = $this->ot->getArticleID();
                $this->newid = $this->nt->getArticleID();
 
-               if ( strlen( trim( $this->ndt ) ) < 2 ) {
+               if ( strlen( trim( $this->ndt ) ) < 1 ) {
                        $this->showForm( wfMsg( "articleexists" ) );
                        return;
                }
                if ( ( ! Namespace::isMovable( $this->ons ) ) ||
                         ( "" == $this->odt ) ||
                         ( "" != $this->ot->getInterwiki() ) ||
+                        ( !$this->ot->userCanEdit() ) ||
+                        ( !$this->oldid ) ||
                     ( ! Namespace::isMovable( $nns ) ) ||
                         ( "" == $this->ndt ) ||
-                        ( "" != $this->nt->getInterwiki() ) ) {
+                        ( "" != $this->nt->getInterwiki() ) ||
+                        ( !$this->nt->userCanEdit() ) || 
+                        ( $this->ons == NS_MEDIAWIKI && $wgMessageCache->isCacheable( $this->odt ) ) ) {
                        $this->showForm( wfMsg( "badarticleerror" ) );
                        return;
                }
@@ -150,6 +160,26 @@ class MovePageForm {
                $u->doUpdate();
                $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
                $u->doUpdate();
+               
+               # Squid purging
+               if ( $wgUseSquid ) {
+                       /* this needs to be done after LinksUpdate */
+                       $urlArr = Array(                                
+                               # purge new title
+                               $wgInternalServer.wfLocalUrl( $this->nt->getPrefixedURL()),
+                               # purge old title
+                               $wgInternalServer.wfLocalUrl( $this->ot->getPrefixedURL())
+                       );                      
+                       wfPurgeSquidServers($urlArr);   
+                       # purge pages linking to new title
+                       $u = new SquidUpdate($this->nt);
+                       array_push( $wgDeferredUpdateList, $u );
+                       # purge pages linking to old title
+                       $u = new SquidUpdate($this->ot);
+                       array_push( $wgDeferredUpdateList, $u );
+                       
+                       
+               }
 
                # Move talk page if (1) the checkbox says to, (2) the source
                # and target namespaces are identical, (3) the namespaces are not
@@ -158,13 +188,10 @@ class MovePageForm {
                if ( ( 1 == $wpMovetalk ) &&
                         ( ! Namespace::isTalk( $this->ons ) ) &&
                         ( $this->ons == $this->nns ) ) {
-
+                       
                        $this->ons = $this->nns = Namespace::getTalk( $this->ons );
-
-                       $this->ot = Title::newFromText( Title::makeName(
-                         $this->ons, $wpOldTitle ) );
-                       $this->nt = Title::newFromText( Title::makeName(
-                         $this->nns, $wpNewTitle ) );
+                       $this->ot = Title::makeTitle( $this->ons, $this->ot->getDBkey() );
+                       $this->nt = Title::makeTitle( $this->nns, $this->nt->getDBkey() );
 
                        # odt, ndt, ofx, nfx remain the same
 
@@ -190,6 +217,26 @@ class MovePageForm {
                                $u->doUpdate();
                                $u = new SearchUpdate( $this->newid, $this->ot->getPrefixedDBkey(), "" );
                                $u->doUpdate();
+                               
+                               # Squid purging
+                               if ( $wgUseSquid ) {
+                                       /* this needs to be done after LinksUpdate */
+                                       $urlArr = Array(                                
+                                               # purge new title
+                                               $wgInternalServer.wfLocalUrl( $this->nt->getPrefixedURL()),
+                                               # purge old title
+                                               $wgInternalServer.wfLocalUrl( $this->ot->getPrefixedURL())
+                                       );                      
+                                       wfPurgeSquidServers($urlArr);   
+                                       # purge pages linking to new title
+                                       $u = new SquidUpdate($this->nt);
+                                       array_push( $wgDeferredUpdateList, $u );
+                                       # purge pages linking to old title
+                                       $u = new SquidUpdate($this->ot);
+                                       array_push( $wgDeferredUpdateList, $u );
+
+
+                               }
                        }
                }
                $success = wfLocalUrl( $wgLang->specialPage( "Movepage" ),
@@ -211,8 +258,7 @@ class MovePageForm {
                $fields = array( "oldtitle", "newtitle" );
                wfCleanFormFields( $fields );
 
-               $text = str_replace( "$1", $oldtitle, wfMsg( "pagemovedtext" ) );
-               $text = str_replace( "$2", $newtitle, $text );
+               $text = wfMsg( "pagemovedtext", $oldtitle, $newtitle );
                $wgOut->addWikiText( $text );
 
                if ( 1 == $talkmoved ) {
@@ -235,7 +281,7 @@ class MovePageForm {
 
                $sql = "SELECT cur_is_redirect,cur_text FROM cur " .
                  "WHERE cur_id={$this->newid}";
-               $res = wfQuery( $sql, $fname );
+               $res = wfQuery( $sql, DB_READ, $fname );
                $obj = wfFetchObject( $res );
 
                if ( 0 == $obj->cur_is_redirect ) { return false; }
@@ -249,7 +295,7 @@ class MovePageForm {
                }
                $sql = "SELECT old_id FROM old WHERE old_namespace={$this->nns} " .
                  "AND old_title='{$this->ndt}'";
-               $res = wfQuery( $sql, $fname );
+               $res = wfQuery( $sql, DB_READ, $fname );
                if ( 0 != wfNumRows( $res ) ) { return false; }
 
                return true;
@@ -260,141 +306,121 @@ class MovePageForm {
 
        function moveOverExistingRedirect()
        {
-               global $wgUser;
+               global $wgUser, $wgLinkCache;
                $fname = "MovePageForm::moveOverExistingRedirect";
                $mt = wfMsg( "movedto" );
 
+               # Change the name of the target page:
         $now = wfTimestampNow();
                $sql = "UPDATE cur SET cur_touched='{$now}'," .
                  "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
                  "WHERE cur_id={$this->oldid}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
+               $wgLinkCache->clearLink( $this->nft );
 
-               $sql = "UPDATE cur SET cur_touched='{$now}'," .
+               # Repurpose the old redirect. We don't save it to history since
+               # by definition if we've got here it's rather uninteresting.
+               $sql = "UPDATE cur SET cur_touched='{$now}',cur_timestamp='{$now}'," .
                  "cur_namespace={$this->ons},cur_title='{$this->odt}'," .
                  "cur_text='#REDIRECT [[{$this->nft}]]\n',cur_comment='" .
                  "{$mt} \\\"{$this->nft}\\\"',cur_user='" .  $wgUser->getID() .
                  "',cur_minor_edit=0,cur_counter=0,cur_restrictions=''," .
                  "cur_user_text='" . wfStrencode( $wgUser->getName() ) . "'," .
                  "cur_is_redirect=1,cur_is_new=0 WHERE cur_id={$this->newid}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
+               $wgLinkCache->clearLink( $this->oft );
 
+               # Fix the redundant names for the past revisions of the target page.
+               # The redirect should have no old revisions.
                $sql = "UPDATE old SET " .
                  "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
                  "old_namespace={$this->ons} AND old_title='{$this->odt}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
                
-               $sql = "UPDATE recentchanges SET ".
-                       "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
-                       "rc_cur_id={$this->oldid}";
-        wfQuery( $sql, $fname );
-
-               $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
-                       rc_comment,rc_user,rc_user_text,rc_timestamp,
-                       rc_cur_time,rc_cur_id,rc_new)
-                       VALUES ({$this->ons},'{$this->odt}'," .
-                 "'{$mt} \\\"{$this->nft}\\\"','" .
-                 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
-          "','{$now}','{$now}',{$this->newid},1)";
-        wfQuery( $sql, $fname );
+               RecentChange::notifyMove( $now, $this->ot, $this->nt, $wgUser, $mt );
 
                # The only link from here should be the old redirect
 
                $sql = "DELETE FROM links WHERE l_from='{$this->nft}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                # Swap links.  Using MAXINT as a temp; if there's ever an article
                # with id 4294967295, this will fail, but I think that's pretty safe
 
                $sql = "UPDATE links SET l_to=4294967295 WHERE l_to={$this->oldid}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "UPDATE links SET l_to={$this->oldid} WHERE l_to={$this->newid}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to=4294967295";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                # Note: the insert below must be after the updates above!
 
                $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
        }
 
        # Move page to non-existing title.
 
        function moveToNewTitle()
        {
-               global $wgUser;
+               global $wgUser, $wgLinkCache;
                $fname = "MovePageForm::moveToNewTitle";
                $mt = wfMsg( "movedto" );
 
+               $now = wfTimestampNow();
+               $won = wfInvertTimestamp( $now );
                $sql = "UPDATE cur SET cur_touched='{$now}'," .
                  "cur_namespace={$this->nns},cur_title='{$this->ndt}' " .
                  "WHERE cur_id={$this->oldid}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
+               $wgLinkCache->clearLink( $this->nft );
 
-               $now = wfTimestampNow();
-               $won = wfInvertTimestamp( $now );
+               $comment = "{$mt} \"{$this->nft}\"";
+               $encComment = wfStrencode( $comment );
                $common = "{$this->ons},'{$this->odt}'," .
-                 "'{$mt} \\\"{$this->nft}\\\"','" .
-                 $wgUser->getID() . "','" . wfStrencode( $wgUser->getName() ) .
-          "','{$now}'";
+                 "'$encComment','" .$wgUser->getID() . "','" . 
+                 wfStrencode( $wgUser->getName() ) ."','{$now}'";
                $sql = "INSERT INTO cur (cur_namespace,cur_title," .
                  "cur_comment,cur_user,cur_user_text,cur_timestamp,inverse_timestamp," .
                  "cur_touched,cur_text,cur_is_redirect,cur_is_new) " .
                  "VALUES ({$common},'{$won}','{$now}','#REDIRECT [[{$this->nft}]]\n',1,1)";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
                $this->newid = wfInsertId();
+               $wgLinkCache->clearLink( $this->oft );
 
                $sql = "UPDATE old SET " .
                  "old_namespace={$this->nns},old_title='{$this->ndt}' WHERE " .
                  "old_namespace={$this->ons} AND old_title='{$this->odt}'";
-               wfQuery( $sql, $fname );
-
-        $sql = "UPDATE recentchanges SET ".
-                       "rc_namespace={$this->nns}, rc_title='{$this->ndt}' WHERE ".
-                       "rc_namespace={$this->ons} AND rc_title='{$this->odt}'";
-        wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
-               $sql = "INSERT INTO recentchanges (rc_namespace,rc_title,
-                       rc_comment,rc_user,rc_user_text,rc_timestamp,
-                       rc_cur_time,rc_cur_id,rc_new)
-                       VALUES ({$common},'{$now}',{$this->newid},1)";
-        wfQuery( $sql, $fname );
+               RecentChange::notifyMove( $now, $this->ot, $this->nt, $wgUser, $comment );
+               Article::onArticleCreate( $this->nt );
 
                $sql = "UPDATE links SET l_from='{$this->nft}' WHERE l_from='{$this->oft}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "UPDATE links SET l_to={$this->newid} WHERE l_to={$this->oldid}";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$this->oft}',{$this->oldid})";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
 
                # Non-existent target may have had broken links to it; these must
                # now be removed and made into good links.
-
-               $sql = "SELECT bl_from FROM brokenlinks WHERE bl_to='{$this->nft}'";
-               $res = wfQuery( $sql, $fname );
-
-               while ( $rec = wfFetchObject( $res ) ) {
-                       $lid = $rec->bl_from;
-                       $lt = wfStrencode( Article::nameOf( $lid ) );
-                       $sql = "INSERT INTO links (l_from,l_to) VALUES ('{$lt}',$this->oldid)";
-                       wfQuery( $sql, $fname );
-               }
-               $sql = "DELETE FROM brokenlinks WHERE bl_to='{$this->nft}'";
-               wfQuery( $sql, $fname );
+               $update = new LinksUpdate( $this->oldid, $this->nft );
+               $update->fixBrokenLinks();
 
                $sql = "UPDATE imagelinks SET il_from='{$this->nft}' WHERE il_from='{$this->oft}'";
-               wfQuery( $sql, $fname );
+               wfQuery( $sql, DB_WRITE, $fname );
        }
 
        function updateWatchlists()
@@ -407,17 +433,7 @@ class MovePageForm {
                if( $oldnamespace == $newnamespace and $oldtitle == $newtitle )
                        return;
 
-               $sql = "SELECT wl_user FROM watchlist
-                       WHERE wl_namespace={$oldnamespace} AND wl_title='{$oldtitle}'";
-               $res = wfQuery( $sql, $fname );
-               if( $s = wfFetchObject( $res ) ) {
-                       $sql = "REPLACE INTO watchlist (wl_user,wl_namespace,wl_title)
-                               VALUES ({$s->wl_user},{$newnamespace},'{$newtitle}')";
-                       while( $s = wfFetchObject( $res ) ) {
-                               $sql .= ",({$s->wl_user},{$newnamespace},'{$newtitle}')";
-                       }
-                       wfQuery( $sql, $fname );
-               }
+               WatchedItem::duplicateEntries( $this->ot, $this->nt );
        }
 
 }