Merge "Bug 35671 - PHP Notice: Undefined index: gettoken in includes/api/ApiMain...
[lhc/web/wiklou.git] / includes / db / DatabasePostgres.php
index ed59e46..6e0a145 100644 (file)
@@ -18,7 +18,7 @@ class PostgresField implements Field {
        static function fromText( $db, $table, $field ) {
                $q = <<<SQL
 SELECT
- attnotnull, attlen, COALESCE(conname, '') AS conname,
+ attnotnull, attlen, conname AS conname,
  COALESCE(condeferred, 'f') AS deferred,
  COALESCE(condeferrable, 'f') AS deferrable,
  CASE WHEN typname = 'int2' THEN 'smallint'
@@ -213,7 +213,7 @@ class DatabasePostgres extends DatabaseBase {
 
        function hasConstraint( $name ) {
                $SQL = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = '" .
-                               pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" . pg_escape_string( $this->mConn, $this->mConn->getCoreSchema() ) ."'";
+                               pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" . pg_escape_string( $this->mConn, $this->getCoreSchema() ) ."'";
                $res = $this->doQuery( $SQL );
                return $this->numRows( $res );
        }
@@ -707,14 +707,19 @@ class DatabasePostgres extends DatabaseBase {
                # Replace reserved words with better ones
                switch( $name ) {
                        case 'user':
-                               return 'mwuser';
+                               return $this->realTableName( 'mwuser', $format );
                        case 'text':
-                               return 'pagecontent';
+                               return $this->realTableName( 'pagecontent', $format );
                        default:
-                               return parent::tableName( $name, $format );
+                               return $this->realTableName( $name, $format );
                }
        }
 
+       /* Don't cheat on installer */
+       function realTableName( $name, $format = 'quoted' ) {
+               return parent::tableName( $name, $format );
+       }
+
        /**
         * Return the next in a sequence, save the value for retrieval via insertId()
         * @return null
@@ -790,32 +795,32 @@ class DatabasePostgres extends DatabaseBase {
                return wfTimestamp( TS_POSTGRES, $ts );
        }
 
-
        /* 
         * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
         * to http://www.php.net/manual/en/ref.pgsql.php
-         *
-         * Parsing a postgres array can be a tricky problem, he's my
-         * take on this, it handles multi-dimensional arrays plus
-         * escaping using a nasty regexp to determine the limits of each
-         * data-item.
+        *
+        * Parsing a postgres array can be a tricky problem, he's my
+        * take on this, it handles multi-dimensional arrays plus
+        * escaping using a nasty regexp to determine the limits of each
+        * data-item.
         *
         * This should really be handled by PHP PostgreSQL module
         *
         * @since 1.20
-        * @param  text   string: postgreql array returned in a text form like {a,b}
-        * @param  output string
-        * @param  limit  int
-        * @param  offset int
+        * @param $text   string: postgreql array returned in a text form like {a,b}
+        * @param $output string
+        * @param $limit  int
+        * @param $offset int
         * @return string
         */
-
        function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
                if( false === $limit ) {
                        $limit = strlen( $text )-1;
                        $output = array();
                }
-               if( '{}' != $text )
+               if( '{}' == $text ) {
+                       return $output;
+               }
                do {
                        if ( '{' != $text{$offset} ) {
                                preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
@@ -824,10 +829,12 @@ class DatabasePostgres extends DatabaseBase {
                                $output[] = ( '"' != $match[1]{0} 
                                                ? $match[1] 
                                                : stripcslashes( substr( $match[1], 1, -1 ) ) );
-                               if ( '},' == $match[3] )
+                               if ( '},' == $match[3] ) {
                                        return $output;
-                       } else  
-                               $offset = $this->pg_array_parse( $text, $output[], $limit, $offset+1 );
+                               }
+                       } else {
+                               $offset = $this->pg_array_parse( $text, $output, $limit, $offset+1 );
+                       }
                } while ( $limit > $offset );
                return $output;
        }
@@ -894,14 +901,14 @@ class DatabasePostgres extends DatabaseBase {
                return explode(",", $row[0]);
        }
 
-       function setSearchPath( $search_path ) {
        /**
         * Update search_path, values should already be sanitized
         * Values may contain magic keywords like "$user"
         * @since 1.20
         *
-        * @param array list of schemas to be searched by default
+        * @param $search_path array list of schemas to be searched by default
         */
+       function setSearchPath( $search_path ) {
                $this->query( "SET search_path = " . implode(", ", $search_path) );
        }
 
@@ -926,7 +933,7 @@ class DatabasePostgres extends DatabaseBase {
                                wfDebug("Schema \"" . $desired_schema . "\" already in the search path\n");
                        } else {
                                /**
-                                * Apped our schema (e.g. 'mediawiki') in front
+                                * Append our schema (e.g. 'mediawiki') in front
                                 * of the search path
                                 * Fixes bug 15816 
                                 */
@@ -934,6 +941,7 @@ class DatabasePostgres extends DatabaseBase {
                                array_unshift( $search_path, 
                                        $this->addIdentifierQuotes( $desired_schema ));
                                $this->setSearchPath( $search_path );   
+                               $this->mCoreSchema = $desired_schema;
                                wfDebug("Schema \"" . $desired_schema . "\" added to the search path\n");
                        }
                } else {
@@ -986,7 +994,7 @@ class DatabasePostgres extends DatabaseBase {
                if ( !$schema ) {
                        $schema = $this->getCoreSchema();
                }
-               $table = $this->tableName( $table, 'raw' );
+               $table = $this->realTableName( $table, 'raw' );
                $etable = $this->addQuotes( $table );
                $eschema = $this->addQuotes( $schema );
                $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "