Redirect-related bugfixes/features:
[lhc/web/wiklou.git] / includes / Title.php
index 88118fd..eb83b43 100644 (file)
@@ -295,10 +295,20 @@ class Title {
         * Extract a redirect destination from a string and return the
         * Title, or null if the text doesn't contain a valid redirect
         *
-        * @param $text \type{String} Text with possible redirect
+        * @param $text \type{\string} Text with possible redirect
+        * @param $getAllTargets \type{\bool} Should we get an array of every target or just the final one?
+        * @param $noRecurse \type{\bool} This will prevent any and all recursion, only getting the very next target
+        *      This is mainly meant for Article::insertRedirect so that the redirect table still works properly
+        *      (makes double redirect and broken redirect reports display accurate results).
         * @return \type{Title} The corresponding Title
-        */
-       public static function newFromRedirect( $text ) {
+        * @return \type{\array} Array of redirect targets (Title objects), with the destination being last
+        */
+       public static function newFromRedirect( $text, $getAllTargets = false, $noRecurse = false ) {
+               global $wgMaxRedirects;
+               // are redirects disabled?
+               // Note that we should get a Title object if possible if $noRecurse is true so that the redirect table functions properly
+               if( !$noRecurse && $wgMaxRedirects < 1 )
+                       return null;
                $redir = MagicWord::get( 'redirect' );
                $text = trim($text);
                if( $redir->matchStartAndRemove( $text ) ) {
@@ -316,11 +326,36 @@ class Title {
                                        $m[1] = urldecode( ltrim( $m[1], ':' ) );
                                }
                                $title = Title::newFromText( $m[1] );
-                               // Redirects to some special pages are not permitted
-                               if( $title instanceof Title 
-                                               && !$title->isSpecial( 'Userlogout' )
-                                               && !$title->isSpecial( 'Filepath' ) ) 
-                               {
+                               // If the initial title is a redirect to bad special pages or is invalid, quit early
+                               if( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
+                                       return null;
+                               }
+                               // If $noRecurse is true, simply return here
+                               if( $noRecurse ) {
+                                       return $title;
+                               }
+                               // recursive check to follow double redirects
+                               $recurse = $wgMaxRedirects;
+                               $targets = array();
+                               while( --$recurse >= 0 ) {
+                                       // Redirects to some special pages are not permitted
+                                       if( $title instanceof Title && $title->isValidRedirectTarget() ) {
+                                               $targets[] = $title;
+                                               if( $title->isRedirect() ) {
+                                                       $article = new Article( $title, 0 );
+                                                       $title = $article->getRedirectTarget();
+                                               } else {
+                                                       break;
+                                               }
+                                       } else {
+                                               break;
+                                       }
+                               }
+                               if( $getAllTargets ) {
+                                       // return every target or null if no targets due to invalid title or whatever
+                                       return ( $targets === array() ) ? null : $targets;
+                               } else {
+                                       // return the final destination -- invalid titles are checked earlier
                                        return $title;
                                }
                        }
@@ -383,7 +418,7 @@ class Title {
 
                $t = preg_replace( "/\\s+/", ' ', $t );
 
-               if ( $ns == NS_IMAGE ) {
+               if ( $ns == NS_FILE ) {
                        $t = preg_replace( "/ (png|gif|jpg|jpeg|ogg)$/", "", $t );
                }
                return trim( $t );
@@ -451,13 +486,13 @@ class Title {
         * Escape a text fragment, say from a link, for a URL
         */
        static function escapeFragmentForURL( $fragment ) {
-               $fragment = str_replace( ' ', '_', $fragment );
-               $fragment = urlencode( Sanitizer::decodeCharReferences( $fragment ) );
-               $replaceArray = array(
-                       '%3A' => ':',
-                       '%' => '.'
-               );
-               return strtr( $fragment, $replaceArray );
+               global $wgEnforceHtmlIds;
+               # Note that we don't urlencode the fragment.  urlencoded Unicode
+               # fragments appear not to work in IE (at least up to 7) or in at least
+               # one version of Opera 9.x.  The W3C validator, for one, doesn't seem
+               # to care if they aren't encoded.
+               return Sanitizer::escapeId( $fragment,
+                       $wgEnforceHtmlIds ? 'noninitial' : 'xml' );
        }
 
 #----------------------------------------------------------------------------
@@ -764,7 +799,9 @@ class Title {
                                                $query = $matches[1];
                                                if( isset( $matches[4] ) ) $query .= $matches[4];
                                                $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
-                                               if( $query != '' ) $url .= '?' . $query;
+                                               if( $query != '' ) {
+                                                       $url = wfAppendQuery( $url, $query );
+                                               }
                                        }
                                }
                                if ( $url === false ) {
@@ -1100,61 +1137,55 @@ class Title {
                $errors = array();
 
                // Use getUserPermissionsErrors instead
-               if ( !wfRunHooks( 'userCan', array( &$this, &$user, $action, &$result ) ) ) {
+               if( !wfRunHooks( 'userCan', array( &$this, &$user, $action, &$result ) ) ) {
                        wfProfileOut( __METHOD__ );
                        return $result ? array() : array( array( 'badaccess-group0' ) );
                }
 
-               if (!wfRunHooks( 'getUserPermissionsErrors', array( &$this, &$user, $action, &$result ) ) ) {
-                       if ($result != array() && is_array($result) && !is_array($result[0]))
+               if( !wfRunHooks( 'getUserPermissionsErrors', array(&$this,&$user,$action,&$result) ) ) {
+                       if( is_array($result) && count($result) && !is_array($result[0]) )
                                $errors[] = $result; # A single array representing an error
-                       else if (is_array($result) && is_array($result[0]))
+                       else if( is_array($result) && is_array($result[0]) )
                                $errors = array_merge( $errors, $result ); # A nested array representing multiple errors
-                       else if ($result != '' && $result != null && $result !== true && $result !== false)
+                       else if( $result !== '' && is_string($result) )
                                $errors[] = array($result); # A string representing a message-id
-                       else if ($result === false )
+                       else if$result === false )
                                $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
                }
-               if ($doExpensiveQueries && !wfRunHooks( 'getUserPermissionsErrorsExpensive', array( &$this, &$user, $action, &$result ) ) ) {
-                       if ($result != array() && is_array($result) && !is_array($result[0]))
+               if( $doExpensiveQueries && !wfRunHooks( 'getUserPermissionsErrorsExpensive', array(&$this,&$user,$action,&$result) ) ) {
+                       if( is_array($result) && count($result) && !is_array($result[0]) )
                                $errors[] = $result; # A single array representing an error
-                       else if (is_array($result) && is_array($result[0]))
+                       else if( is_array($result) && is_array($result[0]) )
                                $errors = array_merge( $errors, $result ); # A nested array representing multiple errors
-                       else if ($result != '' && $result != null && $result !== true && $result !== false)
+                       else if( $result !== '' && is_string($result) )
                                $errors[] = array($result); # A string representing a message-id
-                       else if ($result === false )
+                       else if$result === false )
                                $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
                }
                
+               // TODO: document
                $specialOKActions = array( 'createaccount', 'execute' );
                if( NS_SPECIAL == $this->mNamespace && !in_array( $action, $specialOKActions) ) {
                        $errors[] = array('ns-specialprotected');
                }
 
-               if ( $this->isNamespaceProtected() ) {
-                       $ns = $this->getNamespace() == NS_MAIN
-                               ? wfMsg( 'nstab-main' )
-                               : $this->getNsText();
-                       $errors[] = (NS_MEDIAWIKI == $this->mNamespace
-                               ? array('protectedinterface')
-                               : array( 'namespaceprotected',  $ns ) );
-               }
-
-               if( $this->mDbkeyform == '_' ) {
-                       # FIXME: Is this necessary? Shouldn't be allowed anyway...
-                       $errors[] = array('badaccess-group0');
+               if( $this->isNamespaceProtected() ) {
+                       $ns = $this->getNamespace() == NS_MAIN ?
+                               wfMsg( 'nstab-main' ) : $this->getNsText();
+                       $errors[] = NS_MEDIAWIKI == $this->mNamespace ?
+                               array('protectedinterface') : array( 'namespaceprotected',  $ns );
                }
 
                # protect css/js subpages of user pages
                # XXX: this might be better using restrictions
                # XXX: Find a way to work around the php bug that prevents using $this->userCanEditCssJsSubpage() from working
-               if( $this->isCssJsSubpage()
-                       && !$user->isAllowed('editusercssjs')
-                       && !preg_match('/^'.preg_quote($user->getName(), '/').'\//', $this->mTextform) ) {
+               if( $this->isCssJsSubpage() && !$user->isAllowed('editusercssjs')
+                       && !preg_match('/^'.preg_quote($user->getName(), '/').'\//', $this->mTextform) )
+               {
                        $errors[] = array('customcssjsprotected');
                }
 
-               if ( $doExpensiveQueries && !$this->isCssJsSubpage() ) {
+               if( $doExpensiveQueries && !$this->isCssJsSubpage() ) {
                        # We /could/ use the protection level on the source page, but it's fairly ugly
                        #  as we have to establish a precedence hierarchy for pages included by multiple
                        #  cascade-protected pages. So just restrict it to people with 'protect' permission,
@@ -1179,18 +1210,18 @@ class Title {
 
                foreach( $this->getRestrictions($action) as $right ) {
                        // Backwards compatibility, rewrite sysop -> protect
-                       if ( $right == 'sysop' ) {
+                       if( $right == 'sysop' ) {
                                $right = 'protect';
                        }
                        if( '' != $right && !$user->isAllowed( $right ) ) {
-                               //Users with 'editprotected' permission can edit protected pages
+                               // Users with 'editprotected' permission can edit protected pages
                                if( $action=='edit' && $user->isAllowed( 'editprotected' ) ) {
-                                       //Users with 'editprotected' permission cannot edit protected pages
-                                       //with cascading option turned on.
-                                       if($this->mCascadeRestriction) {
+                                       // Users with 'editprotected' permission cannot edit protected pages
+                                       // with cascading option turned on.
+                                       if( $this->mCascadeRestriction ) {
                                                $errors[] = array( 'protectedpagetext', $right );
                                        } else {
-                                               //Nothing, user can edit!
+                                               // Nothing, user can edit!
                                        }
                                } else {
                                        $errors[] = array( 'protectedpagetext', $right );
@@ -1198,78 +1229,75 @@ class Title {
                        }
                }
 
-               if ($action == 'protect') {
-                       if ($this->getUserPermissionsErrors('edit', $user) != array()) {
+               if( $action == 'protect' ) {
+                       if( $this->getUserPermissionsErrors('edit', $user) != array() ) {
                                $errors[] = array( 'protect-cantedit' ); // If they can't edit, they shouldn't protect.
                        }
                }
 
-               if ($action == 'create') {
+               if( $action == 'create' ) {
                        $title_protection = $this->getTitleProtection();
+                       if( is_array($title_protection) ) {
+                               extract($title_protection); // is this extract() really needed?
 
-                       if (is_array($title_protection)) {
-                               extract($title_protection);
-
-                               if ($pt_create_perm == 'sysop')
-                                       $pt_create_perm = 'protect';
-
-                               if ($pt_create_perm == '' || !$user->isAllowed($pt_create_perm)) {
-                                       $errors[] = array ( 'titleprotected', User::whoIs($pt_user), $pt_reason );
+                               if( $pt_create_perm == 'sysop' ) {
+                                       $pt_create_perm = 'protect'; // B/C
+                               }
+                               if( $pt_create_perm == '' || !$user->isAllowed($pt_create_perm) ) {
+                                       $errors[] = array( 'titleprotected', User::whoIs($pt_user), $pt_reason );
                                }
                        }
 
-                       if( (  $this->isTalkPage() && !$user->isAllowed( 'createtalk' ) ) ||
-                               ( !$this->isTalkPage() && !$user->isAllowed( 'createpage' ) ) ) {
+                       if( ( $this->isTalkPage() && !$user->isAllowed( 'createtalk' ) ) ||
+                               ( !$this->isTalkPage() && !$user->isAllowed( 'createpage' ) ) ) 
+                       {
                                $errors[] = $user->isAnon() ? array ('nocreatetext') : array ('nocreate-loggedin');
                        }
-
-               } elseif ( $action == 'move' ) {
-                       if ( !$user->isAllowed( 'move' ) ) {
+               } elseif( $action == 'move' ) {
+                       if( !$user->isAllowed( 'move' ) ) {
                                // User can't move anything
                                $errors[] = $user->isAnon() ? array ( 'movenologintext' ) : array ('movenotallowed');
-                       } elseif ( !$user->isAllowed( 'move-rootuserpages' ) 
+                       } elseif( !$user->isAllowed( 'move-rootuserpages' ) 
                                        && $this->getNamespace() == NS_USER && !$this->isSubpage() ) 
                        {
                                // Show user page-specific message only if the user can move other pages
                                $errors[] = array( 'cant-move-user-page' );
                        }
-
+                       // Check if user is allowed to move files if it's a file
+                       if( $this->getNamespace() == NS_FILE && !$user->isAllowed( 'movefile' ) ) {
+                               $errors[] = array( 'movenotallowedfile' );
+                       }
                        // Check for immobile pages
-                       if ( !MWNamespace::isMovable( $this->getNamespace() ) ) {
+                       if( !MWNamespace::isMovable( $this->getNamespace() ) ) {
                                // Specific message for this case
                                $errors[] = array( 'immobile-source-namespace', $this->getNsText() );
-                       } elseif ( !$this->isMovable() ) {
+                       } elseif( !$this->isMovable() ) {
                                // Less specific message for rarer cases
                                $errors[] = array( 'immobile-page' );
                        }
-
-               } elseif ( $action == 'move-target' ) {
-                       if ( !$user->isAllowed( 'move' ) ) {
+               } elseif( $action == 'move-target' ) {
+                       if( !$user->isAllowed( 'move' ) ) {
                                // User can't move anything
                                $errors[] = $user->isAnon() ? array ( 'movenologintext' ) : array ('movenotallowed');
-                       } elseif ( !$user->isAllowed( 'move-rootuserpages' ) 
-                                       && $this->getNamespace() == NS_USER && !$this->isSubpage() ) 
+                       } elseif( !$user->isAllowed( 'move-rootuserpages' ) 
+                               && $this->getNamespace() == NS_USER && !$this->isSubpage() ) 
                        {
                                // Show user page-specific message only if the user can move other pages
                                $errors[] = array( 'cant-move-to-user-page' );
                        }
-                       if ( !MWNamespace::isMovable( $this->getNamespace() ) ) {
+                       if( !MWNamespace::isMovable( $this->getNamespace() ) ) {
                                $errors[] = array( 'immobile-target-namespace', $this->getNsText() );
-                       } elseif ( !$this->isMovable() ) {
+                       } elseif( !$this->isMovable() ) {
                                $errors[] = array( 'immobile-target-page' );
                        }
-
-               } elseif ( !$user->isAllowed( $action ) ) {
+               } elseif( !$user->isAllowed( $action ) ) {
                        $return = null;
                        $groups = array_map( array( 'User', 'makeGroupLinkWiki' ),
                                User::getGroupsWithPermission( $action ) );
-                       if ( $groups ) {
+                       if( $groups ) {
                                $return = array( 'badaccess-groups',
-                                       array(
-                                               implode( ', ', $groups ),
-                                               count( $groups ) ) );
-                       }
-                       else {
+                                       array( implode( ', ', $groups ), count( $groups ) ) );
+                       } else {
                                $return = array( "badaccess-group0" );
                        }
                        $errors[] = $return;
@@ -1325,7 +1353,7 @@ class Title {
 
                $expiry_description = '';
                if ( $encodedExpiry != 'infinity' ) {
-                       $expiry_description = ' (' . wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry ) ).')';
+                       $expiry_description = ' (' . wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry ) , $wgContLang->date( $expiry ) , $wgContLang->time( $expiry ) ).')';
                }
                else {
                        $expiry_description .= ' (' . wfMsgForContent( 'protect-expiry-indefinite' ).')';
@@ -1626,7 +1654,7 @@ class Title {
 
                $dbr = wfGetDB( DB_SLAVE );
 
-               if ( $this->getNamespace() == NS_IMAGE ) {
+               if ( $this->getNamespace() == NS_FILE ) {
                        $tables = array ('imagelinks', 'page_restrictions');
                        $where_clauses = array(
                                'il_to' => $this->getDBkey(),
@@ -1863,7 +1891,7 @@ class Title {
                        $dbr = wfGetDB( DB_SLAVE );
                        $n = $dbr->selectField( 'archive', 'COUNT(*)', array( 'ar_namespace' => $this->getNamespace(),
                                'ar_title' => $this->getDBkey() ), $fname );
-                       if( $this->getNamespace() == NS_IMAGE ) {
+                       if( $this->getNamespace() == NS_FILE ) {
                                $n += $dbr->selectField( 'filearchive', 'COUNT(*)',
                                        array( 'fa_name' => $this->getDBkey() ), $fname );
                        }
@@ -1973,7 +2001,6 @@ class Title {
         * @return \type{\bool} true if the update succeded
         */
        public function invalidateCache() {
-               global $wgUseFileCache;
                if( wfReadOnly() ) {
                        return;
                }
@@ -1983,10 +2010,7 @@ class Title {
                        $this->pageCond(), 
                        __METHOD__
                );
-               if( $wgUseFileCache) {
-                       $cache = new HTMLFileCache( $this );
-                       @unlink( $cache->fileCacheName() );
-               }
+               HTMLFileCache::clearFileCache( $this );
                return $success;
        }
 
@@ -2075,14 +2099,22 @@ class Title {
 
                # Namespace or interwiki prefix
                $firstPass = true;
+               $prefixRegexp = "/^(.+?)_*:_*(.*)$/S";
                do {
                        $m = array();
-                       if ( preg_match( "/^(.+?)_*:_*(.*)$/S", $dbkey, $m ) ) {
+                       if ( preg_match( $prefixRegexp, $dbkey, $m ) ) {
                                $p = $m[1];
-                               if ( $ns = $wgContLang->getNsIndex( $p )) {
+                               if ( $ns = $wgContLang->getNsIndex( $p ) ) {
                                        # Ordinary namespace
                                        $dbkey = $m[2];
                                        $this->mNamespace = $ns;
+                                       # For Talk:X pages, check if X has a "namespace" prefix
+                                       if( $ns == NS_TALK && preg_match( $prefixRegexp, $dbkey, $x ) ) {
+                                               if( $wgContLang->getNsIndex( $x[1] ) )
+                                                       return false; # Disallow Talk:File:x type titles...
+                                               else if( Interwiki::isValidInterwiki( $x[1] ) )
+                                                       return false; # Disallow Talk:Interwiki:x type titles...
+                                       }
                                } elseif( Interwiki::isValidInterwiki( $p ) ) {
                                        if( !$firstPass ) {
                                                # Can't make a local interwiki link to an interwiki link.
@@ -2427,6 +2459,9 @@ class Title {
                if( !$this->isMovable() ) {
                        $errors[] = array( 'immobile-source-namespace', $this->getNsText() );
                }
+               if ( $nt->getInterwiki() != '' ) {
+                       $errors[] = array( 'immobile-target-namespace-iw' );
+               }
                if ( !$nt->isMovable() ) {
                        $errors[] = array('immobile-target-namespace', $nt->getNsText() );
                }
@@ -2444,10 +2479,10 @@ class Title {
                }
 
                // Image-specific checks
-               if( $this->getNamespace() == NS_IMAGE ) {
+               if( $this->getNamespace() == NS_FILE ) {
                        $file = wfLocalFile( $this );
                        if( $file->exists() ) {
-                               if( $nt->getNamespace() != NS_IMAGE ) {
+                               if( $nt->getNamespace() != NS_FILE ) {
                                        $errors[] = array('imagenocrossnamespace');
                                }
                                if( $nt->getText() != wfStripIllegalFilenameChars( $nt->getText() ) ) {
@@ -2567,8 +2602,8 @@ class Title {
                        );
                        # Update the protection log
                        $log = new LogPage( 'protect' );
-                       $comment = wfMsgForContent('prot_1movedto2',$this->getPrefixedText(), $nt->getPrefixedText() );
-                       if( $reason ) $comment .= ': ' . $reason;
+                       $comment = wfMsgForContent( 'prot_1movedto2', $this->getPrefixedText(), $nt->getPrefixedText() );
+                       if( $reason ) $comment .= wfMsgForContent( 'colon-separator' ) . $reason;
                        $log->addEntry( 'move_prot', $nt, $comment, array($this->getPrefixedText()) ); // FIXME: $params?
                }
 
@@ -2709,12 +2744,14 @@ class Title {
                                        'pl_namespace' => $nt->getNamespace(),
                                        'pl_title'     => $nt->getDBkey() ),
                                $fname );
+                       $redirectSuppressed = false;
                } else {
                        $this->resetArticleID( 0 );
+                       $redirectSuppressed = true;
                }
-               
+
                # Move an image if this is a file
-               if( $this->getNamespace() == NS_IMAGE ) {
+               if( $this->getNamespace() == NS_FILE ) {
                        $file = wfLocalFile( $this );
                        if( $file->exists() ) {
                                $status = $file->move( $nt );
@@ -2727,7 +2764,7 @@ class Title {
 
                # Log the move
                $log = new LogPage( 'move' );
-               $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText() ) );
+               $log->addEntry( 'move_redir', $this, $reason, array( 1 => $nt->getPrefixedText(), 2 => $redirectSuppressed ) );
 
                # Purge squid
                if ( $wgUseSquid ) {
@@ -2804,12 +2841,14 @@ class Title {
                                        'pl_namespace' => $nt->getNamespace(),
                                        'pl_title'     => $nt->getDBkey() ),
                                $fname );
+                       $redirectSuppressed = false;
                } else {
                        $this->resetArticleID( 0 );
+                       $redirectSuppressed = true;
                }
-               
+
                # Move an image if this is a file
-               if( $this->getNamespace() == NS_IMAGE ) {
+               if( $this->getNamespace() == NS_FILE ) {
                        $file = wfLocalFile( $this );
                        if( $file->exists() ) {
                                $status = $file->move( $nt );
@@ -2822,7 +2861,7 @@ class Title {
 
                # Log the move
                $log = new LogPage( 'move' );
-               $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText()) );
+               $log->addEntry( 'move', $this, $reason, array( 1 => $nt->getPrefixedText(), 2 => $redirectSuppressed ) );
 
                # Purge caches as per article creation
                Article::onArticleCreate( $nt );
@@ -2880,7 +2919,7 @@ class Title {
        public function isValidMoveTarget( $nt ) {
                $dbw = wfGetDB( DB_MASTER );
                # Is it an existsing file?
-               if( $nt->getNamespace() == NS_IMAGE ) {
+               if( $nt->getNamespace() == NS_FILE ) {
                        $file = wfLocalFile( $nt );
                        if( $file->exists() ) {
                                wfDebug( __METHOD__ . ": file exists\n" );
@@ -2991,7 +3030,12 @@ class Title {
         * @return \type{\array} Selection array
         */
        public function pageCond() {
-               return array( 'page_namespace' => $this->mNamespace, 'page_title' => $this->mDbkeyform );
+               if( $this->mArticleID > 0 ) {
+                       // PK avoids secondary lookups in InnoDB, shouldn't hurt other DBs
+                       return array( 'page_id' => $this->mArticleID );
+               } else {
+                       return array( 'page_namespace' => $this->mNamespace, 'page_title' => $this->mDbkeyform );
+               }
        }
 
        /**
@@ -3031,6 +3075,38 @@ class Title {
                        array( 'ORDER BY' => 'rev_id' )
                );
        }
+       
+       /**
+        * Get the first revision of the page
+        *
+        * @param $flags \type{\int} GAID_FOR_UPDATE
+        * @return Revision (or NULL if page doesn't exist)
+        */
+       public function getFirstRevision( $flags=0 ) {
+               $db = ($flags & GAID_FOR_UPDATE) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
+               $pageId = $this->getArticleId($flags);
+               if( !$pageId ) return NULL;
+               $row = $db->selectRow( 'revision', '*',
+                       array( 'rev_page' => $pageId ),
+                       __METHOD__,
+                       array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 1 )
+               );
+               if( !$row ) {
+                       return NULL;
+               } else {
+                       return new Revision( $row );
+               }
+       }
+       
+       /**
+        * Check if this is a new page
+        *
+        * @return bool
+        */
+       public function isNewPage() {
+               $dbr = wfGetDB( DB_SLAVE );
+               return (bool)$dbr->selectField( 'page', 'page_is_new', $this->pageCond(), __METHOD__ );
+       }
 
        /**
         * Get the oldest revision timestamp of this page
@@ -3101,7 +3177,12 @@ class Title {
        }
 
        /**
-        * Check if page exists
+        * Check if page exists.  For historical reasons, this function simply
+        * checks for the existence of the title in the page table, and will
+        * thus return false for interwiki links, special pages and the like.
+        * If you want to know if a title can be meaningfully viewed, you should
+        * probably call the isKnown() method instead.
+        *
         * @return \type{\bool} TRUE or FALSE
         */
        public function exists() {
@@ -3109,21 +3190,65 @@ class Title {
        }
 
        /**
-        * Do we know that this title definitely exists, or should we otherwise
-        * consider that it exists?
+        * Should links to this title be shown as potentially viewable (i.e. as
+        * "bluelinks"), even if there's no record by this title in the page
+        * table?
+        *
+        * This function is semi-deprecated for public use, as well as somewhat
+        * misleadingly named.  You probably just want to call isKnown(), which
+        * calls this function internally.
+        *
+        * (ISSUE: Most of these checks are cheap, but the file existence check
+        * can potentially be quite expensive.  Including it here fixes a lot of
+        * existing code, but we might want to add an optional parameter to skip
+        * it and any other expensive checks.)
         *
         * @return \type{\bool} TRUE or FALSE
         */
        public function isAlwaysKnown() {
-               // If the page is form Mediawiki:message/lang, calling wfMsgWeirdKey causes
-               // the full l10n of that language to be loaded. That takes much memory and
-               // isn't needed. So we strip the language part away.
-               // Also, extension messages which are not loaded, are shown as red, because
-               // we don't call MessageCache::loadAllMessages.
-               list( $basename, /* rest */ ) = explode( '/', $this->mDbkeyform, 2 );
-               return $this->isExternal()
-                       || ( $this->mNamespace == NS_MAIN && $this->mDbkeyform == '' )
-                       || ( $this->mNamespace == NS_MEDIAWIKI && wfMsgWeirdKey( $basename ) );
+               if( $this->mInterwiki != '' ) {
+                       return true;  // any interwiki link might be viewable, for all we know
+               }
+               switch( $this->mNamespace ) {                   
+               case NS_MEDIA:
+               case NS_FILE:
+                       return wfFindFile( $this );  // file exists, possibly in a foreign repo
+               case NS_SPECIAL:
+                       return SpecialPage::exists( $this->getDBKey() );  // valid special page
+               case NS_MAIN:
+                       return $this->mDbkeyform == '';  // selflink, possibly with fragment
+               case NS_MEDIAWIKI:
+                       // If the page is form Mediawiki:message/lang, calling wfMsgWeirdKey causes
+                       // the full l10n of that language to be loaded. That takes much memory and
+                       // isn't needed. So we strip the language part away.
+                       // Also, extension messages which are not loaded, are shown as red, because
+                       // we don't call MessageCache::loadAllMessages.
+                       list( $basename, /* rest */ ) = explode( '/', $this->mDbkeyform, 2 );
+                       return wfMsgWeirdKey( $basename );  // known system message
+               default:
+                       return false;
+               }
+       }
+
+       /**
+        * Does this title refer to a page that can (or might) be meaningfully
+        * viewed?  In particular, this function may be used to determine if
+        * links to the title should be rendered as "bluelinks" (as opposed to
+        * "redlinks" to non-existent pages).
+        *
+        * @return \type{\bool} TRUE or FALSE
+        */
+       public function isKnown() {
+               return $this->exists() || $this->isAlwaysKnown();
+       }
+       
+       /**
+       * Is this in a namespace that allows actual pages?
+       *
+       * @return \type{\bool} TRUE or FALSE
+       */
+       public function canExist() {
+               return $this->mNamespace >= 0 && $this->mNamespace != NS_MEDIA;
        }
 
        /**
@@ -3243,8 +3368,8 @@ class Title {
                        case NS_PROJECT:
                        case NS_PROJECT_TALK:
                                return 'nstab-project';
-                       case NS_IMAGE:
-                       case NS_IMAGE_TALK:
+                       case NS_FILE:
+                       case NS_FILE_TALK:
                                return 'nstab-image';
                        case NS_MEDIAWIKI:
                        case NS_MEDIAWIKI_TALK:
@@ -3336,4 +3461,26 @@ class Title {
                }
                return $redirs;
        }
+       
+       /**
+        * Check if this Title is a valid redirect target
+        *
+        * @return \type{\bool} TRUE or FALSE
+        */
+       public function isValidRedirectTarget() {
+               global $wgInvalidRedirectTargets;
+               
+               // invalid redirect targets are stored in a global array, but explicity disallow Userlogout here
+               if( $this->isSpecial( 'Userlogout' ) ) {
+                       return false;
+               }
+               
+               foreach( $wgInvalidRedirectTargets as $target ) {
+                       if( $this->isSpecial( $target ) ) {
+                               return false;
+                       }
+               }
+               
+               return true;
+       }
 }