r65475 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65474‎ | r65475 | r65476 >
Date:19:06, 23 April 2010
Author:catrope
Status:deferred
Tags:
Comment:
* querypage-work2: Tweak DB backend so stuff like 'tables' => array( 'alias' => 'tablename' ) works
* Use this to unbreak Listredirects, Brokenredirects and Doubleredirects, which were broken by r65471 (which itself attempted to fix alias-related breakage)
* Fix redirect loop in Special:Mostlinkedtemplates
* Remove superfluous wfSpecialMostlinkedtemplates() function
Modified paths:
  • /branches/querypage-work2/phase3/includes/db/Database.php (modified) (history)
  • /branches/querypage-work2/phase3/includes/specials/SpecialBrokenRedirects.php (modified) (history)
  • /branches/querypage-work2/phase3/includes/specials/SpecialDoubleRedirects.php (modified) (history)
  • /branches/querypage-work2/phase3/includes/specials/SpecialListredirects.php (modified) (history)
  • /branches/querypage-work2/phase3/includes/specials/SpecialMostlinkedtemplates.php (modified) (history)

Diff [purge]

Index: branches/querypage-work2/phase3/includes/db/Database.php
@@ -877,7 +877,7 @@
878878 /**
879879 * SELECT wrapper
880880 *
881 - * @param $table Mixed: Array or string, table name(s) (prefix auto-added)
 881+ * @param $table Mixed: Array or string, table name(s) (prefix auto-added). Array keys are table aliases (optional)
882882 * @param $vars Mixed: Array or string, field name(s) to be retrieved
883883 * @param $conds Mixed: Array or string, condition(s) for WHERE
884884 * @param $fname String: Calling function name (use __METHOD__) for logs/profiling
@@ -898,7 +898,7 @@
899899 if ( !empty($join_conds) || ( isset( $options['USE INDEX'] ) && is_array( @$options['USE INDEX'] ) ) )
900900 $from = ' FROM ' . $this->tableNamesWithUseIndexOrJOIN( $table, @$options['USE INDEX'], $join_conds );
901901 else
902 - $from = ' FROM ' . implode( ',', array_map( array( &$this, 'tableName' ), $table ) );
 902+ $from = ' FROM ' . implode( ',', $this->tableNamesWithAlias( $table ) );
903903 } elseif ($table!='') {
904904 if ($table{0}==' ') {
905905 $from = ' FROM ' . $table;
@@ -1445,6 +1445,35 @@
14461446 }
14471447
14481448 /**
 1449+ * Get an aliased table name.
 1450+ * @param $name string Table name, see tableName()
 1451+ * @param $alias string Alias (optional)
 1452+ * @return string SQL name for aliased table. Will not alias a table to its own name
 1453+ */
 1454+ public function tableNameWithAlias( $name, $alias ) {
 1455+ if ( !$alias || $alias == $name ) {
 1456+ return $this->tableName( $name );
 1457+ } else {
 1458+ return $this->tableName( $name ) . ' `' . $alias . '`';
 1459+ }
 1460+ }
 1461+
 1462+ /**
 1463+ * @param $tables array( [alias] => table )
 1464+ * @return array of strings, see tableNameWithAlias()
 1465+ */
 1466+ public function tableNamesWithAlias( $tables ) {
 1467+ $retval = array();
 1468+ foreach ( $tables as $alias => $table ) {
 1469+ if ( is_numeric( $alias ) ) {
 1470+ $alias = $table;
 1471+ }
 1472+ $retval[] = $this->tableNameWithAlias( $table, $alias );
 1473+ }
 1474+ return $retval;
 1475+ }
 1476+
 1477+ /**
14491478 * @private
14501479 */
14511480 function tableNamesWithUseIndexOrJOIN( $tables, $use_index = array(), $join_conds = array() ) {
@@ -1452,25 +1481,29 @@
14531482 $retJOIN = array();
14541483 $use_index_safe = is_array($use_index) ? $use_index : array();
14551484 $join_conds_safe = is_array($join_conds) ? $join_conds : array();
1456 - foreach ( $tables as $table ) {
 1485+ foreach ( $tables as $alias => $table ) {
 1486+ if ( !is_string( $alias ) ) {
 1487+ // No alias? Set it equal to the table name
 1488+ $alias = $table;
 1489+ }
14571490 // Is there a JOIN and INDEX clause for this table?
1458 - if ( isset($join_conds_safe[$table]) && isset($use_index_safe[$table]) ) {
1459 - $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
1460 - $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
1461 - $tableClause .= ' ON (' . $this->makeList((array)$join_conds_safe[$table][1], LIST_AND) . ')';
 1491+ if ( isset($join_conds_safe[$alias]) && isset($use_index_safe[$alias]) ) {
 1492+ $tableClause = $join_conds_safe[$alias][0] . ' ' . $this->tableNameWithAlias( $table, $alias );
 1493+ $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$alias] ) );
 1494+ $tableClause .= ' ON (' . $this->makeList((array)$join_conds_safe[$alias][1], LIST_AND) . ')';
14621495 $retJOIN[] = $tableClause;
14631496 // Is there an INDEX clause?
1464 - } else if ( isset($use_index_safe[$table]) ) {
1465 - $tableClause = $this->tableName( $table );
1466 - $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$table] ) );
 1497+ } else if ( isset($use_index_safe[$alias]) ) {
 1498+ $tableClause = $this->tableNameWithAlias( $table, $alias );
 1499+ $tableClause .= ' ' . $this->useIndexClause( implode( ',', (array)$use_index_safe[$alias] ) );
14671500 $ret[] = $tableClause;
14681501 // Is there a JOIN clause?
1469 - } else if ( isset($join_conds_safe[$table]) ) {
1470 - $tableClause = $join_conds_safe[$table][0] . ' ' . $this->tableName( $table );
1471 - $tableClause .= ' ON (' . $this->makeList((array)$join_conds_safe[$table][1], LIST_AND) . ')';
 1502+ } else if ( isset($join_conds_safe[$alias]) ) {
 1503+ $tableClause = $join_conds_safe[$alias][0] . ' ' . $this->tableNameWithAlias( $table, $alias );
 1504+ $tableClause .= ' ON (' . $this->makeList((array)$join_conds_safe[$alias][1], LIST_AND) . ')';
14721505 $retJOIN[] = $tableClause;
14731506 } else {
1474 - $tableClause = $this->tableName( $table );
 1507+ $tableClause = $this->tableNameWithAlias( $table, $alias );
14751508 $ret[] = $tableClause;
14761509 }
14771510 }
Index: branches/querypage-work2/phase3/includes/specials/SpecialBrokenRedirects.php
@@ -26,7 +26,8 @@
2727
2828 function getQueryInfo() {
2929 return array(
30 - 'tables' => array( 'redirect', 'page p1', 'page p2' ),
 30+ 'tables' => array( 'redirect', 'p1' => 'page',
 31+ 'p2' => 'page' ),
3132 'fields' => array( 'p1.page_namespace AS namespace',
3233 'p1.page_title AS title',
3334 'rd_namespace',
@@ -35,10 +36,10 @@
3637 'conds' => array( 'rd_namespace >= 0',
3738 'p2.page_namespace IS NULL'
3839 ),
39 - 'join_conds' => array( 'page p1' => array( 'LEFT JOIN', array(
 40+ 'join_conds' => array( 'p1' => array( 'LEFT JOIN', array(
4041 'rd_from=p1.page_id',
4142 ) ),
42 - 'page p2' => array( 'LEFT JOIN', array(
 43+ 'p2' => array( 'LEFT JOIN', array(
4344 'rd_namespace=p2.page_namespace',
4445 'rd_title=p2.page_title'
4546 ) )
Index: branches/querypage-work2/phase3/includes/specials/SpecialMostlinkedtemplates.php
@@ -14,7 +14,7 @@
1515 class MostlinkedTemplatesPage extends QueryPage {
1616
1717 function __construct() {
18 - SpecialPage::__construct( 'MostlinkedTemplatesPage' );
 18+ SpecialPage::__construct( 'Mostlinkedtemplates' );
1919 }
2020
2121 /**
@@ -113,13 +113,3 @@
114114 }
115115 }
116116
117 -/**
118 - * Execution function
119 - *
120 - * @param $par Mixed: parameters passed to the page
121 - */
122 -function wfSpecialMostlinkedtemplates( $par = false ) {
123 - list( $limit, $offset ) = wfCheckLimits();
124 - $mlt = new SpecialMostlinkedtemplates();
125 - $mlt->doQuery( $offset, $limit );
126 -}
Index: branches/querypage-work2/phase3/includes/specials/SpecialDoubleRedirects.php
@@ -27,9 +27,9 @@
2828 function reallyGetQueryInfo( $namespace = null, $title = null ) {
2929 $limitToTitle = !( $namespace === null && $title === null );
3030 $retval = array (
31 - 'tables' => array ( 'redirect ra', 'redirect rb',
32 - 'page pa', 'page pb',
33 - 'page pc' ),
 31+ 'tables' => array ( 'ra' => 'redirect',
 32+ 'rb' => 'redirect', 'pa' => 'page',
 33+ 'pb' => 'page', 'pc' => 'page' ),
3434 'fields' => array ( 'pa.page_namespace AS namespace',
3535 'pa.page_title AS title',
3636 'pb.page_namespace AS nsb',
Index: branches/querypage-work2/phase3/includes/specials/SpecialListredirects.php
@@ -25,7 +25,7 @@
2626
2727 function getQueryInfo() {
2828 return array(
29 - 'tables' => array( 'page p1', 'redirect', 'page p2' ),
 29+ 'tables' => array( 'p1' => 'page', 'redirect', 'p2' => 'page' ),
3030 'fields' => array( 'p1.page_namespace AS namespace',
3131 'p1.page_title AS title',
3232 'rd_namespace',
@@ -34,7 +34,7 @@
3535 'conds' => array( 'p1.page_is_redirect' => 1 ),
3636 'join_conds' => array( 'redirect' => array(
3737 'LEFT JOIN', 'rd_from=p1.page_id' ),
38 - 'page p2' => array( 'LEFT JOIN', array(
 38+ 'p2' => array( 'LEFT JOIN', array(
3939 'p2.page_namespace=rd_namespace',
4040 'p2.page_title=rd_title' ) ) )
4141 );

Follow-up revisions

RevisionCommit summaryAuthorDate
r77597Per Roans request, Merge in r65475 (Database.php only) from querypage-work2 b...reedy19:39, 2 December 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r65471Remove optional "AS" keyword from table aliases, unbreaks installs with table...catrope18:17, 23 April 2010

Status & tagging log