Bug 16794 - $wgSharedDB PostgreSQL support
authorMark A. Hershberger <mah@users.mediawiki.org>
Tue, 13 Dec 2011 15:06:30 +0000 (15:06 +0000)
committerMark A. Hershberger <mah@users.mediawiki.org>
Tue, 13 Dec 2011 15:06:30 +0000 (15:06 +0000)
Updated fix from Luca Fulchir

CREDITS
includes/db/DatabasePostgres.php

diff --git a/CREDITS b/CREDITS
index 06d9ae9..5e568a5 100644 (file)
--- a/CREDITS
+++ b/CREDITS
@@ -120,6 +120,7 @@ following names for their contribution to the product.
 * Lejonel
 * liangent
 * Louperivois
+* Luca Fulchir
 * Lucas Garczewski
 * Luigi Corsaro
 * Lupo
index f51640d..1c5b684 100644 (file)
@@ -624,15 +624,69 @@ class DatabasePostgres extends DatabaseBase {
        }
 
        function tableName( $name, $format = 'quoted' ) {
-               # Replace reserved words with better ones
-               switch( $name ) {
+               global $wgSharedDB, $wgSharedTables, $wgDBmwSchema;
+               # Skip quoted tablenames.
+               if ( $this->isQuotedIdentifier( $name ) ) {
+                       return $name;
+               }
+               # Lets test for any bits of text that should never show up in a table
+               # name. Basically anything like JOIN or ON which are actually part of
+               # SQL queries, but may end up inside of the table value to combine
+               # sql. Such as how the API is doing.
+               # Note that we use a whitespace test rather than a \b test to avoid
+               # any remote case where a word like on may be inside of a table name
+               # surrounded by symbols which may be considered word breaks.
+               if ( preg_match( '/(^|\s)(DISTINCT|JOIN|ON|AS)(\s|$)/i', $name ) !== 0 ) {
+                       return $name;
+               }
+               # Split database and table into proper variables.
+               # We reverse the explode so that schema.table and table both output
+               # the correct table.
+               $dbDetails = array_reverse( explode( '.', $name, 2 ) );
+               if ( isset( $dbDetails[1] ) ) {
+                       list( $table, $schema ) = $dbDetails;
+               } else {
+                       list( $table ) = $dbDetails;
+               }
+               if ( $format != 'quoted' ) {
+                       switch( $name ) {
+                               case 'user':
+                                       return 'mwuser';
+                               case 'text':
+                                       return 'pagecontent';
+                               default:
+                                       return $table;
+                       }
+               }
+               if ( !isset( $schema )) {
+                       $schema = "\"{$wgDBmwSchema}\".";
+               } else {
+                       # keep old schema, but quote it.
+                       $schema = "\"{$schema}\".";
+               }
+               # during installation wgDBmwSchema is not set, so we would end up quering
+               # ""."table" => error. Erase the first part if wgDBmwSchema is empty
+               if ( $schema == "\"\"." ) {
+                       $schema = "";
+               }
+               if ( isset( $wgSharedDB ) # We have a shared database (=> schema)
+                 && isset( $wgSharedTables )
+                 && is_array( $wgSharedTables )
+                 && in_array( $table, $wgSharedTables ) ) { # A shared table is selected
+                       $schema = "\"{$wgSharedDB}\".";
+               }
+               switch ( $table ) {
                        case 'user':
-                               return 'mwuser';
+                               $table = "{$schema}\"mwuser\"";
+                               break;
                        case 'text':
-                               return 'pagecontent';
+                               $table = "{$schema}\"pagecontent\"";
+                               break;
                        default:
-                               return parent::tableName( $name, $format );
+                               $table = "{$schema}\"$table\"";
+                               break;
                }
+               return $table;
        }
 
        /**