Index: trunk/phase3/includes/DefaultSettings.php |
— | — | @@ -579,12 +579,20 @@ |
580 | 580 | |
581 | 581 | |
582 | 582 | /** |
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 |
584 | 584 | * for single sign-on. The server for this database must be the same as for the |
585 | 585 | * 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. |
586 | 592 | * EXPERIMENTAL |
587 | 593 | */ |
588 | | -$wgSharedDB = null; |
| 594 | +$wgSharedDB = null; |
| 595 | +$wgSharedPrefix = $wgDBprefix; |
| 596 | +$wgSharedTables = array( 'user' ); |
589 | 597 | |
590 | 598 | /** |
591 | 599 | * Database load balancer |
Index: trunk/phase3/includes/Database.php |
— | — | @@ -1355,30 +1355,55 @@ |
1356 | 1356 | /** |
1357 | 1357 | * Format a table name ready for use in constructing an SQL query |
1358 | 1358 | * |
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. |
1361 | 1361 | * |
1362 | 1362 | * All functions of this object which require a table name call this function |
1363 | 1363 | * themselves. Pass the canonical name to such functions. This is only needed |
1364 | 1364 | * when calling query() directly. |
1365 | 1365 | * |
1366 | 1366 | * @param string $name database table name |
| 1367 | + * @return string full database name |
1367 | 1368 | */ |
1368 | 1369 | 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; |
1381 | 1397 | } |
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; |
1383 | 1408 | } |
1384 | 1409 | |
1385 | 1410 | /** |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -33,6 +33,11 @@ |
34 | 34 | * Removed the emailconfirmed implicit group by default. To re-add it, use: |
35 | 35 | $wgAutopromote['emailconfirmed'] = APCOND_EMAILCONFIRMED; |
36 | 36 | 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 | + |
37 | 42 | |
38 | 43 | === New features in 1.13 === |
39 | 44 | |