Redo WhatLinksHere query and add a *_from_namespace field to link tables
[lhc/web/wiklou.git] / includes / specials / SpecialWhatlinkshere.php
index d980f79..694bc83 100644 (file)
@@ -101,10 +101,10 @@ class SpecialWhatLinksHere extends IncludableSpecialPage {
         * @param int $back Display from this article ID at backwards scrolling (default: 0)
         */
        function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
-               global $wgMaxRedirectLinksRetrieved;
+               global $wgMaxRedirectLinksRetrieved, $wgUseLinkNamespaceDBFields;
+
                $out = $this->getOutput();
                $dbr = wfGetDB( DB_SLAVE );
-               $options = array();
 
                $hidelinks = $this->opts->getValue( 'hidelinks' );
                $hideredirs = $this->opts->getValue( 'hideredirs' );
@@ -113,77 +113,85 @@ class SpecialWhatLinksHere extends IncludableSpecialPage {
 
                $fetchlinks = ( !$hidelinks || !$hideredirs );
 
-               // Make the query
-               $plConds = array(
-                       'page_id=pl_from',
+               // Build query conds in concert for all three tables...
+               $conds['pagelinks'] = array(
                        'pl_namespace' => $target->getNamespace(),
                        'pl_title' => $target->getDBkey(),
                );
-               if ( $hideredirs ) {
-                       $plConds['rd_from'] = null;
-               } elseif ( $hidelinks ) {
-                       $plConds[] = 'rd_from is NOT NULL';
-               }
-
-               $tlConds = array(
-                       'page_id=tl_from',
+               $conds['templatelinks'] = array(
                        'tl_namespace' => $target->getNamespace(),
                        'tl_title' => $target->getDBkey(),
                );
-
-               $ilConds = array(
-                       'page_id=il_from',
+               $conds['imagelinks'] = array(
                        'il_to' => $target->getDBkey(),
                );
 
                $namespace = $this->opts->getValue( 'namespace' );
                if ( is_int( $namespace ) ) {
-                       $plConds['page_namespace'] = $namespace;
-                       $tlConds['page_namespace'] = $namespace;
-                       $ilConds['page_namespace'] = $namespace;
+                       if ( $wgUseLinkNamespaceDBFields ) {
+                               $conds['pagelinks']['pl_from_namespace'] = $namespace;
+                               $conds['templatelinks']['tl_from_namespace'] = $namespace;
+                               $conds['imagelinks']['il_from_namespace'] = $namespace;
+                       } else {
+                               $conds['pagelinks']['page_namespace'] = $namespace;
+                               $conds['templatelinks']['page_namespace'] = $namespace;
+                               $conds['imagelinks']['page_namespace'] = $namespace;
+                       }
                }
 
                if ( $from ) {
-                       $tlConds[] = "tl_from >= $from";
-                       $plConds[] = "pl_from >= $from";
-                       $ilConds[] = "il_from >= $from";
+                       $conds['templatelinks'][] = "tl_from >= $from";
+                       $conds['pagelinks'][] = "pl_from >= $from";
+                       $conds['imagelinks'][] = "il_from >= $from";
                }
 
-               // Read an extra row as an at-end check
-               $queryLimit = $limit + 1;
-
-               $options['LIMIT'] = $queryLimit;
-               $fields = array( 'page_id', 'page_namespace', 'page_title', 'rd_from' );
+               if ( $hideredirs ) {
+                       $conds['pagelinks']['rd_from'] = null;
+               } elseif ( $hidelinks ) {
+                       $conds['pagelinks'][] = 'rd_from is NOT NULL';
+               }
 
-               $joinConds = array( 'redirect' => array( 'LEFT JOIN', array(
-                       'rd_from = page_id',
-                       'rd_namespace' => $target->getNamespace(),
-                       'rd_title' => $target->getDBkey(),
-                       'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
-               ) ) );
+               $queryFunc = function( $dbr, $table, $fromCol ) use ( $conds, $target, $limit ) {
+                       global $wgUseLinkNamespaceDBFields;
+                       // Read an extra row as an at-end check
+                       $queryLimit = $limit + 1;
+                       $on = array(
+                               "rd_from = $fromCol",
+                               'rd_title' => $target->getDBkey(),
+                               'rd_interwiki = ' . $dbr->addQuotes( '' ) . ' OR rd_interwiki IS NULL'
+                       );
+                       if ( $wgUseLinkNamespaceDBFields ) { // migration check
+                               $on['rd_namespace'] = $target->getNamespace();
+                       }
+                       // Inner LIMIT is 2X in case of stale backlinks with no page
+                       $subQuery = $dbr->selectSqlText(
+                               array( $table, 'redirect' ),
+                               array( $fromCol, 'rd_from' ),
+                               $conds[$table],
+                               __CLASS__ . '::showIndirectLinks',
+                               array( 'ORDER BY' => $fromCol, 'LIMIT' => 2 * $queryLimit ),
+                               array( 'redirect' => array( 'LEFT JOIN', $on ) )
+                       );
+                       return $dbr->select(
+                               array( 'page', 'temp_backlink_range' => "($subQuery)" ),
+                               array( 'page_id', 'page_namespace', 'page_title', 'rd_from' ),
+                               array(),
+                               __CLASS__ . '::showIndirectLinks',
+                               array( 'ORDER BY' => 'page_id', 'LIMIT' => $queryLimit ),
+                               array( 'page' => array( 'INNER JOIN', "$fromCol = page_id" ) )
+                       );
+               };
 
                if ( $fetchlinks ) {
-                       $options['ORDER BY'] = 'pl_from';
-                       $plRes = $dbr->select( array( 'pagelinks', 'page', 'redirect' ), $fields,
-                               $plConds, __METHOD__, $options,
-                               $joinConds
-                       );
+                       $plRes = $queryFunc( $dbr, 'pagelinks', 'pl_from' );
                }
 
                if ( !$hidetrans ) {
-                       $options['ORDER BY'] = 'tl_from';
-                       $tlRes = $dbr->select( array( 'templatelinks', 'page', 'redirect' ), $fields,
-                               $tlConds, __METHOD__, $options,
-                               $joinConds
-                       );
+                       $tlRes = $queryFunc( $dbr, 'templatelinks', 'tl_from' );
                }
 
                if ( !$hideimages ) {
-                       $options['ORDER BY'] = 'il_from';
-                       $ilRes = $dbr->select( array( 'imagelinks', 'page', 'redirect' ), $fields,
-                               $ilConds, __METHOD__, $options,
-                               $joinConds
-                       );
+                       $ilRes = $queryFunc( $dbr, 'imagelinks', 'il_from' );
                }
 
                if ( ( !$fetchlinks || !$plRes->numRows() )