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