Index: trunk/phase3/includes/db/DatabaseOracle.php |
— | — | @@ -301,7 +301,6 @@ |
302 | 302 | |
303 | 303 | $sql = preg_replace( '/^EXPLAIN /', 'EXPLAIN PLAN SET STATEMENT_ID = \'' . $explain_id . '\' FOR', $sql, 1, $explain_count ); |
304 | 304 | |
305 | | - |
306 | 305 | wfSuppressWarnings(); |
307 | 306 | |
308 | 307 | if ( ( $this->mLastResult = $stmt = oci_parse( $this->mConn, $sql ) ) === false ) { |
— | — | @@ -560,7 +559,9 @@ |
561 | 560 | |
562 | 561 | if ( ( $sequenceData = $this->getSequenceData( $destTable ) ) !== false && |
563 | 562 | !isset( $varMap[$sequenceData['column']] ) ) |
| 563 | + { |
564 | 564 | $varMap[$sequenceData['column']] = 'GET_SEQUENCE_VALUE(\'' . $sequenceData['sequence'] . '\')'; |
| 565 | + } |
565 | 566 | |
566 | 567 | // count-alias subselect fields to avoid abigious definition errors |
567 | 568 | $i = 0; |
— | — | @@ -675,15 +676,23 @@ |
676 | 677 | return ( isset( $this->sequenceData[$table] ) ) ? $this->sequenceData[$table] : false; |
677 | 678 | } |
678 | 679 | |
679 | | - # REPLACE query wrapper |
680 | | - # Oracle simulates this with a DELETE followed by INSERT |
681 | | - # $row is the row to insert, an associative array |
682 | | - # $uniqueIndexes is an array of indexes. Each element may be either a |
683 | | - # field name or an array of field names |
684 | | - # |
685 | | - # It may be more efficient to leave off unique indexes which are unlikely to collide. |
686 | | - # However if you do this, you run the risk of encountering errors which wouldn't have |
687 | | - # occurred in MySQL |
| 680 | + /** |
| 681 | + * REPLACE query wrapper |
| 682 | + * Oracle simulates this with a DELETE followed by INSERT |
| 683 | + * $row is the row to insert, an associative array |
| 684 | + * $uniqueIndexes is an array of indexes. Each element may be either a |
| 685 | + * field name or an array of field names |
| 686 | + * |
| 687 | + * It may be more efficient to leave off unique indexes which are unlikely to collide. |
| 688 | + * However if you do this, you run the risk of encountering errors which wouldn't have |
| 689 | + * occurred in MySQL. |
| 690 | + * |
| 691 | + * @param $table String: table name |
| 692 | + * @param $uniqueIndexes Array: array of indexes. Each element may be |
| 693 | + * either a field name or an array of field names |
| 694 | + * @param $rows Array: rows to insert to $table |
| 695 | + * @param $fname String: function name, you can use __METHOD__ here |
| 696 | + */ |
688 | 697 | function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabaseOracle::replace' ) { |
689 | 698 | $table = $this->tableName( $table ); |
690 | 699 | |
— | — | @@ -702,9 +711,10 @@ |
703 | 712 | # Delete rows which collide |
704 | 713 | if ( $uniqueIndexes ) { |
705 | 714 | $condsDelete = array(); |
706 | | - foreach ( $uniqueIndexes as $index ) |
| 715 | + foreach ( $uniqueIndexes as $index ) { |
707 | 716 | $condsDelete[$index] = $row[$index]; |
708 | | - if (count($condsDelete) > 0) { |
| 717 | + } |
| 718 | + if ( count( $condsDelete ) > 0 ) { |
709 | 719 | $this->delete( $table, $condsDelete, $fname ); |
710 | 720 | } |
711 | 721 | } |
— | — | @@ -719,9 +729,9 @@ |
720 | 730 | } |
721 | 731 | |
722 | 732 | # DELETE where the condition is a join |
723 | | - function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = "DatabaseOracle::deleteJoin" ) { |
| 733 | + function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabaseOracle::deleteJoin' ) { |
724 | 734 | if ( !$conds ) { |
725 | | - throw new DBUnexpectedError( $this, 'DatabaseOracle::deleteJoin() called with empty $conds' ); |
| 735 | + throw new DBUnexpectedError( $this, 'DatabaseOracle::deleteJoin() called with empty $conds' ); |
726 | 736 | } |
727 | 737 | |
728 | 738 | $delTable = $this->tableName( $delTable ); |
— | — | @@ -738,7 +748,7 @@ |
739 | 749 | # Returns the size of a text field, or -1 for "unlimited" |
740 | 750 | function textFieldSize( $table, $field ) { |
741 | 751 | $fieldInfoData = $this->fieldInfo( $table, $field); |
742 | | - if ( $fieldInfoData->type == "varchar" ) { |
| 752 | + if ( $fieldInfoData->type == 'varchar' ) { |
743 | 753 | $size = $row->size - 4; |
744 | 754 | } else { |
745 | 755 | $size = $row->size; |
— | — | @@ -753,7 +763,6 @@ |
754 | 764 | return "SELECT * FROM ($sql) WHERE rownum >= (1 + $offset) AND rownum < (1 + $limit + $offset)"; |
755 | 765 | } |
756 | 766 | |
757 | | - |
758 | 767 | function unionQueries( $sqls, $all ) { |
759 | 768 | $glue = ' UNION ALL '; |
760 | 769 | return 'SELECT * ' . ( $all ? '':'/* UNION_UNIQUE */ ' ) . 'FROM (' . implode( $glue, $sqls ) . ')' ; |
— | — | @@ -763,28 +772,27 @@ |
764 | 773 | return $this->lastErrno() == 'OCI-00060'; |
765 | 774 | } |
766 | 775 | |
767 | | - |
768 | 776 | function duplicateTableStructure( $oldName, $newName, $temporary = false, $fname = 'DatabaseOracle::duplicateTableStructure' ) { |
769 | 777 | $temporary = $temporary ? 'TRUE' : 'FALSE'; |
770 | | - $oldName = trim(strtoupper($oldName), '"'); |
771 | | - $oldParts = explode('_', $oldName); |
| 778 | + $oldName = trim( strtoupper( $oldName ), '"'); |
| 779 | + $oldParts = explode( '_', $oldName ); |
772 | 780 | |
773 | | - $newName = trim(strtoupper($newName), '"'); |
774 | | - $newParts = explode('_', $newName); |
| 781 | + $newName = trim( strtoupper( $newName ), '"'); |
| 782 | + $newParts = explode( '_', $newName ); |
775 | 783 | |
776 | 784 | $oldPrefix = ''; |
777 | 785 | $newPrefix = ''; |
778 | | - for ($i = count($oldParts)-1; $i >= 0; $i--) { |
779 | | - if ($oldParts[$i] != $newParts[$i]) { |
780 | | - $oldPrefix = implode('_', $oldParts).'_'; |
781 | | - $newPrefix = implode('_', $newParts).'_'; |
| 786 | + for ( $i = count( $oldParts ) - 1; $i >= 0; $i-- ) { |
| 787 | + if ( $oldParts[$i] != $newParts[$i] ) { |
| 788 | + $oldPrefix = implode( '_', $oldParts ) . '_'; |
| 789 | + $newPrefix = implode( '_', $newParts ) . '_'; |
782 | 790 | break; |
783 | 791 | } |
784 | | - unset($oldParts[$i]); |
785 | | - unset($newParts[$i]); |
| 792 | + unset( $oldParts[$i] ); |
| 793 | + unset( $newParts[$i] ); |
786 | 794 | } |
787 | 795 | |
788 | | - $tabName = substr($oldName, strlen($oldPrefix)); |
| 796 | + $tabName = substr( $oldName, strlen( $oldPrefix ) ); |
789 | 797 | |
790 | 798 | return $this->query( 'BEGIN DUPLICATE_TABLE(\'' . $tabName . '\', \'' . $oldPrefix . '\', \''.$newPrefix.'\', ' . $temporary . '); END;', $fname ); |
791 | 799 | } |
— | — | @@ -854,21 +862,21 @@ |
855 | 863 | */ |
856 | 864 | private function fieldInfoMulti( $table, $field ) { |
857 | 865 | $tableWhere = ''; |
858 | | - $field = strtoupper($field); |
859 | | - if (is_array($table)) { |
| 866 | + $field = strtoupper( $field ); |
| 867 | + if ( is_array( $table ) ) { |
860 | 868 | $table = array_map( array( &$this, 'tableName' ), $table ); |
861 | 869 | $tableWhere = 'IN ('; |
862 | | - foreach($table as &$singleTable) { |
863 | | - $singleTable = strtoupper(trim( $singleTable, '"' )); |
864 | | - if (isset($this->mFieldInfoCache["$singleTable.$field"])) { |
| 870 | + foreach( $table as &$singleTable ) { |
| 871 | + $singleTable = strtoupper( trim( $singleTable, '"' ) ); |
| 872 | + if ( isset( $this->mFieldInfoCache["$singleTable.$field"] ) ) { |
865 | 873 | return $this->mFieldInfoCache["$singleTable.$field"]; |
866 | 874 | } |
867 | | - $tableWhere .= '\''.$singleTable.'\','; |
| 875 | + $tableWhere .= '\'' . $singleTable . '\','; |
868 | 876 | } |
869 | | - $tableWhere = rtrim($tableWhere, ',').')'; |
| 877 | + $tableWhere = rtrim( $tableWhere, ',' ) . ')'; |
870 | 878 | } else { |
871 | | - $table = strtoupper(trim( $this->tableName($table), '"' )); |
872 | | - if (isset($this->mFieldInfoCache["$table.$field"])) { |
| 879 | + $table = strtoupper( trim( $this->tableName( $table ), '"' ) ); |
| 880 | + if ( isset( $this->mFieldInfoCache["$table.$field"] ) ) { |
873 | 881 | return $this->mFieldInfoCache["$table.$field"]; |
874 | 882 | } |
875 | 883 | $tableWhere = '= \''.$table.'\''; |
— | — | @@ -881,9 +889,9 @@ |
882 | 890 | return false; |
883 | 891 | } |
884 | 892 | $res = new ORAResult( $this, $fieldInfoStmt ); |
885 | | - if ($res->numRows() == 0 ) { |
886 | | - if (is_array($table)) { |
887 | | - foreach($table as &$singleTable) { |
| 893 | + if ( $res->numRows() == 0 ) { |
| 894 | + if ( is_array( $table ) ) { |
| 895 | + foreach( $table as &$singleTable ) { |
888 | 896 | $this->mFieldInfoCache["$singleTable.$field"] = false; |
889 | 897 | } |
890 | 898 | } else { |
— | — | @@ -992,7 +1000,7 @@ |
993 | 1001 | |
994 | 1002 | function setup_database() { |
995 | 1003 | $res = $this->sourceFile( "../maintenance/ora/tables.sql" ); |
996 | | - if ($res === true) { |
| 1004 | + if ( $res === true ) { |
997 | 1005 | print " done.</li>\n"; |
998 | 1006 | } else { |
999 | 1007 | print " <b>FAILED</b></li>\n"; |
— | — | @@ -1007,7 +1015,7 @@ |
1008 | 1016 | } |
1009 | 1017 | |
1010 | 1018 | // do it like the postgres :D |
1011 | | - $SQL = "INSERT INTO ".$this->tableName('interwiki')." (iw_prefix,iw_url,iw_local) VALUES "; |
| 1019 | + $SQL = "INSERT INTO " . $this->tableName( 'interwiki' ) . " (iw_prefix,iw_url,iw_local) VALUES "; |
1012 | 1020 | while ( !feof( $f ) ) { |
1013 | 1021 | $line = fgets( $f, 1024 ); |
1014 | 1022 | $matches = array(); |
— | — | @@ -1040,7 +1048,7 @@ |
1041 | 1049 | global $wgContLang; |
1042 | 1050 | |
1043 | 1051 | $conds2 = array(); |
1044 | | - $conds = ($conds != null && !is_array($conds)) ? array($conds) : $conds; |
| 1052 | + $conds = ( $conds != null && !is_array( $conds ) ) ? array( $conds ) : $conds; |
1045 | 1053 | foreach ( $conds as $col => $val ) { |
1046 | 1054 | $col_info = $this->fieldInfoMulti( $table, $col ); |
1047 | 1055 | $col_type = $col_info != false ? $col_info->type() : 'CONSTANT'; |
— | — | @@ -1104,7 +1112,7 @@ |
1105 | 1113 | |
1106 | 1114 | if ( $wgContLang != null ) { |
1107 | 1115 | $conds2 = array(); |
1108 | | - $conds = ($conds != null && !is_array($conds)) ? array($conds) : $conds; |
| 1116 | + $conds = ( $conds != null && !is_array( $conds ) ) ? array( $conds ) : $conds; |
1109 | 1117 | foreach ( $conds as $col => $val ) { |
1110 | 1118 | $col_info = $this->fieldInfoMulti( $table, $col ); |
1111 | 1119 | $col_type = $col_info != false ? $col_info->type() : 'CONSTANT'; |
Index: trunk/phase3/includes/db/DatabasePostgres.php |
— | — | @@ -34,21 +34,25 @@ |
35 | 35 | SQL; |
36 | 36 | |
37 | 37 | $table = $db->tableName( $table ); |
38 | | - $res = $db->query(sprintf($q, |
39 | | - $db->addQuotes($wgDBmwschema), |
40 | | - $db->addQuotes($table), |
41 | | - $db->addQuotes($field))); |
42 | | - $row = $db->fetchObject($res); |
43 | | - if (!$row) |
| 38 | + $res = $db->query( |
| 39 | + sprintf( $q, |
| 40 | + $db->addQuotes( $wgDBmwschema ), |
| 41 | + $db->addQuotes( $table ), |
| 42 | + $db->addQuotes( $field ) |
| 43 | + ) |
| 44 | + ); |
| 45 | + $row = $db->fetchObject( $res ); |
| 46 | + if ( !$row ) { |
44 | 47 | return null; |
| 48 | + } |
45 | 49 | $n = new PostgresField; |
46 | 50 | $n->type = $row->typname; |
47 | | - $n->nullable = ($row->attnotnull == 'f'); |
| 51 | + $n->nullable = ( $row->attnotnull == 'f' ); |
48 | 52 | $n->name = $field; |
49 | 53 | $n->tablename = $table; |
50 | 54 | $n->max_length = $row->attlen; |
51 | | - $n->deferrable = ($row->deferrable == 't'); |
52 | | - $n->deferred = ($row->deferred == 't'); |
| 55 | + $n->deferrable = ( $row->deferrable == 't' ); |
| 56 | + $n->deferred = ( $row->deferred == 't' ); |
53 | 57 | $n->conname = $row->conname; |
54 | 58 | return $n; |
55 | 59 | } |
— | — | @@ -96,14 +100,12 @@ |
97 | 101 | var $numeric_version = null; |
98 | 102 | var $mAffectedRows = null; |
99 | 103 | |
100 | | - function __construct($server = false, $user = false, $password = false, $dbName = false, |
| 104 | + function __construct( $server = false, $user = false, $password = false, $dbName = false, |
101 | 105 | $failFunction = false, $flags = 0 ) |
102 | 106 | { |
103 | | - |
104 | 107 | $this->mFailFunction = $failFunction; |
105 | 108 | $this->mFlags = $flags; |
106 | | - $this->open( $server, $user, $password, $dbName); |
107 | | - |
| 109 | + $this->open( $server, $user, $password, $dbName ); |
108 | 110 | } |
109 | 111 | |
110 | 112 | function getType() { |
— | — | @@ -138,10 +140,10 @@ |
139 | 141 | function hasConstraint( $name ) { |
140 | 142 | global $wgDBmwschema; |
141 | 143 | $SQL = "SELECT 1 FROM pg_catalog.pg_constraint c, pg_catalog.pg_namespace n WHERE c.connamespace = n.oid AND conname = '" . pg_escape_string( $this->mConn, $name ) . "' AND n.nspname = '" . pg_escape_string( $this->mConn, $wgDBmwschema ) ."'"; |
142 | | - return $this->numRows($res = $this->doQuery($SQL)); |
| 144 | + return $this->numRows( $res = $this->doQuery( $SQL ) ); |
143 | 145 | } |
144 | 146 | |
145 | | - static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0) |
| 147 | + static function newFromParams( $server, $user, $password, $dbName, $failFunction = false, $flags = 0 ) |
146 | 148 | { |
147 | 149 | return new DatabasePostgres( $server, $user, $password, $dbName, $failFunction, $flags ); |
148 | 150 | } |
— | — | @@ -158,7 +160,7 @@ |
159 | 161 | |
160 | 162 | global $wgDBport; |
161 | 163 | |
162 | | - if (!strlen($user)) { ## e.g. the class is being loaded |
| 164 | + if ( !strlen( $user ) ) { # e.g. the class is being loaded |
163 | 165 | return; |
164 | 166 | } |
165 | 167 | $this->close(); |
— | — | @@ -171,11 +173,12 @@ |
172 | 174 | $connectVars = array( |
173 | 175 | 'dbname' => $dbName, |
174 | 176 | 'user' => $user, |
175 | | - 'password' => $password ); |
176 | | - if ($server!=false && $server!="") { |
| 177 | + 'password' => $password |
| 178 | + ); |
| 179 | + if ( $server != false && $server != '' ) { |
177 | 180 | $connectVars['host'] = $server; |
178 | 181 | } |
179 | | - if ($port!=false && $port!="") { |
| 182 | + if ( $port != false && $port != '' ) { |
180 | 183 | $connectVars['port'] = $port; |
181 | 184 | } |
182 | 185 | $connectString = $this->makeConnectionString( $connectVars, PGSQL_CONNECT_FORCE_NEW ); |
— | — | @@ -187,7 +190,7 @@ |
188 | 191 | if ( !$this->mConn ) { |
189 | 192 | wfDebug( "DB connection error\n" ); |
190 | 193 | wfDebug( "Server: $server, Database: $dbName, User: $user, Password: " . substr( $password, 0, 3 ) . "...\n" ); |
191 | | - wfDebug( $this->lastError()."\n" ); |
| 194 | + wfDebug( $this->lastError() . "\n" ); |
192 | 195 | if ( !$this->mFailFunction ) { |
193 | 196 | throw new DBConnectionError( $this, $phpError ); |
194 | 197 | } else { |
— | — | @@ -198,21 +201,21 @@ |
199 | 202 | $this->mOpened = true; |
200 | 203 | |
201 | 204 | global $wgCommandLineMode; |
202 | | - ## If called from the command-line (e.g. importDump), only show errors |
203 | | - if ($wgCommandLineMode) { |
| 205 | + # If called from the command-line (e.g. importDump), only show errors |
| 206 | + if ( $wgCommandLineMode ) { |
204 | 207 | $this->doQuery( "SET client_min_messages = 'ERROR'" ); |
205 | 208 | } |
206 | 209 | |
207 | 210 | $this->doQuery( "SET client_encoding='UTF8'" ); |
208 | 211 | |
209 | 212 | global $wgDBmwschema, $wgDBts2schema; |
210 | | - if (isset( $wgDBmwschema ) && isset( $wgDBts2schema ) |
| 213 | + if ( isset( $wgDBmwschema ) && isset( $wgDBts2schema ) |
211 | 214 | && $wgDBmwschema !== 'mediawiki' |
212 | 215 | && preg_match( '/^\w+$/', $wgDBmwschema ) |
213 | 216 | && preg_match( '/^\w+$/', $wgDBts2schema ) |
214 | 217 | ) { |
215 | | - $safeschema = $this->quote_ident($wgDBmwschema); |
216 | | - $safeschema2 = $this->quote_ident($wgDBts2schema); |
| 218 | + $safeschema = $this->quote_ident( $wgDBmwschema ); |
| 219 | + $safeschema2 = $this->quote_ident( $wgDBts2schema ); |
217 | 220 | $this->doQuery( "SET search_path = $safeschema, $wgDBts2schema, public" ); |
218 | 221 | } |
219 | 222 | |
— | — | @@ -228,83 +231,81 @@ |
229 | 232 | } |
230 | 233 | |
231 | 234 | |
232 | | - function initial_setup($superuser, $password, $dbName) { |
| 235 | + function initial_setup( $superuser, $password, $dbName ) { |
233 | 236 | // If this is the initial connection, setup the schema stuff and possibly create the user |
234 | 237 | global $wgDBname, $wgDBuser, $wgDBpassword, $wgDBmwschema, $wgDBts2schema; |
235 | 238 | |
236 | | - print "<li>Checking the version of Postgres..."; |
| 239 | + print '<li>Checking the version of Postgres...'; |
237 | 240 | $version = $this->getServerVersion(); |
238 | 241 | $PGMINVER = '8.1'; |
239 | | - if ($version < $PGMINVER) { |
| 242 | + if ( $version < $PGMINVER ) { |
240 | 243 | print "<b>FAILED</b>. Required version is $PGMINVER. You have " . htmlspecialchars( $version ) . "</li>\n"; |
241 | | - dieout("</ul>"); |
| 244 | + dieout( '</ul>' ); |
242 | 245 | } |
243 | | - print "version " . htmlspecialchars( $this->numeric_version ) . " is OK.</li>\n"; |
| 246 | + print 'version ' . htmlspecialchars( $this->numeric_version ) . " is OK.</li>\n"; |
244 | 247 | |
245 | | - $safeuser = $this->quote_ident($wgDBuser); |
| 248 | + $safeuser = $this->quote_ident( $wgDBuser ); |
246 | 249 | // Are we connecting as a superuser for the first time? |
247 | | - if ($superuser) { |
| 250 | + if ( $superuser ) { |
248 | 251 | // Are we really a superuser? Check out our rights |
249 | 252 | $SQL = "SELECT |
250 | 253 | CASE WHEN usesuper IS TRUE THEN |
251 | 254 | CASE WHEN usecreatedb IS TRUE THEN 3 ELSE 1 END |
252 | 255 | ELSE CASE WHEN usecreatedb IS TRUE THEN 2 ELSE 0 END |
253 | 256 | END AS rights |
254 | | - FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($superuser); |
255 | | - $rows = $this->numRows($res = $this->doQuery($SQL)); |
256 | | - if (!$rows) { |
257 | | - print "<li>ERROR: Could not read permissions for user \"" . htmlspecialchars( $superuser ) . "\"</li>\n"; |
258 | | - dieout('</ul>'); |
| 257 | + FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes( $superuser ); |
| 258 | + $rows = $this->numRows( $res = $this->doQuery( $SQL ) ); |
| 259 | + if ( !$rows ) { |
| 260 | + print '<li>ERROR: Could not read permissions for user "' . htmlspecialchars( $superuser ) . "\"</li>\n"; |
| 261 | + dieout( '</ul>' ); |
259 | 262 | } |
260 | | - $perms = pg_fetch_result($res, 0, 0); |
| 263 | + $perms = pg_fetch_result( $res, 0, 0 ); |
261 | 264 | |
262 | | - $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes($wgDBuser); |
263 | | - $rows = $this->numRows($this->doQuery($SQL)); |
264 | | - if ($rows) { |
265 | | - print "<li>User \"" . htmlspecialchars( $wgDBuser ) . "\" already exists, skipping account creation.</li>"; |
266 | | - } |
267 | | - else { |
268 | | - if ($perms != 1 and $perms != 3) { |
269 | | - print "<li>ERROR: the user \"" . htmlspecialchars( $superuser ) . "\" cannot create other users. "; |
| 265 | + $SQL = "SELECT 1 FROM pg_catalog.pg_user WHERE usename = " . $this->addQuotes( $wgDBuser ); |
| 266 | + $rows = $this->numRows( $this->doQuery( $SQL ) ); |
| 267 | + if ( $rows ) { |
| 268 | + print '<li>User "' . htmlspecialchars( $wgDBuser ) . '" already exists, skipping account creation.</li>'; |
| 269 | + } else { |
| 270 | + if ( $perms != 1 && $perms != 3 ) { |
| 271 | + print '<li>ERROR: the user "' . htmlspecialchars( $superuser ) . '" cannot create other users. '; |
270 | 272 | print 'Please use a different Postgres user.</li>'; |
271 | | - dieout('</ul>'); |
| 273 | + dieout( '</ul>' ); |
272 | 274 | } |
273 | | - print "<li>Creating user <b>" . htmlspecialchars( $wgDBuser ) . "</b>..."; |
274 | | - $safepass = $this->addQuotes($wgDBpassword); |
| 275 | + print '<li>Creating user <b>' . htmlspecialchars( $wgDBuser ) . '</b>...'; |
| 276 | + $safepass = $this->addQuotes( $wgDBpassword ); |
275 | 277 | $SQL = "CREATE USER $safeuser NOCREATEDB PASSWORD $safepass"; |
276 | | - $this->doQuery($SQL); |
| 278 | + $this->doQuery( $SQL ); |
277 | 279 | print "OK</li>\n"; |
278 | 280 | } |
279 | 281 | // User now exists, check out the database |
280 | | - if ($dbName != $wgDBname) { |
281 | | - $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this->addQuotes($wgDBname); |
282 | | - $rows = $this->numRows($this->doQuery($SQL)); |
283 | | - if ($rows) { |
284 | | - print "<li>Database \"" . htmlspecialchars( $wgDBname ) . "\" already exists, skipping database creation.</li>"; |
285 | | - } |
286 | | - else { |
287 | | - if ($perms < 1) { |
288 | | - print "<li>ERROR: the user \"" . htmlspecialchars( $superuser ) . "\" cannot create databases. "; |
| 282 | + if ( $dbName != $wgDBname ) { |
| 283 | + $SQL = "SELECT 1 FROM pg_catalog.pg_database WHERE datname = " . $this->addQuotes( $wgDBname ); |
| 284 | + $rows = $this->numRows( $this->doQuery( $SQL ) ); |
| 285 | + if ( $rows ) { |
| 286 | + print '<li>Database "' . htmlspecialchars( $wgDBname ) . '" already exists, skipping database creation.</li>'; |
| 287 | + } else { |
| 288 | + if ( $perms < 1 ) { |
| 289 | + print '<li>ERROR: the user "' . htmlspecialchars( $superuser ) . '" cannot create databases. '; |
289 | 290 | print 'Please use a different Postgres user.</li>'; |
290 | | - dieout('</ul>'); |
| 291 | + dieout( '</ul>' ); |
291 | 292 | } |
292 | | - print "<li>Creating database <b>" . htmlspecialchars( $wgDBname ) . "</b>..."; |
293 | | - $safename = $this->quote_ident($wgDBname); |
| 293 | + print '<li>Creating database <b>' . htmlspecialchars( $wgDBname ) . '</b>...'; |
| 294 | + $safename = $this->quote_ident( $wgDBname ); |
294 | 295 | $SQL = "CREATE DATABASE $safename OWNER $safeuser "; |
295 | | - $this->doQuery($SQL); |
| 296 | + $this->doQuery( $SQL ); |
296 | 297 | print "OK</li>\n"; |
297 | 298 | // Hopefully tsearch2 and plpgsql are in template1... |
298 | 299 | } |
299 | 300 | |
300 | 301 | // Reconnect to check out tsearch2 rights for this user |
301 | | - print "<li>Connecting to \"" . htmlspecialchars( $wgDBname ) . "\" as superuser \"" . |
302 | | - htmlspecialchars( $superuser ) . "\" to check rights..."; |
| 302 | + print '<li>Connecting to "' . htmlspecialchars( $wgDBname ) . '" as superuser "' . |
| 303 | + htmlspecialchars( $superuser ) . '" to check rights...'; |
303 | 304 | |
304 | 305 | $connectVars = array(); |
305 | | - if ($this->mServer!=false && $this->mServer!="") { |
| 306 | + if ( $this->mServer != false && $this->mServer != '' ) { |
306 | 307 | $connectVars['host'] = $this->mServer; |
307 | 308 | } |
308 | | - if ($this->mPort!=false && $this->mPort!="") { |
| 309 | + if ( $this->mPort != false && $this->mPort != '' ) { |
309 | 310 | $connectVars['port'] = $this->mPort; |
310 | 311 | } |
311 | 312 | $connectVars['dbname'] = $wgDBname; |
— | — | @@ -314,47 +315,46 @@ |
315 | 316 | @$this->mConn = pg_connect( $this->makeConnectionString( $connectVars ) ); |
316 | 317 | if ( !$this->mConn ) { |
317 | 318 | print "<b>FAILED TO CONNECT!</b></li>"; |
318 | | - dieout("</ul>"); |
| 319 | + dieout( '</ul>' ); |
319 | 320 | } |
320 | 321 | print "OK</li>\n"; |
321 | 322 | } |
322 | 323 | |
323 | | - if ($this->numeric_version < 8.3) { |
| 324 | + if ( $this->numeric_version < 8.3 ) { |
324 | 325 | // Tsearch2 checks |
325 | | - print "<li>Checking that tsearch2 is installed in the database \"" . |
326 | | - htmlspecialchars( $wgDBname ) . "\"..."; |
327 | | - if (! $this->tableExists("pg_ts_cfg", $wgDBts2schema)) { |
328 | | - print "<b>FAILED</b>. tsearch2 must be installed in the database \"" . |
329 | | - htmlspecialchars( $wgDBname ) . "\"."; |
330 | | - print "Please see <a href='http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>"; |
| 326 | + print '<li>Checking that tsearch2 is installed in the database "' . |
| 327 | + htmlspecialchars( $wgDBname ) . '"...'; |
| 328 | + if ( !$this->tableExists( 'pg_ts_cfg', $wgDBts2schema ) ) { |
| 329 | + print '<b>FAILED</b>. tsearch2 must be installed in the database "' . |
| 330 | + htmlspecialchars( $wgDBname ) . '".'; |
| 331 | + print 'Please see <a href="http://www.devx.com/opensource/Article/21674/0/page/2">this article</a>'; |
331 | 332 | print " for instructions or ask on #postgresql on irc.freenode.net</li>\n"; |
332 | | - dieout("</ul>"); |
| 333 | + dieout( '</ul>' ); |
333 | 334 | } |
334 | 335 | print "OK</li>\n"; |
335 | | - print "<li>Ensuring that user \"" . htmlspecialchars( $wgDBuser ) . |
336 | | - "\" has select rights on the tsearch2 tables..."; |
337 | | - foreach (array('cfg','cfgmap','dict','parser') as $table) { |
| 336 | + print '<li>Ensuring that user "' . htmlspecialchars( $wgDBuser ) . |
| 337 | + '" has select rights on the tsearch2 tables...'; |
| 338 | + foreach ( array( 'cfg', 'cfgmap', 'dict', 'parser' ) as $table ) { |
338 | 339 | $SQL = "GRANT SELECT ON pg_ts_$table TO $safeuser"; |
339 | | - $this->doQuery($SQL); |
| 340 | + $this->doQuery( $SQL ); |
340 | 341 | } |
341 | 342 | print "OK</li>\n"; |
342 | 343 | } |
343 | 344 | |
344 | 345 | // Setup the schema for this user if needed |
345 | | - $result = $this->schemaExists($wgDBmwschema); |
346 | | - $safeschema = $this->quote_ident($wgDBmwschema); |
347 | | - if (!$result) { |
348 | | - print "<li>Creating schema <b>" . htmlspecialchars( $wgDBmwschema ) . "</b> ..."; |
349 | | - $result = $this->doQuery("CREATE SCHEMA $safeschema AUTHORIZATION $safeuser"); |
350 | | - if (!$result) { |
| 346 | + $result = $this->schemaExists( $wgDBmwschema ); |
| 347 | + $safeschema = $this->quote_ident( $wgDBmwschema ); |
| 348 | + if ( !$result ) { |
| 349 | + print '<li>Creating schema <b>' . htmlspecialchars( $wgDBmwschema ) . '</b> ...'; |
| 350 | + $result = $this->doQuery( "CREATE SCHEMA $safeschema AUTHORIZATION $safeuser" ); |
| 351 | + if ( !$result ) { |
351 | 352 | print "<b>FAILED</b>.</li>\n"; |
352 | | - dieout("</ul>"); |
| 353 | + dieout( '</ul>' ); |
353 | 354 | } |
354 | 355 | print "OK</li>\n"; |
355 | | - } |
356 | | - else { |
| 356 | + } else { |
357 | 357 | print "<li>Schema already exists, explicitly granting rights...\n"; |
358 | | - $safeschema2 = $this->addQuotes($wgDBmwschema); |
| 358 | + $safeschema2 = $this->addQuotes( $wgDBmwschema ); |
359 | 359 | $SQL = "SELECT 'GRANT ALL ON '||pg_catalog.quote_ident(relname)||' TO $safeuser;'\n". |
360 | 360 | "FROM pg_catalog.pg_class p, pg_catalog.pg_namespace n\n". |
361 | 361 | "WHERE relnamespace = n.oid AND n.nspname = $safeschema2\n". |
— | — | @@ -364,16 +364,16 @@ |
365 | 365 | "pg_catalog.oidvectortypes(p.proargtypes)||') TO $safeuser;'\n". |
366 | 366 | "FROM pg_catalog.pg_proc p, pg_catalog.pg_namespace n\n". |
367 | 367 | "WHERE p.pronamespace = n.oid AND n.nspname = $safeschema2"; |
368 | | - $res = $this->doQuery($SQL); |
369 | | - if (!$res) { |
| 368 | + $res = $this->doQuery( $SQL ); |
| 369 | + if ( !$res ) { |
370 | 370 | print "<b>FAILED</b>. Could not set rights for the user.</li>\n"; |
371 | | - dieout("</ul>"); |
| 371 | + dieout( '</ul>' ); |
372 | 372 | } |
373 | | - $this->doQuery("SET search_path = $safeschema"); |
374 | | - $rows = $this->numRows($res); |
375 | | - while ($rows) { |
| 373 | + $this->doQuery( "SET search_path = $safeschema" ); |
| 374 | + $rows = $this->numRows( $res ); |
| 375 | + while ( $rows ) { |
376 | 376 | $rows--; |
377 | | - $this->doQuery(pg_fetch_result($res, $rows, 0)); |
| 377 | + $this->doQuery( pg_fetch_result( $res, $rows, 0 ) ); |
378 | 378 | } |
379 | 379 | print "OK</li>"; |
380 | 380 | } |
— | — | @@ -386,207 +386,203 @@ |
387 | 387 | |
388 | 388 | } // end superuser |
389 | 389 | |
390 | | - if (!defined('POSTGRES_SEARCHPATH')) { |
| 390 | + if ( !defined( 'POSTGRES_SEARCHPATH' ) ) { |
391 | 391 | |
392 | | - if ($this->numeric_version < 8.3) { |
| 392 | + if ( $this->numeric_version < 8.3 ) { |
393 | 393 | // Do we have the basic tsearch2 table? |
394 | | - print "<li>Checking for tsearch2 in the schema \"" . htmlspecialchars( $wgDBts2schema ) . "\"..."; |
395 | | - if (! $this->tableExists("pg_ts_dict", $wgDBts2schema)) { |
396 | | - print "<b>FAILED</b>. Make sure tsearch2 is installed. See <a href="; |
397 | | - print "'http://www.devx.com/opensource/Article/21674/0/page/2'>this article</a>"; |
| 394 | + print '<li>Checking for tsearch2 in the schema "' . htmlspecialchars( $wgDBts2schema ) . '"...'; |
| 395 | + if ( !$this->tableExists( 'pg_ts_dict', $wgDBts2schema ) ) { |
| 396 | + print '<b>FAILED</b>. Make sure tsearch2 is installed. See <a href="'; |
| 397 | + print 'http://www.devx.com/opensource/Article/21674/0/page/2">this article</a>'; |
398 | 398 | print " for instructions.</li>\n"; |
399 | | - dieout("</ul>"); |
| 399 | + dieout( '</ul>' ); |
400 | 400 | } |
401 | 401 | print "OK</li>\n"; |
402 | 402 | |
403 | 403 | // Does this user have the rights to the tsearch2 tables? |
404 | | - $ctype = pg_fetch_result($this->doQuery("SHOW lc_ctype"),0,0); |
405 | | - print "<li>Checking tsearch2 permissions..."; |
| 404 | + $ctype = pg_fetch_result( $this->doQuery( 'SHOW lc_ctype' ), 0, 0 ); |
| 405 | + print '<li>Checking tsearch2 permissions...'; |
406 | 406 | // Let's check all four, just to be safe |
407 | 407 | error_reporting( 0 ); |
408 | | - $ts2tables = array('cfg','cfgmap','dict','parser'); |
409 | | - $safetsschema = $this->quote_ident($wgDBts2schema); |
410 | | - foreach ( $ts2tables AS $tname ) { |
| 408 | + $ts2tables = array( 'cfg', 'cfgmap', 'dict', 'parser' ); |
| 409 | + $safetsschema = $this->quote_ident( $wgDBts2schema ); |
| 410 | + foreach ( $ts2tables as $tname ) { |
411 | 411 | $SQL = "SELECT count(*) FROM $safetsschema.pg_ts_$tname"; |
412 | | - $res = $this->doQuery($SQL); |
413 | | - if (!$res) { |
| 412 | + $res = $this->doQuery( $SQL ); |
| 413 | + if ( !$res ) { |
414 | 414 | print "<b>FAILED</b> to access " . htmlspecialchars( "pg_ts_$tname" ) . |
415 | 415 | ". Make sure that the user \"". htmlspecialchars( $wgDBuser ) . |
416 | 416 | "\" has SELECT access to all four tsearch2 tables</li>\n"; |
417 | | - dieout("</ul>"); |
| 417 | + dieout( '</ul>' ); |
418 | 418 | } |
419 | 419 | } |
420 | 420 | $SQL = "SELECT ts_name FROM $safetsschema.pg_ts_cfg WHERE locale = " . $this->addQuotes( $ctype ) ; |
421 | 421 | $SQL .= " ORDER BY CASE WHEN ts_name <> 'default' THEN 1 ELSE 0 END"; |
422 | | - $res = $this->doQuery($SQL); |
| 422 | + $res = $this->doQuery( $SQL ); |
423 | 423 | error_reporting( E_ALL ); |
424 | | - if (!$res) { |
| 424 | + if ( !$res ) { |
425 | 425 | print "<b>FAILED</b>. Could not determine the tsearch2 locale information</li>\n"; |
426 | 426 | dieout("</ul>"); |
427 | 427 | } |
428 | | - print "OK</li>"; |
| 428 | + print 'OK</li>'; |
429 | 429 | |
430 | 430 | // Will the current locale work? Can we force it to? |
431 | | - print "<li>Verifying tsearch2 locale with " . htmlspecialchars( $ctype ) . "..."; |
432 | | - $rows = $this->numRows($res); |
| 431 | + print '<li>Verifying tsearch2 locale with ' . htmlspecialchars( $ctype ) . '...'; |
| 432 | + $rows = $this->numRows( $res ); |
433 | 433 | $resetlocale = 0; |
434 | | - if (!$rows) { |
| 434 | + if ( !$rows ) { |
435 | 435 | print "<b>not found</b></li>\n"; |
436 | | - print "<li>Attempting to set default tsearch2 locale to \"" . htmlspecialchars( $ctype ) . "\"..."; |
| 436 | + print '<li>Attempting to set default tsearch2 locale to "' . htmlspecialchars( $ctype ) . '"...'; |
437 | 437 | $resetlocale = 1; |
438 | | - } |
439 | | - else { |
440 | | - $tsname = pg_fetch_result($res, 0, 0); |
441 | | - if ($tsname != 'default') { |
| 438 | + } else { |
| 439 | + $tsname = pg_fetch_result( $res, 0, 0 ); |
| 440 | + if ( $tsname != 'default' ) { |
442 | 441 | print "<b>not set to default (" . htmlspecialchars( $tsname ) . ")</b>"; |
443 | 442 | print "<li>Attempting to change tsearch2 default locale to \"" . |
444 | 443 | htmlspecialchars( $ctype ) . "\"..."; |
445 | 444 | $resetlocale = 1; |
446 | 445 | } |
447 | 446 | } |
448 | | - if ($resetlocale) { |
| 447 | + if ( $resetlocale ) { |
449 | 448 | $SQL = "UPDATE $safetsschema.pg_ts_cfg SET locale = " . $this->addQuotes( $ctype ) . " WHERE ts_name = 'default'"; |
450 | | - $res = $this->doQuery($SQL); |
451 | | - if (!$res) { |
452 | | - print "<b>FAILED</b>. "; |
453 | | - print "Please make sure that the locale in pg_ts_cfg for \"default\" is set to \"" . |
| 449 | + $res = $this->doQuery( $SQL ); |
| 450 | + if ( !$res ) { |
| 451 | + print '<b>FAILED</b>. '; |
| 452 | + print 'Please make sure that the locale in pg_ts_cfg for "default" is set to "' . |
454 | 453 | htmlspecialchars( $ctype ) . "\"</li>\n"; |
455 | | - dieout("</ul>"); |
| 454 | + dieout( '</ul>' ); |
456 | 455 | } |
457 | | - print "OK</li>"; |
| 456 | + print 'OK</li>'; |
458 | 457 | } |
459 | 458 | |
460 | 459 | // Final test: try out a simple tsearch2 query |
461 | 460 | $SQL = "SELECT $safetsschema.to_tsvector('default','MediaWiki tsearch2 testing')"; |
462 | | - $res = $this->doQuery($SQL); |
463 | | - if (!$res) { |
464 | | - print "<b>FAILED</b>. Specifically, \"" . htmlspecialchars( $SQL ) . "\" did not work.</li>"; |
465 | | - dieout("</ul>"); |
| 461 | + $res = $this->doQuery( $SQL ); |
| 462 | + if ( !$res ) { |
| 463 | + print '<b>FAILED</b>. Specifically, "' . htmlspecialchars( $SQL ) . '" did not work.</li>'; |
| 464 | + dieout( '</ul>' ); |
466 | 465 | } |
467 | | - print "OK</li>"; |
| 466 | + print 'OK</li>'; |
468 | 467 | } |
469 | 468 | |
470 | 469 | // Install plpgsql if needed |
471 | 470 | $this->setup_plpgsql(); |
472 | 471 | |
473 | 472 | // Does the schema already exist? Who owns it? |
474 | | - $result = $this->schemaExists($wgDBmwschema); |
475 | | - if (!$result) { |
476 | | - print "<li>Creating schema <b>" . htmlspecialchars( $wgDBmwschema ) . "</b> ..."; |
| 473 | + $result = $this->schemaExists( $wgDBmwschema ); |
| 474 | + if ( !$result ) { |
| 475 | + print '<li>Creating schema <b>' . htmlspecialchars( $wgDBmwschema ) . '</b> ...'; |
477 | 476 | error_reporting( 0 ); |
478 | | - $safeschema = $this->quote_ident($wgDBmwschema); |
479 | | - $result = $this->doQuery("CREATE SCHEMA $safeschema"); |
| 477 | + $safeschema = $this->quote_ident( $wgDBmwschema ); |
| 478 | + $result = $this->doQuery( "CREATE SCHEMA $safeschema" ); |
480 | 479 | error_reporting( E_ALL ); |
481 | | - if (!$result) { |
482 | | - print "<b>FAILED</b>. The user \"" . htmlspecialchars( $wgDBuser ) . |
483 | | - "\" must be able to access the schema. ". |
484 | | - "You can try making them the owner of the database, or try creating the schema with a ". |
485 | | - "different user, and then grant access to the \"" . |
| 480 | + if ( !$result ) { |
| 481 | + print '<b>FAILED</b>. The user "' . htmlspecialchars( $wgDBuser ) . |
| 482 | + '" must be able to access the schema. '. |
| 483 | + 'You can try making them the owner of the database, or try creating the schema with a '. |
| 484 | + 'different user, and then grant access to the "' . |
486 | 485 | htmlspecialchars( $wgDBuser ) . "\" user.</li>\n"; |
487 | | - dieout("</ul>"); |
| 486 | + dieout( '</ul>' ); |
488 | 487 | } |
489 | 488 | print "OK</li>\n"; |
490 | | - } |
491 | | - else if ($result != $wgDBuser) { |
492 | | - print "<li>Schema \"" . htmlspecialchars( $wgDBmwschema ) . "\" exists but is not owned by \"" . |
| 489 | + } elseif ( $result != $wgDBuser ) { |
| 490 | + print '<li>Schema "' . htmlspecialchars( $wgDBmwschema ) . '" exists but is not owned by "' . |
493 | 491 | htmlspecialchars( $wgDBuser ) . "\". Not ideal.</li>\n"; |
494 | | - } |
495 | | - else { |
496 | | - print "<li>Schema \"" . htmlspecialchars( $wgDBmwschema ) . "\" exists and is owned by \"" . |
| 492 | + } else { |
| 493 | + print '<li>Schema "' . htmlspecialchars( $wgDBmwschema ) . '" exists and is owned by "' . |
497 | 494 | htmlspecialchars( $wgDBuser ) . "\". Excellent.</li>\n"; |
498 | 495 | } |
499 | 496 | |
500 | 497 | // Always return GMT time to accomodate the existing integer-based timestamp assumption |
501 | | - print "<li>Setting the timezone to GMT for user \"" . htmlspecialchars( $wgDBuser ) . "\" ..."; |
| 498 | + print "<li>Setting the timezone to GMT for user \"" . htmlspecialchars( $wgDBuser ) . '" ...'; |
502 | 499 | $SQL = "ALTER USER $safeuser SET timezone = 'GMT'"; |
503 | | - $result = pg_query($this->mConn, $SQL); |
504 | | - if (!$result) { |
| 500 | + $result = pg_query( $this->mConn, $SQL ); |
| 501 | + if ( !$result ) { |
505 | 502 | print "<b>FAILED</b>.</li>\n"; |
506 | | - dieout("</ul>"); |
| 503 | + dieout( '</ul>' ); |
507 | 504 | } |
508 | 505 | print "OK</li>\n"; |
509 | 506 | // Set for the rest of this session |
510 | 507 | $SQL = "SET timezone = 'GMT'"; |
511 | | - $result = pg_query($this->mConn, $SQL); |
512 | | - if (!$result) { |
| 508 | + $result = pg_query( $this->mConn, $SQL ); |
| 509 | + if ( !$result ) { |
513 | 510 | print "<li>Failed to set timezone</li>\n"; |
514 | | - dieout("</ul>"); |
| 511 | + dieout( '</ul>' ); |
515 | 512 | } |
516 | 513 | |
517 | | - print "<li>Setting the datestyle to ISO, YMD for user \"" . htmlspecialchars( $wgDBuser ) . "\" ..."; |
| 514 | + print '<li>Setting the datestyle to ISO, YMD for user "' . htmlspecialchars( $wgDBuser ) . '" ...'; |
518 | 515 | $SQL = "ALTER USER $safeuser SET datestyle = 'ISO, YMD'"; |
519 | | - $result = pg_query($this->mConn, $SQL); |
520 | | - if (!$result) { |
| 516 | + $result = pg_query( $this->mConn, $SQL ); |
| 517 | + if ( !$result ) { |
521 | 518 | print "<b>FAILED</b>.</li>\n"; |
522 | | - dieout("</ul>"); |
| 519 | + dieout( '</ul>' ); |
523 | 520 | } |
524 | 521 | print "OK</li>\n"; |
525 | 522 | // Set for the rest of this session |
526 | 523 | $SQL = "SET datestyle = 'ISO, YMD'"; |
527 | | - $result = pg_query($this->mConn, $SQL); |
528 | | - if (!$result) { |
| 524 | + $result = pg_query( $this->mConn, $SQL ); |
| 525 | + if ( !$result ) { |
529 | 526 | print "<li>Failed to set datestyle</li>\n"; |
530 | | - dieout("</ul>"); |
| 527 | + dieout( '</ul>' ); |
531 | 528 | } |
532 | 529 | |
533 | 530 | // Fix up the search paths if needed |
534 | | - print "<li>Setting the search path for user \"" . htmlspecialchars( $wgDBuser ) . "\" ..."; |
535 | | - $path = $this->quote_ident($wgDBmwschema); |
536 | | - if ($wgDBts2schema !== $wgDBmwschema) |
537 | | - $path .= ", ". $this->quote_ident($wgDBts2schema); |
538 | | - if ($wgDBmwschema !== 'public' and $wgDBts2schema !== 'public') |
539 | | - $path .= ", public"; |
| 531 | + print '<li>Setting the search path for user "' . htmlspecialchars( $wgDBuser ) . '" ...'; |
| 532 | + $path = $this->quote_ident( $wgDBmwschema ); |
| 533 | + if ( $wgDBts2schema !== $wgDBmwschema ) { |
| 534 | + $path .= ', '. $this->quote_ident( $wgDBts2schema ); |
| 535 | + } |
| 536 | + if ( $wgDBmwschema !== 'public' && $wgDBts2schema !== 'public' ) { |
| 537 | + $path .= ', public'; |
| 538 | + } |
540 | 539 | $SQL = "ALTER USER $safeuser SET search_path = $path"; |
541 | | - $result = pg_query($this->mConn, $SQL); |
542 | | - if (!$result) { |
| 540 | + $result = pg_query( $this->mConn, $SQL ); |
| 541 | + if ( !$result ) { |
543 | 542 | print "<b>FAILED</b>.</li>\n"; |
544 | | - dieout("</ul>"); |
| 543 | + dieout( '</ul>' ); |
545 | 544 | } |
546 | 545 | print "OK</li>\n"; |
547 | 546 | // Set for the rest of this session |
548 | 547 | $SQL = "SET search_path = $path"; |
549 | | - $result = pg_query($this->mConn, $SQL); |
550 | | - if (!$result) { |
| 548 | + $result = pg_query( $this->mConn, $SQL ); |
| 549 | + if ( !$result ) { |
551 | 550 | print "<li>Failed to set search_path</li>\n"; |
552 | | - dieout("</ul>"); |
| 551 | + dieout( '</ul>' ); |
553 | 552 | } |
554 | | - define( "POSTGRES_SEARCHPATH", $path ); |
| 553 | + define( 'POSTGRES_SEARCHPATH', $path ); |
555 | 554 | } |
556 | 555 | } |
557 | 556 | |
558 | | - |
559 | 557 | function setup_plpgsql() { |
560 | | - print "<li>Checking for PL/pgSQL ..."; |
| 558 | + print '<li>Checking for PL/pgSQL ...'; |
561 | 559 | $SQL = "SELECT 1 FROM pg_catalog.pg_language WHERE lanname = 'plpgsql'"; |
562 | | - $rows = $this->numRows($this->doQuery($SQL)); |
563 | | - if ($rows < 1) { |
| 560 | + $rows = $this->numRows( $this->doQuery( $SQL ) ); |
| 561 | + if ( $rows < 1 ) { |
564 | 562 | // plpgsql is not installed, but if we have a pg_pltemplate table, we should be able to create it |
565 | | - print "not installed. Attempting to install PL/pgSQL ..."; |
| 563 | + print 'not installed. Attempting to install PL/pgSQL ...'; |
566 | 564 | $SQL = "SELECT 1 FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace) ". |
567 | 565 | "WHERE relname = 'pg_pltemplate' AND nspname='pg_catalog'"; |
568 | | - $rows = $this->numRows($this->doQuery($SQL)); |
| 566 | + $rows = $this->numRows( $this->doQuery( $SQL ) ); |
569 | 567 | global $wgDBname; |
570 | | - if ($rows >= 1) { |
571 | | - $olde = error_reporting(0); |
572 | | - error_reporting($olde - E_WARNING); |
573 | | - $result = $this->doQuery("CREATE LANGUAGE plpgsql"); |
574 | | - error_reporting($olde); |
575 | | - if (!$result) { |
576 | | - print "<b>FAILED</b>. You need to install the language PL/pgSQL in the database <tt>" . |
577 | | - htmlspecialchars( $wgDBname ) . "</tt></li>"; |
578 | | - dieout("</ul>"); |
| 568 | + if ( $rows >= 1 ) { |
| 569 | + $olde = error_reporting( 0 ); |
| 570 | + error_reporting( $olde - E_WARNING ); |
| 571 | + $result = $this->doQuery( 'CREATE LANGUAGE plpgsql' ); |
| 572 | + error_reporting( $olde ); |
| 573 | + if ( !$result ) { |
| 574 | + print '<b>FAILED</b>. You need to install the language PL/pgSQL in the database <tt>' . |
| 575 | + htmlspecialchars( $wgDBname ) . '</tt></li>'; |
| 576 | + dieout( '</ul>' ); |
579 | 577 | } |
| 578 | + } else { |
| 579 | + print '<b>FAILED</b>. You need to install the language PL/pgSQL in the database <tt>' . |
| 580 | + htmlspecialchars( $wgDBname ) . '</tt></li>'; |
| 581 | + dieout( '</ul>' ); |
580 | 582 | } |
581 | | - else { |
582 | | - print "<b>FAILED</b>. You need to install the language PL/pgSQL in the database <tt>" . |
583 | | - htmlspecialchars( $wgDBname ) . "</tt></li>"; |
584 | | - dieout("</ul>"); |
585 | | - } |
586 | 583 | } |
587 | 584 | print "OK</li>\n"; |
588 | 585 | } |
589 | 586 | |
590 | | - |
591 | 587 | /** |
592 | 588 | * Closes a database connection, if it is open |
593 | 589 | * Returns success, true if already closed |
— | — | @@ -601,10 +597,10 @@ |
602 | 598 | } |
603 | 599 | |
604 | 600 | function doQuery( $sql ) { |
605 | | - if (function_exists('mb_convert_encoding')) { |
606 | | - $sql = mb_convert_encoding($sql,'UTF-8'); |
| 601 | + if ( function_exists( 'mb_convert_encoding' ) ) { |
| 602 | + $sql = mb_convert_encoding( $sql, 'UTF-8' ); |
607 | 603 | } |
608 | | - $this->mLastResult = pg_query( $this->mConn, $sql); |
| 604 | + $this->mLastResult = pg_query( $this->mConn, $sql ); |
609 | 605 | $this->mAffectedRows = null; // use pg_affected_rows(mLastResult) |
610 | 606 | return $this->mLastResult; |
611 | 607 | } |
— | — | @@ -618,7 +614,7 @@ |
619 | 615 | $res = $res->result; |
620 | 616 | } |
621 | 617 | if ( !@pg_free_result( $res ) ) { |
622 | | - throw new DBUnexpectedError($this, "Unable to free Postgres result\n" ); |
| 618 | + throw new DBUnexpectedError( $this, "Unable to free Postgres result\n" ); |
623 | 619 | } |
624 | 620 | } |
625 | 621 | |
— | — | @@ -632,8 +628,8 @@ |
633 | 629 | # TODO: |
634 | 630 | # hashar : not sure if the following test really trigger if the object |
635 | 631 | # fetching failed. |
636 | | - if( pg_last_error($this->mConn) ) { |
637 | | - throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) ); |
| 632 | + if( pg_last_error( $this->mConn ) ) { |
| 633 | + throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) ); |
638 | 634 | } |
639 | 635 | return $row; |
640 | 636 | } |
— | — | @@ -643,8 +639,8 @@ |
644 | 640 | $res = $res->result; |
645 | 641 | } |
646 | 642 | @$row = pg_fetch_array( $res ); |
647 | | - if( pg_last_error($this->mConn) ) { |
648 | | - throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) ); |
| 643 | + if( pg_last_error( $this->mConn ) ) { |
| 644 | + throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) ); |
649 | 645 | } |
650 | 646 | return $row; |
651 | 647 | } |
— | — | @@ -654,17 +650,19 @@ |
655 | 651 | $res = $res->result; |
656 | 652 | } |
657 | 653 | @$n = pg_num_rows( $res ); |
658 | | - if( pg_last_error($this->mConn) ) { |
659 | | - throw new DBUnexpectedError($this, 'SQL error: ' . htmlspecialchars( pg_last_error($this->mConn) ) ); |
| 654 | + if( pg_last_error( $this->mConn ) ) { |
| 655 | + throw new DBUnexpectedError( $this, 'SQL error: ' . htmlspecialchars( pg_last_error( $this->mConn ) ) ); |
660 | 656 | } |
661 | 657 | return $n; |
662 | 658 | } |
| 659 | + |
663 | 660 | function numFields( $res ) { |
664 | 661 | if ( $res instanceof ResultWrapper ) { |
665 | 662 | $res = $res->result; |
666 | 663 | } |
667 | 664 | return pg_num_fields( $res ); |
668 | 665 | } |
| 666 | + |
669 | 667 | function fieldName( $res, $n ) { |
670 | 668 | if ( $res instanceof ResultWrapper ) { |
671 | 669 | $res = $res->result; |
— | — | @@ -689,10 +687,9 @@ |
690 | 688 | function lastError() { |
691 | 689 | if ( $this->mConn ) { |
692 | 690 | return pg_last_error(); |
| 691 | + } else { |
| 692 | + return 'No database connection'; |
693 | 693 | } |
694 | | - else { |
695 | | - return "No database connection"; |
696 | | - } |
697 | 694 | } |
698 | 695 | function lastErrno() { |
699 | 696 | return pg_last_error() ? 1 : 0; |
— | — | @@ -703,8 +700,9 @@ |
704 | 701 | // Forced result for simulated queries |
705 | 702 | return $this->mAffectedRows; |
706 | 703 | } |
707 | | - if( empty( $this->mLastResult ) ) |
| 704 | + if( empty( $this->mLastResult ) ) { |
708 | 705 | return 0; |
| 706 | + } |
709 | 707 | return pg_affected_rows( $this->mLastResult ); |
710 | 708 | } |
711 | 709 | |
— | — | @@ -715,8 +713,7 @@ |
716 | 714 | * Returns -1 if count cannot be found |
717 | 715 | * Takes same arguments as Database::select() |
718 | 716 | */ |
719 | | - |
720 | | - function estimateRowCount( $table, $vars='*', $conds='', $fname = 'DatabasePostgres::estimateRowCount', $options = array() ) { |
| 717 | + function estimateRowCount( $table, $vars = '*', $conds='', $fname = 'DatabasePostgres::estimateRowCount', $options = array() ) { |
721 | 718 | $options['EXPLAIN'] = true; |
722 | 719 | $res = $this->select( $table, $vars, $conds, $fname, $options ); |
723 | 720 | $rows = -1; |
— | — | @@ -730,7 +727,6 @@ |
731 | 728 | return $rows; |
732 | 729 | } |
733 | 730 | |
734 | | - |
735 | 731 | /** |
736 | 732 | * Returns information about an index |
737 | 733 | * If errors are explicitly ignored, returns NULL on failure |
— | — | @@ -749,16 +745,18 @@ |
750 | 746 | return false; |
751 | 747 | } |
752 | 748 | |
753 | | - function indexUnique ($table, $index, $fname = 'DatabasePostgres::indexUnique' ) { |
| 749 | + function indexUnique( $table, $index, $fname = 'DatabasePostgres::indexUnique' ) { |
754 | 750 | $sql = "SELECT indexname FROM pg_indexes WHERE tablename='{$table}'". |
755 | 751 | " AND indexdef LIKE 'CREATE UNIQUE%(" . |
756 | 752 | $this->strencode( $this->indexName( $index ) ) . |
757 | 753 | ")'"; |
758 | 754 | $res = $this->query( $sql, $fname ); |
759 | | - if ( !$res ) |
| 755 | + if ( !$res ) { |
760 | 756 | return null; |
761 | | - while ($row = $this->fetchObject( $res )) |
| 757 | + } |
| 758 | + while ( $row = $this->fetchObject( $res ) ) { |
762 | 759 | return true; |
| 760 | + } |
763 | 761 | return false; |
764 | 762 | |
765 | 763 | } |
— | — | @@ -786,14 +784,14 @@ |
787 | 785 | $this->getServerVersion(); |
788 | 786 | } |
789 | 787 | |
790 | | - if ( !is_array( $options ) ) |
| 788 | + if ( !is_array( $options ) ) { |
791 | 789 | $options = array( $options ); |
| 790 | + } |
792 | 791 | |
793 | 792 | if ( isset( $args[0] ) && is_array( $args[0] ) ) { |
794 | 793 | $multi = true; |
795 | 794 | $keys = array_keys( $args[0] ); |
796 | | - } |
797 | | - else { |
| 795 | + } else { |
798 | 796 | $multi = false; |
799 | 797 | $keys = array_keys( $args ); |
800 | 798 | } |
— | — | @@ -804,7 +802,7 @@ |
805 | 803 | // If we are not in a transaction, we need to be for savepoint trickery |
806 | 804 | $didbegin = 0; |
807 | 805 | if ( $ignore ) { |
808 | | - if (! $this->mTrxLevel) { |
| 806 | + if ( !$this->mTrxLevel ) { |
809 | 807 | $this->begin(); |
810 | 808 | $didbegin = 1; |
811 | 809 | } |
— | — | @@ -828,8 +826,7 @@ |
829 | 827 | $sql .= '(' . $this->makeList( $row ) . ')'; |
830 | 828 | } |
831 | 829 | $res = (bool)$this->query( $sql, $fname, $ignore ); |
832 | | - } |
833 | | - else { |
| 830 | + } else { |
834 | 831 | $res = true; |
835 | 832 | $origsql = $sql; |
836 | 833 | foreach ( $args as $row ) { |
— | — | @@ -837,17 +834,16 @@ |
838 | 835 | $tempsql .= '(' . $this->makeList( $row ) . ')'; |
839 | 836 | |
840 | 837 | if ( $ignore ) { |
841 | | - pg_query($this->mConn, "SAVEPOINT $ignore"); |
| 838 | + pg_query( $this->mConn, "SAVEPOINT $ignore" ); |
842 | 839 | } |
843 | 840 | |
844 | 841 | $tempres = (bool)$this->query( $tempsql, $fname, $ignore ); |
845 | 842 | |
846 | 843 | if ( $ignore ) { |
847 | 844 | $bar = pg_last_error(); |
848 | | - if ($bar != false) { |
| 845 | + if ( $bar != false ) { |
849 | 846 | pg_query( $this->mConn, "ROLLBACK TO $ignore" ); |
850 | | - } |
851 | | - else { |
| 847 | + } else { |
852 | 848 | pg_query( $this->mConn, "RELEASE $ignore" ); |
853 | 849 | $numrowsinserted++; |
854 | 850 | } |
— | — | @@ -855,12 +851,12 @@ |
856 | 852 | |
857 | 853 | // If any of them fail, we fail overall for this function call |
858 | 854 | // Note that this will be ignored if IGNORE is set |
859 | | - if (! $tempres) |
| 855 | + if ( !$tempres ) { |
860 | 856 | $res = false; |
| 857 | + } |
861 | 858 | } |
862 | 859 | } |
863 | | - } |
864 | | - else { |
| 860 | + } else { |
865 | 861 | // Not multi, just a lone insert |
866 | 862 | if ( $ignore ) { |
867 | 863 | pg_query($this->mConn, "SAVEPOINT $ignore"); |
— | — | @@ -870,10 +866,9 @@ |
871 | 867 | $res = (bool)$this->query( $sql, $fname, $ignore ); |
872 | 868 | if ( $ignore ) { |
873 | 869 | $bar = pg_last_error(); |
874 | | - if ($bar != false) { |
| 870 | + if ( $bar != false ) { |
875 | 871 | pg_query( $this->mConn, "ROLLBACK TO $ignore" ); |
876 | | - } |
877 | | - else { |
| 872 | + } else { |
878 | 873 | pg_query( $this->mConn, "RELEASE $ignore" ); |
879 | 874 | $numrowsinserted++; |
880 | 875 | } |
— | — | @@ -881,7 +876,7 @@ |
882 | 877 | } |
883 | 878 | if ( $ignore ) { |
884 | 879 | $olde = error_reporting( $olde ); |
885 | | - if ($didbegin) { |
| 880 | + if ( $didbegin ) { |
886 | 881 | $this->commit(); |
887 | 882 | } |
888 | 883 | |
— | — | @@ -892,9 +887,7 @@ |
893 | 888 | return true; |
894 | 889 | } |
895 | 890 | |
896 | | - |
897 | 891 | return $res; |
898 | | - |
899 | 892 | } |
900 | 893 | |
901 | 894 | /** |
— | — | @@ -904,7 +897,7 @@ |
905 | 898 | * $conds may be "*" to copy the whole table |
906 | 899 | * srcTable may be an array of tables. |
907 | 900 | * @todo FIXME: implement this a little better (seperate select/insert)? |
908 | | - */ |
| 901 | + */ |
909 | 902 | function insertSelect( $destTable, $srcTable, $varMap, $conds, $fname = 'DatabasePostgres::insertSelect', |
910 | 903 | $insertOptions = array(), $selectOptions = array() ) |
911 | 904 | { |
— | — | @@ -942,7 +935,7 @@ |
943 | 936 | " SELECT $startOpts " . implode( ',', $varMap ) . |
944 | 937 | " FROM $srcTable $useIndex"; |
945 | 938 | |
946 | | - if ( $conds != '*') { |
| 939 | + if ( $conds != '*' ) { |
947 | 940 | $sql .= ' WHERE ' . $this->makeList( $conds, LIST_AND ); |
948 | 941 | } |
949 | 942 | |
— | — | @@ -1006,19 +999,21 @@ |
1007 | 1000 | return $currval; |
1008 | 1001 | } |
1009 | 1002 | |
1010 | | - # REPLACE query wrapper |
1011 | | - # Postgres simulates this with a DELETE followed by INSERT |
1012 | | - # $row is the row to insert, an associative array |
1013 | | - # $uniqueIndexes is an array of indexes. Each element may be either a |
1014 | | - # field name or an array of field names |
1015 | | - # |
1016 | | - # It may be more efficient to leave off unique indexes which are unlikely to collide. |
1017 | | - # However if you do this, you run the risk of encountering errors which wouldn't have |
1018 | | - # occurred in MySQL |
| 1003 | + /** |
| 1004 | + * REPLACE query wrapper |
| 1005 | + * Postgres simulates this with a DELETE followed by INSERT |
| 1006 | + * $row is the row to insert, an associative array |
| 1007 | + * $uniqueIndexes is an array of indexes. Each element may be either a |
| 1008 | + * field name or an array of field names |
| 1009 | + * |
| 1010 | + * It may be more efficient to leave off unique indexes which are unlikely to collide. |
| 1011 | + * However if you do this, you run the risk of encountering errors which wouldn't have |
| 1012 | + * occurred in MySQL |
| 1013 | + */ |
1019 | 1014 | function replace( $table, $uniqueIndexes, $rows, $fname = 'DatabasePostgres::replace' ) { |
1020 | 1015 | $table = $this->tableName( $table ); |
1021 | 1016 | |
1022 | | - if (count($rows)==0) { |
| 1017 | + if ( count( $rows ) == 0 ) { |
1023 | 1018 | return; |
1024 | 1019 | } |
1025 | 1020 | |
— | — | @@ -1035,7 +1030,7 @@ |
1036 | 1031 | foreach ( $uniqueIndexes as $index ) { |
1037 | 1032 | if ( $first ) { |
1038 | 1033 | $first = false; |
1039 | | - $sql .= "("; |
| 1034 | + $sql .= '('; |
1040 | 1035 | } else { |
1041 | 1036 | $sql .= ') OR ('; |
1042 | 1037 | } |
— | — | @@ -1067,7 +1062,7 @@ |
1068 | 1063 | # DELETE where the condition is a join |
1069 | 1064 | function deleteJoin( $delTable, $joinTable, $delVar, $joinVar, $conds, $fname = 'DatabasePostgres::deleteJoin' ) { |
1070 | 1065 | if ( !$conds ) { |
1071 | | - throw new DBUnexpectedError($this, 'Database::deleteJoin() called with empty $conds' ); |
| 1066 | + throw new DBUnexpectedError( $this, 'DatabasePostgres::deleteJoin() called with empty $conds' ); |
1072 | 1067 | } |
1073 | 1068 | |
1074 | 1069 | $delTable = $this->tableName( $delTable ); |
— | — | @@ -1088,18 +1083,18 @@ |
1089 | 1084 | FROM pg_class c, pg_attribute a, pg_type t |
1090 | 1085 | WHERE relname='$table' AND a.attrelid=c.oid AND |
1091 | 1086 | a.atttypid=t.oid and a.attname='$field'"; |
1092 | | - $res =$this->query($sql); |
1093 | | - $row=$this->fetchObject($res); |
1094 | | - if ($row->ftype=="varchar") { |
1095 | | - $size=$row->size-4; |
| 1087 | + $res =$this->query( $sql ); |
| 1088 | + $row = $this->fetchObject( $res ); |
| 1089 | + if ( $row->ftype == 'varchar' ) { |
| 1090 | + $size = $row->size - 4; |
1096 | 1091 | } else { |
1097 | | - $size=$row->size; |
| 1092 | + $size = $row->size; |
1098 | 1093 | } |
1099 | 1094 | return $size; |
1100 | 1095 | } |
1101 | 1096 | |
1102 | | - function limitResult($sql, $limit, $offset=false) { |
1103 | | - return "$sql LIMIT $limit ".(is_numeric($offset)?" OFFSET {$offset} ":""); |
| 1097 | + function limitResult( $sql, $limit, $offset = false ) { |
| 1098 | + return "$sql LIMIT $limit " . ( is_numeric( $offset ) ? " OFFSET {$offset} " : '' ); |
1104 | 1099 | } |
1105 | 1100 | |
1106 | 1101 | function wasDeadlock() { |
— | — | @@ -1110,33 +1105,31 @@ |
1111 | 1106 | return $this->query( 'CREATE ' . ( $temporary ? 'TEMPORARY ' : '' ) . " TABLE $newName (LIKE $oldName INCLUDING DEFAULTS)", $fname ); |
1112 | 1107 | } |
1113 | 1108 | |
1114 | | - function timestamp( $ts=0 ) { |
1115 | | - return wfTimestamp(TS_POSTGRES,$ts); |
| 1109 | + function timestamp( $ts = 0 ) { |
| 1110 | + return wfTimestamp( TS_POSTGRES, $ts ); |
1116 | 1111 | } |
1117 | 1112 | |
1118 | 1113 | /** |
1119 | 1114 | * Return aggregated value function call |
1120 | 1115 | */ |
1121 | | - function aggregateValue ($valuedata,$valuename='value') { |
| 1116 | + function aggregateValue( $valuedata, $valuename = 'value' ) { |
1122 | 1117 | return $valuedata; |
1123 | 1118 | } |
1124 | 1119 | |
1125 | | - |
1126 | 1120 | function reportQueryError( $error, $errno, $sql, $fname, $tempIgnore = false ) { |
1127 | 1121 | // Ignore errors during error handling to avoid infinite recursion |
1128 | 1122 | $ignore = $this->ignoreErrors( true ); |
1129 | 1123 | $this->mErrorCount++; |
1130 | 1124 | |
1131 | | - if ($ignore || $tempIgnore) { |
1132 | | - wfDebug("SQL ERROR (ignored): $error\n"); |
| 1125 | + if ( $ignore || $tempIgnore ) { |
| 1126 | + wfDebug( "SQL ERROR (ignored): $error\n" ); |
1133 | 1127 | $this->ignoreErrors( $ignore ); |
1134 | | - } |
1135 | | - else { |
| 1128 | + } else { |
1136 | 1129 | $message = "A database error has occurred. Did you forget to run maintenance/update.php after upgrading? See: http://www.mediawiki.org/wiki/Manual:Upgrading#Run_the_update_script\n" . |
1137 | 1130 | "Query: $sql\n" . |
1138 | 1131 | "Function: $fname\n" . |
1139 | 1132 | "Error: $errno $error\n"; |
1140 | | - throw new DBUnexpectedError($this, $message); |
| 1133 | + throw new DBUnexpectedError( $this, $message ); |
1141 | 1134 | } |
1142 | 1135 | } |
1143 | 1136 | |
— | — | @@ -1144,7 +1137,7 @@ |
1145 | 1138 | * @return string wikitext of a link to the server software's web site |
1146 | 1139 | */ |
1147 | 1140 | public static function getSoftwareLink() { |
1148 | | - return "[http://www.postgresql.org/ PostgreSQL]"; |
| 1141 | + return '[http://www.postgresql.org/ PostgreSQL]'; |
1149 | 1142 | } |
1150 | 1143 | |
1151 | 1144 | /** |
— | — | @@ -1173,21 +1166,23 @@ |
1174 | 1167 | */ |
1175 | 1168 | function relationExists( $table, $types, $schema = false ) { |
1176 | 1169 | global $wgDBmwschema; |
1177 | | - if ( !is_array( $types ) ) |
| 1170 | + if ( !is_array( $types ) ) { |
1178 | 1171 | $types = array( $types ); |
1179 | | - if ( !$schema ) |
| 1172 | + } |
| 1173 | + if ( !$schema ) { |
1180 | 1174 | $schema = $wgDBmwschema; |
| 1175 | + } |
1181 | 1176 | $etable = $this->addQuotes( $table ); |
1182 | 1177 | $eschema = $this->addQuotes( $schema ); |
1183 | 1178 | $SQL = "SELECT 1 FROM pg_catalog.pg_class c, pg_catalog.pg_namespace n " |
1184 | 1179 | . "WHERE c.relnamespace = n.oid AND c.relname = $etable AND n.nspname = $eschema " |
1185 | | - . "AND c.relkind IN ('" . implode("','", $types) . "')"; |
| 1180 | + . "AND c.relkind IN ('" . implode( "','", $types ) . "')"; |
1186 | 1181 | $res = $this->query( $SQL ); |
1187 | 1182 | $count = $res ? $res->numRows() : 0; |
1188 | 1183 | return $count ? true : false; |
1189 | 1184 | } |
1190 | 1185 | |
1191 | | - /* |
| 1186 | + /** |
1192 | 1187 | * For backward compatibility, this function checks both tables and |
1193 | 1188 | * views. |
1194 | 1189 | */ |
— | — | @@ -1208,35 +1203,45 @@ |
1209 | 1204 | AND tgrelid=pg_class.oid |
1210 | 1205 | AND nspname=%s AND relname=%s AND tgname=%s |
1211 | 1206 | SQL; |
1212 | | - $res = $this->query(sprintf($q, |
1213 | | - $this->addQuotes($wgDBmwschema), |
1214 | | - $this->addQuotes($table), |
1215 | | - $this->addQuotes($trigger))); |
1216 | | - if (!$res) |
| 1207 | + $res = $this->query( |
| 1208 | + sprintf( |
| 1209 | + $q, |
| 1210 | + $this->addQuotes( $wgDBmwschema ), |
| 1211 | + $this->addQuotes( $table ), |
| 1212 | + $this->addQuotes( $trigger ) |
| 1213 | + ) |
| 1214 | + ); |
| 1215 | + if ( !$res ) { |
1217 | 1216 | return null; |
| 1217 | + } |
1218 | 1218 | $rows = $res->numRows(); |
1219 | 1219 | return $rows; |
1220 | 1220 | } |
1221 | 1221 | |
1222 | 1222 | function ruleExists( $table, $rule ) { |
1223 | 1223 | global $wgDBmwschema; |
1224 | | - $exists = $this->selectField("pg_rules", "rulename", |
1225 | | - array( "rulename" => $rule, |
1226 | | - "tablename" => $table, |
1227 | | - "schemaname" => $wgDBmwschema ) ); |
| 1224 | + $exists = $this->selectField( 'pg_rules', 'rulename', |
| 1225 | + array( |
| 1226 | + 'rulename' => $rule, |
| 1227 | + 'tablename' => $table, |
| 1228 | + 'schemaname' => $wgDBmwschema |
| 1229 | + ) |
| 1230 | + ); |
1228 | 1231 | return $exists === $rule; |
1229 | 1232 | } |
1230 | 1233 | |
1231 | 1234 | function constraintExists( $table, $constraint ) { |
1232 | 1235 | global $wgDBmwschema; |
1233 | | - $SQL = sprintf("SELECT 1 FROM information_schema.table_constraints ". |
| 1236 | + $SQL = sprintf( "SELECT 1 FROM information_schema.table_constraints ". |
1234 | 1237 | "WHERE constraint_schema = %s AND table_name = %s AND constraint_name = %s", |
1235 | | - $this->addQuotes($wgDBmwschema), |
1236 | | - $this->addQuotes($table), |
1237 | | - $this->addQuotes($constraint)); |
1238 | | - $res = $this->query($SQL); |
1239 | | - if (!$res) |
| 1238 | + $this->addQuotes( $wgDBmwschema ), |
| 1239 | + $this->addQuotes( $table ), |
| 1240 | + $this->addQuotes( $constraint ) |
| 1241 | + ); |
| 1242 | + $res = $this->query( $SQL ); |
| 1243 | + if ( !$res ) { |
1240 | 1244 | return null; |
| 1245 | + } |
1241 | 1246 | $rows = $res->numRows(); |
1242 | 1247 | return $rows; |
1243 | 1248 | } |
— | — | @@ -1245,7 +1250,7 @@ |
1246 | 1251 | * Query whether a given schema exists. Returns the name of the owner |
1247 | 1252 | */ |
1248 | 1253 | function schemaExists( $schema ) { |
1249 | | - $eschema = preg_replace("/'/", "''", $schema); |
| 1254 | + $eschema = preg_replace( "/'/", "''", $schema ); |
1250 | 1255 | $SQL = "SELECT rolname FROM pg_catalog.pg_namespace n, pg_catalog.pg_roles r " |
1251 | 1256 | ."WHERE n.nspowner=r.oid AND n.nspname = '$eschema'"; |
1252 | 1257 | $res = $this->query( $SQL ); |
— | — | @@ -1259,7 +1264,7 @@ |
1260 | 1265 | } |
1261 | 1266 | |
1262 | 1267 | function fieldInfo( $table, $field ) { |
1263 | | - return PostgresField::fromText($this, $table, $field); |
| 1268 | + return PostgresField::fromText( $this, $table, $field ); |
1264 | 1269 | } |
1265 | 1270 | |
1266 | 1271 | /** |
— | — | @@ -1282,90 +1287,90 @@ |
1283 | 1288 | |
1284 | 1289 | // Make sure that we can write to the correct schema |
1285 | 1290 | // If not, Postgres will happily and silently go to the next search_path item |
1286 | | - $ctest = "mediawiki_test_table"; |
1287 | | - $safeschema = $this->quote_ident($wgDBmwschema); |
1288 | | - if ($this->tableExists($ctest, $wgDBmwschema)) { |
1289 | | - $this->doQuery("DROP TABLE $safeschema.$ctest"); |
| 1291 | + $ctest = 'mediawiki_test_table'; |
| 1292 | + $safeschema = $this->quote_ident( $wgDBmwschema ); |
| 1293 | + if ( $this->tableExists( $ctest, $wgDBmwschema ) ) { |
| 1294 | + $this->doQuery( "DROP TABLE $safeschema.$ctest" ); |
1290 | 1295 | } |
1291 | 1296 | $SQL = "CREATE TABLE $safeschema.$ctest(a int)"; |
1292 | 1297 | $olde = error_reporting( 0 ); |
1293 | | - $res = $this->doQuery($SQL); |
| 1298 | + $res = $this->doQuery( $SQL ); |
1294 | 1299 | error_reporting( $olde ); |
1295 | | - if (!$res) { |
1296 | | - print "<b>FAILED</b>. Make sure that the user \"" . htmlspecialchars( $wgDBuser ) . |
1297 | | - "\" can write to the schema \"" . htmlspecialchars( $wgDBmwschema ) . "\"</li>\n"; |
1298 | | - dieout(""); # Will close the main list <ul> and finish the page. |
| 1300 | + if ( !$res ) { |
| 1301 | + print '<b>FAILED</b>. Make sure that the user "' . htmlspecialchars( $wgDBuser ) . |
| 1302 | + '" can write to the schema "' . htmlspecialchars( $wgDBmwschema ) . "\"</li>\n"; |
| 1303 | + dieout( '' ); # Will close the main list <ul> and finish the page. |
1299 | 1304 | } |
1300 | | - $this->doQuery("DROP TABLE $safeschema.$ctest"); |
| 1305 | + $this->doQuery( "DROP TABLE $safeschema.$ctest" ); |
1301 | 1306 | |
1302 | 1307 | $res = $this->sourceFile( "../maintenance/postgres/tables.sql" ); |
1303 | | - if ($res === true) { |
| 1308 | + if ( $res === true ) { |
1304 | 1309 | print " done.</li>\n"; |
1305 | 1310 | } else { |
1306 | 1311 | print " <b>FAILED</b></li>\n"; |
1307 | 1312 | dieout( htmlspecialchars( $res ) ); |
1308 | 1313 | } |
1309 | 1314 | |
1310 | | - ## Update version information |
1311 | | - $mwv = $this->addQuotes($wgVersion); |
1312 | | - $pgv = $this->addQuotes($this->getServerVersion()); |
1313 | | - $pgu = $this->addQuotes($this->mUser); |
1314 | | - $mws = $this->addQuotes($wgDBmwschema); |
1315 | | - $tss = $this->addQuotes($wgDBts2schema); |
1316 | | - $pgp = $this->addQuotes($wgDBport); |
1317 | | - $dbn = $this->addQuotes($this->mDBname); |
1318 | | - $ctype = $this->addQuotes( pg_fetch_result($this->doQuery("SHOW lc_ctype"),0,0) ); |
| 1315 | + # Update version information |
| 1316 | + $mwv = $this->addQuotes( $wgVersion); |
| 1317 | + $pgv = $this->addQuotes( $this->getServerVersion() ); |
| 1318 | + $pgu = $this->addQuotes( $this->mUser ); |
| 1319 | + $mws = $this->addQuotes( $wgDBmwschema ); |
| 1320 | + $tss = $this->addQuotes( $wgDBts2schema ); |
| 1321 | + $pgp = $this->addQuotes( $wgDBport ); |
| 1322 | + $dbn = $this->addQuotes( $this->mDBname ); |
| 1323 | + $ctype = $this->addQuotes( pg_fetch_result( $this->doQuery( 'SHOW lc_ctype' ), 0, 0 ) ); |
1319 | 1324 | |
1320 | | - echo "<li>Populating interwiki table... "; |
1321 | | - ## Avoid the non-standard "REPLACE INTO" syntax |
| 1325 | + echo '<li>Populating interwiki table... '; |
| 1326 | + # Avoid the non-standard "REPLACE INTO" syntax |
1322 | 1327 | $f = fopen( "../maintenance/interwiki.sql", 'r' ); |
1323 | 1328 | if ( !$f ) { |
1324 | | - print "<b>FAILED</b></li>"; |
1325 | | - dieout( "Could not find the interwiki.sql file" ); |
| 1329 | + print '<b>FAILED</b></li>'; |
| 1330 | + dieout( 'Could not find the interwiki.sql file' ); |
1326 | 1331 | } |
1327 | | - ## We simply assume it is already empty as we have just created it |
| 1332 | + # We simply assume it is already empty as we have just created it |
1328 | 1333 | $SQL = "INSERT INTO interwiki(iw_prefix,iw_url,iw_local) VALUES "; |
1329 | | - while ( ! feof( $f ) ) { |
1330 | | - $line = fgets($f,1024); |
| 1334 | + while ( !feof( $f ) ) { |
| 1335 | + $line = fgets( $f, 1024 ); |
1331 | 1336 | $matches = array(); |
1332 | | - if (!preg_match('/^\s*(\(.+?),(\d)\)/', $line, $matches)) { |
| 1337 | + if ( !preg_match( '/^\s*(\(.+?),(\d)\)/', $line, $matches ) ) { |
1333 | 1338 | continue; |
1334 | 1339 | } |
1335 | | - $this->query("$SQL $matches[1],$matches[2])"); |
| 1340 | + $this->query( "$SQL $matches[1],$matches[2])" ); |
1336 | 1341 | } |
1337 | 1342 | print " successfully populated.</li>\n"; |
1338 | 1343 | |
1339 | | - $this->doQuery("COMMIT"); |
| 1344 | + $this->doQuery( 'COMMIT' ); |
1340 | 1345 | } |
1341 | 1346 | |
1342 | 1347 | function encodeBlob( $b ) { |
1343 | | - return new Blob ( pg_escape_bytea( $this->mConn, $b ) ) ; |
| 1348 | + return new Blob( pg_escape_bytea( $this->mConn, $b ) ); |
1344 | 1349 | } |
1345 | 1350 | |
1346 | 1351 | function decodeBlob( $b ) { |
1347 | | - if ($b instanceof Blob) { |
| 1352 | + if ( $b instanceof Blob ) { |
1348 | 1353 | $b = $b->fetch(); |
1349 | 1354 | } |
1350 | 1355 | return pg_unescape_bytea( $b ); |
1351 | 1356 | } |
1352 | 1357 | |
1353 | | - function strencode( $s ) { ## Should not be called by us |
| 1358 | + function strencode( $s ) { # Should not be called by us |
1354 | 1359 | return pg_escape_string( $this->mConn, $s ); |
1355 | 1360 | } |
1356 | 1361 | |
1357 | 1362 | function addQuotes( $s ) { |
1358 | 1363 | if ( is_null( $s ) ) { |
1359 | 1364 | return 'NULL'; |
1360 | | - } else if ( is_bool( $s ) ) { |
| 1365 | + } elseif ( is_bool( $s ) ) { |
1361 | 1366 | return intval( $s ); |
1362 | | - } else if ($s instanceof Blob) { |
1363 | | - return "'".$s->fetch($s)."'"; |
| 1367 | + } elseif ( $s instanceof Blob ) { |
| 1368 | + return "'" . $s->fetch( $s ) . "'"; |
1364 | 1369 | } |
1365 | 1370 | return "'" . pg_escape_string( $this->mConn, $s ) . "'"; |
1366 | 1371 | } |
1367 | 1372 | |
1368 | 1373 | function quote_ident( $s ) { |
1369 | | - return '"' . preg_replace( '/"/', '""', $s) . '"'; |
| 1374 | + return '"' . preg_replace( '/"/', '""', $s ) . '"'; |
1370 | 1375 | } |
1371 | 1376 | |
1372 | 1377 | /** |
— | — | @@ -1379,15 +1384,14 @@ |
1380 | 1385 | * @return string SQL string |
1381 | 1386 | */ |
1382 | 1387 | protected function replaceVars( $ins ) { |
1383 | | - |
1384 | 1388 | $ins = parent::replaceVars( $ins ); |
1385 | 1389 | |
1386 | | - if ($this->numeric_version >= 8.3) { |
| 1390 | + if ( $this->numeric_version >= 8.3 ) { |
1387 | 1391 | // Thanks for not providing backwards-compatibility, 8.3 |
1388 | 1392 | $ins = preg_replace( "/to_tsvector\s*\(\s*'default'\s*,/", 'to_tsvector(', $ins ); |
1389 | 1393 | } |
1390 | 1394 | |
1391 | | - if ($this->numeric_version <= 8.1) { // Our minimum version |
| 1395 | + if ( $this->numeric_version <= 8.1 ) { // Our minimum version |
1392 | 1396 | $ins = str_replace( 'USING gin', 'USING gist', $ins ); |
1393 | 1397 | } |
1394 | 1398 | |
— | — | @@ -1414,19 +1418,31 @@ |
1415 | 1419 | } |
1416 | 1420 | } |
1417 | 1421 | |
1418 | | - if ( isset( $options['GROUP BY'] ) ) $preLimitTail .= " GROUP BY " . $options['GROUP BY']; |
1419 | | - if ( isset( $options['HAVING'] ) ) $preLimitTail .= " HAVING {$options['HAVING']}"; |
1420 | | - if ( isset( $options['ORDER BY'] ) ) $preLimitTail .= " ORDER BY " . $options['ORDER BY']; |
| 1422 | + if ( isset( $options['GROUP BY'] ) ) { |
| 1423 | + $preLimitTail .= ' GROUP BY ' . $options['GROUP BY']; |
| 1424 | + } |
| 1425 | + if ( isset( $options['HAVING'] ) ) { |
| 1426 | + $preLimitTail .= " HAVING {$options['HAVING']}"; |
| 1427 | + } |
| 1428 | + if ( isset( $options['ORDER BY'] ) ) { |
| 1429 | + $preLimitTail .= ' ORDER BY ' . $options['ORDER BY']; |
| 1430 | + } |
1421 | 1431 | |
1422 | | - //if (isset($options['LIMIT'])) { |
1423 | | - // $tailOpts .= $this->limitResult('', $options['LIMIT'], |
1424 | | - // isset($options['OFFSET']) ? $options['OFFSET'] |
1425 | | - // : false); |
| 1432 | + //if ( isset( $options['LIMIT'] ) ) { |
| 1433 | + // $tailOpts .= $this->limitResult( '', $options['LIMIT'], |
| 1434 | + // isset( $options['OFFSET'] ) ? $options['OFFSET'] |
| 1435 | + // : false ); |
1426 | 1436 | //} |
1427 | 1437 | |
1428 | | - if ( isset( $noKeyOptions['FOR UPDATE'] ) ) $postLimitTail .= ' FOR UPDATE'; |
1429 | | - if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) $postLimitTail .= ' LOCK IN SHARE MODE'; |
1430 | | - if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) $startOpts .= 'DISTINCT'; |
| 1438 | + if ( isset( $noKeyOptions['FOR UPDATE'] ) ) { |
| 1439 | + $postLimitTail .= ' FOR UPDATE'; |
| 1440 | + } |
| 1441 | + if ( isset( $noKeyOptions['LOCK IN SHARE MODE'] ) ) { |
| 1442 | + $postLimitTail .= ' LOCK IN SHARE MODE'; |
| 1443 | + } |
| 1444 | + if ( isset( $noKeyOptions['DISTINCT'] ) || isset( $noKeyOptions['DISTINCTROW'] ) ) { |
| 1445 | + $startOpts .= 'DISTINCT'; |
| 1446 | + } |
1431 | 1447 | |
1432 | 1448 | return array( $startOpts, $useIndex, $preLimitTail, $postLimitTail ); |
1433 | 1449 | } |
— | — | @@ -1446,6 +1462,6 @@ |
1447 | 1463 | } |
1448 | 1464 | |
1449 | 1465 | public function getSearchEngine() { |
1450 | | - return "SearchPostgres"; |
| 1466 | + return 'SearchPostgres'; |
1451 | 1467 | } |
1452 | 1468 | } // end DatabasePostgres class |