Re-commit fixed r37006
[lhc/web/wiklou.git] / includes / Title.php
index a1a5e27..ed6810c 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /**
  * See title.txt
- *
+ * @file
  */
 
 /** */
@@ -192,12 +192,12 @@ class Title {
         *       but not used for anything else
         *
         * @param int $id the page_id corresponding to the Title to create
-        * @param int $flags, use FOR_UPDATE to use master
+        * @param int $flags, use GAID_FOR_UPDATE to use master
         * @return Title the new object, or NULL on an error
         */
        public static function newFromID( $id, $flags = 0 ) {
                $fname = 'Title::newFromID';
-               $db = ($flags & FOR_UPDATE) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
+               $db = ($flags & GAID_FOR_UPDATE) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
                $row = $db->selectRow( 'page', array( 'page_namespace', 'page_title' ),
                        array( 'page_id' => $id ), $fname );
                if ( $row !== false ) {
@@ -697,16 +697,15 @@ class Title {
         * @return string Base name
         */
        public function getBaseText() {
-               global $wgNamespacesWithSubpages;
-               if( !empty( $wgNamespacesWithSubpages[$this->mNamespace] ) ) {
-                       $parts = explode( '/', $this->getText() );
-                       # Don't discard the real title if there's no subpage involved
-                       if( count( $parts ) > 1 )
-                               unset( $parts[ count( $parts ) - 1 ] );
-                       return implode( '/', $parts );
-               } else {
+               if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
                        return $this->getText();
                }
+
+               $parts = explode( '/', $this->getText() );
+               # Don't discard the real title if there's no subpage involved
+               if( count( $parts ) > 1 )
+                       unset( $parts[ count( $parts ) - 1 ] );
+               return implode( '/', $parts );
        }
 
        /**
@@ -714,13 +713,11 @@ class Title {
         * @return string Subpage name
         */
        public function getSubpageText() {
-               global $wgNamespacesWithSubpages;
-               if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) && $wgNamespacesWithSubpages[ $this->mNamespace ] ) {
-                       $parts = explode( '/', $this->mTextform );
-                       return( $parts[ count( $parts ) - 1 ] );
-               } else {
+               if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
                        return( $this->mTextform );
                }
+               $parts = explode( '/', $this->mTextform );
+               return( $parts[ count( $parts ) - 1 ] );
        }
 
        /**
@@ -1052,16 +1049,23 @@ class Title {
         * @param string $action action that permission needs to be checked for
         * @param User $user user to check
         * @param bool $doExpensiveQueries Set this to false to avoid doing unnecessary queries.
+        * @param array $ignoreErrors Set this to a list of message keys whose corresponding errors may be ignored.
         * @return array Array of arrays of the arguments to wfMsg to explain permissions problems.
         */
-       public function getUserPermissionsErrors( $action, $user, $doExpensiveQueries = true ) {
+       public function getUserPermissionsErrors( $action, $user, $doExpensiveQueries = true, $ignoreErrors = array() ) {
+               if( !StubObject::isRealObject( $user ) ) {
+                       //Since StubObject is always used on globals, we can unstub $wgUser here and set $user = $wgUser
+                       global $wgUser;
+                       $wgUser->_unstub( '', 5 );
+                       $user = $wgUser;
+               }
                $errors = $this->getUserPermissionsErrorsInternal( $action, $user, $doExpensiveQueries );
 
                global $wgContLang;
                global $wgLang;
                global $wgEmailConfirmToEdit;
 
-               if ( $wgEmailConfirmToEdit && !$user->isEmailConfirmed() ) {
+               if ( $wgEmailConfirmToEdit && !$user->isEmailConfirmed() && $action != 'createaccount' ) {
                        $errors[] = array( 'confirmedittext' );
                }
 
@@ -1113,6 +1117,16 @@ class Title {
                        $errors[] = array( ($block->mAuto ? 'autoblockedtext' : 'blockedtext'), $link, $reason, $ip, $name, 
                                $blockid, $blockExpiry, $intended, $blockTimestamp );
                }
+               
+               // Remove the errors being ignored.
+               
+               foreach( $errors as $index => $error ) {
+                       $error_key = is_array($error) ? $error[0] : $error;
+                       
+                       if (in_array( $error_key, $ignoreErrors )) {
+                               unset($errors[$index]);
+                       }
+               }
 
                return $errors;
        }
@@ -1158,8 +1172,9 @@ class Title {
                        else if ($result === false )
                                $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
                }
-
-               if( NS_SPECIAL == $this->mNamespace ) {
+               
+               $specialOKActions = array( 'createaccount', 'execute' );
+               if( NS_SPECIAL == $this->mNamespace && !in_array( $action, $specialOKActions) ) {
                        $errors[] = array('ns-specialprotected');
                }
 
@@ -1445,7 +1460,9 @@ class Title {
                         * Check for explicit whitelisting
                         */
                        $name = $this->getPrefixedText();
-                       if( in_array( $name, $wgWhitelistRead, true ) )
+                       $dbName = $this->getPrefixedDBKey();
+                       // Check with and without underscores
+                       if( in_array($name,$wgWhitelistRead,true) || in_array($dbName,$wgWhitelistRead,true) )
                                return true;
 
                        /**
@@ -1491,13 +1508,35 @@ class Title {
         * @return bool
         */
        public function isSubpage() {
-               global $wgNamespacesWithSubpages;
+               return MWNamespace::hasSubpages( $this->mNamespace )
+                       ? strpos( $this->getText(), '/' ) !== false
+                       : false;
+       }
 
-               if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) ) {
-                       return ( strpos( $this->getText(), '/' ) !== false && $wgNamespacesWithSubpages[ $this->mNamespace ] == true );
-               } else {
+       /**
+        * Does this have subpages?  (Warning, usually requires an extra DB query.)
+        * @return bool
+        */
+       public function hasSubpages() {
+               if( !MWNamespace::hasSubpages( $this->mNamespace ) ) {
+                       # Duh
                        return false;
                }
+
+               # We dynamically add a member variable for the purpose of this method
+               # alone to cache the result.  There's no point in having it hanging
+               # around uninitialized in every Title object; therefore we only add it
+               # if needed and don't declare it statically.
+               if( isset( $this->mHasSubpages ) ) {
+                       return $this->mHasSubpages;
+               }
+
+               $db = wfGetDB( DB_SLAVE );
+               return $this->mHasSubpages = (bool)$db->selectField( 'page', '1',
+                       "page_namespace = {$this->mNamespace} AND page_title LIKE '"
+                       . $db->escapeLike( $this->mDbkeyform ) . "/%'",
+                       __METHOD__
+               );
        }
 
        /**
@@ -1583,16 +1622,13 @@ class Title {
         * The restriction array is an array of each type, each of which contains an array of unique groups
         */
        public function getCascadeProtectionSources( $get_pages = true ) {
-               global $wgEnableCascadingProtection, $wgRestrictionTypes;
+               global $wgRestrictionTypes;
 
                # Define our dimension of restrictions types
                $pagerestrictions = array();
                foreach( $wgRestrictionTypes as $action )
                        $pagerestrictions[$action] = array();
 
-               if (!$wgEnableCascadingProtection)
-                       return array( false, $pagerestrictions );
-
                if ( isset( $this->mCascadeSources ) && $get_pages ) {
                        return array( $this->mCascadeSources, $this->mCascadingRestrictions );
                } else if ( isset( $this->mHasCascadingRestrictions ) && !$get_pages ) {
@@ -1695,7 +1731,8 @@ class Title {
                # Backwards-compatibility: also load the restrictions from the page record (old format).
 
                if ( $oldFashionedRestrictions === NULL ) {
-                       $oldFashionedRestrictions = $dbr->selectField( 'page', 'page_restrictions', array( 'page_id' => $this->getArticleId() ), __METHOD__ );
+                       $oldFashionedRestrictions = $dbr->selectField( 'page', 'page_restrictions', 
+                               array( 'page_id' => $this->getArticleId() ), __METHOD__ );
                }
 
                if ($oldFashionedRestrictions != '') {
@@ -1776,6 +1813,8 @@ class Title {
                                        } else { // Get rid of the old restrictions
                                                Title::purgeExpiredRestrictions();
                                        }
+                               } else {
+                                       $this->mRestrictionsExpiry = Block::decodeExpiry('');
                                }
                                $this->mRestrictionsLoaded = true;
                        }
@@ -1904,7 +1943,7 @@ class Title {
                $db = ($flags & GAID_FOR_UPDATE) ? wfGetDB(DB_MASTER) : wfGetDB(DB_SLAVE);
                return $this->mLatestID = $db->selectField( 'revision',
                        "max(rev_id)",
-                       array('rev_page' => $this->getArticleID()),
+                       array('rev_page' => $this->getArticleID($flags)),
                        'Title::getLatestRevID' );
        }
 
@@ -2180,13 +2219,6 @@ class Title {
                        return false;
                }
 
-               // Normalise special page names
-               if ( $this->mNamespace == NS_SPECIAL ) {
-                       list( $name, $subpage ) = SpecialPage::resolveAliasWithSubpage( $dbkey );
-                       $dbkey = SpecialPage::getLocalNameFor( $name, $subpage );
-               }
-
-
                # Fill fields
                $this->mDbkeyform = $dbkey;
                $this->mUrlform = wfUrlencode( $dbkey );
@@ -2371,34 +2403,37 @@ class Title {
 
        /**
         * Check whether a given move operation would be valid.
-        * Returns true if ok, or a message key string for an error message
-        * if invalid. (Scarrrrry ugly interface this.)
+        * Returns true if ok, or a getUserPermissionsErrors()-like array otherwise
         * @param Title &$nt the new title
         * @param bool $auth indicates whether $wgUser's permissions
         *      should be checked
-        * @return mixed true on success, message name on failure
+        * @param string $reason is the log summary of the move, used for spam checking
+        * @return mixed True on success, getUserPermissionsErrors()-like array on failure
         */
-       public function isValidMoveOperation( &$nt, $auth = true ) {
-               if( !$this or !$nt ) {
-                       return 'badtitletext';
+       public function isValidMoveOperation( &$nt, $auth = true, $reason = '' ) {
+               $errors = array();      
+               if( !$nt ) {
+                       // Normally we'd add this to $errors, but we'll get
+                       // lots of syntax errors if $nt is not an object
+                       return array(array('badtitletext'));
                }
                if( $this->equals( $nt ) ) {
-                       return 'selfmove';
+                       $errors[] = array('selfmove');
                }
                if( !$this->isMovable() || !$nt->isMovable() ) {
-                       return 'immobile_namespace';
+                       $errors[] = array('immobile_namespace');
                }
 
                $oldid = $this->getArticleID();
                $newid = $nt->getArticleID();
 
                if ( strlen( $nt->getDBkey() ) < 1 ) {
-                       return 'articleexists';
+                       $errors[] = array('articleexists');
                }
                if ( ( '' == $this->getDBkey() ) ||
                         ( !$oldid ) ||
                     ( '' == $nt->getDBkey() ) ) {
-                       return 'badarticleerror';
+                       $errors[] = array('badarticleerror');
                }
 
                // Image-specific checks
@@ -2406,28 +2441,27 @@ class Title {
                        $file = wfLocalFile( $this );
                        if( $file->exists() ) {
                                if( $nt->getNamespace() != NS_IMAGE ) {
-                                       return 'imagenocrossnamespace';
+                                       $errors[] = array('imagenocrossnamespace');
                                }
                                if( !File::checkExtensionCompatibility( $file, $nt->getDbKey() ) ) {
-                                       return 'imagetypemismatch';
+                                       $errors[] = array('imagetypemismatch');
                                }
                        }
                }
 
                if ( $auth ) {
                        global $wgUser;
-                       $errors = array_merge($this->getUserPermissionsErrors('move', $wgUser),
+                       $errors = array_merge($errors, 
+                                       $this->getUserPermissionsErrors('move', $wgUser),
                                        $this->getUserPermissionsErrors('edit', $wgUser),
                                        $nt->getUserPermissionsErrors('move', $wgUser),
                                        $nt->getUserPermissionsErrors('edit', $wgUser));
-                       if($errors !== array())
-                               return $errors[0][0];
                }
 
                global $wgUser;
                $err = null;
-               if( !wfRunHooks( 'AbortMove', array( $this, $nt, $wgUser, &$err ) ) ) {
-                       return 'hookaborted';
+               if( !wfRunHooks( 'AbortMove', array( $this, $nt, $wgUser, &$err, $reason ) ) ) {
+                       $errors[] = array('hookaborted', $err);
                }
 
                # The move is allowed only if (1) the target doesn't exist, or
@@ -2436,15 +2470,18 @@ class Title {
 
                if ( 0 != $newid ) { # Target exists; check for validity
                        if ( ! $this->isValidMoveTarget( $nt ) ) {
-                               return 'articleexists';
+                               $errors[] = array('articleexists');
                        }
                } else {
                        $tp = $nt->getTitleProtection();
-                       if ( $tp and !$wgUser->isAllowed( $tp['pt_create_perm'] ) ) {
-                               return 'cantmove-titleprotected';
+                       $right = ( $tp['pt_create_perm'] == 'sysop' ) ? 'protect' : $tp['pt_create_perm'];
+                       if ( $tp and !$wgUser->isAllowed( $right ) ) {
+                               $errors[] = array('cantmove-titleprotected');
                        }
                }
-               return true;
+               if(empty($errors))
+                       return true;
+               return $errors;
        }
 
        /**
@@ -2455,11 +2492,11 @@ class Title {
         * @param string $reason The reason for the move
         * @param bool $createRedirect Whether to create a redirect from the old title to the new title.
         *  Ignored if the user doesn't have the suppressredirect right.
-        * @return mixed true on success, message name on failure
+        * @return mixed true on success, getUserPermissionsErrors()-like array on failure
         */
        public function moveTo( &$nt, $auth = true, $reason = '', $createRedirect = true ) {
-               $err = $this->isValidMoveOperation( $nt, $auth );
-               if( is_string( $err ) ) {
+               $err = $this->isValidMoveOperation( $nt, $auth, $reason );
+               if( is_array( $err ) ) {
                        return $err;
                }
 
@@ -2471,7 +2508,8 @@ class Title {
                        $err = $this->moveToNewTitle( $nt, $reason, $createRedirect );
                        $pageCountChange = ($createRedirect ? 1 : 0);
                }
-               if( is_string( $err ) ) {
+
+               if( is_array( $err ) ) {
                        return $err;
                }
                $redirid = $this->getArticleID();
@@ -2568,18 +2606,9 @@ class Title {
                $now = wfTimestampNow();
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
-               $dbw = wfGetDB( DB_MASTER );
 
-               # Move an image if it is
-               if( $this->getNamespace() == NS_IMAGE ) {
-                       $file = wfLocalFile( $this );
-                       if( $file->exists() ) {
-                               $status = $file->move( $nt );
-                               if( !$status->isOk() ) {
-                                       return $status->getWikiText();
-                               }
-                       }
-               }
+               $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin();
 
                # Delete the old redirect. We don't save it to history since
                # by definition if we've got here it's rather uninteresting.
@@ -2603,6 +2632,9 @@ class Title {
                # Save a null revision in the page's history notifying of the move
                $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
                $nullRevId = $nullRevision->insertOn( $dbw );
+               
+               $article = new Article( $this );
+               wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, false) );
 
                # Change the name of the target page:
                $dbw->update( 'page',
@@ -2618,8 +2650,7 @@ class Title {
                $nt->resetArticleID( $oldid );
 
                # Recreate the redirect, this time in the other direction.
-               if($createRedirect || !$wgUser->isAllowed('suppressredirect'))
-               {
+               if( $createRedirect || !$wgUser->isAllowed('suppressredirect') ) {
                        $mwRedir = MagicWord::get( 'redirect' );
                        $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
                        $redirectArticle = new Article( $this );
@@ -2630,6 +2661,8 @@ class Title {
                                'text'    => $redirectText ) );
                        $redirectRevision->insertOn( $dbw );
                        $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
+                       
+                       wfRunHooks( 'NewRevisionFromEditComplete', array($redirectArticle, $redirectRevision, false) );
 
                        # Now, we record the link from the redirect to the new title.
                        # It should have no other outgoing links...
@@ -2643,6 +2676,19 @@ class Title {
                } else {
                        $this->resetArticleID( 0 );
                }
+               
+               # Move an image if this is a file
+               if( $this->getNamespace() == NS_IMAGE ) {
+                       $file = wfLocalFile( $this );
+                       if( $file->exists() ) {
+                               $status = $file->move( $nt );
+                               if( !$status->isOk() ) {
+                                       $dbw->rollback();
+                                       return $status->getErrorsArray();
+                               }
+                       }
+               }
+               $dbw->commit();
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -2654,6 +2700,7 @@ class Title {
                        $u = new SquidUpdate( $urls );
                        $u->doUpdate();
                }
+               
        }
 
        /**
@@ -2673,23 +2720,17 @@ class Title {
 
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
+               
                $dbw = wfGetDB( DB_MASTER );
+               $dbw->begin();
                $now = $dbw->timestamp();
 
-               # Move an image if it is
-               if( $this->getNamespace() == NS_IMAGE ) {
-                       $file = wfLocalFile( $this );
-                       if( $file->exists() ) {
-                               $status = $file->move( $nt );
-                               if( !$status->isOk() ) {
-                                       return $status->getWikiText();
-                               }
-                       }
-               }
-
                # Save a null revision in the page's history notifying of the move
                $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
                $nullRevId = $nullRevision->insertOn( $dbw );
+               
+               $article = new Article( $this );
+               wfRunHooks( 'NewRevisionFromEditComplete', array($article, $nullRevision, false) );
 
                # Rename page entry
                $dbw->update( 'page',
@@ -2704,8 +2745,7 @@ class Title {
                );
                $nt->resetArticleID( $oldid );
 
-               if($createRedirect || !$wgUser->isAllowed('suppressredirect'))
-               {
+               if( $createRedirect || !$wgUser->isAllowed('suppressredirect') ) {
                        # Insert redirect
                        $mwRedir = MagicWord::get( 'redirect' );
                        $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $nt->getPrefixedText() . "]]\n";
@@ -2717,6 +2757,8 @@ class Title {
                                'text'    => $redirectText ) );
                        $redirectRevision->insertOn( $dbw );
                        $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
+                       
+                       wfRunHooks( 'NewRevisionFromEditComplete', array($redirectArticle, $redirectRevision, false) );
 
                        # Record the just-created redirect's linking to the page
                        $dbw->insert( 'pagelinks',
@@ -2728,6 +2770,19 @@ class Title {
                } else {
                        $this->resetArticleID( 0 );
                }
+               
+               # Move an image if this is a file
+               if( $this->getNamespace() == NS_IMAGE ) {
+                       $file = wfLocalFile( $this );
+                       if( $file->exists() ) {
+                               $status = $file->move( $nt );
+                               if( !$status->isOk() ) {
+                                       $dbw->rollback();
+                                       return $status->getErrorsArray();
+                               }
+                       }
+               }
+               $dbw->commit();
 
                # Log the move
                $log = new LogPage( 'move' );
@@ -2739,6 +2794,7 @@ class Title {
                # Purge old title from squid
                # The new title, and links to the new title, are purged in Article::onArticleCreate()
                $this->purgeSquid();
+               
        }
 
        /**
@@ -2835,13 +2891,13 @@ class Title {
                         ." AND cl_from <> '0'"
                         ." ORDER BY cl_sortkey";
 
-               $res = $dbr->query ( $sql ) ;
+               $res = $dbr->query( $sql );
 
-               if($dbr->numRows($res) > 0) {
-                       while ( $x = $dbr->fetchObject ( $res ) )
+               if( $dbr->numRows( $res ) > 0 ) {
+                       while( $x = $dbr->fetchObject( $res ) )
                                //$data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to);
-                               $data[$wgContLang->getNSText ( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
-                       $dbr->freeResult ( $res ) ;
+                               $data[$wgContLang->getNSText( NS_CATEGORY ).':'.$x->cl_to] = $this->getFullText();
+                       $dbr->freeResult( $res );
                } else {
                        $data = array();
                }
@@ -2857,8 +2913,8 @@ class Title {
                $stack = array();
                $parents = $this->getParentCategories();
 
-               if($parents != '') {
-                       foreach($parents as $parent => $current) {
+               if( $parents ) {
+                       foreach( $parents as $parent => $current ) {
                                if ( array_key_exists( $parent, $children ) ) {
                                        # Circular reference
                                        $stack[$parent] = array();
@@ -2890,26 +2946,38 @@ class Title {
         * Get the revision ID of the previous revision
         *
         * @param integer $revision  Revision ID. Get the revision that was before this one.
+        * @param integer $flags, GAID_FOR_UPDATE
         * @return integer $oldrevision|false
         */
-       public function getPreviousRevisionID( $revision ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               return $dbr->selectField( 'revision', 'rev_id',
-                       'rev_page=' . intval( $this->getArticleId() ) .
-                       ' AND rev_id<' . intval( $revision ) . ' ORDER BY rev_id DESC' );
+       public function getPreviousRevisionID( $revision, $flags=0 ) {
+               $db = ($flags & GAID_FOR_UPDATE) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
+               return $db->selectField( 'revision', 'rev_id',
+                       array(
+                               'rev_page' => $this->getArticleId($flags),
+                               'rev_id < ' . intval( $revision )
+                       ),
+                       __METHOD__,
+                       array( 'ORDER BY' => 'rev_id DESC' )
+               );
        }
 
        /**
         * Get the revision ID of the next revision
         *
         * @param integer $revision  Revision ID. Get the revision that was after this one.
+        * @param integer $flags, GAID_FOR_UPDATE
         * @return integer $oldrevision|false
         */
-       public function getNextRevisionID( $revision ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               return $dbr->selectField( 'revision', 'rev_id',
-                       'rev_page=' . intval( $this->getArticleId() ) .
-                       ' AND rev_id>' . intval( $revision ) . ' ORDER BY rev_id' );
+       public function getNextRevisionID( $revision, $flags=0 ) {
+               $db = ($flags & GAID_FOR_UPDATE) ? wfGetDB( DB_MASTER ) : wfGetDB( DB_SLAVE );
+               return $db->selectField( 'revision', 'rev_id',
+                       array(
+                               'rev_page' => $this->getArticleId($flags),
+                               'rev_id > ' . intval( $revision )
+                       ),
+                       __METHOD__,
+                       array( 'ORDER BY' => 'rev_id' )
+               );
        }
 
        /**
@@ -3130,12 +3198,25 @@ class Title {
                return MWNamespace::isContent( $this->getNamespace() );
        }
 
-       public function getRedirectsHere() {
+       public function getRedirectsHere( $ns = null ) {
                $redirs = array();
-               $dbr = wfGetDB( DB_SLAVE );
-               list($page,$redirect) = $dbr->tableNamesN( 'page', 'redirect' );
-               $result = $dbr->query( "SELECT page_title, page_namespace FROM $page JOIN $redirect ON page_id = rd_from WHERE rd_title = "
-                       . $dbr->addQuotes( $this->getDBKey() ) . " AND rd_namespace = " . $this->getNamespace(), __METHOD__ );
+               
+               $dbr = wfGetDB( DB_SLAVE );     
+               $where = array(
+                       'rd_namespace' => $this->getNamespace(),
+                       'rd_title' => $this->getDBkey(),
+                       'rd_from = page_id'
+               );
+               if ( !is_null($ns) ) $where['page_namespace'] = $ns;
+               
+               $result = $dbr->select(
+                       array( 'redirect', 'page' ),
+                       array( 'page_namespace', 'page_title' ),
+                       $where,
+                       __METHOD__
+               );
+
+
                while( $row = $dbr->fetchObject( $result ) ) {
                        $redirs[] = self::newFromRow( $row );
                }