Better lastErrno, from a patch on bug #6732
[lhc/web/wiklou.git] / includes / DatabasePostgres.php
1 <?php
2
3 /**
4 * This is PostgreSQL database abstraction layer.
5 *
6 * As it includes more generic version for DB functions,
7 * than MySQL ones, some of them should be moved to parent
8 * Database class.
9 *
10 * @package MediaWiki
11 */
12
13 /**
14 * Depends on database
15 */
16 require_once( 'Database.php' );
17
18 class DatabasePostgres extends Database {
19 var $mInsertId = NULL;
20 var $mLastResult = NULL;
21
22 function DatabasePostgres($server = false, $user = false, $password = false, $dbName = false,
23 $failFunction = false, $flags = 0 )
24 {
25
26 global $wgOut, $wgDBprefix, $wgCommandLineMode;
27 # Can't get a reference if it hasn't been set yet
28 if ( !isset( $wgOut ) ) {
29 $wgOut = NULL;
30 }
31 $this->mOut =& $wgOut;
32 $this->mFailFunction = $failFunction;
33 $this->mCascadingDeletes = true;
34 $this->mCleanupTriggers = true;
35 $this->mFlags = $flags;
36 $this->open( $server, $user, $password, $dbName);
37
38 }
39
40 static function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
41 $failFunction = false, $flags = 0)
42 {
43 return new DatabasePostgres( $server, $user, $password, $dbName, $failFunction, $flags );
44 }
45
46 /**
47 * Usually aborts on failure
48 * If the failFunction is set to a non-zero integer, returns success
49 */
50 function open( $server, $user, $password, $dbName ) {
51 # Test for PostgreSQL support, to avoid suppressed fatal error
52 if ( !function_exists( 'pg_connect' ) ) {
53 throw new DBConnectionError( $this, "PostgreSQL functions missing, have you compiled PHP with the --with-pgsql option?\n" );
54 }
55
56
57 global $wgDBport;
58
59 $this->close();
60 $this->mServer = $server;
61 $port = $wgDBport;
62 $this->mUser = $user;
63 $this->mPassword = $password;
64 $this->mDBname = $dbName;
65
66 $success = false;
67 $hstring="";
68 if ($server!=false && $server!="") {
69 $hstring="host=$server ";
70 }
71 if ($port!=false && $port!="") {
72 $hstring .= "port=$port ";
73 }
74
75 if (!strlen($user)) { ## e.g. the class is being loaded
76 return;
77 }
78
79 error_reporting( E_ALL );
80 @$this->mConn = pg_connect("$hstring dbname=$dbName user=$user password=$password");
81
82 if ( $this->mConn == false ) {
83 wfDebug( "DB connection error\n" );
84 wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" );
85 wfDebug( $this->lastError()."\n" );
86 return false;
87 }
88
89 $this->mOpened = true;
90 ## If this is the initial connection, setup the schema stuff and possibly create the user
91 if (defined('MEDIAWIKI_INSTALL')) {
92 global $wgDBname, $wgDBuser, $wgDBpass, $wgDBsuperuser, $wgDBmwschema, $wgDBts2schema;
93 print "OK</li>\n";
94
95 ## Are we connecting as a superuser for the first time?
96 if ($wgDBsuperuser) {
97 $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBuser);
98 $rows = $this->numRows($this->doQuery($SQL));
99 if ($rows) {
100 print "<li>User \"$wgDBuser\" already exists, skipping account creation.</li>";
101 }
102 else {
103 ## Can we create users?
104 $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usesuper IS TRUE AND ".
105 "usename = " . $this->addQuotes($wgDBsuperuser);
106 $rows = $this->numRows($this->doQuery($SQL));
107 if (!$rows) {
108 print "<li>ERROR: the user \"$wgDBsuperuser\" cannot create other users. ";
109 print 'Please use a different Postgres user.</li>';
110 dieout('</ul>');
111 }
112 print "<li>Creating user <b>$wgDBuser</b>...";
113 $safepass = $this->addQuotes($wgDBpass);
114 $SQL = "CREATE USER \"$wgDBuser\" NOCREATEDB PASSWORD $safepass";
115 $this->doQuery($SQL);
116 print "OK</li>\n";
117 }
118 ## User now exists, check out the database
119 $safename = $this->addQuotes($wgDBname);
120 $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = $safename";
121 $rows = $this->numRows($this->doQuery($SQL));
122 if ($rows) {
123 print "<li>Database \"$wgDBname\" already exists, skipping database creation.</li>";
124 }
125 else {
126 print "<li>Creating database <b>$wgDBname</b>...";
127 $SQL = "CREATE DATABASE \"$wgDBname\" OWNER \"$wgDBuser\" ";
128 $this->doQuery($SQL);
129 print "OK</li>\n";
130 ## Hopefully tsearch2 and plpgsql are in template1...
131 }
132
133 ## Reconnect to check out tsearch2 rights for this user
134 print "<li>Connecting to \"$wgDBname\" as superuser \"$wgDBsuperuser\" to check rights...";
135 @$this->mConn = pg_connect("$hstring dbname=$wgDBname user=$user password=$password");
136 if ( $this->mConn == false ) {
137 print "<b>FAILED TO CONNECT!</b></li>";
138 dieout("</uL>");
139 }
140 print "OK!";
141 print "<li>Checking that tsearch2 is installed in the database \"$wgDBname\"...";
142 if (! $this->tableExists("pg_ts_cfg", $wgDBts2schema)) {
143 print "<b>FAILED</b>. tsearch2 must be installed in the database \"$wgDBname\".";
144 print "Please see 'http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>";
145 print " for instructions or ask on #postgresql on irc.freenode.net</li>\n";
146 dieout("</ul>");
147 }
148 print "OK</li>\n";
149 print "Ensuring that user \"$wgDBuser\" has select rights on the tsearch2 tables...";
150 foreach (array('cfg','cfgmap','dict','parser') as $table) {
151 $SQL = "GRANT SELECT ON pg_ts_$table TO \"$wgDBuser\"";
152 $this->doQuery($SQL);
153 }
154
155 $wgDBsuperuser = '';
156 return true; ## Reconnect as regular user
157 }
158
159 if (!defined('POSTGRES_SEARCHPATH')) {
160
161 ## Do we have the basic tsearch2 table?
162 print "<li>Checking for tsearch2 in the schema \"$wgDBts2schema\"...";
163 if (! $this->tableExists("pg_ts_dict", $wgDBts2schema)) {
164 print "<b>FAILED</b>. Make sure tsearch2 is installed. See <a href=";
165 print "'http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>";
166 print " for instructions.</li>\n";
167 dieout("</ul>");
168 }
169 print "OK</li>\n";
170
171 ## Does this user have the rights to the tsearch2 tables?
172 print "<li>Checking tsearch2 permissions...";
173 $SQL = "SELECT 1 FROM $wgDBts2schema.pg_ts_cfg";
174 error_reporting( 0 );
175 $res = $this->doQuery($SQL);
176 error_reporting( E_ALL );
177 if (!$res) {
178 print "<b>FAILED</b>. Make sure that the user \"$wgDBuser\" has SELECT access to the tsearch2 tables</li>\n";
179 dieout("</uL>");
180 }
181 print "OK</li>";
182
183 ## Do we have plpgsql installed?
184 print "<li>Checking for plpgsql ...";
185 $SQL = "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'";
186 $rows = $this->numRows($this->doQuery($SQL));
187 if ($rows < 1) {
188 print "<b>FAILED</b>. Make sure the language plpgsql is installed for the database <tt>$wgDBname</tt></li>";
189 dieout("</ul>");
190 }
191 print "OK</li>\n";
192
193 ## Does the schema already exist? Who owns it?
194 $result = $this->schemaExists($wgDBmwschema);
195 if (!$result) {
196 print "<li>Creating schema <b>$wgDBmwschema</b> ...";
197 $result = $this->doQuery("CREATE SCHEMA $wgDBmwschema");
198 if (!$result) {
199 print "<b>FAILED</b>.</li>\n";
200 return false;
201 }
202 print "ok</li>\n";
203 }
204 else if ($result != $user) {
205 print "<li>Schema \"$wgDBmwschema\" exists but is not owned by \"$user\". Not ideal.</li>\n";
206 }
207 else {
208 print "<li>Schema \"$wgDBmwschema\" exists and is owned by \"$user\". Excellent.</li>\n";
209 }
210
211 ## Fix up the search paths if needed
212 print "<li>Setting the search path for user \"$user\" ...";
213 $path = "$wgDBmwschema";
214 if ($wgDBts2schema !== $wgDBmwschema)
215 $path .= ", $wgDBts2schema";
216 if ($wgDBmwschema !== 'public' and $wgDBts2schema !== 'public')
217 $path .= ", public";
218 $SQL = "ALTER USER $user SET search_path = $path";
219 $result = pg_query($this->mConn, $SQL);
220 if (!$result) {
221 print "<b>FAILED</b>.</li>\n";
222 return false;
223 }
224 print "ok</li>\n";
225 ## Set for the rest of this session
226 $SQL = "SET search_path = $path";
227 $result = pg_query($this->mConn, $SQL);
228 if (!$result) {
229 print "<li>Failed to set search_path</li>\n";
230 return false;
231 }
232 define( "POSTGRES_SEARCHPATH", $path );
233 }}
234
235 return $this->mConn;
236 }
237
238 /**
239 * Closes a database connection, if it is open
240 * Returns success, true if already closed
241 */
242 function close() {
243 $this->mOpened = false;
244 if ( $this->mConn ) {
245 return pg_close( $this->mConn );
246 } else {
247 return true;
248 }
249 }
250
251 function doQuery( $sql ) {
252 return $this->mLastResult=pg_query( $this->mConn , $sql);
253 }
254
255 function queryIgnore( $sql, $fname = '' ) {
256 return $this->query( $sql, $fname, true );
257 }
258
259 function freeResult( $res ) {
260 if ( !@pg_free_result( $res ) ) {
261 throw new DBUnexpectedError($this, "Unable to free PostgreSQL result\n" );
262 }
263 }
264
265 function fetchObject( $res ) {
266 @$row = pg_fetch_object( $res );
267 # FIXME: HACK HACK HACK HACK debug
268
269 # TODO:
270 # hashar : not sure if the following test really trigger if the object
271 # fetching failled.
272 if( pg_last_error($this->mConn) ) {
273 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) );
274 }
275 return $row;
276 }
277
278 function fetchRow( $res ) {
279 @$row = pg_fetch_array( $res );
280 if( pg_last_error($this->mConn) ) {
281 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) );
282 }
283 return $row;
284 }
285
286 function numRows( $res ) {
287 @$n = pg_num_rows( $res );
288 if( pg_last_error($this->mConn) ) {
289 throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) );
290 }
291 return $n;
292 }
293 function numFields( $res ) { return pg_num_fields( $res ); }
294 function fieldName( $res, $n ) { return pg_field_name( $res, $n ); }
295
296 /**
297 * This must be called after nextSequenceVal
298 */
299 function insertId() {
300 return $this->mInsertId;
301 }
302
303 function dataSeek( $res, $row ) { return pg_result_seek( $res, $row ); }
304 function lastError() {
305 if ( $this->mConn ) {
306 return pg_last_error();
307 }
308 else {
309 return "No database connection";
310 }
311 }
312 function lastErrno() {
313 return pg_last_error() ? 1 : 0;
314 }
315
316 function affectedRows() {
317 return pg_affected_rows( $this->mLastResult );
318 }
319
320 /**
321 * Returns information about an index
322 * If errors are explicitly ignored, returns NULL on failure
323 */
324 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
325 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
326 $res = $this->query( $sql, $fname );
327 if ( !$res ) {
328 return NULL;
329 }
330
331 while ( $row = $this->fetchObject( $res ) ) {
332 if ( $row->indexname == $index ) {
333 return $row;
334 }
335 }
336 return false;
337 }
338
339 function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
340 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'".
341 " AND indexdef LIKE 'CREATE UNIQUE%({$index})'";
342 $res = $this->query( $sql, $fname );
343 if ( !$res )
344 return NULL;
345 while ($row = $this->fetchObject( $res ))
346 return true;
347 return false;
348
349 }
350
351 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
352 # PostgreSQL doesn't support options
353 # We have a go at faking one of them
354 # TODO: DELAYED, LOW_PRIORITY
355
356 if ( !is_array($options))
357 $options = array($options);
358
359 if ( in_array( 'IGNORE', $options ) )
360 $oldIgnore = $this->ignoreErrors( true );
361
362 # IGNORE is performed using single-row inserts, ignoring errors in each
363 # FIXME: need some way to distiguish between key collision and other types of error
364 $oldIgnore = $this->ignoreErrors( true );
365 if ( !is_array( reset( $a ) ) ) {
366 $a = array( $a );
367 }
368 foreach ( $a as $row ) {
369 parent::insert( $table, $row, $fname, array() );
370 }
371 $this->ignoreErrors( $oldIgnore );
372 $retVal = true;
373
374 if ( in_array( 'IGNORE', $options ) )
375 $this->ignoreErrors( $oldIgnore );
376
377 return $retVal;
378 }
379
380 function tableName( $name ) {
381 # Replace backticks into double quotes
382 $name = strtr($name,'`','"');
383
384 # Now quote PG reserved keywords
385 switch( $name ) {
386 case 'user':
387 case 'old':
388 case 'group':
389 return '"' . $name . '"';
390
391 default:
392 return $name;
393 }
394 }
395
396 /**
397 * Return the next in a sequence, save the value for retrieval via insertId()
398 */
399 function nextSequenceValue( $seqName ) {
400 $safeseq = preg_replace( "/'/", "''", $seqName );
401 $res = $this->query( "SELECT nextval('$safeseq')" );
402 $row = $this->fetchRow( $res );
403 $this->mInsertId = $row[0];
404 $this->freeResult( $res );
405 return $this->mInsertId;
406 }
407
408 /**
409 * USE INDEX clause
410 * PostgreSQL doesn't have them and returns ""
411 */
412 function useIndexClause( $index ) {
413 return '';
414 }
415
416 # REPLACE query wrapper
417 # PostgreSQL simulates this with a DELETE followed by INSERT
418 # $row is the row to insert, an associative array
419 # $uniqueIndexes is an array of indexes. Each element may be either a
420 # field name or an array of field names
421 #
422 # It may be more efficient to leave off unique indexes which are unlikely to collide.
423 # However if you do this, you run the risk of encountering errors which wouldn't have
424 # occurred in MySQL
425 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
426 $table = $this->tableName( $table );
427
428 if (count($rows)==0) {
429 return;
430 }
431
432 # Single row case
433 if ( !is_array( reset( $rows ) ) ) {
434 $rows = array( $rows );
435 }
436
437 foreach( $rows as $row ) {
438 # Delete rows which collide
439 if ( $uniqueIndexes ) {
440 $sql = "DELETE FROM $table WHERE ";
441 $first = true;
442 foreach ( $uniqueIndexes as $index ) {
443 if ( $first ) {
444 $first = false;
445 $sql .= "(";
446 } else {
447 $sql .= ') OR (';
448 }
449 if ( is_array( $index ) ) {
450 $first2 = true;
451 foreach ( $index as $col ) {
452 if ( $first2 ) {
453 $first2 = false;
454 } else {
455 $sql .= ' AND ';
456 }
457 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
458 }
459 } else {
460 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
461 }
462 }
463 $sql .= ')';
464 $this->query( $sql, $fname );
465 }
466
467 # Now insert the row
468 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
469 $this->makeList( $row, LIST_COMMA ) . ')';
470 $this->query( $sql, $fname );
471 }
472 }
473
474 # DELETE where the condition is a join
475 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
476 if ( !$conds ) {
477 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
478 }
479
480 $delTable = $this->tableName( $delTable );
481 $joinTable = $this->tableName( $joinTable );
482 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
483 if ( $conds != '*' ) {
484 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
485 }
486 $sql .= ')';
487
488 $this->query( $sql, $fname );
489 }
490
491 # Returns the size of a text field, or -1 for "unlimited"
492 function textFieldSize( $table, $field ) {
493 $table = $this->tableName( $table );
494 $sql = "SELECT t.typname as ftype,a.atttypmod as size
495 FROM pg_class c, pg_attribute a, pg_type t
496 WHERE relname='$table' AND a.attrelid=c.oid AND
497 a.atttypid=t.oid and a.attname='$field'";
498 $res =$this->query($sql);
499 $row=$this->fetchObject($res);
500 if ($row->ftype=="varchar") {
501 $size=$row->size-4;
502 } else {
503 $size=$row->size;
504 }
505 $this->freeResult( $res );
506 return $size;
507 }
508
509 function lowPriorityOption() {
510 return '';
511 }
512
513 function limitResult($sql, $limit,$offset) {
514 return "$sql LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
515 }
516
517 /**
518 * Returns an SQL expression for a simple conditional.
519 * Uses CASE on PostgreSQL.
520 *
521 * @param string $cond SQL expression which will result in a boolean value
522 * @param string $trueVal SQL expression to return if true
523 * @param string $falseVal SQL expression to return if false
524 * @return string SQL fragment
525 */
526 function conditional( $cond, $trueVal, $falseVal ) {
527 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
528 }
529
530 # FIXME: actually detecting deadlocks might be nice
531 function wasDeadlock() {
532 return false;
533 }
534
535 function timestamp( $ts=0 ) {
536 return wfTimestamp(TS_POSTGRES,$ts);
537 }
538
539 /**
540 * Return aggregated value function call
541 */
542 function aggregateValue ($valuedata,$valuename='value') {
543 return $valuedata;
544 }
545
546
547 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
548 $message = "A database error has occurred\n" .
549 "Query: $sql\n" .
550 "Function: $fname\n" .
551 "Error: $errno $error\n";
552 throw new DBUnexpectedError($this, $message);
553 }
554
555 /**
556 * @return string wikitext of a link to the server software's web site
557 */
558 function getSoftwareLink() {
559 return "[http://www.postgresql.org/ PostgreSQL]";
560 }
561
562 /**
563 * @return string Version information from the database
564 */
565 function getServerVersion() {
566 $res = $this->query( "SELECT version()" );
567 $row = $this->fetchRow( $res );
568 $version = $row[0];
569 $this->freeResult( $res );
570 return $version;
571 }
572
573
574 /**
575 * Query whether a given table exists (in the given schema, or the default mw one if not given)
576 */
577 function tableExists( $table, $schema = false ) {
578 global $wgDBmwschema;
579 if (! $schema )
580 $schema = $wgDBmwschema;
581 $etable = preg_replace("/'/", "''", $table);
582 $eschema = preg_replace("/'/", "''", $schema);
583 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
584 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema'";
585 $res = $this->query( $SQL );
586 $count = $res ? pg_num_rows($res) : 0;
587 if ($res)
588 $this->freeResult( $res );
589 return $count;
590 }
591
592
593 /**
594 * Query whether a given schema exists. Returns the name of the owner
595 */
596 function schemaExists( $schema ) {
597 $eschema = preg_replace("/'/", "''", $schema);
598 $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
599 ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'";
600 $res = $this->query( $SQL );
601 $owner = $res ? pg_num_rows($res) ? pg_fetch_result($res, 0, 0) : false : false;
602 if ($res)
603 $this->freeResult($res);
604 return $owner;
605 }
606
607 /**
608 * Query whether a given column exists in the mediawiki schema
609 */
610 function fieldExists( $table, $field ) {
611 global $wgDBmwschema;
612 $etable = preg_replace("/'/", "''", $table);
613 $eschema = preg_replace("/'/", "''", $wgDBmwschema);
614 $ecol = preg_replace("/'/", "''", $field);
615 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a "
616 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' "
617 . "AND a.attrelid = c.oid AND a.attname = '$ecol'";
618 $res = $this->query( $SQL );
619 $count = $res ? pg_num_rows($res) : 0;
620 if ($res)
621 $this->freeResult( $res );
622 return $count;
623 }
624
625 function fieldInfo( $table, $field ) {
626 $res = $this->query( "SELECT $field FROM $table LIMIT 1" );
627 $type = pg_field_type( $res, 0 );
628 return $type;
629 }
630
631 function begin( $fname = 'DatabasePostgrs::begin' ) {
632 $this->query( 'BEGIN', $fname );
633 $this->mTrxLevel = 1;
634 }
635 function immediateCommit( $fname = 'DatabasePostgres::immediateCommit' ) {
636 return true;
637 }
638 function commit( $fname = 'DatabasePostgres::commit' ) {
639 $this->query( 'COMMIT', $fname );
640 $this->mTrxLevel = 0;
641 }
642
643 /* Not even sure why this is used in the main codebase... */
644 function limitResultForUpdate($sql, $num) {
645 return $sql;
646 }
647
648 function setup_database() {
649 global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport;
650
651 dbsource( "../maintenance/postgres/tables.sql", $this);
652
653 ## Update version information
654 $mwv = $this->addQuotes($wgVersion);
655 $pgv = $this->addQuotes($this->getServerVersion());
656 $pgu = $this->addQuotes($this->mUser);
657 $mws = $this->addQuotes($wgDBmwschema);
658 $tss = $this->addQuotes($wgDBts2schema);
659 $pgp = $this->addQuotes($wgDBport);
660 $dbn = $this->addQuotes($this->mDBname);
661
662 $SQL = "UPDATE mediawiki_version SET mw_version=$mwv, pg_version=$pgv, pg_user=$pgu, ".
663 "mw_schema = $mws, ts2_schema = $tss, pg_port=$pgp, pg_dbname=$dbn ".
664 "WHERE type = 'Creation'";
665 $this->query($SQL);
666
667 ## Avoid the non-standard "REPLACE INTO" syntax
668 $f = fopen( "../maintenance/interwiki.sql", 'r' );
669 if ($f == false ) {
670 dieout( "<li>Could not find the interwiki.sql file");
671 }
672 ## We simply assume it is already empty as we have just created it
673 $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
674 while ( ! feof( $f ) ) {
675 $line = fgets($f,1024);
676 if (!preg_match("/^\s*(\(.+?),(\d)\)/", $line, $matches)) {
677 continue;
678 }
679 $yesno = $matches[2]; ## ? "'true'" : "'false'";
680 $this->query("$SQL $matches[1],$matches[2])");
681 }
682 print " (table interwiki successfully populated)...\n";
683 }
684
685 function encodeBlob($b) {
686 return array('bytea',pg_escape_bytea($b));
687 }
688 function decodeBlob($b) {
689 return pg_unescape_bytea( $b );
690 }
691
692 function strencode( $s ) { ## Should not be called by us
693 return pg_escape_string( $s );
694 }
695
696 function addQuotes( $s ) {
697 if ( is_null( $s ) ) {
698 return 'NULL';
699 } else if (is_array( $s )) { ## Assume it is bytea data
700 return "E'$s[1]'";
701 }
702 return "'" . pg_escape_string($s) . "'";
703 return "E'" . pg_escape_string($s) . "'";
704 }
705
706 }
707
708 ?>