4 * This is Postgres database abstraction layer.
6 * As it includes more generic version for DB functions,
7 * than MySQL ones, some of them should be moved to parent
13 class DatabasePostgres
extends Database
{
14 var $mInsertId = NULL;
15 var $mLastResult = NULL;
17 function DatabasePostgres($server = false, $user = false, $password = false, $dbName = false,
18 $failFunction = false, $flags = 0 )
22 # Can't get a reference if it hasn't been set yet
23 if ( !isset( $wgOut ) ) {
26 $this->mOut
=& $wgOut;
27 $this->mFailFunction
= $failFunction;
28 $this->mCascadingDeletes
= true;
29 $this->mCleanupTriggers
= true;
30 $this->mStrictIPs
= true;
31 $this->mFlags
= $flags;
32 $this->open( $server, $user, $password, $dbName);
36 function realTimestamps() {
40 static function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
41 $failFunction = false, $flags = 0)
43 return new DatabasePostgres( $server, $user, $password, $dbName, $failFunction, $flags );
47 * Usually aborts on failure
48 * If the failFunction is set to a non-zero integer, returns success
50 function open( $server, $user, $password, $dbName ) {
51 # Test for Postgres support, to avoid suppressed fatal error
52 if ( !function_exists( 'pg_connect' ) ) {
53 throw new DBConnectionError( $this, "Postgres functions missing, have you compiled PHP with the --with-pgsql option?\n (Note: if you recently installed PHP, you may need to restart your webserver and database)\n" );
60 $this->mServer
= $server;
63 $this->mPassword
= $password;
64 $this->mDBname
= $dbName;
67 if ($server!=false && $server!="") {
68 $hstring="host=$server ";
70 if ($port!=false && $port!="") {
71 $hstring .= "port=$port ";
74 if (!strlen($user)) { ## e.g. the class is being loaded
78 error_reporting( E_ALL
);
79 @$this->mConn
= pg_connect("$hstring dbname=$dbName user=$user password=$password");
81 if ( $this->mConn
== false ) {
82 wfDebug( "DB connection error\n" );
83 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
84 wfDebug( $this->lastError()."\n" );
88 $this->mOpened
= true;
89 ## If this is the initial connection, setup the schema stuff and possibly create the user
90 if (defined('MEDIAWIKI_INSTALL')) {
91 global $wgDBname, $wgDBuser, $wgDBpassword, $wgDBsuperuser, $wgDBmwschema,
95 print "<li>Checking the version of Postgres...";
96 $version = pg_fetch_result($this->doQuery("SELECT version()"),0,0);
98 if (!preg_match('/PostgreSQL (\d+\.\d+)(\S+)/', $version, $thisver)) {
99 print "<b>FAILED</b> (could not determine the version)</li>\n";
103 if ($thisver[1] < $PGMINVER) {
104 print "<b>FAILED</b>. Required version is $PGMINVER. You have $thisver[1]$thisver[2]</li>\n";
107 print "version $thisver[1]$thisver[2] is OK.</li>\n";
109 $safeuser = $this->quote_ident($wgDBuser);
110 ## Are we connecting as a superuser for the first time?
111 if ($wgDBsuperuser) {
112 ## Are we really a superuser? Check out our rights
114 CASE WHEN usesuper IS TRUE THEN
115 CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END
116 ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END
118 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBsuperuser);
119 $rows = $this->numRows($res = $this->doQuery($SQL));
121 print "<li>ERROR: Could not read permissions for user \"$wgDBsuperuser\"</li>\n";
124 $perms = pg_fetch_result($res, 0, 0);
126 $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBuser);
127 $rows = $this->numRows($this->doQuery($SQL));
129 print "<li>User \"$wgDBuser\" already exists, skipping account creation.</li>";
132 if ($perms != 1 and $perms != 3) {
133 print "<li>ERROR: the user \"$wgDBsuperuser\" cannot create other users. ";
134 print 'Please use a different Postgres user.</li>';
137 print "<li>Creating user <b>$wgDBuser</b>...";
138 $safepass = $this->addQuotes($wgDBpassword);
139 $SQL = "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass";
140 $this->doQuery($SQL);
143 ## User now exists, check out the database
144 if ($dbName != $wgDBname) {
145 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this->addQuotes($wgDBname);
146 $rows = $this->numRows($this->doQuery($SQL));
148 print "<li>Database \"$wgDBname\" already exists, skipping database creation.</li>";
152 print "<li>ERROR: the user \"$wgDBsuperuser\" cannot create databases. ";
153 print 'Please use a different Postgres user.</li>';
156 print "<li>Creating database <b>$wgDBname</b>...";
157 $safename = $this->quote_ident($wgDBname);
158 $SQL = "CREATE DATABASE $safename OWNER $safeuser ";
159 $this->doQuery($SQL);
161 ## Hopefully tsearch2 and plpgsql are in template1...
164 ## Reconnect to check out tsearch2 rights for this user
165 print "<li>Connecting to \"$wgDBname\" as superuser \"$wgDBsuperuser\" to check rights...";
166 @$this->mConn
= pg_connect("$hstring dbname=$wgDBname user=$user password=$password");
167 if ( $this->mConn
== false ) {
168 print "<b>FAILED TO CONNECT!</b></li>";
175 print "<li>Checking that tsearch2 is installed in the database \"$wgDBname\"...";
176 if (! $this->tableExists("pg_ts_cfg", $wgDBts2schema)) {
177 print "<b>FAILED</b>. tsearch2 must be installed in the database \"$wgDBname\".";
178 print "Please see <a href='http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>";
179 print " for instructions or ask on #postgresql on irc.freenode.net</li>\n";
183 print "<li>Ensuring that user \"$wgDBuser\" has select rights on the tsearch2 tables...";
184 foreach (array('cfg','cfgmap','dict','parser') as $table) {
185 $SQL = "GRANT SELECT ON pg_ts_$table TO $safeuser";
186 $this->doQuery($SQL);
191 ## Setup the schema for this user if needed
192 $result = $this->schemaExists($wgDBmwschema);
193 $safeschema = $this->quote_ident($wgDBmwschema);
195 print "<li>Creating schema <b>$wgDBmwschema</b> ...";
196 $result = $this->doQuery("CREATE SCHEMA $safeschema AUTHORIZATION $safeuser");
198 print "<b>FAILED</b>.</li>\n";
204 print "<li>Schema already exists, explicitly granting rights...\n";
205 $safeschema2 = $this->addQuotes($wgDBmwschema);
206 $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n".
207 "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n".
208 "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n".
209 "AND p.relkind IN ('r','S','v')\n";
211 $SQL .= "SELECT 'GRANT ALL ON FUNCTION '||pg_catalog.quote_ident(proname)||'('||\n".
212 "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n".
213 "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n".
214 "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2";
215 $res = $this->doQuery($SQL);
217 print "<b>FAILED</b>. Could not set rights for the user.</li>\n";
220 $this->doQuery("SET search_path = $safeschema");
221 $rows = $this->numRows($res);
224 $this->doQuery(pg_fetch_result($res, $rows, 0));
230 return true; ## Reconnect as regular user
233 if (!defined('POSTGRES_SEARCHPATH')) {
235 ## Do we have the basic tsearch2 table?
236 print "<li>Checking for tsearch2 in the schema \"$wgDBts2schema\"...";
237 if (! $this->tableExists("pg_ts_dict", $wgDBts2schema)) {
238 print "<b>FAILED</b>. Make sure tsearch2 is installed. See <a href=";
239 print "'http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>";
240 print " for instructions.</li>\n";
245 ## Does this user have the rights to the tsearch2 tables?
246 $ctype = pg_fetch_result($this->doQuery("SHOW lc_ctype"),0,0);
247 print "<li>Checking tsearch2 permissions...";
248 $SQL = "SELECT ts_name FROM $wgDBts2schema.pg_ts_cfg WHERE locale = '$ctype'";
249 $SQL .= " ORDER BY CASE WHEN ts_name <> 'default' THEN 1 ELSE 0 END";
250 error_reporting( 0 );
251 $res = $this->doQuery($SQL);
252 error_reporting( E_ALL
);
254 print "<b>FAILED</b>. Make sure that the user \"$wgDBuser\" has SELECT access to the tsearch2 tables</li>\n";
259 ## Will the current locale work? Can we force it to?
260 print "<li>Verifying tsearch2 locale with $ctype...";
261 $rows = $this->numRows($res);
264 print "<b>not found</b></li>\n";
265 print "<li>Attempting to set default tsearch2 locale to \"$ctype\"...";
269 $tsname = pg_fetch_result($res, 0, 0);
270 if ($tsname != 'default') {
271 print "<b>not set to default ($tsname)</b>";
272 print "<li>Attempting to change tsearch2 default locale to \"$ctype\"...";
277 $SQL = "UPDATE $wgDBts2schema.pg_ts_cfg SET locale = '$ctype' WHERE ts_name = 'default'";
278 $res = $this->doQuery($SQL);
280 print "<b>FAILED</b>. ";
281 print "Please make sure that the locale in pg_ts_cfg for \"default\" is set to \"ctype\"</li>\n";
287 ## Final test: try out a simple tsearch2 query
288 $SQL = "SELECT $wgDBts2schema.to_tsvector('default','MediaWiki tsearch2 testing')";
289 $res = $this->doQuery($SQL);
291 print "<b>FAILED</b>. Specifically, \"$SQL\" did not work.</li>";
296 ## Do we have plpgsql installed?
297 print "<li>Checking for Pl/Pgsql ...";
298 $SQL = "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'";
299 $rows = $this->numRows($this->doQuery($SQL));
301 // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it
302 print "not installed. Attempting to install Pl/Pgsql ...";
303 $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ".
304 "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'";
305 $rows = $this->numRows($this->doQuery($SQL));
307 $result = $this->doQuery("CREATE LANGUAGE plpgsql");
309 print "<b>FAILED</b>. You need to install the language plpgsql in the database <tt>$wgDBname</tt></li>";
314 print "<b>FAILED</b>. You need to install the language plpgsql in the database <tt>$wgDBname</tt></li>";
320 ## Does the schema already exist? Who owns it?
321 $result = $this->schemaExists($wgDBmwschema);
323 print "<li>Creating schema <b>$wgDBmwschema</b> ...";
324 $result = $this->doQuery("CREATE SCHEMA $wgDBmwschema");
326 print "<b>FAILED</b>.</li>\n";
331 else if ($result != $user) {
332 print "<li>Schema \"$wgDBmwschema\" exists but is not owned by \"$user\". Not ideal.</li>\n";
335 print "<li>Schema \"$wgDBmwschema\" exists and is owned by \"$user\". Excellent.</li>\n";
338 ## Fix up the search paths if needed
339 print "<li>Setting the search path for user \"$user\" ...";
340 $path = $this->quote_ident($wgDBmwschema);
341 if ($wgDBts2schema !== $wgDBmwschema)
342 $path .= ", ". $this->quote_ident($wgDBts2schema);
343 if ($wgDBmwschema !== 'public' and $wgDBts2schema !== 'public')
345 $SQL = "ALTER USER $safeuser SET search_path = $path";
346 $result = pg_query($this->mConn
, $SQL);
348 print "<b>FAILED</b>.</li>\n";
352 ## Set for the rest of this session
353 $SQL = "SET search_path = $path";
354 $result = pg_query($this->mConn
, $SQL);
356 print "<li>Failed to set search_path</li>\n";
359 define( "POSTGRES_SEARCHPATH", $path );
362 global $wgCommandLineMode;
363 ## If called from the command-line (e.g. importDump), only show errors
364 if ($wgCommandLineMode) {
365 $this->doQuery("SET client_min_messages = 'ERROR'");
372 * Closes a database connection, if it is open
373 * Returns success, true if already closed
376 $this->mOpened
= false;
377 if ( $this->mConn
) {
378 return pg_close( $this->mConn
);
384 function doQuery( $sql ) {
385 return $this->mLastResult
=pg_query( $this->mConn
, $sql);
388 function queryIgnore( $sql, $fname = '' ) {
389 return $this->query( $sql, $fname, true );
392 function freeResult( $res ) {
393 if ( !@pg_free_result
( $res ) ) {
394 throw new DBUnexpectedError($this, "Unable to free Postgres result\n" );
398 function fetchObject( $res ) {
399 @$row = pg_fetch_object( $res );
400 # FIXME: HACK HACK HACK HACK debug
403 # hashar : not sure if the following test really trigger if the object
405 if( pg_last_error($this->mConn
) ) {
406 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
411 function fetchRow( $res ) {
412 @$row = pg_fetch_array( $res );
413 if( pg_last_error($this->mConn
) ) {
414 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
419 function numRows( $res ) {
420 @$n = pg_num_rows( $res );
421 if( pg_last_error($this->mConn
) ) {
422 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn
) ) );
426 function numFields( $res ) { return pg_num_fields( $res ); }
427 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
430 * This must be called after nextSequenceVal
432 function insertId() {
433 return $this->mInsertId
;
436 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
437 function lastError() {
438 if ( $this->mConn
) {
439 return pg_last_error();
442 return "No database connection";
445 function lastErrno() {
446 return pg_last_error() ?
1 : 0;
449 function affectedRows() {
450 return pg_affected_rows( $this->mLastResult
);
454 * Returns information about an index
455 * If errors are explicitly ignored, returns NULL on failure
457 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
458 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
459 $res = $this->query( $sql, $fname );
464 while ( $row = $this->fetchObject( $res ) ) {
465 if ( $row->indexname
== $index ) {
468 // BUG: !!!! This code needs to be synced up with database.php
475 function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
476 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'".
477 " AND indexdef LIKE 'CREATE UNIQUE%({$index})'";
478 $res = $this->query( $sql, $fname );
481 while ($row = $this->fetchObject( $res ))
487 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
488 # Postgres doesn't support options
489 # We have a go at faking one of them
490 # TODO: DELAYED, LOW_PRIORITY
492 if ( !is_array($options))
493 $options = array($options);
495 if ( in_array( 'IGNORE', $options ) )
496 $oldIgnore = $this->ignoreErrors( true );
498 # IGNORE is performed using single-row inserts, ignoring errors in each
499 # FIXME: need some way to distiguish between key collision and other types of error
500 $oldIgnore = $this->ignoreErrors( true );
501 if ( !is_array( reset( $a ) ) ) {
504 foreach ( $a as $row ) {
505 parent
::insert( $table, $row, $fname, array() );
507 $this->ignoreErrors( $oldIgnore );
510 if ( in_array( 'IGNORE', $options ) )
511 $this->ignoreErrors( $oldIgnore );
516 function tableName( $name ) {
517 # Replace reserved words with better ones
522 return 'pagecontent';
529 * Return the next in a sequence, save the value for retrieval via insertId()
531 function nextSequenceValue( $seqName ) {
532 $safeseq = preg_replace( "/'/", "''", $seqName );
533 $res = $this->query( "SELECT nextval('$safeseq')" );
534 $row = $this->fetchRow( $res );
535 $this->mInsertId
= $row[0];
536 $this->freeResult( $res );
537 return $this->mInsertId
;
541 * Postgres does not have a "USE INDEX" clause, so return an empty string
543 function useIndexClause( $index ) {
547 # REPLACE query wrapper
548 # Postgres simulates this with a DELETE followed by INSERT
549 # $row is the row to insert, an associative array
550 # $uniqueIndexes is an array of indexes. Each element may be either a
551 # field name or an array of field names
553 # It may be more efficient to leave off unique indexes which are unlikely to collide.
554 # However if you do this, you run the risk of encountering errors which wouldn't have
556 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
557 $table = $this->tableName( $table );
559 if (count($rows)==0) {
564 if ( !is_array( reset( $rows ) ) ) {
565 $rows = array( $rows );
568 foreach( $rows as $row ) {
569 # Delete rows which collide
570 if ( $uniqueIndexes ) {
571 $sql = "DELETE FROM $table WHERE ";
573 foreach ( $uniqueIndexes as $index ) {
580 if ( is_array( $index ) ) {
582 foreach ( $index as $col ) {
588 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
591 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
595 $this->query( $sql, $fname );
599 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES
) .') VALUES (' .
600 $this->makeList( $row, LIST_COMMA
) . ')';
601 $this->query( $sql, $fname );
605 # DELETE where the condition is a join
606 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
608 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
611 $delTable = $this->tableName( $delTable );
612 $joinTable = $this->tableName( $joinTable );
613 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
614 if ( $conds != '*' ) {
615 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND
);
619 $this->query( $sql, $fname );
622 # Returns the size of a text field, or -1 for "unlimited"
623 function textFieldSize( $table, $field ) {
624 $table = $this->tableName( $table );
625 $sql = "SELECT t.typname as ftype,a.atttypmod as size
626 FROM pg_class c, pg_attribute a, pg_type t
627 WHERE relname='$table' AND a.attrelid=c.oid AND
628 a.atttypid=t.oid and a.attname='$field'";
629 $res =$this->query($sql);
630 $row=$this->fetchObject($res);
631 if ($row->ftype
=="varchar") {
636 $this->freeResult( $res );
640 function lowPriorityOption() {
644 function limitResult($sql, $limit,$offset) {
645 return "$sql LIMIT $limit ".(is_numeric($offset)?
" OFFSET {$offset} ":"");
649 * Returns an SQL expression for a simple conditional.
650 * Uses CASE on Postgres
652 * @param string $cond SQL expression which will result in a boolean value
653 * @param string $trueVal SQL expression to return if true
654 * @param string $falseVal SQL expression to return if false
655 * @return string SQL fragment
657 function conditional( $cond, $trueVal, $falseVal ) {
658 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
661 # FIXME: actually detecting deadlocks might be nice
662 function wasDeadlock() {
666 function timestamp( $ts=0 ) {
667 return wfTimestamp(TS_POSTGRES
,$ts);
671 * Return aggregated value function call
673 function aggregateValue ($valuedata,$valuename='value') {
678 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
679 # Ignore errors during error handling to avoid infinite recursion
680 $ignore = $this->ignoreErrors( true );
681 ++
$this->mErrorCount
;
683 if ($ignore ||
$tempIgnore) {
684 wfDebug("SQL ERROR (ignored): $error\n");
685 $this->ignoreErrors( $ignore );
688 $message = "A database error has occurred\n" .
690 "Function: $fname\n" .
691 "Error: $errno $error\n";
692 throw new DBUnexpectedError($this, $message);
697 * @return string wikitext of a link to the server software's web site
699 function getSoftwareLink() {
700 return "[http://www.postgresql.org/ PostgreSQL]";
704 * @return string Version information from the database
706 function getServerVersion() {
707 $res = $this->query( "SELECT version()" );
708 $row = $this->fetchRow( $res );
710 $this->freeResult( $res );
716 * Query whether a given table exists (in the given schema, or the default mw one if not given)
718 function tableExists( $table, $schema = false ) {
719 global $wgDBmwschema;
721 $schema = $wgDBmwschema;
722 $etable = preg_replace("/'/", "''", $table);
723 $eschema = preg_replace("/'/", "''", $schema);
724 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
725 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' "
726 . "AND c.relkind IN ('r','v')";
727 $res = $this->query( $SQL );
728 $count = $res ?
pg_num_rows($res) : 0;
730 $this->freeResult( $res );
736 * Query whether a given schema exists. Returns the name of the owner
738 function schemaExists( $schema ) {
739 $eschema = preg_replace("/'/", "''", $schema);
740 $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
741 ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'";
742 $res = $this->query( $SQL );
743 $owner = $res ?
pg_num_rows($res) ?
pg_fetch_result($res, 0, 0) : false : false;
745 $this->freeResult($res);
750 * Query whether a given column exists in the mediawiki schema
752 function fieldExists( $table, $field ) {
753 global $wgDBmwschema;
754 $etable = preg_replace("/'/", "''", $table);
755 $eschema = preg_replace("/'/", "''", $wgDBmwschema);
756 $ecol = preg_replace("/'/", "''", $field);
757 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a "
758 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' "
759 . "AND a.attrelid = c.oid AND a.attname = '$ecol'";
760 $res = $this->query( $SQL );
761 $count = $res ?
pg_num_rows($res) : 0;
763 $this->freeResult( $res );
767 function fieldInfo( $table, $field ) {
768 $res = $this->query( "SELECT $field FROM $table LIMIT 1" );
769 $type = pg_field_type( $res, 0 );
773 function begin( $fname = 'DatabasePostgrs::begin' ) {
774 $this->query( 'BEGIN', $fname );
775 $this->mTrxLevel
= 1;
777 function immediateCommit( $fname = 'DatabasePostgres::immediateCommit' ) {
780 function commit( $fname = 'DatabasePostgres::commit' ) {
781 $this->query( 'COMMIT', $fname );
782 $this->mTrxLevel
= 0;
785 /* Not even sure why this is used in the main codebase... */
786 function limitResultForUpdate($sql, $num) {
790 function setup_database() {
791 global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport;
793 dbsource( "../maintenance/postgres/tables.sql", $this);
795 ## Update version information
796 $mwv = $this->addQuotes($wgVersion);
797 $pgv = $this->addQuotes($this->getServerVersion());
798 $pgu = $this->addQuotes($this->mUser
);
799 $mws = $this->addQuotes($wgDBmwschema);
800 $tss = $this->addQuotes($wgDBts2schema);
801 $pgp = $this->addQuotes($wgDBport);
802 $dbn = $this->addQuotes($this->mDBname
);
803 $ctype = pg_fetch_result($this->doQuery("SHOW lc_ctype"),0,0);
805 $SQL = "UPDATE mediawiki_version SET mw_version=$mwv, pg_version=$pgv, pg_user=$pgu, ".
806 "mw_schema = $mws, ts2_schema = $tss, pg_port=$pgp, pg_dbname=$dbn, ".
808 "WHERE type = 'Creation'";
811 ## Avoid the non-standard "REPLACE INTO" syntax
812 $f = fopen( "../maintenance/interwiki.sql", 'r' );
814 dieout( "<li>Could not find the interwiki.sql file");
816 ## We simply assume it is already empty as we have just created it
817 $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
818 while ( ! feof( $f ) ) {
819 $line = fgets($f,1024);
821 if (!preg_match('/^\s*(\(.+?),(\d)\)/', $line, $matches)) {
824 $this->query("$SQL $matches[1],$matches[2])");
826 print " (table interwiki successfully populated)...\n";
829 function encodeBlob($b) {
830 return array('bytea',pg_escape_bytea($b));
832 function decodeBlob($b) {
833 return pg_unescape_bytea( $b );
836 function strencode( $s ) { ## Should not be called by us
837 return pg_escape_string( $s );
840 function addQuotes( $s ) {
841 if ( is_null( $s ) ) {
843 } else if (is_array( $s )) { ## Assume it is bytea data
846 return "'" . pg_escape_string($s) . "'";
847 // Unreachable: return "E'" . pg_escape_string($s) . "'";
850 function quote_ident( $s ) {
851 return '"' . preg_replace( '/"/', '""', $s) . '"';