Merge "RCFilters: Adjust to use MenuTagMultiselectWidget"
[lhc/web/wiklou.git] / includes / libs / rdbms / database / DatabasePostgres.php
1 <?php
2 /**
3 * This is the Postgres database abstraction layer.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup Database
22 */
23 namespace Wikimedia\Rdbms;
24
25 use Wikimedia\Timestamp\ConvertibleTimestamp;
26 use Wikimedia\WaitConditionLoop;
27 use MediaWiki;
28 use DBUnexpectedError;
29 use DBConnectionError;
30 use Exception;
31
32 /**
33 * @ingroup Database
34 */
35 class DatabasePostgres extends Database {
36 /** @var int|bool */
37 protected $port;
38
39 /** @var resource */
40 protected $mLastResult = null;
41 /** @var int The number of rows affected as an integer */
42 protected $mAffectedRows = null;
43
44 /** @var int */
45 private $mInsertId = null;
46 /** @var float|string */
47 private $numericVersion = null;
48 /** @var string Connect string to open a PostgreSQL connection */
49 private $connectString;
50 /** @var string */
51 private $mCoreSchema;
52 /** @var string[] Map of (reserved table name => alternate table name) */
53 private $keywordTableMap = [];
54
55 /**
56 * @see Database::__construct()
57 * @param array $params Additional parameters include:
58 * - keywordTableMap : Map of reserved table names to alternative table names to use
59 */
60 public function __construct( array $params ) {
61 $this->port = isset( $params['port'] ) ? $params['port'] : false;
62 $this->keywordTableMap = isset( $params['keywordTableMap'] )
63 ? $params['keywordTableMap']
64 : [];
65
66 parent::__construct( $params );
67 }
68
69 public function getType() {
70 return 'postgres';
71 }
72
73 public function implicitGroupby() {
74 return false;
75 }
76
77 public function implicitOrderby() {
78 return false;
79 }
80
81 public function hasConstraint( $name ) {
82 $conn = $this->getBindingHandle();
83
84 $sql = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n " .
85 "WHERE c.connamespace = n.oid AND conname = '" .
86 pg_escape_string( $conn, $name ) . "' AND n.nspname = '" .
87 pg_escape_string( $conn, $this->getCoreSchema() ) . "'";
88 $res = $this->doQuery( $sql );
89
90 return $this->numRows( $res );
91 }
92
93 public function open( $server, $user, $password, $dbName ) {
94 # Test for Postgres support, to avoid suppressed fatal error
95 if ( !function_exists( 'pg_connect' ) ) {
96 throw new DBConnectionError(
97 $this,
98 "Postgres functions missing, have you compiled PHP with the --with-pgsql\n" .
99 "option? (Note: if you recently installed PHP, you may need to restart your\n" .
100 "webserver and database)\n"
101 );
102 }
103
104 $this->mServer = $server;
105 $this->mUser = $user;
106 $this->mPassword = $password;
107 $this->mDBname = $dbName;
108
109 $connectVars = [
110 'dbname' => $dbName,
111 'user' => $user,
112 'password' => $password
113 ];
114 if ( $server != false && $server != '' ) {
115 $connectVars['host'] = $server;
116 }
117 if ( (int)$this->port > 0 ) {
118 $connectVars['port'] = (int)$this->port;
119 }
120 if ( $this->mFlags & self::DBO_SSL ) {
121 $connectVars['sslmode'] = 1;
122 }
123
124 $this->connectString = $this->makeConnectionString( $connectVars );
125 $this->close();
126 $this->installErrorHandler();
127
128 try {
129 // Use new connections to let LoadBalancer/LBFactory handle reuse
130 $this->mConn = pg_connect( $this->connectString, PGSQL_CONNECT_FORCE_NEW );
131 } catch ( Exception $ex ) {
132 $this->restoreErrorHandler();
133 throw $ex;
134 }
135
136 $phpError = $this->restoreErrorHandler();
137
138 if ( !$this->mConn ) {
139 $this->queryLogger->debug(
140 "DB connection error\n" .
141 "Server: $server, Database: $dbName, User: $user, Password: " .
142 substr( $password, 0, 3 ) . "...\n"
143 );
144 $this->queryLogger->debug( $this->lastError() . "\n" );
145 throw new DBConnectionError( $this, str_replace( "\n", ' ', $phpError ) );
146 }
147
148 $this->mOpened = true;
149
150 # If called from the command-line (e.g. importDump), only show errors
151 if ( $this->cliMode ) {
152 $this->doQuery( "SET client_min_messages = 'ERROR'" );
153 }
154
155 $this->query( "SET client_encoding='UTF8'", __METHOD__ );
156 $this->query( "SET datestyle = 'ISO, YMD'", __METHOD__ );
157 $this->query( "SET timezone = 'GMT'", __METHOD__ );
158 $this->query( "SET standard_conforming_strings = on", __METHOD__ );
159 if ( $this->getServerVersion() >= 9.0 ) {
160 $this->query( "SET bytea_output = 'escape'", __METHOD__ ); // PHP bug 53127
161 }
162
163 $this->determineCoreSchema( $this->mSchema );
164 // The schema to be used is now in the search path; no need for explicit qualification
165 $this->mSchema = '';
166
167 return $this->mConn;
168 }
169
170 /**
171 * Postgres doesn't support selectDB in the same way MySQL does. So if the
172 * DB name doesn't match the open connection, open a new one
173 * @param string $db
174 * @return bool
175 */
176 public function selectDB( $db ) {
177 if ( $this->mDBname !== $db ) {
178 return (bool)$this->open( $this->mServer, $this->mUser, $this->mPassword, $db );
179 } else {
180 return true;
181 }
182 }
183
184 /**
185 * @param string[] $vars
186 * @return string
187 */
188 private function makeConnectionString( $vars ) {
189 $s = '';
190 foreach ( $vars as $name => $value ) {
191 $s .= "$name='" . str_replace( "'", "\\'", $value ) . "' ";
192 }
193
194 return $s;
195 }
196
197 protected function closeConnection() {
198 return $this->mConn ? pg_close( $this->mConn ) : true;
199 }
200
201 public function doQuery( $sql ) {
202 $conn = $this->getBindingHandle();
203
204 $sql = mb_convert_encoding( $sql, 'UTF-8' );
205 // Clear previously left over PQresult
206 while ( $res = pg_get_result( $conn ) ) {
207 pg_free_result( $res );
208 }
209 if ( pg_send_query( $conn, $sql ) === false ) {
210 throw new DBUnexpectedError( $this, "Unable to post new query to PostgreSQL\n" );
211 }
212 $this->mLastResult = pg_get_result( $conn );
213 $this->mAffectedRows = null;
214 if ( pg_result_error( $this->mLastResult ) ) {
215 return false;
216 }
217
218 return $this->mLastResult;
219 }
220
221 protected function dumpError() {
222 $diags = [
223 PGSQL_DIAG_SEVERITY,
224 PGSQL_DIAG_SQLSTATE,
225 PGSQL_DIAG_MESSAGE_PRIMARY,
226 PGSQL_DIAG_MESSAGE_DETAIL,
227 PGSQL_DIAG_MESSAGE_HINT,
228 PGSQL_DIAG_STATEMENT_POSITION,
229 PGSQL_DIAG_INTERNAL_POSITION,
230 PGSQL_DIAG_INTERNAL_QUERY,
231 PGSQL_DIAG_CONTEXT,
232 PGSQL_DIAG_SOURCE_FILE,
233 PGSQL_DIAG_SOURCE_LINE,
234 PGSQL_DIAG_SOURCE_FUNCTION
235 ];
236 foreach ( $diags as $d ) {
237 $this->queryLogger->debug( sprintf( "PgSQL ERROR(%d): %s\n",
238 $d, pg_result_error_field( $this->mLastResult, $d ) ) );
239 }
240 }
241
242 public function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) {
243 if ( $tempIgnore ) {
244 /* Check for constraint violation */
245 if ( $errno === '23505' ) {
246 parent::reportQueryError( $error, $errno, $sql, $fname, $tempIgnore );
247
248 return;
249 }
250 }
251 /* Transaction stays in the ERROR state until rolled back */
252 if ( $this->mTrxLevel ) {
253 $this->rollback( __METHOD__ );
254 }
255 parent::reportQueryError( $error, $errno, $sql, $fname, false );
256 }
257
258 public function freeResult( $res ) {
259 if ( $res instanceof ResultWrapper ) {
260 $res = $res->result;
261 }
262 MediaWiki\suppressWarnings();
263 $ok = pg_free_result( $res );
264 MediaWiki\restoreWarnings();
265 if ( !$ok ) {
266 throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" );
267 }
268 }
269
270 public function fetchObject( $res ) {
271 if ( $res instanceof ResultWrapper ) {
272 $res = $res->result;
273 }
274 MediaWiki\suppressWarnings();
275 $row = pg_fetch_object( $res );
276 MediaWiki\restoreWarnings();
277 # @todo FIXME: HACK HACK HACK HACK debug
278
279 # @todo hashar: not sure if the following test really trigger if the object
280 # fetching failed.
281 $conn = $this->getBindingHandle();
282 if ( pg_last_error( $conn ) ) {
283 throw new DBUnexpectedError(
284 $this,
285 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
286 );
287 }
288
289 return $row;
290 }
291
292 public function fetchRow( $res ) {
293 if ( $res instanceof ResultWrapper ) {
294 $res = $res->result;
295 }
296 MediaWiki\suppressWarnings();
297 $row = pg_fetch_array( $res );
298 MediaWiki\restoreWarnings();
299
300 $conn = $this->getBindingHandle();
301 if ( pg_last_error( $conn ) ) {
302 throw new DBUnexpectedError(
303 $this,
304 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
305 );
306 }
307
308 return $row;
309 }
310
311 public function numRows( $res ) {
312 if ( $res instanceof ResultWrapper ) {
313 $res = $res->result;
314 }
315 MediaWiki\suppressWarnings();
316 $n = pg_num_rows( $res );
317 MediaWiki\restoreWarnings();
318
319 $conn = $this->getBindingHandle();
320 if ( pg_last_error( $conn ) ) {
321 throw new DBUnexpectedError(
322 $this,
323 'SQL error: ' . htmlspecialchars( pg_last_error( $conn ) )
324 );
325 }
326
327 return $n;
328 }
329
330 public function numFields( $res ) {
331 if ( $res instanceof ResultWrapper ) {
332 $res = $res->result;
333 }
334
335 return pg_num_fields( $res );
336 }
337
338 public function fieldName( $res, $n ) {
339 if ( $res instanceof ResultWrapper ) {
340 $res = $res->result;
341 }
342
343 return pg_field_name( $res, $n );
344 }
345
346 /**
347 * Return the result of the last call to nextSequenceValue();
348 * This must be called after nextSequenceValue().
349 *
350 * @return int|null
351 */
352 public function insertId() {
353 return $this->mInsertId;
354 }
355
356 public function dataSeek( $res, $row ) {
357 if ( $res instanceof ResultWrapper ) {
358 $res = $res->result;
359 }
360
361 return pg_result_seek( $res, $row );
362 }
363
364 public function lastError() {
365 if ( $this->mConn ) {
366 if ( $this->mLastResult ) {
367 return pg_result_error( $this->mLastResult );
368 } else {
369 return pg_last_error();
370 }
371 }
372
373 return $this->getLastPHPError() ?: 'No database connection';
374 }
375
376 public function lastErrno() {
377 if ( $this->mLastResult ) {
378 return pg_result_error_field( $this->mLastResult, PGSQL_DIAG_SQLSTATE );
379 } else {
380 return false;
381 }
382 }
383
384 public function affectedRows() {
385 if ( !is_null( $this->mAffectedRows ) ) {
386 // Forced result for simulated queries
387 return $this->mAffectedRows;
388 }
389 if ( empty( $this->mLastResult ) ) {
390 return 0;
391 }
392
393 return pg_affected_rows( $this->mLastResult );
394 }
395
396 /**
397 * Estimate rows in dataset
398 * Returns estimated count, based on EXPLAIN output
399 * This is not necessarily an accurate estimate, so use sparingly
400 * Returns -1 if count cannot be found
401 * Takes same arguments as Database::select()
402 *
403 * @param string $table
404 * @param string $vars
405 * @param string $conds
406 * @param string $fname
407 * @param array $options
408 * @return int
409 */
410 public function estimateRowCount( $table, $vars = '*', $conds = '',
411 $fname = __METHOD__, $options = []
412 ) {
413 $options['EXPLAIN'] = true;
414 $res = $this->select( $table, $vars, $conds, $fname, $options );
415 $rows = -1;
416 if ( $res ) {
417 $row = $this->fetchRow( $res );
418 $count = [];
419 if ( preg_match( '/rows=(\d+)/', $row[0], $count ) ) {
420 $rows = (int)$count[1];
421 }
422 }
423
424 return $rows;
425 }
426
427 public function indexInfo( $table, $index, $fname = __METHOD__ ) {
428 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='$table'";
429 $res = $this->query( $sql, $fname );
430 if ( !$res ) {
431 return null;
432 }
433 foreach ( $res as $row ) {
434 if ( $row->indexname == $this->indexName( $index ) ) {
435 return $row;
436 }
437 }
438
439 return false;
440 }
441
442 public function indexAttributes( $index, $schema = false ) {
443 if ( $schema === false ) {
444 $schema = $this->getCoreSchema();
445 }
446 /*
447 * A subquery would be not needed if we didn't care about the order
448 * of attributes, but we do
449 */
450 $sql = <<<__INDEXATTR__
451
452 SELECT opcname,
453 attname,
454 i.indoption[s.g] as option,
455 pg_am.amname
456 FROM
457 (SELECT generate_series(array_lower(isub.indkey,1), array_upper(isub.indkey,1)) AS g
458 FROM
459 pg_index isub
460 JOIN pg_class cis
461 ON cis.oid=isub.indexrelid
462 JOIN pg_namespace ns
463 ON cis.relnamespace = ns.oid
464 WHERE cis.relname='$index' AND ns.nspname='$schema') AS s,
465 pg_attribute,
466 pg_opclass opcls,
467 pg_am,
468 pg_class ci
469 JOIN pg_index i
470 ON ci.oid=i.indexrelid
471 JOIN pg_class ct
472 ON ct.oid = i.indrelid
473 JOIN pg_namespace n
474 ON ci.relnamespace = n.oid
475 WHERE
476 ci.relname='$index' AND n.nspname='$schema'
477 AND attrelid = ct.oid
478 AND i.indkey[s.g] = attnum
479 AND i.indclass[s.g] = opcls.oid
480 AND pg_am.oid = opcls.opcmethod
481 __INDEXATTR__;
482 $res = $this->query( $sql, __METHOD__ );
483 $a = [];
484 if ( $res ) {
485 foreach ( $res as $row ) {
486 $a[] = [
487 $row->attname,
488 $row->opcname,
489 $row->amname,
490 $row->option ];
491 }
492 } else {
493 return null;
494 }
495
496 return $a;
497 }
498
499 public function indexUnique( $table, $index, $fname = __METHOD__ ) {
500 $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'" .
501 " AND indexdef LIKE 'CREATE UNIQUE%(" .
502 $this->strencode( $this->indexName( $index ) ) .
503 ")'";
504 $res = $this->query( $sql, $fname );
505 if ( !$res ) {
506 return null;
507 }
508
509 return $res->numRows() > 0;
510 }
511
512 public function selectSQLText(
513 $table, $vars, $conds = '', $fname = __METHOD__, $options = [], $join_conds = []
514 ) {
515 // Change the FOR UPDATE option as necessary based on the join conditions. Then pass
516 // to the parent function to get the actual SQL text.
517 // In Postgres when using FOR UPDATE, only the main table and tables that are inner joined
518 // can be locked. That means tables in an outer join cannot be FOR UPDATE locked. Trying to
519 // do so causes a DB error. This wrapper checks which tables can be locked and adjusts it
520 // accordingly.
521 // MySQL uses "ORDER BY NULL" as an optimization hint, but that is illegal in PostgreSQL.
522 if ( is_array( $options ) ) {
523 $forUpdateKey = array_search( 'FOR UPDATE', $options, true );
524 if ( $forUpdateKey !== false && $join_conds ) {
525 unset( $options[$forUpdateKey] );
526
527 foreach ( $join_conds as $table_cond => $join_cond ) {
528 if ( 0 === preg_match( '/^(?:LEFT|RIGHT|FULL)(?: OUTER)? JOIN$/i', $join_cond[0] ) ) {
529 $options['FOR UPDATE'][] = $table_cond;
530 }
531 }
532 }
533
534 if ( isset( $options['ORDER BY'] ) && $options['ORDER BY'] == 'NULL' ) {
535 unset( $options['ORDER BY'] );
536 }
537 }
538
539 return parent::selectSQLText( $table, $vars, $conds, $fname, $options, $join_conds );
540 }
541
542 /**
543 * INSERT wrapper, inserts an array into a table
544 *
545 * $args may be a single associative array, or an array of these with numeric keys,
546 * for multi-row insert (Postgres version 8.2 and above only).
547 *
548 * @param string $table Name of the table to insert to.
549 * @param array $args Items to insert into the table.
550 * @param string $fname Name of the function, for profiling
551 * @param array|string $options String or array. Valid options: IGNORE
552 * @return bool Success of insert operation. IGNORE always returns true.
553 */
554 public function insert( $table, $args, $fname = __METHOD__, $options = [] ) {
555 if ( !count( $args ) ) {
556 return true;
557 }
558
559 $table = $this->tableName( $table );
560 if ( !isset( $this->numericVersion ) ) {
561 $this->getServerVersion();
562 }
563
564 if ( !is_array( $options ) ) {
565 $options = [ $options ];
566 }
567
568 if ( isset( $args[0] ) && is_array( $args[0] ) ) {
569 $multi = true;
570 $keys = array_keys( $args[0] );
571 } else {
572 $multi = false;
573 $keys = array_keys( $args );
574 }
575
576 // If IGNORE is set, we use savepoints to emulate mysql's behavior
577 $savepoint = $olde = null;
578 $numrowsinserted = 0;
579 if ( in_array( 'IGNORE', $options ) ) {
580 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
581 $olde = error_reporting( 0 );
582 // For future use, we may want to track the number of actual inserts
583 // Right now, insert (all writes) simply return true/false
584 }
585
586 $sql = "INSERT INTO $table (" . implode( ',', $keys ) . ') VALUES ';
587
588 if ( $multi ) {
589 if ( $this->numericVersion >= 8.2 && !$savepoint ) {
590 $first = true;
591 foreach ( $args as $row ) {
592 if ( $first ) {
593 $first = false;
594 } else {
595 $sql .= ',';
596 }
597 $sql .= '(' . $this->makeList( $row ) . ')';
598 }
599 $res = (bool)$this->query( $sql, $fname, $savepoint );
600 } else {
601 $res = true;
602 $origsql = $sql;
603 foreach ( $args as $row ) {
604 $tempsql = $origsql;
605 $tempsql .= '(' . $this->makeList( $row ) . ')';
606
607 if ( $savepoint ) {
608 $savepoint->savepoint();
609 }
610
611 $tempres = (bool)$this->query( $tempsql, $fname, $savepoint );
612
613 if ( $savepoint ) {
614 $bar = pg_result_error( $this->mLastResult );
615 if ( $bar != false ) {
616 $savepoint->rollback();
617 } else {
618 $savepoint->release();
619 $numrowsinserted++;
620 }
621 }
622
623 // If any of them fail, we fail overall for this function call
624 // Note that this will be ignored if IGNORE is set
625 if ( !$tempres ) {
626 $res = false;
627 }
628 }
629 }
630 } else {
631 // Not multi, just a lone insert
632 if ( $savepoint ) {
633 $savepoint->savepoint();
634 }
635
636 $sql .= '(' . $this->makeList( $args ) . ')';
637 $res = (bool)$this->query( $sql, $fname, $savepoint );
638 if ( $savepoint ) {
639 $bar = pg_result_error( $this->mLastResult );
640 if ( $bar != false ) {
641 $savepoint->rollback();
642 } else {
643 $savepoint->release();
644 $numrowsinserted++;
645 }
646 }
647 }
648 if ( $savepoint ) {
649 error_reporting( $olde );
650 $savepoint->commit();
651
652 // Set the affected row count for the whole operation
653 $this->mAffectedRows = $numrowsinserted;
654
655 // IGNORE always returns true
656 return true;
657 }
658
659 return $res;
660 }
661
662 /**
663 * INSERT SELECT wrapper
664 * $varMap must be an associative array of the form [ 'dest1' => 'source1', ... ]
665 * Source items may be literals rather then field names, but strings should
666 * be quoted with Database::addQuotes()
667 * $conds may be "*" to copy the whole table
668 * srcTable may be an array of tables.
669 * @todo FIXME: Implement this a little better (seperate select/insert)?
670 *
671 * @param string $destTable
672 * @param array|string $srcTable
673 * @param array $varMap
674 * @param array $conds
675 * @param string $fname
676 * @param array $insertOptions
677 * @param array $selectOptions
678 * @return bool
679 */
680 public function nativeInsertSelect(
681 $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
682 $insertOptions = [], $selectOptions = []
683 ) {
684 $destTable = $this->tableName( $destTable );
685
686 if ( !is_array( $insertOptions ) ) {
687 $insertOptions = [ $insertOptions ];
688 }
689
690 /*
691 * If IGNORE is set, we use savepoints to emulate mysql's behavior
692 * Ignore LOW PRIORITY option, since it is MySQL-specific
693 */
694 $savepoint = $olde = null;
695 $numrowsinserted = 0;
696 if ( in_array( 'IGNORE', $insertOptions ) ) {
697 $savepoint = new SavepointPostgres( $this, 'mw', $this->queryLogger );
698 $olde = error_reporting( 0 );
699 $savepoint->savepoint();
700 }
701
702 if ( !is_array( $selectOptions ) ) {
703 $selectOptions = [ $selectOptions ];
704 }
705 list( $startOpts, $useIndex, $tailOpts, $ignoreIndex ) =
706 $this->makeSelectOptions( $selectOptions );
707 if ( is_array( $srcTable ) ) {
708 $srcTable = implode( ',', array_map( [ $this, 'tableName' ], $srcTable ) );
709 } else {
710 $srcTable = $this->tableName( $srcTable );
711 }
712
713 $sql = "INSERT INTO $destTable (" . implode( ',', array_keys( $varMap ) ) . ')' .
714 " SELECT $startOpts " . implode( ',', $varMap ) .
715 " FROM $srcTable $useIndex $ignoreIndex ";
716
717 if ( $conds != '*' ) {
718 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
719 }
720
721 $sql .= " $tailOpts";
722
723 $res = (bool)$this->query( $sql, $fname, $savepoint );
724 if ( $savepoint ) {
725 $bar = pg_result_error( $this->mLastResult );
726 if ( $bar != false ) {
727 $savepoint->rollback();
728 } else {
729 $savepoint->release();
730 $numrowsinserted++;
731 }
732 error_reporting( $olde );
733 $savepoint->commit();
734
735 // Set the affected row count for the whole operation
736 $this->mAffectedRows = $numrowsinserted;
737
738 // IGNORE always returns true
739 return true;
740 }
741
742 return $res;
743 }
744
745 public function tableName( $name, $format = 'quoted' ) {
746 // Replace reserved words with better ones
747 $name = $this->remappedTableName( $name );
748
749 return parent::tableName( $name, $format );
750 }
751
752 /**
753 * @param string $name
754 * @return string Value of $name or remapped name if $name is a reserved keyword
755 */
756 public function remappedTableName( $name ) {
757 return isset( $this->keywordTableMap[$name] ) ? $this->keywordTableMap[$name] : $name;
758 }
759
760 /**
761 * @param string $name
762 * @param string $format
763 * @return string Qualified and encoded (if requested) table name
764 */
765 public function realTableName( $name, $format = 'quoted' ) {
766 return parent::tableName( $name, $format );
767 }
768
769 public function nextSequenceValue( $seqName ) {
770 $safeseq = str_replace( "'", "''", $seqName );
771 $res = $this->query( "SELECT nextval('$safeseq')" );
772 $row = $this->fetchRow( $res );
773 $this->mInsertId = $row[0];
774
775 return $this->mInsertId;
776 }
777
778 /**
779 * Return the current value of a sequence. Assumes it has been nextval'ed in this session.
780 *
781 * @param string $seqName
782 * @return int
783 */
784 public function currentSequenceValue( $seqName ) {
785 $safeseq = str_replace( "'", "''", $seqName );
786 $res = $this->query( "SELECT currval('$safeseq')" );
787 $row = $this->fetchRow( $res );
788 $currval = $row[0];
789
790 return $currval;
791 }
792
793 public function textFieldSize( $table, $field ) {
794 $table = $this->tableName( $table );
795 $sql = "SELECT t.typname as ftype,a.atttypmod as size
796 FROM pg_class c, pg_attribute a, pg_type t
797 WHERE relname='$table' AND a.attrelid=c.oid AND
798 a.atttypid=t.oid and a.attname='$field'";
799 $res = $this->query( $sql );
800 $row = $this->fetchObject( $res );
801 if ( $row->ftype == 'varchar' ) {
802 $size = $row->size - 4;
803 } else {
804 $size = $row->size;
805 }
806
807 return $size;
808 }
809
810 public function limitResult( $sql, $limit, $offset = false ) {
811 return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' );
812 }
813
814 public function wasDeadlock() {
815 return $this->lastErrno() == '40P01';
816 }
817
818 public function duplicateTableStructure(
819 $oldName, $newName, $temporary = false, $fname = __METHOD__
820 ) {
821 $newName = $this->addIdentifierQuotes( $newName );
822 $oldName = $this->addIdentifierQuotes( $oldName );
823
824 return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName " .
825 "(LIKE $oldName INCLUDING DEFAULTS)", $fname );
826 }
827
828 public function listTables( $prefix = null, $fname = __METHOD__ ) {
829 $eschema = $this->addQuotes( $this->getCoreSchema() );
830 $result = $this->query(
831 "SELECT tablename FROM pg_tables WHERE schemaname = $eschema", $fname );
832 $endArray = [];
833
834 foreach ( $result as $table ) {
835 $vars = get_object_vars( $table );
836 $table = array_pop( $vars );
837 if ( !$prefix || strpos( $table, $prefix ) === 0 ) {
838 $endArray[] = $table;
839 }
840 }
841
842 return $endArray;
843 }
844
845 public function timestamp( $ts = 0 ) {
846 $ct = new ConvertibleTimestamp( $ts );
847
848 return $ct->getTimestamp( TS_POSTGRES );
849 }
850
851 /**
852 * Posted by cc[plus]php[at]c2se[dot]com on 25-Mar-2009 09:12
853 * to https://secure.php.net/manual/en/ref.pgsql.php
854 *
855 * Parsing a postgres array can be a tricky problem, he's my
856 * take on this, it handles multi-dimensional arrays plus
857 * escaping using a nasty regexp to determine the limits of each
858 * data-item.
859 *
860 * This should really be handled by PHP PostgreSQL module
861 *
862 * @since 1.19
863 * @param string $text Postgreql array returned in a text form like {a,b}
864 * @param string[] $output
865 * @param int|bool $limit
866 * @param int $offset
867 * @return string[]
868 */
869 private function pg_array_parse( $text, &$output, $limit = false, $offset = 1 ) {
870 if ( false === $limit ) {
871 $limit = strlen( $text ) - 1;
872 $output = [];
873 }
874 if ( '{}' == $text ) {
875 return $output;
876 }
877 do {
878 if ( '{' != $text[$offset] ) {
879 preg_match( "/(\\{?\"([^\"\\\\]|\\\\.)*\"|[^,{}]+)+([,}]+)/",
880 $text, $match, 0, $offset );
881 $offset += strlen( $match[0] );
882 $output[] = ( '"' != $match[1][0]
883 ? $match[1]
884 : stripcslashes( substr( $match[1], 1, -1 ) ) );
885 if ( '},' == $match[3] ) {
886 return $output;
887 }
888 } else {
889 $offset = $this->pg_array_parse( $text, $output, $limit, $offset + 1 );
890 }
891 } while ( $limit > $offset );
892
893 return $output;
894 }
895
896 public function aggregateValue( $valuedata, $valuename = 'value' ) {
897 return $valuedata;
898 }
899
900 public function getSoftwareLink() {
901 return '[{{int:version-db-postgres-url}} PostgreSQL]';
902 }
903
904 /**
905 * Return current schema (executes SELECT current_schema())
906 * Needs transaction
907 *
908 * @since 1.19
909 * @return string Default schema for the current session
910 */
911 public function getCurrentSchema() {
912 $res = $this->query( "SELECT current_schema()", __METHOD__ );
913 $row = $this->fetchRow( $res );
914
915 return $row[0];
916 }
917
918 /**
919 * Return list of schemas which are accessible without schema name
920 * This is list does not contain magic keywords like "$user"
921 * Needs transaction
922 *
923 * @see getSearchPath()
924 * @see setSearchPath()
925 * @since 1.19
926 * @return array List of actual schemas for the current sesson
927 */
928 public function getSchemas() {
929 $res = $this->query( "SELECT current_schemas(false)", __METHOD__ );
930 $row = $this->fetchRow( $res );
931 $schemas = [];
932
933 /* PHP pgsql support does not support array type, "{a,b}" string is returned */
934
935 return $this->pg_array_parse( $row[0], $schemas );
936 }
937
938 /**
939 * Return search patch for schemas
940 * This is different from getSchemas() since it contain magic keywords
941 * (like "$user").
942 * Needs transaction
943 *
944 * @since 1.19
945 * @return array How to search for table names schemas for the current user
946 */
947 public function getSearchPath() {
948 $res = $this->query( "SHOW search_path", __METHOD__ );
949 $row = $this->fetchRow( $res );
950
951 /* PostgreSQL returns SHOW values as strings */
952
953 return explode( ",", $row[0] );
954 }
955
956 /**
957 * Update search_path, values should already be sanitized
958 * Values may contain magic keywords like "$user"
959 * @since 1.19
960 *
961 * @param array $search_path List of schemas to be searched by default
962 */
963 private function setSearchPath( $search_path ) {
964 $this->query( "SET search_path = " . implode( ", ", $search_path ) );
965 }
966
967 /**
968 * Determine default schema for the current application
969 * Adjust this session schema search path if desired schema exists
970 * and is not alread there.
971 *
972 * We need to have name of the core schema stored to be able
973 * to query database metadata.
974 *
975 * This will be also called by the installer after the schema is created
976 *
977 * @since 1.19
978 *
979 * @param string $desiredSchema
980 */
981 public function determineCoreSchema( $desiredSchema ) {
982 $this->begin( __METHOD__, self::TRANSACTION_INTERNAL );
983 if ( $this->schemaExists( $desiredSchema ) ) {
984 if ( in_array( $desiredSchema, $this->getSchemas() ) ) {
985 $this->mCoreSchema = $desiredSchema;
986 $this->queryLogger->debug(
987 "Schema \"" . $desiredSchema . "\" already in the search path\n" );
988 } else {
989 /**
990 * Prepend our schema (e.g. 'mediawiki') in front
991 * of the search path
992 * Fixes T17816
993 */
994 $search_path = $this->getSearchPath();
995 array_unshift( $search_path,
996 $this->addIdentifierQuotes( $desiredSchema ) );
997 $this->setSearchPath( $search_path );
998 $this->mCoreSchema = $desiredSchema;
999 $this->queryLogger->debug(
1000 "Schema \"" . $desiredSchema . "\" added to the search path\n" );
1001 }
1002 } else {
1003 $this->mCoreSchema = $this->getCurrentSchema();
1004 $this->queryLogger->debug(
1005 "Schema \"" . $desiredSchema . "\" not found, using current \"" .
1006 $this->mCoreSchema . "\"\n" );
1007 }
1008 /* Commit SET otherwise it will be rollbacked on error or IGNORE SELECT */
1009 $this->commit( __METHOD__, self::FLUSHING_INTERNAL );
1010 }
1011
1012 /**
1013 * Return schema name for core application tables
1014 *
1015 * @since 1.19
1016 * @return string Core schema name
1017 */
1018 public function getCoreSchema() {
1019 return $this->mCoreSchema;
1020 }
1021
1022 public function getServerVersion() {
1023 if ( !isset( $this->numericVersion ) ) {
1024 $conn = $this->getBindingHandle();
1025 $versionInfo = pg_version( $conn );
1026 if ( version_compare( $versionInfo['client'], '7.4.0', 'lt' ) ) {
1027 // Old client, abort install
1028 $this->numericVersion = '7.3 or earlier';
1029 } elseif ( isset( $versionInfo['server'] ) ) {
1030 // Normal client
1031 $this->numericVersion = $versionInfo['server'];
1032 } else {
1033 // T18937: broken pgsql extension from PHP<5.3
1034 $this->numericVersion = pg_parameter_status( $conn, 'server_version' );
1035 }
1036 }
1037
1038 return $this->numericVersion;
1039 }
1040
1041 /**
1042 * Query whether a given relation exists (in the given schema, or the
1043 * default mw one if not given)
1044 * @param string $table
1045 * @param array|string $types
1046 * @param bool|string $schema
1047 * @return bool
1048 */
1049 private function relationExists( $table, $types, $schema = false ) {
1050 if ( !is_array( $types ) ) {
1051 $types = [ $types ];
1052 }
1053 if ( $schema === false ) {
1054 $schema = $this->getCoreSchema();
1055 }
1056 $etable = $this->addQuotes( $table );
1057 $eschema = $this->addQuotes( $schema );
1058 $sql = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n "
1059 . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema "
1060 . "AND c.relkind IN ('" . implode( "','", $types ) . "')";
1061 $res = $this->query( $sql );
1062 $count = $res ? $res->numRows() : 0;
1063
1064 return (bool)$count;
1065 }
1066
1067 /**
1068 * For backward compatibility, this function checks both tables and views.
1069 * @param string $table
1070 * @param string $fname
1071 * @param bool|string $schema
1072 * @return bool
1073 */
1074 public function tableExists( $table, $fname = __METHOD__, $schema = false ) {
1075 return $this->relationExists( $table, [ 'r', 'v' ], $schema );
1076 }
1077
1078 public function sequenceExists( $sequence, $schema = false ) {
1079 return $this->relationExists( $sequence, 'S', $schema );
1080 }
1081
1082 public function triggerExists( $table, $trigger ) {
1083 $q = <<<SQL
1084 SELECT 1 FROM pg_class, pg_namespace, pg_trigger
1085 WHERE relnamespace=pg_namespace.oid AND relkind='r'
1086 AND tgrelid=pg_class.oid
1087 AND nspname=%s AND relname=%s AND tgname=%s
1088 SQL;
1089 $res = $this->query(
1090 sprintf(
1091 $q,
1092 $this->addQuotes( $this->getCoreSchema() ),
1093 $this->addQuotes( $table ),
1094 $this->addQuotes( $trigger )
1095 )
1096 );
1097 if ( !$res ) {
1098 return null;
1099 }
1100 $rows = $res->numRows();
1101
1102 return $rows;
1103 }
1104
1105 public function ruleExists( $table, $rule ) {
1106 $exists = $this->selectField( 'pg_rules', 'rulename',
1107 [
1108 'rulename' => $rule,
1109 'tablename' => $table,
1110 'schemaname' => $this->getCoreSchema()
1111 ]
1112 );
1113
1114 return $exists === $rule;
1115 }
1116
1117 public function constraintExists( $table, $constraint ) {
1118 $sql = sprintf( "SELECT 1 FROM information_schema.table_constraints " .
1119 "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s",
1120 $this->addQuotes( $this->getCoreSchema() ),
1121 $this->addQuotes( $table ),
1122 $this->addQuotes( $constraint )
1123 );
1124 $res = $this->query( $sql );
1125 if ( !$res ) {
1126 return null;
1127 }
1128 $rows = $res->numRows();
1129
1130 return $rows;
1131 }
1132
1133 /**
1134 * Query whether a given schema exists. Returns true if it does, false if it doesn't.
1135 * @param string $schema
1136 * @return bool
1137 */
1138 public function schemaExists( $schema ) {
1139 if ( !strlen( $schema ) ) {
1140 return false; // short-circuit
1141 }
1142
1143 $exists = $this->selectField(
1144 '"pg_catalog"."pg_namespace"', 1, [ 'nspname' => $schema ], __METHOD__ );
1145
1146 return (bool)$exists;
1147 }
1148
1149 /**
1150 * Returns true if a given role (i.e. user) exists, false otherwise.
1151 * @param string $roleName
1152 * @return bool
1153 */
1154 public function roleExists( $roleName ) {
1155 $exists = $this->selectField( '"pg_catalog"."pg_roles"', 1,
1156 [ 'rolname' => $roleName ], __METHOD__ );
1157
1158 return (bool)$exists;
1159 }
1160
1161 /**
1162 * @var string $table
1163 * @var string $field
1164 * @return PostgresField|null
1165 */
1166 public function fieldInfo( $table, $field ) {
1167 return PostgresField::fromText( $this, $table, $field );
1168 }
1169
1170 /**
1171 * pg_field_type() wrapper
1172 * @param ResultWrapper|resource $res ResultWrapper or PostgreSQL query result resource
1173 * @param int $index Field number, starting from 0
1174 * @return string
1175 */
1176 public function fieldType( $res, $index ) {
1177 if ( $res instanceof ResultWrapper ) {
1178 $res = $res->result;
1179 }
1180
1181 return pg_field_type( $res, $index );
1182 }
1183
1184 public function encodeBlob( $b ) {
1185 return new PostgresBlob( pg_escape_bytea( $b ) );
1186 }
1187
1188 public function decodeBlob( $b ) {
1189 if ( $b instanceof PostgresBlob ) {
1190 $b = $b->fetch();
1191 } elseif ( $b instanceof Blob ) {
1192 return $b->fetch();
1193 }
1194
1195 return pg_unescape_bytea( $b );
1196 }
1197
1198 public function strencode( $s ) {
1199 // Should not be called by us
1200 return pg_escape_string( $this->getBindingHandle(), $s );
1201 }
1202
1203 public function addQuotes( $s ) {
1204 $conn = $this->getBindingHandle();
1205
1206 if ( is_null( $s ) ) {
1207 return 'NULL';
1208 } elseif ( is_bool( $s ) ) {
1209 return intval( $s );
1210 } elseif ( $s instanceof Blob ) {
1211 if ( $s instanceof PostgresBlob ) {
1212 $s = $s->fetch();
1213 } else {
1214 $s = pg_escape_bytea( $conn, $s->fetch() );
1215 }
1216 return "'$s'";
1217 }
1218
1219 return "'" . pg_escape_string( $conn, $s ) . "'";
1220 }
1221
1222 /**
1223 * Postgres specific version of replaceVars.
1224 * Calls the parent version in Database.php
1225 *
1226 * @param string $ins SQL string, read from a stream (usually tables.sql)
1227 * @return string SQL string
1228 */
1229 protected function replaceVars( $ins ) {
1230 $ins = parent::replaceVars( $ins );
1231
1232 if ( $this->numericVersion >= 8.3 ) {
1233 // Thanks for not providing backwards-compatibility, 8.3
1234 $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins );
1235 }
1236
1237 if ( $this->numericVersion <= 8.1 ) { // Our minimum version
1238 $ins = str_replace( 'USING gin', 'USING gist', $ins );
1239 }
1240
1241 return $ins;
1242 }
1243
1244 public function makeSelectOptions( $options ) {
1245 $preLimitTail = $postLimitTail = '';
1246 $startOpts = $useIndex = $ignoreIndex = '';
1247
1248 $noKeyOptions = [];
1249 foreach ( $options as $key => $option ) {
1250 if ( is_numeric( $key ) ) {
1251 $noKeyOptions[$option] = true;
1252 }
1253 }
1254
1255 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1256
1257 $preLimitTail .= $this->makeOrderBy( $options );
1258
1259 if ( isset( $options['FOR UPDATE'] ) ) {
1260 $postLimitTail .= ' FOR UPDATE OF ' .
1261 implode( ', ', array_map( [ $this, 'tableName' ], $options['FOR UPDATE'] ) );
1262 } elseif ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1263 $postLimitTail .= ' FOR UPDATE';
1264 }
1265
1266 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1267 $startOpts .= 'DISTINCT';
1268 }
1269
1270 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1271 }
1272
1273 public function getDBname() {
1274 return $this->mDBname;
1275 }
1276
1277 public function getServer() {
1278 return $this->mServer;
1279 }
1280
1281 public function buildConcat( $stringList ) {
1282 return implode( ' || ', $stringList );
1283 }
1284
1285 public function buildGroupConcatField(
1286 $delimiter, $table, $field, $conds = '', $options = [], $join_conds = []
1287 ) {
1288 $fld = "array_to_string(array_agg($field)," . $this->addQuotes( $delimiter ) . ')';
1289
1290 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1291 }
1292
1293 public function buildStringCast( $field ) {
1294 return $field . '::text';
1295 }
1296
1297 public function streamStatementEnd( &$sql, &$newLine ) {
1298 # Allow dollar quoting for function declarations
1299 if ( substr( $newLine, 0, 4 ) == '$mw$' ) {
1300 if ( $this->delimiter ) {
1301 $this->delimiter = false;
1302 } else {
1303 $this->delimiter = ';';
1304 }
1305 }
1306
1307 return parent::streamStatementEnd( $sql, $newLine );
1308 }
1309
1310 public function doLockTables( array $read, array $write, $method ) {
1311 $tablesWrite = [];
1312 foreach ( $write as $table ) {
1313 $tablesWrite[] = $this->tableName( $table );
1314 }
1315 $tablesRead = [];
1316 foreach ( $read as $table ) {
1317 $tablesRead[] = $this->tableName( $table );
1318 }
1319
1320 // Acquire locks for the duration of the current transaction...
1321 if ( $tablesWrite ) {
1322 $this->query(
1323 'LOCK TABLE ONLY ' . implode( ',', $tablesWrite ) . ' IN EXCLUSIVE MODE',
1324 $method
1325 );
1326 }
1327 if ( $tablesRead ) {
1328 $this->query(
1329 'LOCK TABLE ONLY ' . implode( ',', $tablesRead ) . ' IN SHARE MODE',
1330 $method
1331 );
1332 }
1333
1334 return true;
1335 }
1336
1337 public function lockIsFree( $lockName, $method ) {
1338 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1339 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1340 $result = $this->query( "SELECT (CASE(pg_try_advisory_lock($key))
1341 WHEN 'f' THEN 'f' ELSE pg_advisory_unlock($key) END) AS lockstatus", $method );
1342 $row = $this->fetchObject( $result );
1343
1344 return ( $row->lockstatus === 't' );
1345 }
1346
1347 public function lock( $lockName, $method, $timeout = 5 ) {
1348 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1349 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1350 $loop = new WaitConditionLoop(
1351 function () use ( $lockName, $key, $timeout, $method ) {
1352 $res = $this->query( "SELECT pg_try_advisory_lock($key) AS lockstatus", $method );
1353 $row = $this->fetchObject( $res );
1354 if ( $row->lockstatus === 't' ) {
1355 parent::lock( $lockName, $method, $timeout ); // record
1356 return true;
1357 }
1358
1359 return WaitConditionLoop::CONDITION_CONTINUE;
1360 },
1361 $timeout
1362 );
1363
1364 return ( $loop->invoke() === $loop::CONDITION_REACHED );
1365 }
1366
1367 public function unlock( $lockName, $method ) {
1368 // http://www.postgresql.org/docs/8.2/static/functions-admin.html#FUNCTIONS-ADVISORY-LOCKS
1369 $key = $this->addQuotes( $this->bigintFromLockName( $lockName ) );
1370 $result = $this->query( "SELECT pg_advisory_unlock($key) as lockstatus", $method );
1371 $row = $this->fetchObject( $result );
1372
1373 if ( $row->lockstatus === 't' ) {
1374 parent::unlock( $lockName, $method ); // record
1375 return true;
1376 }
1377
1378 $this->queryLogger->debug( __METHOD__ . " failed to release lock\n" );
1379
1380 return false;
1381 }
1382
1383 /**
1384 * @param string $lockName
1385 * @return string Integer
1386 */
1387 private function bigintFromLockName( $lockName ) {
1388 return \Wikimedia\base_convert( substr( sha1( $lockName ), 0, 15 ), 16, 10 );
1389 }
1390 }
1391
1392 class_alias( DatabasePostgres::class, 'DatabasePostgres' );