Fix query syntax which broke a couple functions' debug info (foreport from 1.4)
[lhc/web/wiklou.git] / includes / Title.php
index bdc57ca..4fb3dc2 100644 (file)
@@ -11,6 +11,13 @@ require_once( 'normal/UtfNormal.php' );
 $wgTitleInterwikiCache = array();
 define ( 'GAID_FOR_UPDATE', 1 );
 
+# Title::newFromTitle maintains a cache to avoid
+# expensive re-normalization of commonly used titles.
+# On a batch operation this can become a memory leak
+# if not bounded. After hitting this many titles,
+# reset the cache.
+define( 'MW_TITLECACHE_MAX', 1000 );
+
 /**
  * Title class
  * - Represents a title, which may contain an interwiki designation or namespace
@@ -52,7 +59,7 @@ class Title {
                $this->mInterwiki = $this->mUrlform =
                $this->mTextform = $this->mDbkeyform = '';
                $this->mArticleID = -1;
-               $this->mNamespace = 0;
+               $this->mNamespace = NS_MAIN;
                $this->mRestrictionsLoaded = false;
                $this->mRestrictions = array();
                # Dont change the following, NS_MAIN is hardcoded in several place
@@ -91,10 +98,14 @@ class Title {
         * @static
         * @access public
         */
-       /* static */ function &newFromText( $text, $defaultNamespace = 0 ) {    
+       function &newFromText( $text, $defaultNamespace = NS_MAIN ) {   
                $fname = 'Title::newFromText';
                wfProfileIn( $fname );
                
+               if( is_object( $text ) ) {
+                       wfDebugDieBacktrace( 'Title::newFromText given an object' );
+               }
+               
                /**
                 * Wiki pages often contain multiple links to the same page.
                 * Title normalization and parsing can become expensive on
@@ -104,7 +115,7 @@ class Title {
                 * In theory these are value objects and won't get changed...
                 */
                static $titleCache = array();
-               if( $defaultNamespace == 0 && isset( $titleCache[$text] ) ) {
+               if( $defaultNamespace == NS_MAIN && isset( $titleCache[$text] ) ) {
                        wfProfileOut( $fname );
                        return $titleCache[$text];
                }
@@ -129,7 +140,11 @@ class Title {
                $t->mDefaultNamespace = $defaultNamespace;
 
                if( $t->secureAndSplit() ) {
-                       if( $defaultNamespace == 0 ) {
+                       if( $defaultNamespace == NS_MAIN ) {
+                               if( count( $titleCache ) >= MW_TITLECACHE_MAX ) {
+                                       # Avoid memory leaks on mass operations...
+                                       $titleCache = array();
+                               }
                                $titleCache[$text] =& $t;
                        }
                        wfProfileOut( $fname );
@@ -601,10 +616,9 @@ class Title {
         * @access public
         */
        function getFullURL( $query = '' ) {
-               global $wgContLang, $wgArticlePath, $wgServer, $wgScript;
+               global $wgContLang, $wgServer, $wgScript;
 
                if ( '' == $this->mInterwiki ) {
-                       $p = $wgArticlePath;
                        return $wgServer . $this->getLocalUrl( $query );
                } else {
                        $baseUrl = $this->getInterwikiLink( $this->mInterwiki );
@@ -614,6 +628,14 @@ class Title {
                                $namepace .= ':';
                        }
                        $url = str_replace( '$1', $namespace . $this->mUrlform, $baseUrl );
+                       if( $query != '' ) {
+                               if( false === strpos( $url, '?' ) ) {
+                                       $url .= '?';
+                               } else {
+                                       $url .= '&';
+                               }
+                               $url .= $query;
+                       }
                        if ( '' != $this->mFragment ) {
                                $url .= '#' . $this->mFragment;
                        }
@@ -621,6 +643,35 @@ class Title {
                }
        }
 
+       /** 
+        * Get a relative directory for putting an HTML version of this article into
+        */
+       function getHashedDirectory() {
+               $dbkey = $this->getPrefixedDBkey();
+               if ( strlen( $dbkey ) < 2 ) {
+                       $dbkey = sprintf( "%2s", $dbkey );
+               }
+               $dir = '';
+               for ( $i=0; $i<=1; $i++ ) {
+                       if ( $i ) {
+                               $dir .= '/';
+                       }
+                       if ( ord( $dbkey{$i} ) < 128 && ord( $dbkey{$i} ) > 32 ) {
+                               $dir .= strtolower( $dbkey{$i} );
+                       } else {
+                               $dir .= sprintf( "%02X", ord( $dbkey{$i} ) );
+                       }
+               }
+               return $dir;
+       }
+       
+       function getHashedFilename() {
+               $dbkey = $this->getPrefixedDBkey();
+               $dir = $this->getHashedDirectory();
+               $friendlyName = strtr( $dbkey, '/\\:*?"<>|', '_________' );
+               return "$dir/$friendlyName.html";       
+       }
+       
        /**
         * Get a URL with no fragment or server name
         * @param string $query an optional query string; if not specified,
@@ -629,16 +680,29 @@ class Title {
         * @access public
         */
        function getLocalURL( $query = '' ) {
-               global $wgLang, $wgArticlePath, $wgScript;
+               global $wgLang, $wgArticlePath, $wgScript, $wgMakeDumpLinks;
                
                if ( $this->isExternal() ) {
                        return $this->getFullURL();
                }
-
+               
                $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
-               if ( $query == '' ) {
+               if ( $wgMakeDumpLinks ) {
+                       $url = str_replace( '$1', wfUrlencode( $this->getHashedFilename() ), $wgArticlePath );
+               } elseif ( $query == '' ) {
                        $url = str_replace( '$1', $dbkey, $wgArticlePath );
                } else {
+                       if( preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches ) ) {
+                               global $wgActionPaths;
+                               $action = urldecode( $matches[2] );
+                               if( isset( $wgActionPaths[$action] ) ) {
+                                       $query = $matches[1];
+                                       if( isset( $matches[4] ) ) $query .= $matches[4];
+                                       $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
+                                       if( $query != '' ) $url .= '?' . $query;
+                                       return $url;
+                               }
+                       }
                        if ( $query == '-' ) {
                                $query = '';
                        }
@@ -859,6 +923,15 @@ class Title {
                }
                return false;
        }
+       
+       /**
+        * Is this a talk page of some sort?
+        * @return bool
+        * @access public
+        */
+       function isTalkPage() {
+               return Namespace::isTalk( $this->getNamespace() );
+       }
 
        /**
         * Is this a .css or .js subpage of a user page?
@@ -1062,7 +1135,7 @@ class Title {
 
                # Clean up whitespace
                #
-               $t = preg_replace( '/[\\s_]+/', '_', $this->mDbkeyform );
+               $t = preg_replace( '/[ _]+/', '_', $this->mDbkeyform );
                $t = trim( $t, '_' );
 
                if ( '' == $t ) {
@@ -1070,8 +1143,7 @@ class Title {
                        return false;
                }
                
-               global $wgUseLatin1;
-               if( !$wgUseLatin1 && false !== strpos( $t, UTF8_REPLACEMENT ) ) {
+               if( false !== strpos( $t, UTF8_REPLACEMENT ) ) {
                        # Contained illegal UTF-8 sequences or forbidden Unicode chars.
                        wfProfileOut( $fname );
                        return false;
@@ -1171,6 +1243,7 @@ class Title {
                # We shouldn't need to query the DB for the size.
                #$maxSize = $dbr->textFieldSize( 'page', 'page_title' );
                if ( strlen( $r ) > 255 ) {
+                       wfProfileOut( $fname );
                        return false;
                }
 
@@ -1188,6 +1261,18 @@ class Title {
                        $t = $r;
                }
                
+               /**
+                * Can't make a link to a namespace alone...
+                * "empty" local links can only be self-links
+                * with a fragment identifier.
+                */
+               if( $t == '' &&
+                       $this->mInterwiki == '' &&
+                       $this->mNamespace != NS_MAIN ) {
+                       wfProfileOut( $fname );
+                       return false;
+               }
+               
                # Fill fields
                $this->mDbkeyform = $t;
                $this->mUrlform = wfUrlencode( $t );
@@ -1320,6 +1405,7 @@ class Title {
         * @access public
         */
        function moveTo( &$nt, $auth = true ) {
+               global $wgUser;
                if( !$this or !$nt ) {
                        return 'badtitletext';
                }
@@ -1386,6 +1472,7 @@ class Title {
                $u = new SearchUpdate( $newid, $this->getPrefixedDBkey(), '' );
                $u->doUpdate();
 
+               wfRunHooks( 'TitleMoveComplete', array(&$this, &$nt, &$wgUser, $oldid, $newid) );
                return true;
        }
        
@@ -1403,7 +1490,6 @@ class Title {
                $comment = wfMsgForContent( '1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
                
                $now = wfTimestampNow();
-               $won = wfInvertTimestamp( $now );
                $rand = wfRandom();
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
@@ -1430,36 +1516,24 @@ class Title {
 
                # Recreate the redirect, this time in the other direction.
                $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
-               $dbw->insert( 'revision', array(
-                       'rev_id' => $dbw->nextSequenceValue('rev_rev_id_seq'),
-                       'rev_comment' => $comment,
-                       'rev_user' => $wgUser->getID(),
-                       'rev_user_text' => $wgUser->getName(),
-                       'rev_timestamp' => $now,
-                       'inverse_timestamp' => $won ), $fname
-               );
-               $revid = $dbw->insertId();
-               $dbw->insert( 'text', array(
-                       'old_id' => $revid,
-                       'old_flags' => '',
-                       'old_text' => $redirectText,
-                       ), $fname
-               );
-               $dbw->insert( 'page', array(
-                       'page_id' => $dbw->nextSequenceValue('page_page_id_seq'),
-                       'page_namespace' => $this->getNamespace(),
-                       'page_title' => $this->getDBkey(),
-                       'page_touched' => $now,
-                       'page_is_redirect' => 1,
-                       'page_random' => $rand,
-                       'page_is_new' => 1,
-                       'page_latest' => $revid), $fname
-               );
-               $newid = $dbw->insertId();
+               $redirectArticle = new Article( $this );
+               $newid = $redirectArticle->insertOn( $dbw );
+               $redirectRevision = new Revision( array(
+                       'page'    => $newid, 
+                       'comment' => $comment,
+                       'text'    => $redirectText ) );
+               $revid = $redirectRevision->insertOn( $dbw );
+               $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
                $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
-               
-               RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
 
+               # Record in RC
+               // Replaced by a log entry
+               // RecentChange::notifyMoveOverRedirect( $now, $this, $nt, $wgUser, $comment );
+
+               # Log the move
+               $log = new LogPage( 'move' );
+               $log->addEntry( 'move_redir', $this, '', array(1 => $nt->getText()) );
+               
                # Swap links
                
                # Load titles and IDs
@@ -1497,7 +1571,7 @@ class Title {
                                $sql .= "($id, $oldid)";
                        }
 
-                       $dbw->query( $sql, DB_MASTER, $fname );
+                       $dbw->query( $sql, $fname );
                }
 
                # Now, we record the link from the redirect to the new title.
@@ -1533,7 +1607,6 @@ class Title {
                $oldid = $this->getArticleID();
                $dbw =& wfGetDB( DB_MASTER );
                $now = $dbw->timestamp();
-               $won = wfInvertTimestamp( wfTimestamp(TS_MW,$now) );
                wfSeedRandom();
                $rand = wfRandom();
 
@@ -1552,36 +1625,23 @@ class Title {
 
                # Insert redirect
                $redirectText = $wgMwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
-               $dbw->insert( 'revision', array(
-                       'rev_id' => $dbw->nextSequenceValue('rev_rev_id_seq'),
-                       'rev_comment' => $comment,
-                       'rev_user' => $wgUser->getID(),
-                       'rev_user_text' => $wgUser->getName(),
-                       'rev_timestamp' => $now,
-                       'inverse_timestamp' => $won ), $fname
-               );
-               $revid = $dbw->insertId();
-               $dbw->insert( 'text', array(
-                       'old_id' => $revid,
-                       'old_flags' => '',
-                       'old_text' => $redirectText
-                       ), $fname
-               );
-               $dbw->insert( 'page', array(
-                       'page_id' => $dbw->nextSequenceValue('page_page_id_seq'),
-                       'page_namespace' => $this->getNamespace(),
-                       'page_title' => $this->getDBkey(),
-                       'page_touched' => $now,
-                       'page_is_redirect' => 1,
-                       'page_random' => $rand,
-                       'page_is_new' => 1,
-                       'page_latest' => $revid), $fname
-               );
-               $newid = $dbw->insertId();
+               $redirectArticle = new Article( $this );
+               $newid = $redirectArticle->insertOn( $dbw );
+               $redirectRevision = new Revision( array(
+                       'page'    => $newid, 
+                       'comment' => $comment,
+                       'text'    => $redirectText ) );
+               $revid = $redirectRevision->insertOn( $dbw );
+               $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
                $wgLinkCache->clearLink( $this->getPrefixedDBkey() );
 
                # Record in RC
-               RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
+               // Replaced by a log entry
+               // RecentChange::notifyMoveToNew( $now, $this, $nt, $wgUser, $comment );
+
+               # Log the move
+               $log = new LogPage( 'move' );
+               $log->addEntry( 'move', $this, '', array(1 => $nt->getText()) );
 
                # Purge squid and linkscc as per article creation
                Article::onArticleCreate( $nt );
@@ -1674,44 +1734,16 @@ class Title {
                
                $fname = 'Title::createRedirect';
                $dbw =& wfGetDB( DB_MASTER );
-               $now = wfTimestampNow();
-               $won = wfInvertTimestamp( $now );
-               
-               $seqVal = $dbw->nextSequenceValue( 'page_page_id_seq' );
-               $dbw->insert( 'page', array(
-                       'page_id' => $seqVal,
-                       'page_namespace' => $this->getNamespace(),
-                       'page_title' => $this->getDBkey(),
-                       'page_touched' => $now,
-                       'page_is_redirect' => 1,
-                       'page_is_new' => 1,
-                       'page_latest' => NULL,
-               ), $fname );
-               $newid = $dbw->insertId();
-
-               $seqVal = $dbw->nextSequenceValue( 'text_old_id_seq' );
-               $dbw->insert( 'text', array(
-                       'old_id' => $seqVal,
-                       'old_flags' => '',
-                       'old_text' => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n"
-               ), $fname );
-               $revisionId = $dbw->insertId();
-               
-               $dbw->insert( 'revision', array(
-                       'rev_id' => $seqVal,
-                       'rev_page' => $newid,
-                       'rev_comment' => $comment,
-                       'rev_user' => $wgUser->getID(),
-                       'rev_user_text' => $wgUser->getName(),
-                       'rev_timestamp' => $now,
-                       'inverse_timestamp' => $won,
-               ), $fname );
                
-               $dbw->update( 'page',
-                       /* SET */   array( 'page_latest' => $revisionId ),
-                       /* WHERE */ array( 'page_id' => $newid ),
-                       $fname );
-               $this->resetArticleID( $newid );
+               $article = new Article( $this );
+               $newid = $article->insertOn( $dbw );
+               $revision = new Revision( array(
+                       'page'      => $newid,
+                       'comment'   => $comment,
+                       'text'      => "#REDIRECT [[" . $dest->getPrefixedText() . "]]\n",
+                       ) );
+               $revisionId = $revision->insertOn( $dbw );
+               $article->updateRevisionOn( $dbw, $revision, 0 );
                
                # Link table
                if ( $dest->getArticleID() ) {
@@ -1846,6 +1878,18 @@ class Title {
                        'rev_page=' . IntVal( $this->getArticleId() ) .
                        ' AND rev_id>' . IntVal( $revision ) . ' ORDER BY rev_id' );
        }
+       
+       /**
+        * Compare with another title.
+        *
+        * @param Title $title
+        * @return bool
+        */
+       function equals( &$title ) {
+               return $this->getInterwiki() == $title->getInterwiki()
+                       && $this->getNamespace() == $title->getNamespace()
+                       && $this->getDbkey() == $title->getDbkey();
+       }
 
 }
 ?>