rdbms: drop inappropriate and poorly documented "DatabaseOraclePostInit" hook
[lhc/web/wiklou.git] / includes / db / DatabaseOracle.php
1 <?php
2 /**
3 * This is the Oracle 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
24 use Wikimedia\Timestamp\ConvertibleTimestamp;
25 use Wikimedia\Rdbms\Database;
26 use Wikimedia\Rdbms\DatabaseDomain;
27 use Wikimedia\Rdbms\Blob;
28 use Wikimedia\Rdbms\ResultWrapper;
29 use Wikimedia\Rdbms\DBConnectionError;
30 use Wikimedia\Rdbms\DBUnexpectedError;
31 use Wikimedia\Rdbms\DBExpectedError;
32
33 /**
34 * @ingroup Database
35 */
36 class DatabaseOracle extends Database {
37 /** @var resource */
38 protected $mLastResult = null;
39
40 /** @var int The number of rows affected as an integer */
41 protected $mAffectedRows;
42
43 /** @var bool */
44 private $ignoreDupValOnIndex = false;
45
46 /** @var bool|array */
47 private $sequenceData = null;
48
49 /** @var string Character set for Oracle database */
50 private $defaultCharset = 'AL32UTF8';
51
52 /** @var array */
53 private $mFieldInfoCache = [];
54
55 /** @var string[] Map of (reserved table name => alternate table name) */
56 private $keywordTableMap = [];
57
58 /**
59 * @see Database::__construct()
60 * @param array $params Additional parameters include:
61 * - keywordTableMap : Map of reserved table names to alternative table names to use
62 */
63 function __construct( array $params ) {
64 $this->keywordTableMap = $params['keywordTableMap'] ?? [];
65 $params['tablePrefix'] = strtoupper( $params['tablePrefix'] );
66 parent::__construct( $params );
67 }
68
69 function __destruct() {
70 if ( $this->opened ) {
71 Wikimedia\suppressWarnings();
72 $this->close();
73 Wikimedia\restoreWarnings();
74 }
75 }
76
77 function getType() {
78 return 'oracle';
79 }
80
81 function implicitGroupby() {
82 return false;
83 }
84
85 function implicitOrderby() {
86 return false;
87 }
88
89 protected function open( $server, $user, $password, $dbName, $schema, $tablePrefix ) {
90 if ( !function_exists( 'oci_connect' ) ) {
91 throw new DBConnectionError(
92 $this,
93 "Oracle functions missing, have you compiled PHP with the --with-oci8 option?\n " .
94 "(Note: if you recently installed PHP, you may need to restart your webserver\n " .
95 "and database)\n" );
96 }
97
98 $this->close();
99 $this->user = $user;
100 $this->password = $password;
101 if ( !$server ) {
102 // Backward compatibility (server used to be null and TNS was supplied in dbname)
103 $this->server = $dbName;
104 $realDatabase = $user;
105 } else {
106 // $server now holds the TNS endpoint
107 $this->server = $server;
108 // $dbName is schema name if different from username
109 $realDatabase = $dbName ?: $user;
110 }
111
112 if ( !strlen( $user ) ) { # e.g. the class is being loaded
113 return null;
114 }
115
116 $session_mode = ( $this->flags & DBO_SYSDBA ) ? OCI_SYSDBA : OCI_DEFAULT;
117
118 Wikimedia\suppressWarnings();
119 if ( $this->flags & DBO_PERSISTENT ) {
120 $this->conn = oci_pconnect(
121 $this->user,
122 $this->password,
123 $this->server,
124 $this->defaultCharset,
125 $session_mode
126 );
127 } elseif ( $this->flags & DBO_DEFAULT ) {
128 $this->conn = oci_new_connect(
129 $this->user,
130 $this->password,
131 $this->server,
132 $this->defaultCharset,
133 $session_mode
134 );
135 } else {
136 $this->conn = oci_connect(
137 $this->user,
138 $this->password,
139 $this->server,
140 $this->defaultCharset,
141 $session_mode
142 );
143 }
144 Wikimedia\restoreWarnings();
145
146 if ( $this->user != $realDatabase ) {
147 // change current schema in session
148 $this->selectDB( $realDatabase );
149 } else {
150 $this->currentDomain = new DatabaseDomain(
151 $realDatabase,
152 null,
153 $tablePrefix
154 );
155 }
156
157 if ( !$this->conn ) {
158 throw new DBConnectionError( $this, $this->lastError() );
159 }
160
161 $this->opened = true;
162
163 # removed putenv calls because they interfere with the system globaly
164 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
165 $this->doQuery( 'ALTER SESSION SET NLS_TIMESTAMP_TZ_FORMAT=\'DD-MM-YYYY HH24:MI:SS.FF6\'' );
166 $this->doQuery( 'ALTER SESSION SET NLS_NUMERIC_CHARACTERS=\'.,\'' );
167
168 return (bool)$this->conn;
169 }
170
171 /**
172 * Closes a database connection, if it is open
173 * Returns success, true if already closed
174 * @return bool
175 */
176 protected function closeConnection() {
177 return oci_close( $this->conn );
178 }
179
180 function execFlags() {
181 return $this->trxLevel ? OCI_NO_AUTO_COMMIT : OCI_COMMIT_ON_SUCCESS;
182 }
183
184 /**
185 * @param string $sql
186 * @return bool|mixed|ORAResult
187 */
188 protected function doQuery( $sql ) {
189 if ( !mb_check_encoding( (string)$sql, 'UTF-8' ) ) {
190 throw new DBUnexpectedError( $this, "SQL encoding is invalid\n$sql" );
191 }
192
193 // handle some oracle specifics
194 // remove AS column/table/subquery namings
195 if ( !$this->getFlag( DBO_DDLMODE ) ) {
196 $sql = preg_replace( '/ as /i', ' ', $sql );
197 }
198
199 // Oracle has issues with UNION clause if the statement includes LOB fields
200 // So we do a UNION ALL and then filter the results array with array_unique
201 $union_unique = ( preg_match( '/\/\* UNION_UNIQUE \*\/ /', $sql ) != 0 );
202 // EXPLAIN syntax in Oracle is EXPLAIN PLAN FOR and it return nothing
203 // you have to select data from plan table after explain
204 $explain_id = MWTimestamp::getLocalInstance()->format( 'dmYHis' );
205
206 $sql = preg_replace(
207 '/^EXPLAIN /',
208 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR',
209 $sql,
210 1,
211 $explain_count
212 );
213
214 Wikimedia\suppressWarnings();
215
216 $this->mLastResult = $stmt = oci_parse( $this->conn, $sql );
217 if ( $stmt === false ) {
218 $e = oci_error( $this->conn );
219 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
220
221 return false;
222 }
223
224 if ( !oci_execute( $stmt, $this->execFlags() ) ) {
225 $e = oci_error( $stmt );
226 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
227 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
228
229 return false;
230 }
231 }
232
233 Wikimedia\restoreWarnings();
234
235 if ( $explain_count > 0 ) {
236 return $this->doQuery( 'SELECT id, cardinality "ROWS" FROM plan_table ' .
237 'WHERE statement_id = \'' . $explain_id . '\'' );
238 } elseif ( oci_statement_type( $stmt ) == 'SELECT' ) {
239 return new ORAResult( $this, $stmt, $union_unique );
240 } else {
241 $this->mAffectedRows = oci_num_rows( $stmt );
242
243 return true;
244 }
245 }
246
247 function queryIgnore( $sql, $fname = '' ) {
248 return $this->query( $sql, $fname, true );
249 }
250
251 /**
252 * Frees resources associated with the LOB descriptor
253 * @param ResultWrapper|ORAResult $res
254 */
255 function freeResult( $res ) {
256 if ( $res instanceof ResultWrapper ) {
257 $res = $res->result;
258 }
259
260 $res->free();
261 }
262
263 /**
264 * @param ResultWrapper|ORAResult $res
265 * @return mixed
266 */
267 function fetchObject( $res ) {
268 if ( $res instanceof ResultWrapper ) {
269 $res = $res->result;
270 }
271
272 return $res->fetchObject();
273 }
274
275 /**
276 * @param ResultWrapper|ORAResult $res
277 * @return mixed
278 */
279 function fetchRow( $res ) {
280 if ( $res instanceof ResultWrapper ) {
281 $res = $res->result;
282 }
283
284 return $res->fetchRow();
285 }
286
287 /**
288 * @param ResultWrapper|ORAResult $res
289 * @return int
290 */
291 function numRows( $res ) {
292 if ( $res instanceof ResultWrapper ) {
293 $res = $res->result;
294 }
295
296 return $res->numRows();
297 }
298
299 /**
300 * @param ResultWrapper|ORAResult $res
301 * @return int
302 */
303 function numFields( $res ) {
304 if ( $res instanceof ResultWrapper ) {
305 $res = $res->result;
306 }
307
308 return $res->numFields();
309 }
310
311 function fieldName( $stmt, $n ) {
312 return oci_field_name( $stmt, $n );
313 }
314
315 function insertId() {
316 $res = $this->query( "SELECT lastval_pkg.getLastval FROM dual" );
317 $row = $this->fetchRow( $res );
318 return is_null( $row[0] ) ? null : (int)$row[0];
319 }
320
321 /**
322 * @param mixed $res
323 * @param int $row
324 */
325 function dataSeek( $res, $row ) {
326 if ( $res instanceof ORAResult ) {
327 $res->seek( $row );
328 } else {
329 $res->result->seek( $row );
330 }
331 }
332
333 function lastError() {
334 if ( $this->conn === false ) {
335 $e = oci_error();
336 } else {
337 $e = oci_error( $this->conn );
338 }
339
340 return $e['message'];
341 }
342
343 function lastErrno() {
344 if ( $this->conn === false ) {
345 $e = oci_error();
346 } else {
347 $e = oci_error( $this->conn );
348 }
349
350 return $e['code'];
351 }
352
353 protected function fetchAffectedRowCount() {
354 return $this->mAffectedRows;
355 }
356
357 /**
358 * Returns information about an index
359 * If errors are explicitly ignored, returns NULL on failure
360 * @param string $table
361 * @param string $index
362 * @param string $fname
363 * @return bool
364 */
365 function indexInfo( $table, $index, $fname = __METHOD__ ) {
366 return false;
367 }
368
369 function indexUnique( $table, $index, $fname = __METHOD__ ) {
370 return false;
371 }
372
373 function insert( $table, $a, $fname = __METHOD__, $options = [] ) {
374 if ( !count( $a ) ) {
375 return true;
376 }
377
378 if ( !is_array( $options ) ) {
379 $options = [ $options ];
380 }
381
382 if ( in_array( 'IGNORE', $options ) ) {
383 $this->ignoreDupValOnIndex = true;
384 }
385
386 if ( !is_array( reset( $a ) ) ) {
387 $a = [ $a ];
388 }
389
390 foreach ( $a as &$row ) {
391 $this->insertOneRow( $table, $row, $fname );
392 }
393
394 if ( in_array( 'IGNORE', $options ) ) {
395 $this->ignoreDupValOnIndex = false;
396 }
397
398 return true;
399 }
400
401 private function fieldBindStatement( $table, $col, &$val, $includeCol = false ) {
402 $col_info = $this->fieldInfoMulti( $table, $col );
403 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
404
405 $bind = '';
406 if ( is_numeric( $col ) ) {
407 $bind = $val;
408 $val = null;
409
410 return $bind;
411 } elseif ( $includeCol ) {
412 $bind = "$col = ";
413 }
414
415 if ( $val == '' && $val !== 0 && $col_type != 'BLOB' && $col_type != 'CLOB' ) {
416 $val = null;
417 }
418
419 if ( $val === 'NULL' ) {
420 $val = null;
421 }
422
423 if ( $val === null ) {
424 if (
425 $col_info != false &&
426 $col_info->isNullable() == 0 &&
427 $col_info->defaultValue() != null
428 ) {
429 $bind .= 'DEFAULT';
430 } else {
431 $bind .= 'NULL';
432 }
433 } else {
434 $bind .= ':' . $col;
435 }
436
437 return $bind;
438 }
439
440 /**
441 * @param string $table
442 * @param array $row
443 * @param string $fname
444 * @return bool
445 * @throws DBUnexpectedError
446 */
447 private function insertOneRow( $table, $row, $fname ) {
448 $table = $this->tableName( $table );
449 // "INSERT INTO tables (a, b, c)"
450 $sql = "INSERT INTO " . $table . " (" . implode( ',', array_keys( $row ) ) . ')';
451 $sql .= " VALUES (";
452
453 // for each value, append ":key"
454 $first = true;
455 foreach ( $row as $col => &$val ) {
456 if ( !$first ) {
457 $sql .= ', ';
458 } else {
459 $first = false;
460 }
461 if ( $this->isQuotedIdentifier( $val ) ) {
462 $sql .= $this->removeIdentifierQuotes( $val );
463 unset( $row[$col] );
464 } else {
465 $sql .= $this->fieldBindStatement( $table, $col, $val );
466 }
467 }
468 $sql .= ')';
469
470 $this->mLastResult = $stmt = oci_parse( $this->conn, $sql );
471 if ( $stmt === false ) {
472 $e = oci_error( $this->conn );
473 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
474
475 return false;
476 }
477 foreach ( $row as $col => &$val ) {
478 $col_info = $this->fieldInfoMulti( $table, $col );
479 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
480
481 if ( $val === null ) {
482 // do nothing ... null was inserted in statement creation
483 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
484 if ( is_object( $val ) ) {
485 $val = $val->fetch();
486 }
487
488 // backward compatibility
489 if (
490 preg_match( '/^timestamp.*/i', $col_type ) == 1 &&
491 strtolower( $val ) == 'infinity'
492 ) {
493 $val = $this->getInfinity();
494 }
495
496 $val = $this->getVerifiedUTF8( $val );
497 if ( oci_bind_by_name( $stmt, ":$col", $val, -1, SQLT_CHR ) === false ) {
498 $e = oci_error( $stmt );
499 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
500
501 return false;
502 }
503 } else {
504 /** @var OCI_Lob[] $lob */
505 $lob[$col] = oci_new_descriptor( $this->conn, OCI_D_LOB );
506 if ( $lob[$col] === false ) {
507 $e = oci_error( $stmt );
508 throw new DBUnexpectedError(
509 $this,
510 "Cannot create LOB descriptor: " . $e['message']
511 );
512 }
513
514 if ( is_object( $val ) ) {
515 $val = $val->fetch();
516 }
517
518 if ( $col_type == 'BLOB' ) {
519 $lob[$col]->writeTemporary( $val, OCI_TEMP_BLOB );
520 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_BLOB );
521 } else {
522 $lob[$col]->writeTemporary( $val, OCI_TEMP_CLOB );
523 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_CLOB );
524 }
525 }
526 }
527
528 Wikimedia\suppressWarnings();
529
530 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
531 $e = oci_error( $stmt );
532 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
533 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
534
535 return false;
536 } else {
537 $this->mAffectedRows = oci_num_rows( $stmt );
538 }
539 } else {
540 $this->mAffectedRows = oci_num_rows( $stmt );
541 }
542
543 Wikimedia\restoreWarnings();
544
545 if ( isset( $lob ) ) {
546 foreach ( $lob as $lob_v ) {
547 $lob_v->free();
548 }
549 }
550
551 if ( !$this->trxLevel ) {
552 oci_commit( $this->conn );
553 }
554
555 return oci_free_statement( $stmt );
556 }
557
558 function nativeInsertSelect( $destTable, $srcTable, $varMap, $conds, $fname = __METHOD__,
559 $insertOptions = [], $selectOptions = [], $selectJoinConds = []
560 ) {
561 $destTable = $this->tableName( $destTable );
562
563 $sequenceData = $this->getSequenceData( $destTable );
564 if ( $sequenceData !== false &&
565 !isset( $varMap[$sequenceData['column']] )
566 ) {
567 $varMap[$sequenceData['column']] =
568 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')';
569 }
570
571 // count-alias subselect fields to avoid abigious definition errors
572 $i = 0;
573 foreach ( $varMap as &$val ) {
574 $val .= ' field' . $i;
575 $i++;
576 }
577
578 $selectSql = $this->selectSQLText(
579 $srcTable,
580 array_values( $varMap ),
581 $conds,
582 $fname,
583 $selectOptions,
584 $selectJoinConds
585 );
586
587 $sql = "INSERT INTO $destTable (" .
588 implode( ',', array_keys( $varMap ) ) . ') ' . $selectSql;
589
590 if ( in_array( 'IGNORE', $insertOptions ) ) {
591 $this->ignoreDupValOnIndex = true;
592 }
593
594 $this->query( $sql, $fname );
595
596 if ( in_array( 'IGNORE', $insertOptions ) ) {
597 $this->ignoreDupValOnIndex = false;
598 }
599 }
600
601 public function upsert( $table, array $rows, $uniqueIndexes, array $set,
602 $fname = __METHOD__
603 ) {
604 if ( $rows === [] ) {
605 return true; // nothing to do
606 }
607
608 if ( !is_array( reset( $rows ) ) ) {
609 $rows = [ $rows ];
610 }
611
612 $sequenceData = $this->getSequenceData( $table );
613 if ( $sequenceData !== false ) {
614 // add sequence column to each list of columns, when not set
615 foreach ( $rows as &$row ) {
616 if ( !isset( $row[$sequenceData['column']] ) ) {
617 $row[$sequenceData['column']] =
618 $this->addIdentifierQuotes( 'GET_SEQUENCE_VALUE(\'' .
619 $sequenceData['sequence'] . '\')' );
620 }
621 }
622 }
623
624 return parent::upsert( $table, $rows, $uniqueIndexes, $set, $fname );
625 }
626
627 public function tableName( $name, $format = 'quoted' ) {
628 // Replace reserved words with better ones
629 $name = $this->remappedTableName( $name );
630
631 return strtoupper( parent::tableName( $name, $format ) );
632 }
633
634 /**
635 * @param string $name
636 * @return string Value of $name or remapped name if $name is a reserved keyword
637 */
638 public function remappedTableName( $name ) {
639 return $this->keywordTableMap[$name] ?? $name;
640 }
641
642 function tableNameInternal( $name ) {
643 $name = $this->tableName( $name );
644
645 return preg_replace( '/.*\.(.*)/', '$1', $name );
646 }
647
648 /**
649 * Return sequence_name if table has a sequence
650 *
651 * @param string $table
652 * @return string[]|bool
653 */
654 private function getSequenceData( $table ) {
655 if ( $this->sequenceData == null ) {
656 $dbname = $this->currentDomain->getDatabase();
657 $prefix = $this->currentDomain->getTablePrefix();
658 // See https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions040.htm
659 $decodeArgs = [ 'atc.table_name' ]; // the switch
660 foreach ( $this->keywordTableMap as $reserved => $alternative ) {
661 $search = strtoupper( $prefix . $alternative ); // case
662 $replace = strtoupper( $prefix . $reserved ); // result
663 $decodeArgs[] = $this->addQuotes( $search );
664 $decodeArgs[] = $this->addQuotes( $replace );
665 }
666 $decodeArgs[] = [ 'atc.table_name' ]; // default
667 $decodeArgs = implode( ', ', $decodeArgs );
668
669 $result = $this->doQuery(
670 "SELECT lower(asq.sequence_name), lower(atc.table_name), lower(atc.column_name)
671 FROM all_sequences asq, all_tab_columns atc
672 WHERE decode({$decodeArgs}) || '_' ||
673 atc.column_name || '_SEQ' = '{$prefix}' || asq.sequence_name
674 AND asq.sequence_owner = upper('{$dbname}')
675 AND atc.owner = upper('{$dbname}')"
676 );
677
678 while ( ( $row = $result->fetchRow() ) !== false ) {
679 $this->sequenceData[$row[1]] = [
680 'sequence' => $row[0],
681 'column' => $row[2]
682 ];
683 }
684 }
685 $table = strtolower( $this->removeIdentifierQuotes( $this->tableName( $table ) ) );
686
687 return $this->sequenceData[$table] ?? false;
688 }
689
690 /**
691 * Returns the size of a text field, or -1 for "unlimited"
692 *
693 * @param string $table
694 * @param string $field
695 * @return mixed
696 */
697 function textFieldSize( $table, $field ) {
698 $fieldInfoData = $this->fieldInfo( $table, $field );
699
700 return $fieldInfoData->maxLength();
701 }
702
703 function limitResult( $sql, $limit, $offset = false ) {
704 if ( $offset === false ) {
705 $offset = 0;
706 }
707
708 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)";
709 }
710
711 function encodeBlob( $b ) {
712 return new Blob( $b );
713 }
714
715 function unionQueries( $sqls, $all ) {
716 $glue = ' UNION ALL ';
717
718 return 'SELECT * ' . ( $all ? '' : '/* UNION_UNIQUE */ ' ) .
719 'FROM (' . implode( $glue, $sqls ) . ')';
720 }
721
722 function wasDeadlock() {
723 return $this->lastErrno() == 'OCI-00060';
724 }
725
726 function duplicateTableStructure( $oldName, $newName, $temporary = false,
727 $fname = __METHOD__
728 ) {
729 $temporary = $temporary ? 'TRUE' : 'FALSE';
730 $tablePrefix = $this->currentDomain->getTablePrefix();
731
732 $newName = strtoupper( $newName );
733 $oldName = strtoupper( $oldName );
734
735 $tabName = substr( $newName, strlen( $tablePrefix ) );
736 $oldPrefix = substr( $oldName, 0, strlen( $oldName ) - strlen( $tabName ) );
737 $newPrefix = strtoupper( $tablePrefix );
738
739 return $this->doQuery( "BEGIN DUPLICATE_TABLE( '$tabName', " .
740 "'$oldPrefix', '$newPrefix', $temporary ); END;" );
741 }
742
743 function listTables( $prefix = null, $fname = __METHOD__ ) {
744 $listWhere = '';
745 if ( !empty( $prefix ) ) {
746 $listWhere = ' AND table_name LIKE \'' . strtoupper( $prefix ) . '%\'';
747 }
748
749 $owner = strtoupper( $this->getDBname() );
750 $result = $this->doQuery( "SELECT table_name FROM all_tables " .
751 "WHERE owner='$owner' AND table_name NOT LIKE '%!_IDX\$_' ESCAPE '!' $listWhere" );
752
753 // dirty code ... i know
754 $endArray = [];
755 $endArray[] = strtoupper( $prefix . 'MWUSER' );
756 $endArray[] = strtoupper( $prefix . 'PAGE' );
757 $endArray[] = strtoupper( $prefix . 'IMAGE' );
758 $fixedOrderTabs = $endArray;
759 while ( ( $row = $result->fetchRow() ) !== false ) {
760 if ( !in_array( $row['table_name'], $fixedOrderTabs ) ) {
761 $endArray[] = $row['table_name'];
762 }
763 }
764
765 return $endArray;
766 }
767
768 public function dropTable( $tableName, $fName = __METHOD__ ) {
769 $tableName = $this->tableName( $tableName );
770 if ( !$this->tableExists( $tableName ) ) {
771 return false;
772 }
773
774 return $this->doQuery( "DROP TABLE $tableName CASCADE CONSTRAINTS PURGE" );
775 }
776
777 public function timestamp( $ts = 0 ) {
778 $t = new ConvertibleTimestamp( $ts );
779 // Let errors bubble up to avoid putting garbage in the DB
780 return $t->getTimestamp( TS_ORACLE );
781 }
782
783 /**
784 * Return aggregated value function call
785 *
786 * @param array $valuedata
787 * @param string $valuename
788 * @return mixed
789 */
790 public function aggregateValue( $valuedata, $valuename = 'value' ) {
791 return $valuedata;
792 }
793
794 /**
795 * @return string Wikitext of a link to the server software's web site
796 */
797 public function getSoftwareLink() {
798 return '[{{int:version-db-oracle-url}} Oracle]';
799 }
800
801 /**
802 * @return string Version information from the database
803 */
804 function getServerVersion() {
805 // better version number, fallback on driver
806 $rset = $this->doQuery(
807 'SELECT version FROM product_component_version ' .
808 'WHERE UPPER(product) LIKE \'ORACLE DATABASE%\''
809 );
810 $row = $rset->fetchRow();
811 if ( !$row ) {
812 return oci_server_version( $this->conn );
813 }
814
815 return $row['version'];
816 }
817
818 /**
819 * Query whether a given index exists
820 * @param string $table
821 * @param string $index
822 * @param string $fname
823 * @return bool
824 */
825 function indexExists( $table, $index, $fname = __METHOD__ ) {
826 $table = $this->tableName( $table );
827 $table = strtoupper( $this->removeIdentifierQuotes( $table ) );
828 $index = strtoupper( $index );
829 $owner = strtoupper( $this->getDBname() );
830 $sql = "SELECT 1 FROM all_indexes WHERE owner='$owner' AND index_name='{$table}_{$index}'";
831 $res = $this->doQuery( $sql );
832 if ( $res ) {
833 $count = $res->numRows();
834 $res->free();
835 } else {
836 $count = 0;
837 }
838
839 return $count != 0;
840 }
841
842 /**
843 * Query whether a given table exists (in the given schema, or the default mw one if not given)
844 * @param string $table
845 * @param string $fname
846 * @return bool
847 */
848 function tableExists( $table, $fname = __METHOD__ ) {
849 $table = $this->tableName( $table );
850 $table = $this->addQuotes( strtoupper( $this->removeIdentifierQuotes( $table ) ) );
851 $owner = $this->addQuotes( strtoupper( $this->getDBname() ) );
852 $sql = "SELECT 1 FROM all_tables WHERE owner=$owner AND table_name=$table";
853 $res = $this->doQuery( $sql );
854 if ( $res && $res->numRows() > 0 ) {
855 $exists = true;
856 } else {
857 $exists = false;
858 }
859
860 $res->free();
861
862 return $exists;
863 }
864
865 /**
866 * Function translates mysql_fetch_field() functionality on ORACLE.
867 * Caching is present for reducing query time.
868 * For internal calls. Use fieldInfo for normal usage.
869 * Returns false if the field doesn't exist
870 *
871 * @param array|string $table
872 * @param string $field
873 * @return ORAField|ORAResult|false
874 */
875 private function fieldInfoMulti( $table, $field ) {
876 $field = strtoupper( $field );
877 if ( is_array( $table ) ) {
878 $table = array_map( [ $this, 'tableNameInternal' ], $table );
879 $tableWhere = 'IN (';
880 foreach ( $table as &$singleTable ) {
881 $singleTable = $this->removeIdentifierQuotes( $singleTable );
882 if ( isset( $this->mFieldInfoCache["$singleTable.$field"] ) ) {
883 return $this->mFieldInfoCache["$singleTable.$field"];
884 }
885 $tableWhere .= '\'' . $singleTable . '\',';
886 }
887 $tableWhere = rtrim( $tableWhere, ',' ) . ')';
888 } else {
889 $table = $this->removeIdentifierQuotes( $this->tableNameInternal( $table ) );
890 if ( isset( $this->mFieldInfoCache["$table.$field"] ) ) {
891 return $this->mFieldInfoCache["$table.$field"];
892 }
893 $tableWhere = '= \'' . $table . '\'';
894 }
895
896 $fieldInfoStmt = oci_parse(
897 $this->conn,
898 'SELECT * FROM wiki_field_info_full WHERE table_name ' .
899 $tableWhere . ' and column_name = \'' . $field . '\''
900 );
901 if ( oci_execute( $fieldInfoStmt, $this->execFlags() ) === false ) {
902 $e = oci_error( $fieldInfoStmt );
903 $this->reportQueryError( $e['message'], $e['code'], 'fieldInfo QUERY', __METHOD__ );
904
905 return false;
906 }
907 $res = new ORAResult( $this, $fieldInfoStmt );
908 if ( $res->numRows() == 0 ) {
909 if ( is_array( $table ) ) {
910 foreach ( $table as &$singleTable ) {
911 $this->mFieldInfoCache["$singleTable.$field"] = false;
912 }
913 } else {
914 $this->mFieldInfoCache["$table.$field"] = false;
915 }
916 $fieldInfoTemp = null;
917 } else {
918 $fieldInfoTemp = new ORAField( $res->fetchRow() );
919 $table = $fieldInfoTemp->tableName();
920 $this->mFieldInfoCache["$table.$field"] = $fieldInfoTemp;
921 }
922 $res->free();
923
924 return $fieldInfoTemp;
925 }
926
927 /**
928 * @throws DBUnexpectedError
929 * @param string $table
930 * @param string $field
931 * @return ORAField
932 */
933 function fieldInfo( $table, $field ) {
934 if ( is_array( $table ) ) {
935 throw new DBUnexpectedError(
936 $this,
937 'DatabaseOracle::fieldInfo called with table array!'
938 );
939 }
940
941 return $this->fieldInfoMulti( $table, $field );
942 }
943
944 protected function doBegin( $fname = __METHOD__ ) {
945 $this->trxLevel = 1;
946 $this->doQuery( 'SET CONSTRAINTS ALL DEFERRED' );
947 }
948
949 protected function doCommit( $fname = __METHOD__ ) {
950 if ( $this->trxLevel ) {
951 $ret = oci_commit( $this->conn );
952 if ( !$ret ) {
953 throw new DBUnexpectedError( $this, $this->lastError() );
954 }
955 $this->trxLevel = 0;
956 $this->doQuery( 'SET CONSTRAINTS ALL IMMEDIATE' );
957 }
958 }
959
960 protected function doRollback( $fname = __METHOD__ ) {
961 if ( $this->trxLevel ) {
962 oci_rollback( $this->conn );
963 $this->trxLevel = 0;
964 $this->doQuery( 'SET CONSTRAINTS ALL IMMEDIATE' );
965 }
966 }
967
968 function sourceStream(
969 $fp,
970 callable $lineCallback = null,
971 callable $resultCallback = null,
972 $fname = __METHOD__, callable $inputCallback = null
973 ) {
974 $cmd = '';
975 $done = false;
976 $dollarquote = false;
977
978 $replacements = [];
979 // Defines must comply with ^define\s*([^\s=]*)\s*=\s?'\{\$([^\}]*)\}';
980 while ( !feof( $fp ) ) {
981 if ( $lineCallback ) {
982 $lineCallback();
983 }
984 $line = trim( fgets( $fp, 1024 ) );
985 $sl = strlen( $line ) - 1;
986
987 if ( $sl < 0 ) {
988 continue;
989 }
990 if ( $line[0] == '-' && $line[1] == '-' ) {
991 continue;
992 }
993
994 // Allow dollar quoting for function declarations
995 if ( substr( $line, 0, 8 ) == '/*$mw$*/' ) {
996 if ( $dollarquote ) {
997 $dollarquote = false;
998 $line = str_replace( '/*$mw$*/', '', $line ); // remove dollarquotes
999 $done = true;
1000 } else {
1001 $dollarquote = true;
1002 }
1003 } elseif ( !$dollarquote ) {
1004 if ( $line[$sl] == ';' && ( $sl < 2 || $line[$sl - 1] != ';' ) ) {
1005 $done = true;
1006 $line = substr( $line, 0, $sl );
1007 }
1008 }
1009
1010 if ( $cmd != '' ) {
1011 $cmd .= ' ';
1012 }
1013 $cmd .= "$line\n";
1014
1015 if ( $done ) {
1016 $cmd = str_replace( ';;', ";", $cmd );
1017 if ( strtolower( substr( $cmd, 0, 6 ) ) == 'define' ) {
1018 if ( preg_match( '/^define\s*([^\s=]*)\s*=\s*\'\{\$([^\}]*)\}\'/', $cmd, $defines ) ) {
1019 $replacements[$defines[2]] = $defines[1];
1020 }
1021 } else {
1022 foreach ( $replacements as $mwVar => $scVar ) {
1023 $cmd = str_replace( '&' . $scVar . '.', '`{$' . $mwVar . '}`', $cmd );
1024 }
1025
1026 $cmd = $this->replaceVars( $cmd );
1027 if ( $inputCallback ) {
1028 $inputCallback( $cmd );
1029 }
1030 $res = $this->doQuery( $cmd );
1031 if ( $resultCallback ) {
1032 call_user_func( $resultCallback, $res, $this );
1033 }
1034
1035 if ( $res === false ) {
1036 $err = $this->lastError();
1037
1038 return "Query \"{$cmd}\" failed with error code \"$err\".\n";
1039 }
1040 }
1041
1042 $cmd = '';
1043 $done = false;
1044 }
1045 }
1046
1047 return true;
1048 }
1049
1050 protected function doSelectDomain( DatabaseDomain $domain ) {
1051 if ( $domain->getSchema() !== null ) {
1052 // We use the *database* aspect of $domain for schema, not the domain schema
1053 throw new DBExpectedError( $this, __CLASS__ . ": domain schemas are not supported." );
1054 }
1055
1056 $database = $domain->getDatabase();
1057 if ( $database === null || $database === $this->user ) {
1058 // Backward compatibility
1059 $this->currentDomain = $domain;
1060
1061 return true;
1062 }
1063
1064 // https://docs.oracle.com/javadb/10.8.3.0/ref/rrefsqlj32268.html
1065 $encDatabase = $this->addIdentifierQuotes( strtoupper( $database ) );
1066 $sql = "ALTER SESSION SET CURRENT_SCHEMA=$encDatabase";
1067 $stmt = oci_parse( $this->conn, $sql );
1068 Wikimedia\suppressWarnings();
1069 $success = oci_execute( $stmt );
1070 Wikimedia\restoreWarnings();
1071 if ( $success ) {
1072 // Update that domain fields on success (no exception thrown)
1073 $this->currentDomain = $domain;
1074 } else {
1075 $e = oci_error( $stmt );
1076 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1077 }
1078
1079 return true;
1080 }
1081
1082 function strencode( $s ) {
1083 return str_replace( "'", "''", $s );
1084 }
1085
1086 function addQuotes( $s ) {
1087 return "'" . $this->strencode( $this->getVerifiedUTF8( $s ) ) . "'";
1088 }
1089
1090 public function addIdentifierQuotes( $s ) {
1091 if ( !$this->getFlag( DBO_DDLMODE ) ) {
1092 $s = '/*Q*/' . $s;
1093 }
1094
1095 return $s;
1096 }
1097
1098 public function removeIdentifierQuotes( $s ) {
1099 return strpos( $s, '/*Q*/' ) === false ? $s : substr( $s, 5 );
1100 }
1101
1102 public function isQuotedIdentifier( $s ) {
1103 return strpos( $s, '/*Q*/' ) !== false;
1104 }
1105
1106 private function wrapFieldForWhere( $table, &$col, &$val ) {
1107 $col_info = $this->fieldInfoMulti( $table, $col );
1108 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1109 if ( $col_type == 'CLOB' ) {
1110 $col = 'TO_CHAR(' . $col . ')';
1111 $val = $this->getVerifiedUTF8( $val );
1112 } elseif ( $col_type == 'VARCHAR2' ) {
1113 $val = $this->getVerifiedUTF8( $val );
1114 }
1115 }
1116
1117 private function wrapConditionsForWhere( $table, $conds, $parentCol = null ) {
1118 $conds2 = [];
1119 foreach ( $conds as $col => $val ) {
1120 if ( is_array( $val ) ) {
1121 $conds2[$col] = $this->wrapConditionsForWhere( $table, $val, $col );
1122 } else {
1123 if ( is_numeric( $col ) && $parentCol != null ) {
1124 $this->wrapFieldForWhere( $table, $parentCol, $val );
1125 } else {
1126 $this->wrapFieldForWhere( $table, $col, $val );
1127 }
1128 $conds2[$col] = $val;
1129 }
1130 }
1131
1132 return $conds2;
1133 }
1134
1135 function selectRow( $table, $vars, $conds, $fname = __METHOD__,
1136 $options = [], $join_conds = []
1137 ) {
1138 if ( is_array( $conds ) ) {
1139 $conds = $this->wrapConditionsForWhere( $table, $conds );
1140 }
1141
1142 return parent::selectRow( $table, $vars, $conds, $fname, $options, $join_conds );
1143 }
1144
1145 /**
1146 * Returns an optional USE INDEX clause to go after the table, and a
1147 * string to go at the end of the query
1148 *
1149 * @param array $options An associative array of options to be turned into
1150 * an SQL query, valid keys are listed in the function.
1151 * @return array
1152 */
1153 function makeSelectOptions( $options ) {
1154 $preLimitTail = $postLimitTail = '';
1155 $startOpts = '';
1156
1157 $noKeyOptions = [];
1158 foreach ( $options as $key => $option ) {
1159 if ( is_numeric( $key ) ) {
1160 $noKeyOptions[$option] = true;
1161 }
1162 }
1163
1164 $preLimitTail .= $this->makeGroupByWithHaving( $options );
1165
1166 $preLimitTail .= $this->makeOrderBy( $options );
1167
1168 if ( isset( $noKeyOptions['FOR UPDATE'] ) ) {
1169 $postLimitTail .= ' FOR UPDATE';
1170 }
1171
1172 if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) {
1173 $startOpts .= 'DISTINCT';
1174 }
1175
1176 if ( isset( $options['USE INDEX'] ) && !is_array( $options['USE INDEX'] ) ) {
1177 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
1178 } else {
1179 $useIndex = '';
1180 }
1181
1182 if ( isset( $options['IGNORE INDEX'] ) && !is_array( $options['IGNORE INDEX'] ) ) {
1183 $ignoreIndex = $this->ignoreIndexClause( $options['IGNORE INDEX'] );
1184 } else {
1185 $ignoreIndex = '';
1186 }
1187
1188 return [ $startOpts, $useIndex, $preLimitTail, $postLimitTail, $ignoreIndex ];
1189 }
1190
1191 public function delete( $table, $conds, $fname = __METHOD__ ) {
1192 global $wgActorTableSchemaMigrationStage;
1193
1194 if ( is_array( $conds ) ) {
1195 $conds = $this->wrapConditionsForWhere( $table, $conds );
1196 }
1197 // a hack for deleting pages, users and images (which have non-nullable FKs)
1198 // all deletions on these tables have transactions so final failure rollbacks these updates
1199 // @todo: Normalize the schema to match MySQL, no special FKs and such
1200 $table = $this->tableName( $table );
1201 if ( $table == $this->tableName( 'user' ) &&
1202 ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_WRITE_OLD )
1203 ) {
1204 $this->update( 'archive', [ 'ar_user' => 0 ],
1205 [ 'ar_user' => $conds['user_id'] ], $fname );
1206 $this->update( 'ipblocks', [ 'ipb_user' => 0 ],
1207 [ 'ipb_user' => $conds['user_id'] ], $fname );
1208 $this->update( 'image', [ 'img_user' => 0 ],
1209 [ 'img_user' => $conds['user_id'] ], $fname );
1210 $this->update( 'oldimage', [ 'oi_user' => 0 ],
1211 [ 'oi_user' => $conds['user_id'] ], $fname );
1212 $this->update( 'filearchive', [ 'fa_deleted_user' => 0 ],
1213 [ 'fa_deleted_user' => $conds['user_id'] ], $fname );
1214 $this->update( 'filearchive', [ 'fa_user' => 0 ],
1215 [ 'fa_user' => $conds['user_id'] ], $fname );
1216 $this->update( 'uploadstash', [ 'us_user' => 0 ],
1217 [ 'us_user' => $conds['user_id'] ], $fname );
1218 $this->update( 'recentchanges', [ 'rc_user' => 0 ],
1219 [ 'rc_user' => $conds['user_id'] ], $fname );
1220 $this->update( 'logging', [ 'log_user' => 0 ],
1221 [ 'log_user' => $conds['user_id'] ], $fname );
1222 } elseif ( $table == $this->tableName( 'image' ) ) {
1223 $this->update( 'oldimage', [ 'oi_name' => 0 ],
1224 [ 'oi_name' => $conds['img_name'] ], $fname );
1225 }
1226
1227 return parent::delete( $table, $conds, $fname );
1228 }
1229
1230 /**
1231 * @param string $table
1232 * @param array $values
1233 * @param array $conds
1234 * @param string $fname
1235 * @param array $options
1236 * @return bool
1237 * @throws DBUnexpectedError
1238 */
1239 function update( $table, $values, $conds, $fname = __METHOD__, $options = [] ) {
1240 $table = $this->tableName( $table );
1241 $opts = $this->makeUpdateOptions( $options );
1242 $sql = "UPDATE $opts $table SET ";
1243
1244 $first = true;
1245 foreach ( $values as $col => &$val ) {
1246 $sqlSet = $this->fieldBindStatement( $table, $col, $val, true );
1247
1248 if ( !$first ) {
1249 $sqlSet = ', ' . $sqlSet;
1250 } else {
1251 $first = false;
1252 }
1253 $sql .= $sqlSet;
1254 }
1255
1256 if ( $conds !== [] && $conds !== '*' ) {
1257 $conds = $this->wrapConditionsForWhere( $table, $conds );
1258 $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND );
1259 }
1260
1261 $this->mLastResult = $stmt = oci_parse( $this->conn, $sql );
1262 if ( $stmt === false ) {
1263 $e = oci_error( $this->conn );
1264 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1265
1266 return false;
1267 }
1268 foreach ( $values as $col => &$val ) {
1269 $col_info = $this->fieldInfoMulti( $table, $col );
1270 $col_type = $col_info != false ? $col_info->type() : 'CONSTANT';
1271
1272 if ( $val === null ) {
1273 // do nothing ... null was inserted in statement creation
1274 } elseif ( $col_type != 'BLOB' && $col_type != 'CLOB' ) {
1275 if ( is_object( $val ) ) {
1276 $val = $val->getData();
1277 }
1278
1279 if (
1280 preg_match( '/^timestamp.*/i', $col_type ) == 1 &&
1281 strtolower( $val ) == 'infinity'
1282 ) {
1283 $val = '31-12-2030 12:00:00.000000';
1284 }
1285
1286 $val = $this->getVerifiedUTF8( $val );
1287 if ( oci_bind_by_name( $stmt, ":$col", $val ) === false ) {
1288 $e = oci_error( $stmt );
1289 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1290
1291 return false;
1292 }
1293 } else {
1294 /** @var OCI_Lob[] $lob */
1295 $lob[$col] = oci_new_descriptor( $this->conn, OCI_D_LOB );
1296 if ( $lob[$col] === false ) {
1297 $e = oci_error( $stmt );
1298 throw new DBUnexpectedError(
1299 $this,
1300 "Cannot create LOB descriptor: " . $e['message']
1301 );
1302 }
1303
1304 if ( is_object( $val ) ) {
1305 $val = $val->getData();
1306 }
1307
1308 if ( $col_type == 'BLOB' ) {
1309 $lob[$col]->writeTemporary( $val );
1310 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, SQLT_BLOB );
1311 } else {
1312 $lob[$col]->writeTemporary( $val );
1313 oci_bind_by_name( $stmt, ":$col", $lob[$col], -1, OCI_B_CLOB );
1314 }
1315 }
1316 }
1317
1318 Wikimedia\suppressWarnings();
1319
1320 if ( oci_execute( $stmt, $this->execFlags() ) === false ) {
1321 $e = oci_error( $stmt );
1322 if ( !$this->ignoreDupValOnIndex || $e['code'] != '1' ) {
1323 $this->reportQueryError( $e['message'], $e['code'], $sql, __METHOD__ );
1324
1325 return false;
1326 } else {
1327 $this->mAffectedRows = oci_num_rows( $stmt );
1328 }
1329 } else {
1330 $this->mAffectedRows = oci_num_rows( $stmt );
1331 }
1332
1333 Wikimedia\restoreWarnings();
1334
1335 if ( isset( $lob ) ) {
1336 foreach ( $lob as $lob_v ) {
1337 $lob_v->free();
1338 }
1339 }
1340
1341 if ( !$this->trxLevel ) {
1342 oci_commit( $this->conn );
1343 }
1344
1345 return oci_free_statement( $stmt );
1346 }
1347
1348 function bitNot( $field ) {
1349 // expecting bit-fields smaller than 4bytes
1350 return 'BITNOT(' . $field . ')';
1351 }
1352
1353 function bitAnd( $fieldLeft, $fieldRight ) {
1354 return 'BITAND(' . $fieldLeft . ', ' . $fieldRight . ')';
1355 }
1356
1357 function bitOr( $fieldLeft, $fieldRight ) {
1358 return 'BITOR(' . $fieldLeft . ', ' . $fieldRight . ')';
1359 }
1360
1361 public function buildGroupConcatField(
1362 $delim, $table, $field, $conds = '', $join_conds = []
1363 ) {
1364 $fld = "LISTAGG($field," . $this->addQuotes( $delim ) . ") WITHIN GROUP (ORDER BY $field)";
1365
1366 return '(' . $this->selectSQLText( $table, $fld, $conds, null, [], $join_conds ) . ')';
1367 }
1368
1369 public function buildSubstring( $input, $startPosition, $length = null ) {
1370 $this->assertBuildSubstringParams( $startPosition, $length );
1371 $params = [ $input, $startPosition ];
1372 if ( $length !== null ) {
1373 $params[] = $length;
1374 }
1375 return 'SUBSTR(' . implode( ',', $params ) . ')';
1376 }
1377
1378 /**
1379 * @param string $field Field or column to cast
1380 * @return string
1381 * @since 1.28
1382 */
1383 public function buildStringCast( $field ) {
1384 return 'CAST ( ' . $field . ' AS VARCHAR2 )';
1385 }
1386
1387 public function getInfinity() {
1388 return '31-12-2030 12:00:00.000000';
1389 }
1390
1391 /**
1392 * @param string $s
1393 * @return string
1394 */
1395 private function getVerifiedUTF8( $s ) {
1396 if ( mb_check_encoding( (string)$s, 'UTF-8' ) ) {
1397 return $s; // valid
1398 }
1399
1400 throw new DBUnexpectedError( $this, "Non BLOB/CLOB field must be UTF-8." );
1401 }
1402 }