Introducing 'frameless' keyword to [[Image:]] syntax which respects the user preferen...
[lhc/web/wiklou.git] / includes / DatabaseOracle.php
1 <?php
2
3 /**
4 * This is the Oracle database abstraction layer.
5 * @addtogroup Database
6 */
7 class ORABlob {
8 var $mData;
9
10 function __construct($data) {
11 $this->mData = $data;
12 }
13
14 function getData() {
15 return $this->mData;
16 }
17 }
18
19 /**
20 * The oci8 extension is fairly weak and doesn't support oci_num_rows, among
21 * other things. We use a wrapper class to handle that and other
22 * Oracle-specific bits, like converting column names back to lowercase.
23 * @addtogroup Database
24 */
25 class ORAResult {
26 private $rows;
27 private $cursor;
28 private $stmt;
29 private $nrows;
30 private $db;
31
32 function __construct(&$db, $stmt) {
33 $this->db =& $db;
34 if (($this->nrows = oci_fetch_all($stmt, $this->rows, 0, -1, OCI_FETCHSTATEMENT_BY_ROW | OCI_NUM)) === false) {
35 $e = oci_error($stmt);
36 $db->reportQueryError($e['message'], $e['code'], '', __FUNCTION__);
37 return;
38 }
39
40 $this->cursor = 0;
41 $this->stmt = $stmt;
42 }
43
44 function free() {
45 oci_free_statement($this->stmt);
46 }
47
48 function seek($row) {
49 $this->cursor = min($row, $this->nrows);
50 }
51
52 function numRows() {
53 return $this->nrows;
54 }
55
56 function numFields() {
57 return oci_num_fields($this->stmt);
58 }
59
60 function fetchObject() {
61 if ($this->cursor >= $this->nrows)
62 return false;
63
64 $row = $this->rows[$this->cursor++];
65 $ret = new stdClass();
66 foreach ($row as $k => $v) {
67 $lc = strtolower(oci_field_name($this->stmt, $k + 1));
68 $ret->$lc = $v;
69 }
70
71 return $ret;
72 }
73
74 function fetchAssoc() {
75 if ($this->cursor >= $this->nrows)
76 return false;
77
78 $row = $this->rows[$this->cursor++];
79 $ret = array();
80 foreach ($row as $k => $v) {
81 $lc = strtolower(oci_field_name($this->stmt, $k + 1));
82 $ret[$lc] = $v;
83 $ret[$k] = $v;
84 }
85 return $ret;
86 }
87 }
88
89 /**
90 * @addtogroup Database
91 */
92 class DatabaseOracle extends Database {
93 var $mInsertId = NULL;
94 var $mLastResult = NULL;
95 var $numeric_version = NULL;
96 var $lastResult = null;
97 var $cursor = 0;
98 var $mAffectedRows;
99
100 function DatabaseOracle($server = false, $user = false, $password = false, $dbName = false,
101 $failFunction = false, $flags = 0 )
102 {
103
104 global $wgOut;
105 # Can't get a reference if it hasn't been set yet
106 if ( !isset( $wgOut ) ) {
107 $wgOut = NULL;
108 }
109 $this->mOut =& $wgOut;
110 $this->mFailFunction = $failFunction;
111 $this->mFlags = $flags;
112 $this->open( $server, $user, $password, $dbName);
113
114 }
115
116 function cascadingDeletes() {
117 return true;
118 }
119 function cleanupTriggers() {
120 return true;
121 }
122 function strictIPs() {
123 return true;
124 }
125 function realTimestamps() {
126 return true;
127 }
128 function implicitGroupby() {
129 return false;
130 }
131 function searchableIPs() {
132 return true;
133 }
134
135 static function newFromParams( $server = false, $user = false, $password = false, $dbName = false,
136 $failFunction = false, $flags = 0)
137 {
138 return new DatabaseOracle( $server, $user, $password, $dbName, $failFunction, $flags );
139 }
140
141 /**
142 * Usually aborts on failure
143 * If the failFunction is set to a non-zero integer, returns success
144 */
145 function open( $server, $user, $password, $dbName ) {
146 if ( !function_exists( 'oci_connect' ) ) {
147 throw new DBConnectionError( $this, "Oracle functions missing, have you compiled PHP with the --with-oci8 option?\n (Note: if you recently installed PHP, you may need to restart your webserver and database)\n" );
148 }
149
150 # Needed for proper UTF-8 functionality
151 putenv("NLS_LANG=AMERICAN_AMERICA.AL32UTF8");
152
153 $this->close();
154 $this->mServer = $server;
155 $this->mUser = $user;
156 $this->mPassword = $password;
157 $this->mDBname = $dbName;
158
159 if (!strlen($user)) { ## e.g. the class is being loaded
160 return;
161 }
162
163 error_reporting( E_ALL );
164 $this->mConn = oci_connect($user, $password, $dbName);
165
166 if ($this->mConn == false) {
167 wfDebug("DB connection error\n");
168 wfDebug("Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n");
169 wfDebug($this->lastError()."\n");
170 return false;
171 }
172
173 $this->mOpened = true;
174 return $this->mConn;
175 }
176
177 /**
178 * Closes a database connection, if it is open
179 * Returns success, true if already closed
180 */
181 function close() {
182 $this->mOpened = false;
183 if ( $this->mConn ) {
184 return oci_close( $this->mConn );
185 } else {
186 return true;
187 }
188 }
189
190 function execFlags() {
191 return $this->mTrxLevel ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
192 }
193
194 function doQuery($sql) {
195 wfDebug("SQL: [$sql]\n");
196 if (!mb_check_encoding($sql)) {
197 throw new MWException("SQL encoding is invalid");
198 }
199
200 if (($this->mLastResult = $stmt = oci_parse($this->mConn, $sql)) === false) {
201 $e = oci_error($this->mConn);
202 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__);
203 }
204
205 if (oci_execute($stmt, $this->execFlags()) == false) {
206 $e = oci_error($stmt);
207 $this->reportQueryError($e['message'], $e['code'], $sql, __FUNCTION__);
208 }
209 if (oci_statement_type($stmt) == "SELECT")
210 return new ORAResult($this, $stmt);
211 else {
212 $this->mAffectedRows = oci_num_rows($stmt);
213 return true;
214 }
215 }
216
217 function queryIgnore($sql, $fname = '') {
218 return $this->query($sql, $fname, true);
219 }
220
221 function freeResult($res) {
222 $res->free();
223 }
224
225 function fetchObject($res) {
226 return $res->fetchObject();
227 }
228
229 function fetchRow($res) {
230 return $res->fetchAssoc();
231 }
232
233 function numRows($res) {
234 return $res->numRows();
235 }
236
237 function numFields($res) {
238 return $res->numFields();
239 }
240
241 function fieldName($stmt, $n) {
242 return pg_field_name($stmt, $n);
243 }
244
245 /**
246 * This must be called after nextSequenceVal
247 */
248 function insertId() {
249 return $this->mInsertId;
250 }
251
252 function dataSeek($res, $row) {
253 $res->seek($row);
254 }
255
256 function lastError() {
257 if ($this->mConn === false)
258 $e = oci_error();
259 else
260 $e = oci_error($this->mConn);
261 return $e['message'];
262 }
263
264 function lastErrno() {
265 if ($this->mConn === false)
266 $e = oci_error();
267 else
268 $e = oci_error($this->mConn);
269 return $e['code'];
270 }
271
272 function affectedRows() {
273 return $this->mAffectedRows;
274 }
275
276 /**
277 * Returns information about an index
278 * If errors are explicitly ignored, returns NULL on failure
279 */
280 function indexInfo( $table, $index, $fname = 'Database::indexExists' ) {
281 return false;
282 }
283
284 function indexUnique ($table, $index, $fname = 'Database::indexUnique' ) {
285 return false;
286 }
287
288 function insert( $table, $a, $fname = 'Database::insert', $options = array() ) {
289 if (!is_array($options))
290 $options = array($options);
291
292 #if (in_array('IGNORE', $options))
293 # $oldIgnore = $this->ignoreErrors(true);
294
295 # IGNORE is performed using single-row inserts, ignoring errors in each
296 # FIXME: need some way to distiguish between key collision and other types of error
297 //$oldIgnore = $this->ignoreErrors(true);
298 if (!is_array(reset($a))) {
299 $a = array($a);
300 }
301 foreach ($a as $row) {
302 $this->insertOneRow($table, $row, $fname);
303 }
304 //$this->ignoreErrors($oldIgnore);
305 $retVal = true;
306
307 //if (in_array('IGNORE', $options))
308 // $this->ignoreErrors($oldIgnore);
309
310 return $retVal;
311 }
312
313 function insertOneRow($table, $row, $fname) {
314 // "INSERT INTO tables (a, b, c)"
315 $sql = "INSERT INTO " . $this->tableName($table) . " (" . join(',', array_keys($row)) . ')';
316 $sql .= " VALUES (";
317
318 // for each value, append ":key"
319 $first = true;
320 $returning = '';
321 foreach ($row as $col => $val) {
322 if (is_object($val)) {
323 $what = "EMPTY_BLOB()";
324 assert($returning === '');
325 $returning = " RETURNING $col INTO :bval";
326 $blobcol = $col;
327 } else
328 $what = ":$col";
329
330 if ($first)
331 $sql .= "$what";
332 else
333 $sql.= ", $what";
334 $first = false;
335 }
336 $sql .= ") $returning";
337
338 $stmt = oci_parse($this->mConn, $sql);
339 foreach ($row as $col => $val) {
340 if (!is_object($val)) {
341 if (oci_bind_by_name($stmt, ":$col", $row[$col]) === false)
342 $this->reportQueryError($this->lastErrno(), $this->lastError(), $sql, __METHOD__);
343 }
344 }
345
346 if (($bval = oci_new_descriptor($this->mConn, OCI_D_LOB)) === false) {
347 $e = oci_error($stmt);
348 throw new DBUnexpectedError($this, "Cannot create LOB descriptor: " . $e['message']);
349 }
350
351 if (strlen($returning))
352 oci_bind_by_name($stmt, ":bval", $bval, -1, SQLT_BLOB);
353
354 if (oci_execute($stmt, OCI_DEFAULT) === false) {
355 $e = oci_error($stmt);
356 $this->reportQueryError($e['message'], $e['code'], $sql, __METHOD__);
357 }
358 if (strlen($returning)) {
359 $bval->save($row[$blobcol]->getData());
360 $bval->free();
361 }
362 if (!$this->mTrxLevel)
363 oci_commit($this->mConn);
364
365 oci_free_statement($stmt);
366 }
367
368 function tableName( $name ) {
369 # Replace reserved words with better ones
370 switch( $name ) {
371 case 'user':
372 return 'mwuser';
373 case 'text':
374 return 'pagecontent';
375 default:
376 return $name;
377 }
378 }
379
380 /**
381 * Return the next in a sequence, save the value for retrieval via insertId()
382 */
383 function nextSequenceValue($seqName) {
384 $res = $this->query("SELECT $seqName.nextval FROM dual");
385 $row = $this->fetchRow($res);
386 $this->mInsertId = $row[0];
387 $this->freeResult($res);
388 return $this->mInsertId;
389 }
390
391 /**
392 * Oracle does not have a "USE INDEX" clause, so return an empty string
393 */
394 function useIndexClause($index) {
395 return '';
396 }
397
398 # REPLACE query wrapper
399 # Oracle simulates this with a DELETE followed by INSERT
400 # $row is the row to insert, an associative array
401 # $uniqueIndexes is an array of indexes. Each element may be either a
402 # field name or an array of field names
403 #
404 # It may be more efficient to leave off unique indexes which are unlikely to collide.
405 # However if you do this, you run the risk of encountering errors which wouldn't have
406 # occurred in MySQL
407 function replace( $table, $uniqueIndexes, $rows, $fname = 'Database::replace' ) {
408 $table = $this->tableName($table);
409
410 if (count($rows)==0) {
411 return;
412 }
413
414 # Single row case
415 if (!is_array(reset($rows))) {
416 $rows = array($rows);
417 }
418
419 foreach( $rows as $row ) {
420 # Delete rows which collide
421 if ( $uniqueIndexes ) {
422 $sql = "DELETE FROM $table WHERE ";
423 $first = true;
424 foreach ( $uniqueIndexes as $index ) {
425 if ( $first ) {
426 $first = false;
427 $sql .= "(";
428 } else {
429 $sql .= ') OR (';
430 }
431 if ( is_array( $index ) ) {
432 $first2 = true;
433 foreach ( $index as $col ) {
434 if ( $first2 ) {
435 $first2 = false;
436 } else {
437 $sql .= ' AND ';
438 }
439 $sql .= $col.'=' . $this->addQuotes( $row[$col] );
440 }
441 } else {
442 $sql .= $index.'=' . $this->addQuotes( $row[$index] );
443 }
444 }
445 $sql .= ')';
446 $this->query( $sql, $fname );
447 }
448
449 # Now insert the row
450 $sql = "INSERT INTO $table (" . $this->makeList( array_keys( $row ), LIST_NAMES ) .') VALUES (' .
451 $this->makeList( $row, LIST_COMMA ) . ')';
452 $this->query($sql, $fname);
453 }
454 }
455
456 # DELETE where the condition is a join
457 function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "Database::deleteJoin" ) {
458 if ( !$conds ) {
459 throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' );
460 }
461
462 $delTable = $this->tableName( $delTable );
463 $joinTable = $this->tableName( $joinTable );
464 $sql = "DELETE FROM $delTable WHERE $delVar IN (SELECT $joinVar FROM $joinTable ";
465 if ( $conds != '*' ) {
466 $sql .= 'WHERE ' . $this->makeList( $conds, LIST_AND );
467 }
468 $sql .= ')';
469
470 $this->query( $sql, $fname );
471 }
472
473 # Returns the size of a text field, or -1 for "unlimited"
474 function textFieldSize( $table, $field ) {
475 $table = $this->tableName( $table );
476 $sql = "SELECT t.typname as ftype,a.atttypmod as size
477 FROM pg_class c, pg_attribute a, pg_type t
478 WHERE relname='$table' AND a.attrelid=c.oid AND
479 a.atttypid=t.oid and a.attname='$field'";
480 $res =$this->query($sql);
481 $row=$this->fetchObject($res);
482 if ($row->ftype=="varchar") {
483 $size=$row->size-4;
484 } else {
485 $size=$row->size;
486 }
487 $this->freeResult( $res );
488 return $size;
489 }
490
491 function lowPriorityOption() {
492 return '';
493 }
494
495 function limitResult($sql, $limit, $offset) {
496 if ($offset === false)
497 $offset = 0;
498 return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < 1 + $limit + $offset";
499 }
500
501 /**
502 * Returns an SQL expression for a simple conditional.
503 * Uses CASE on Oracle
504 *
505 * @param string $cond SQL expression which will result in a boolean value
506 * @param string $trueVal SQL expression to return if true
507 * @param string $falseVal SQL expression to return if false
508 * @return string SQL fragment
509 */
510 function conditional( $cond, $trueVal, $falseVal ) {
511 return " (CASE WHEN $cond THEN $trueVal ELSE $falseVal END) ";
512 }
513
514 function wasDeadlock() {
515 return $this->lastErrno() == 'OCI-00060';
516 }
517
518 function timestamp($ts = 0) {
519 return wfTimestamp(TS_ORACLE, $ts);
520 }
521
522 /**
523 * Return aggregated value function call
524 */
525 function aggregateValue ($valuedata,$valuename='value') {
526 return $valuedata;
527 }
528
529 function reportQueryError($error, $errno, $sql, $fname, $tempIgnore = false) {
530 # Ignore errors during error handling to avoid infinite
531 # recursion
532 $ignore = $this->ignoreErrors(true);
533 ++$this->mErrorCount;
534
535 if ($ignore || $tempIgnore) {
536 echo "error ignored! query = [$sql]\n";
537 wfDebug("SQL ERROR (ignored): $error\n");
538 $this->ignoreErrors( $ignore );
539 }
540 else {
541 echo "error!\n";
542 $message = "A database error has occurred\n" .
543 "Query: $sql\n" .
544 "Function: $fname\n" .
545 "Error: $errno $error\n";
546 throw new DBUnexpectedError($this, $message);
547 }
548 }
549
550 /**
551 * @return string wikitext of a link to the server software's web site
552 */
553 function getSoftwareLink() {
554 return "[http://www.oracle.com/ Oracle]";
555 }
556
557 /**
558 * @return string Version information from the database
559 */
560 function getServerVersion() {
561 return oci_server_version($this->mConn);
562 }
563
564 /**
565 * Query whether a given table exists (in the given schema, or the default mw one if not given)
566 */
567 function tableExists($table) {
568 $etable= $this->addQuotes($table);
569 $SQL = "SELECT 1 FROM user_tables WHERE table_name='$etable'";
570 $res = $this->query($SQL);
571 $count = $res ? oci_num_rows($res) : 0;
572 if ($res)
573 $this->freeResult($res);
574 return $count;
575 }
576
577 /**
578 * Query whether a given column exists in the mediawiki schema
579 */
580 function fieldExists( $table, $field ) {
581 return true; // XXX
582 }
583
584 function fieldInfo( $table, $field ) {
585 return false; // XXX
586 }
587
588 function begin( $fname = '' ) {
589 $this->mTrxLevel = 1;
590 }
591 function immediateCommit( $fname = '' ) {
592 return true;
593 }
594 function commit( $fname = '' ) {
595 oci_commit($this->mConn);
596 $this->mTrxLevel = 0;
597 }
598
599 /* Not even sure why this is used in the main codebase... */
600 function limitResultForUpdate($sql, $num) {
601 return $sql;
602 }
603
604 function strencode($s) {
605 return str_replace("'", "''", $s);
606 }
607
608 function encodeBlob($b) {
609 return new ORABlob($b);
610 }
611 function decodeBlob($b) {
612 return $b; //return $b->load();
613 }
614
615 function addQuotes( $s ) {
616 global $wgLang;
617 $s = $wgLang->checkTitleEncoding($s);
618 return "'" . $this->strencode($s) . "'";
619 }
620
621 function quote_ident( $s ) {
622 return $s;
623 }
624
625 /* For now, does nothing */
626 function selectDB( $db ) {
627 return true;
628 }
629
630 /**
631 * Returns an optional USE INDEX clause to go after the table, and a
632 * string to go at the end of the query
633 *
634 * @private
635 *
636 * @param array $options an associative array of options to be turned into
637 * an SQL query, valid keys are listed in the function.
638 * @return array
639 */
640 function makeSelectOptions( $options ) {
641 $preLimitTail = $postLimitTail = '';
642 $startOpts = '';
643
644 $noKeyOptions = array();
645 foreach ( $options as $key => $option ) {
646 if ( is_numeric( $key ) ) {
647 $noKeyOptions[$option] = true;
648 }
649 }
650
651 if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY {$options['GROUP BY']}";
652 if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY {$options['ORDER BY']}";
653
654 if (isset($options['LIMIT'])) {
655 // $tailOpts .= $this->limitResult('', $options['LIMIT'],
656 // isset($options['OFFSET']) ? $options['OFFSET']
657 // : false);
658 }
659
660 #if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $tailOpts .= ' FOR UPDATE';
661 #if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $tailOpts .= ' LOCK IN SHARE MODE';
662 if ( isset( $noKeyOptions['DISTINCT'] ) && isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT';
663
664 if ( isset( $options['USE INDEX'] ) && ! is_array( $options['USE INDEX'] ) ) {
665 $useIndex = $this->useIndexClause( $options['USE INDEX'] );
666 } else {
667 $useIndex = '';
668 }
669
670 return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail );
671 }
672
673 public function setTimeout( $timeout ) {
674 // @todo fixme no-op
675 }
676
677 function ping() {
678 wfDebug( "Function ping() not written for DatabasePostgres.php yet");
679 return true;
680 }
681
682
683 } // end DatabaseOracle class
684
685 ?>