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