2fe2c79a6898b49f801078b7789eea4be940bded
[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 reserved words with better ones
382 switch( $name ) {
383 case 'user':
384 return 'mwuser';
385 case 'text':
386 return 'pagecontent';
387 default:
388 return $name;
389 }
390 }
391
392 /**
393 * Return the next in a sequence, save the value for retrieval via insertId()
394 */
395 function nextSequenceValue( $seqName ) {
396 $safeseq = preg_replace( "/'/", "''", $seqName );
397 $res = $this->query( "SELECT nextval('$safeseq')" );
398 $row = $this->fetchRow( $res );
399 $this->mInsertId = $row[0];
400 $this->freeResult( $res );
401 return $this->mInsertId;
402 }
403
404 /**
405 * USE INDEX clause
406 * PostgreSQL doesn't have them and returns ""
407 */
408 function useIndexClause( $index ) {
409 return '';
410 }
411
412 # REPLACE query wrapper
413 # PostgreSQL simulates this with a DELETE followed by INSERT
414 # $row is the row to insert, an associative array
415 # $uniqueIndexes is an array of indexes. Each element may be either a
416 # field name or an array of field names
417 #
418 # It may be more efficient to leave off unique indexes which are unlikely to collide.
419 # However if you do this, you run the risk of encountering errors which wouldn't have
420 # occurred in MySQL
421 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
422 $table = $this->tableName( $table );
423
424 if (count($rows)==0) {
425 return;
426 }
427
428 # Single row case
429 if ( !is_array( reset( $rows ) ) ) {
430 $rows = array( $rows );
431 }
432
433 foreach( $rows as $row ) {
434 # Delete rows which collide
435 if ( $uniqueIndexes ) {
436 $sql = "DELETE FROM $table WHERE ";
437 $first = true;
438 foreach ( $uniqueIndexes as $index ) {
439 if ( $first ) {
440 $first = false;
441 $sql .= "(";
442 } else {
443 $sql .= ') OR (';
444 }
445 if ( is_array( $index ) ) {
446 $first2 = true;
447 foreach ( $index as $col ) {
448 if ( $first2 ) {
449 $first2 = false;
450 } else {
451 $sql .= ' AND ';
452 }
453 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
454 }
455 } else {
456 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
457 }
458 }
459 $sql .= ')';
460 $this->query( $sql, $fname );
461 }
462
463 # Now insert the row
464 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
465 $this->makeList( $row, LIST_COMMA ) . ')';
466 $this->query( $sql, $fname );
467 }
468 }
469
470 # DELETE where the condition is a join
471 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
472 if ( !$conds ) {
473 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
474 }
475
476 $delTable = $this->tableName( $delTable );
477 $joinTable = $this->tableName( $joinTable );
478 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
479 if ( $conds != '*' ) {
480 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
481 }
482 $sql .= ')';
483
484 $this->query( $sql, $fname );
485 }
486
487 # Returns the size of a text field, or -1 for "unlimited"
488 function textFieldSize( $table, $field ) {
489 $table = $this->tableName( $table );
490 $sql = "SELECT t.typname as ftype,a.atttypmod as size
491 FROM pg_class c, pg_attribute a, pg_type t
492 WHERE relname='$table' AND a.attrelid=c.oid AND
493 a.atttypid=t.oid and a.attname='$field'";
494 $res =$this->query($sql);
495 $row=$this->fetchObject($res);
496 if ($row->ftype=="varchar") {
497 $size=$row->size-4;
498 } else {
499 $size=$row->size;
500 }
501 $this->freeResult( $res );
502 return $size;
503 }
504
505 function lowPriorityOption() {
506 return '';
507 }
508
509 function limitResult($sql, $limit,$offset) {
510 return "$sql LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":"");
511 }
512
513 /**
514 * Returns an SQL expression for a simple conditional.
515 * Uses CASE on PostgreSQL.
516 *
517 * @param string $cond SQL expression which will result in a boolean value
518 * @param string $trueVal SQL expression to return if true
519 * @param string $falseVal SQL expression to return if false
520 * @return string SQL fragment
521 */
522 function conditional( $cond, $trueVal, $falseVal ) {
523 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
524 }
525
526 # FIXME: actually detecting deadlocks might be nice
527 function wasDeadlock() {
528 return false;
529 }
530
531 function timestamp( $ts=0 ) {
532 return wfTimestamp(TS_POSTGRES,$ts);
533 }
534
535 /**
536 * Return aggregated value function call
537 */
538 function aggregateValue ($valuedata,$valuename='value') {
539 return $valuedata;
540 }
541
542
543 function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
544 $message = "A database error has occurred\n" .
545 "Query: $sql\n" .
546 "Function: $fname\n" .
547 "Error: $errno $error\n";
548 throw new DBUnexpectedError($this, $message);
549 }
550
551 /**
552 * @return string wikitext of a link to the server software's web site
553 */
554 function getSoftwareLink() {
555 return "[http://www.postgresql.org/ PostgreSQL]";
556 }
557
558 /**
559 * @return string Version information from the database
560 */
561 function getServerVersion() {
562 $res = $this->query( "SELECT version()" );
563 $row = $this->fetchRow( $res );
564 $version = $row[0];
565 $this->freeResult( $res );
566 return $version;
567 }
568
569
570 /**
571 * Query whether a given table exists (in the given schema, or the default mw one if not given)
572 */
573 function tableExists( $table, $schema = false ) {
574 global $wgDBmwschema;
575 if (! $schema )
576 $schema = $wgDBmwschema;
577 $etable = preg_replace("/'/", "''", $table);
578 $eschema = preg_replace("/'/", "''", $schema);
579 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
580 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema'";
581 $res = $this->query( $SQL );
582 $count = $res ? pg_num_rows($res) : 0;
583 if ($res)
584 $this->freeResult( $res );
585 return $count;
586 }
587
588
589 /**
590 * Query whether a given schema exists. Returns the name of the owner
591 */
592 function schemaExists( $schema ) {
593 $eschema = preg_replace("/'/", "''", $schema);
594 $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r "
595 ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'";
596 $res = $this->query( $SQL );
597 $owner = $res ? pg_num_rows($res) ? pg_fetch_result($res, 0, 0) : false : false;
598 if ($res)
599 $this->freeResult($res);
600 return $owner;
601 }
602
603 /**
604 * Query whether a given column exists in the mediawiki schema
605 */
606 function fieldExists( $table, $field ) {
607 global $wgDBmwschema;
608 $etable = preg_replace("/'/", "''", $table);
609 $eschema = preg_replace("/'/", "''", $wgDBmwschema);
610 $ecol = preg_replace("/'/", "''", $field);
611 $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n, pg_catalog.pg_attribute a "
612 . "WHERE c.relnamespace = n.oid AND c.relname = '$etable' AND n.nspname = '$eschema' "
613 . "AND a.attrelid = c.oid AND a.attname = '$ecol'";
614 $res = $this->query( $SQL );
615 $count = $res ? pg_num_rows($res) : 0;
616 if ($res)
617 $this->freeResult( $res );
618 return $count;
619 }
620
621 function fieldInfo( $table, $field ) {
622 $res = $this->query( "SELECT $field FROM $table LIMIT 1" );
623 $type = pg_field_type( $res, 0 );
624 return $type;
625 }
626
627 function begin( $fname = 'DatabasePostgrs::begin' ) {
628 $this->query( 'BEGIN', $fname );
629 $this->mTrxLevel = 1;
630 }
631 function immediateCommit( $fname = 'DatabasePostgres::immediateCommit' ) {
632 return true;
633 }
634 function commit( $fname = 'DatabasePostgres::commit' ) {
635 $this->query( 'COMMIT', $fname );
636 $this->mTrxLevel = 0;
637 }
638
639 /* Not even sure why this is used in the main codebase... */
640 function limitResultForUpdate($sql, $num) {
641 return $sql;
642 }
643
644 function setup_database() {
645 global $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgDBport;
646
647 dbsource( "../maintenance/postgres/tables.sql", $this);
648
649 ## Update version information
650 $mwv = $this->addQuotes($wgVersion);
651 $pgv = $this->addQuotes($this->getServerVersion());
652 $pgu = $this->addQuotes($this->mUser);
653 $mws = $this->addQuotes($wgDBmwschema);
654 $tss = $this->addQuotes($wgDBts2schema);
655 $pgp = $this->addQuotes($wgDBport);
656 $dbn = $this->addQuotes($this->mDBname);
657
658 $SQL = "UPDATE mediawiki_version SET mw_version=$mwv, pg_version=$pgv, pg_user=$pgu, ".
659 "mw_schema = $mws, ts2_schema = $tss, pg_port=$pgp, pg_dbname=$dbn ".
660 "WHERE type = 'Creation'";
661 $this->query($SQL);
662
663 ## Avoid the non-standard "REPLACE INTO" syntax
664 $f = fopen( "../maintenance/interwiki.sql", 'r' );
665 if ($f == false ) {
666 dieout( "<li>Could not find the interwiki.sql file");
667 }
668 ## We simply assume it is already empty as we have just created it
669 $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES ";
670 while ( ! feof( $f ) ) {
671 $line = fgets($f,1024);
672 if (!preg_match("/^\s*(\(.+?),(\d)\)/", $line, $matches)) {
673 continue;
674 }
675 $yesno = $matches[2]; ## ? "'true'" : "'false'";
676 $this->query("$SQL $matches[1],$matches[2])");
677 }
678 print " (table interwiki successfully populated)...\n";
679 }
680
681 function encodeBlob($b) {
682 return array('bytea',pg_escape_bytea($b));
683 }
684 function decodeBlob($b) {
685 return pg_unescape_bytea( $b );
686 }
687
688 function strencode( $s ) { ## Should not be called by us
689 return pg_escape_string( $s );
690 }
691
692 function addQuotes( $s ) {
693 if ( is_null( $s ) ) {
694 return 'NULL';
695 } else if (is_array( $s )) { ## Assume it is bytea data
696 return "E'$s[1]'";
697 }
698 return "'" . pg_escape_string($s) . "'";
699 return "E'" . pg_escape_string($s) . "'";
700 }
701
702 }
703
704 ?>