r34353 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r34352‎ | r34353 | r34354 >
Date:04:44, 7 May 2008
Author:dantman
Status:old
Tags:
Comment:
New config variables. $wgSharedPrefix and $wgSharedTables.
This is a complete rewrite of Database::tableName.
The new tableName also has no PHP errors, and outputs a cleanly quoted table name 100% of the time.
This should solve bug 2396
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/Database.php (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/DefaultSettings.php
@@ -579,12 +579,20 @@
580580
581581
582582 /**
583 - * Shared database for multiple wikis. Presently used for storing a user table
 583+ * Shared database for multiple wikis. Commonly used for storing a user table
584584 * for single sign-on. The server for this database must be the same as for the
585585 * main database.
 586+ * For backwards compatibility the shared prefix is set to the same as the local
 587+ * prefix, and the user table is listed in the default list of shared tables.
 588+ *
 589+ * $wgSharedTables may be customized with a list of tables to share in the shared
 590+ * datbase. However it is advised to limit what tables you do share as many of
 591+ * MediaWiki's tables may have side effects if you try to share them.
586592 * EXPERIMENTAL
587593 */
588 -$wgSharedDB = null;
 594+$wgSharedDB = null;
 595+$wgSharedPrefix = $wgDBprefix;
 596+$wgSharedTables = array( 'user' );
589597
590598 /**
591599 * Database load balancer
Index: trunk/phase3/includes/Database.php
@@ -1355,30 +1355,55 @@
13561356 /**
13571357 * Format a table name ready for use in constructing an SQL query
13581358 *
1359 - * This does two important things: it quotes table names which as necessary,
1360 - * and it adds a table prefix if there is one.
 1359+ * This does two important things: it quotes the table names to clean them up,
 1360+ * and it adds a table prefix if only given a table name with no quotes.
13611361 *
13621362 * All functions of this object which require a table name call this function
13631363 * themselves. Pass the canonical name to such functions. This is only needed
13641364 * when calling query() directly.
13651365 *
13661366 * @param string $name database table name
 1367+ * @return string full database name
13671368 */
13681369 function tableName( $name ) {
1369 - global $wgSharedDB;
1370 - # Skip quoted literals
1371 - if ( $name{0} != '`' ) {
1372 - if ( $this->mTablePrefix !== '' && strpos( $name, '.' ) === false ) {
1373 - $name = "{$this->mTablePrefix}$name";
1374 - }
1375 - if ( isset( $wgSharedDB ) && "{$this->mTablePrefix}user" == $name ) {
1376 - $name = "`$wgSharedDB`.`$name`";
1377 - } else {
1378 - # Standard quoting
1379 - $name = "`$name`";
1380 - }
 1370+ global $wgSharedDB, $wgSharedPrefix, $wgSharedTables;
 1371+ # Skip the entire process when we have a string quoted on both ends.
 1372+ # Note that we check the end so that we will still quote any use of
 1373+ # use of `database`.table. But won't break things if someone wants
 1374+ # to query a database table with a dot in the name.
 1375+ if ( $name[0] == '`' && substr( $name, -1, 1 ) == '`' ) return $name;
 1376+
 1377+ # Split database and table into proper variables.
 1378+ # We reverse the explode so that database.table and table both output
 1379+ # the correct table.
 1380+ @list( $table, $database ) = array_reverse( explode( '.', $name, 2 ) );
 1381+ $prefix = $this->mTablePrefix; # Default prefix
 1382+
 1383+ # A database name has been specified in input. Quote the table name
 1384+ # because we don't want any prefixes added.
 1385+ if( isset($database) ) $table = ( $table[0] == '`' ? $table : "`{$table}`" );
 1386+
 1387+ # Note that we use the long format because php will complain in in_array if
 1388+ # the input is not an array, and will complain in is_array if it is not set.
 1389+ if( !isset( $database ) # Don't use shared database if pre selected.
 1390+ && isset( $wgSharedDB ) # We have a shared database
 1391+ && $table[0] != '`' # Paranoia check to prevent shared tables listing '`table`'
 1392+ && isset( $wgSharedTables )
 1393+ && is_array( $wgSharedTables )
 1394+ && in_array( $table, $wgSharedTables ) ) { # A shared table is selected
 1395+ $database = $wgSharedDB;
 1396+ $prefix = isset( $wgSharedprefix ) ? $wgSharedprefix : $prefix;
13811397 }
1382 - return $name;
 1398+
 1399+ # Quote the $database and $table and apply the prefix if not quoted.
 1400+ if( isset($database) ) $database = ( $database[0] == '`' ? $database : "`{$database}`" );
 1401+ $table = ( $table[0] == '`' ? $table : "`{$prefix}{$table}`" );
 1402+
 1403+ # Merge our database and table into our final table name.
 1404+ $tableName = ( isset($database) ? "{$database}.{$table}" : "{$table}" );
 1405+
 1406+ # We're finished, return.
 1407+ return $tableName;
13831408 }
13841409
13851410 /**
Index: trunk/phase3/RELEASE-NOTES
@@ -33,6 +33,11 @@
3434 * Removed the emailconfirmed implicit group by default. To re-add it, use:
3535 $wgAutopromote['emailconfirmed'] = APCOND_EMAILCONFIRMED;
3636 in your LocalSettings.php.
 37+* (bug 2396) New shared database configuration variables. $wgSharedPrefix allows
 38+ you to use a shared database with a different prefix. Or you can now use a local
 39+ database and use prefixes to separate wiki and the shared tables. And the new
 40+ $wgSharedTables variable allows you to specify a list of tables to share.
 41+
3742
3843 === New features in 1.13 ===
3944

Follow-up revisions

RevisionCommit summaryAuthorDate
r34354Merge from trunk r34353.dantman05:35, 7 May 2008
r34388Fixing regression from r34353: using Database::select() for a join messes up ...catrope18:57, 7 May 2008

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r34278Revert r34199, r34205 for now....brion22:21, 5 May 2008

Status & tagging log