Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStoreLight.php |
— | — | @@ -995,8 +995,8 @@ |
996 | 996 | $fieldarray[$fieldname] = $dbtypes[$typeid]; |
997 | 997 | } |
998 | 998 | $indexes = array_merge( $indexes, $proptable->indexes ); |
999 | | - SMWSQLHelpers::setupTable( $db->tableName( $proptable->name ), $fieldarray, $db, $reportTo ); |
1000 | | - SMWSQLHelpers::setupIndex( $db->tableName( $proptable->name ), $indexes, $db ); |
| 999 | + SMWSQLHelpers::setupTable( $proptable->name, $fieldarray, $db, $reportTo ); |
| 1000 | + SMWSQLHelpers::setupIndex( $proptable->name, $indexes, $db ); |
1001 | 1001 | } |
1002 | 1002 | |
1003 | 1003 | $this->reportProgress( "Database initialised successfully.\n\n", $verbose ); |
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLStore2.php |
— | — | @@ -1182,9 +1182,11 @@ |
1183 | 1183 | |
1184 | 1184 | public function getStatistics() { |
1185 | 1185 | wfProfileIn( 'SMWSQLStore2::getStatistics (SMW)' ); |
| 1186 | + |
1186 | 1187 | $db = wfGetDB( DB_SLAVE ); |
1187 | 1188 | $result = array(); |
1188 | 1189 | $proptables = self::getPropertyTables(); |
| 1190 | + |
1189 | 1191 | // count number of declared properties by counting "has type" annotations |
1190 | 1192 | $typeprop = SMWPropertyValue::makeProperty( '_TYPE' ); |
1191 | 1193 | $typetable = $proptables[self::findPropertyTableID( $typeprop )]; |
— | — | @@ -1192,10 +1194,12 @@ |
1193 | 1195 | $row = $db->fetchObject( $res ); |
1194 | 1196 | $result['DECLPROPS'] = $row->count; |
1195 | 1197 | $db->freeResult( $res ); |
| 1198 | + |
1196 | 1199 | // count property uses by counting rows in property tables, |
1197 | 1200 | // count used properties by counting distinct properties in each table |
1198 | 1201 | $result['PROPUSES'] = 0; |
1199 | 1202 | $result['USEDPROPS'] = 0; |
| 1203 | + |
1200 | 1204 | foreach ( self::getPropertyTables() as $proptable ) { |
1201 | 1205 | /// Note: subproperties that are part of container values are counted individually; |
1202 | 1206 | /// It does not seem to be important to filter them by adding more conditions. |
— | — | @@ -1203,6 +1207,7 @@ |
1204 | 1208 | $row = $db->fetchObject( $res ); |
1205 | 1209 | $result['PROPUSES'] += $row->count; |
1206 | 1210 | $db->freeResult( $res ); |
| 1211 | + |
1207 | 1212 | if ( $proptable->fixedproperty == false ) { |
1208 | 1213 | $res = $db->select( $proptable->name, 'COUNT(DISTINCT(p_id)) AS count', '', 'SMW::getStatistics' ); |
1209 | 1214 | $row = $db->fetchObject( $res ); |
— | — | @@ -1211,6 +1216,7 @@ |
1212 | 1217 | $res = $db->select( $proptable->name, '*', '', 'SMW::getStatistics', array( 'LIMIT' => 1 ) ); |
1213 | 1218 | if ( $db->numRows( $res ) > 0 ) $result['USEDPROPS']++; |
1214 | 1219 | } |
| 1220 | + |
1215 | 1221 | $db->freeResult( $res ); |
1216 | 1222 | } |
1217 | 1223 | |
— | — | @@ -1260,7 +1266,6 @@ |
1261 | 1267 | protected function setupTables( $verbose, $db ) { |
1262 | 1268 | global $wgDBtype; |
1263 | 1269 | |
1264 | | - extract( $db->tableNames( 'smw_ids', 'smw_spec2', 'smw_conccache', 'smw_conc2' ) ); |
1265 | 1270 | $reportTo = $verbose ? $this : null; // Use $this to report back from static SMWSQLHelpers. |
1266 | 1271 | |
1267 | 1272 | // Repeatedly used DB field types defined here for convenience. |
— | — | @@ -1276,6 +1281,8 @@ |
1277 | 1282 | 'w' => SMWSQLHelpers::getStandardDBType( 'iw' ) |
1278 | 1283 | ); |
1279 | 1284 | |
| 1285 | + $smw_spec2 = $db->tableName( 'smw_spec2' ); |
| 1286 | + |
1280 | 1287 | // DB update: field renaming between SMW 1.3 and SMW 1.4. |
1281 | 1288 | if ( ( $db->tableExists( $smw_spec2 ) ) && ( $db->fieldExists( $smw_spec2, 'sp_id', 'SMWSQLStore2::setup' ) ) ) { |
1282 | 1289 | if ( $wgDBtype == 'postgres' ) { |
— | — | @@ -1287,7 +1294,7 @@ |
1288 | 1295 | |
1289 | 1296 | // Set up table for internal IDs used in this store: |
1290 | 1297 | SMWSQLHelpers::setupTable( |
1291 | | - $smw_ids, |
| 1298 | + 'smw_ids', |
1292 | 1299 | array( |
1293 | 1300 | 'smw_id' => $dbtypes['p'] . ' NOT NULL' . ( $wgDBtype == 'postgres' ? ' PRIMARY KEY' : ' KEY AUTO_INCREMENT' ), |
1294 | 1301 | 'smw_namespace' => $dbtypes['n'] . ' NOT NULL', |
— | — | @@ -1299,11 +1306,11 @@ |
1300 | 1307 | $reportTo |
1301 | 1308 | ); |
1302 | 1309 | |
1303 | | - SMWSQLHelpers::setupIndex( $smw_ids, array( 'smw_id', 'smw_title,smw_namespace,smw_iw', 'smw_sortkey' ), $db ); |
| 1310 | + SMWSQLHelpers::setupIndex( 'smw_ids', array( 'smw_id', 'smw_title,smw_namespace,smw_iw', 'smw_sortkey' ), $db ); |
1304 | 1311 | |
1305 | 1312 | // Set up concept cache: member elements (s)->concepts (o) |
1306 | 1313 | SMWSQLHelpers::setupTable( |
1307 | | - $smw_conccache, |
| 1314 | + 'smw_conccache', |
1308 | 1315 | array( |
1309 | 1316 | 's_id' => $dbtypes['p'] . ' NOT NULL', |
1310 | 1317 | 'o_id' => $dbtypes['p'] . ' NOT NULL' |
— | — | @@ -1312,11 +1319,11 @@ |
1313 | 1320 | $reportTo |
1314 | 1321 | ); |
1315 | 1322 | |
1316 | | - SMWSQLHelpers::setupIndex( $smw_conccache, array( 'o_id' ), $db ); |
| 1323 | + SMWSQLHelpers::setupIndex( 'smw_conccache', array( 'o_id' ), $db ); |
1317 | 1324 | |
1318 | 1325 | // Set up concept descriptions. |
1319 | 1326 | SMWSQLHelpers::setupTable( |
1320 | | - $smw_conc2, |
| 1327 | + 'smw_conc2', |
1321 | 1328 | array( |
1322 | 1329 | 's_id' => $dbtypes['p'] . ' NOT NULL' . ( $wgDBtype == 'postgres' ? ' PRIMARY KEY' : ' KEY' ), |
1323 | 1330 | 'concept_txt' => $dbtypes['l'], |
— | — | @@ -1331,7 +1338,7 @@ |
1332 | 1339 | $reportTo |
1333 | 1340 | ); |
1334 | 1341 | |
1335 | | - SMWSQLHelpers::setupIndex( $smw_conc2, array( 's_id' ), $db ); |
| 1342 | + SMWSQLHelpers::setupIndex( 'smw_conc2', array( 's_id' ), $db ); |
1336 | 1343 | |
1337 | 1344 | // Set up all property tables as defined: |
1338 | 1345 | $this->setupPropertyTables( $dbtypes, $db, $reportTo ); |
— | — | @@ -1378,8 +1385,8 @@ |
1379 | 1386 | |
1380 | 1387 | $indexes = array_merge( $indexes, $proptable->indexes ); |
1381 | 1388 | |
1382 | | - SMWSQLHelpers::setupTable( $db->tableName( $proptable->name ), $fieldarray, $db, $reportTo ); |
1383 | | - SMWSQLHelpers::setupIndex( $db->tableName( $proptable->name ), $indexes, $db ); |
| 1389 | + SMWSQLHelpers::setupTable( $proptable->name, $fieldarray, $db, $reportTo ); |
| 1390 | + SMWSQLHelpers::setupIndex( $proptable->name, $indexes, $db ); |
1384 | 1391 | } |
1385 | 1392 | } |
1386 | 1393 | |
— | — | @@ -2432,6 +2439,7 @@ |
2433 | 2440 | */ |
2434 | 2441 | public static function getPropertyTables() { |
2435 | 2442 | if ( count( self::$prop_tables ) > 0 ) return self::$prop_tables; // don't initialise twice |
| 2443 | + |
2436 | 2444 | self::$prop_tables['smw_rels2'] = new SMWSQLStore2Table( 'smw_rels2', |
2437 | 2445 | array( 'o_id' => 'p' ), |
2438 | 2446 | array( 'o_id' ) ); |
Index: trunk/extensions/SemanticMediaWiki/includes/storage/SMW_SQLHelpers.php |
— | — | @@ -5,6 +5,8 @@ |
6 | 6 | * |
7 | 7 | * @author Markus Krötzsch |
8 | 8 | * @author Marcel Gsteiger |
| 9 | + * @author Jeroen De Dauw |
| 10 | + * |
9 | 11 | * @file |
10 | 12 | * @ingroup SMWStore |
11 | 13 | */ |
— | — | @@ -12,6 +14,7 @@ |
13 | 15 | /** |
14 | 16 | * Static class to collect some helper functions that SMW uses |
15 | 17 | * for settnig up SQL databases. |
| 18 | + * |
16 | 19 | * @ingroup SMWStore |
17 | 20 | */ |
18 | 21 | class SMWSQLHelpers { |
— | — | @@ -26,13 +29,15 @@ |
27 | 30 | */ |
28 | 31 | static public function getStandardDBType( $input ) { |
29 | 32 | global $wgDBtype; |
| 33 | + |
30 | 34 | switch ( $input ) { |
31 | | - case 'id': return $wgDBtype == 'postgres' ? 'SERIAL':'INT(8) UNSIGNED'; // like page_id in MW page table |
32 | | - case 'namespace': return $wgDBtype == 'postgres' ? 'BIGINT':'INT(11)'; // like page_namespace in MW page table |
33 | | - case 'title': return $wgDBtype == 'postgres' ? 'TEXT':'VARBINARY(255)'; // like page_title in MW page table |
34 | | - case 'iw': return $wgDBtype == 'postgres' ? 'TEXT':'VARCHAR(32) binary'; // like iw_prefix in MW interwiki table |
35 | | - case 'blob': return $wgDBtype == 'postgres' ? 'BYTEA':'MEDIUMBLOB'; // larger blobs of character data, usually not subject to SELECT conditions |
| 35 | + case 'id': return $wgDBtype == 'postgres' ? 'SERIAL' : 'INT(8) UNSIGNED'; // like page_id in MW page table |
| 36 | + case 'namespace': return $wgDBtype == 'postgres' ? 'BIGINT' : 'INT(11)'; // like page_namespace in MW page table |
| 37 | + case 'title': return $wgDBtype == 'postgres' ? 'TEXT' : 'VARBINARY(255)'; // like page_title in MW page table |
| 38 | + case 'iw': return $wgDBtype == 'postgres' ? 'TEXT' : 'VARCHAR(32) binary'; // like iw_prefix in MW interwiki table |
| 39 | + case 'blob': return $wgDBtype == 'postgres' ? 'BYTEA' : 'MEDIUMBLOB'; // larger blobs of character data, usually not subject to SELECT conditions |
36 | 40 | } |
| 41 | + |
37 | 42 | return false; |
38 | 43 | } |
39 | 44 | |
— | — | @@ -58,29 +63,45 @@ |
59 | 64 | * |
60 | 65 | * @note The function partly ignores the order in which fields are set up. |
61 | 66 | * Only if the type of some field changes will its order be adjusted explicitly. |
| 67 | + * |
| 68 | + * @param string $tableName The table name. Does not need to have been passed to DatabaseBase->tableName yet. |
| 69 | + * @param array $columns The field names to put indexes on |
| 70 | + * @param DatabaseBase $db |
| 71 | + * @param $reportTo |
| 72 | + * |
| 73 | + * TODO: split up megamoth srysly o_O |
62 | 74 | */ |
63 | | - public static function setupTable( $table, $fields, $db, $reportTo = null ) { |
| 75 | + public static function setupTable( $tableName, array $fields, DatabaseBase $db, $reportTo = null ) { |
64 | 76 | global $wgDBname, $wgDBtype, $wgDBTableOptions; |
| 77 | + |
| 78 | + $tableName = $db->tableName( $tableName ); |
65 | 79 | $fname = 'SMWSQLHelpers::setupTable'; |
66 | 80 | |
67 | | - SMWSQLHelpers::reportProgress( "Setting up table $table ...\n", $reportTo ); |
68 | | - if ( $db->tableExists( $table ) === false ) { // create new table |
69 | | - $sql = 'CREATE TABLE ' . ( $wgDBtype == 'postgres' ? '': "`$wgDBname`." ) . $table . ' ('; |
| 81 | + SMWSQLHelpers::reportProgress( "Setting up table $tableName ...\n", $reportTo ); |
| 82 | + |
| 83 | + if ( $db->tableExists( $tableName ) === false ) { // create new table |
| 84 | + $sql = 'CREATE TABLE ' . ( $wgDBtype == 'postgres' ? '' : "`$wgDBname`." ) . $tableName . ' ('; |
70 | 85 | $first = true; |
| 86 | + |
71 | 87 | foreach ( $fields as $name => $type ) { |
72 | 88 | if ( $first ) { |
73 | 89 | $first = false; |
74 | 90 | } else { |
75 | 91 | $sql .= ','; |
76 | 92 | } |
| 93 | + |
77 | 94 | $sql .= $name . ' ' . $type; |
78 | 95 | } |
79 | | - $sql .= ') ' . ( $wgDBtype == 'postgres' ? '':$wgDBTableOptions ); |
| 96 | + |
| 97 | + $sql .= ') ' . ( $wgDBtype == 'postgres' ? '' : $wgDBTableOptions ); |
80 | 98 | $db->query( $sql, $fname ); |
| 99 | + |
81 | 100 | SMWSQLHelpers::reportProgress( " ... new table created\n", $reportTo ); |
| 101 | + |
82 | 102 | return array(); |
83 | 103 | } else { // check table signature |
84 | 104 | SMWSQLHelpers::reportProgress( " ... table exists already, checking structure ...\n", $reportTo ); |
| 105 | + |
85 | 106 | if ( $wgDBtype == 'postgres' ) { // postgresql |
86 | 107 | // use the data dictionary in postgresql to get an output comparable to DESCRIBE |
87 | 108 | // To find out what kind of magic takes place here (and to remove the bugs included), simply use: |
— | — | @@ -98,19 +119,22 @@ |
99 | 120 | . ' SELECT c.oid ' |
100 | 121 | . ' FROM pg_catalog.pg_class c ' |
101 | 122 | . ' LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace ' |
102 | | - . ' WHERE c.relname ~ \'^(' . $table . ')$\' ' |
| 123 | + . ' WHERE c.relname ~ \'^(' . $tableName . ')$\' ' |
103 | 124 | . ' AND pg_catalog.pg_table_is_visible(c.oid) ' |
104 | 125 | . ' LIMIT 1 ' |
105 | 126 | . ' ) AND a.attnum > 0 AND NOT a.attisdropped ' |
106 | 127 | . ' ORDER BY a.attnum'; |
107 | | - } else { // mysql |
108 | | - $sql = 'DESCRIBE ' . $table; |
| 128 | + } else { // MySQL |
| 129 | + $sql = 'DESCRIBE ' . $tableName; |
109 | 130 | } |
| 131 | + |
110 | 132 | $res = $db->query( $sql, $fname ); |
111 | 133 | $curfields = array(); |
112 | 134 | $result = array(); |
| 135 | + |
113 | 136 | while ( $row = $db->fetchObject( $res ) ) { |
114 | 137 | $type = strtoupper( $row->Type ); |
| 138 | + |
115 | 139 | if ( $wgDBtype == 'postgres' ) { // postgresql |
116 | 140 | if ( eregi( '^nextval\\(.+\\)$', $row->Extra ) ) { |
117 | 141 | $type = 'SERIAL NOT NULL'; |
— | — | @@ -121,44 +145,57 @@ |
122 | 146 | if ( substr( $type, 0, 8 ) == 'VARCHAR(' ) { |
123 | 147 | $type .= ' binary'; // just assume this to be the case for VARCHAR, though DESCRIBE will not tell us |
124 | 148 | } |
| 149 | + |
125 | 150 | if ( $row->Null != 'YES' ) { |
126 | 151 | $type .= ' NOT NULL'; |
127 | 152 | } |
| 153 | + |
128 | 154 | if ( $row->Key == 'PRI' ) { /// FIXME: updating "KEY" is not possible, the below query will fail in this case. |
129 | 155 | $type .= ' KEY'; |
130 | 156 | } |
| 157 | + |
131 | 158 | if ( $row->Extra == 'auto_increment' ) { |
132 | 159 | $type .= ' AUTO_INCREMENT'; |
133 | 160 | } |
134 | 161 | } |
| 162 | + |
135 | 163 | $curfields[$row->Field] = $type; |
136 | 164 | } |
137 | 165 | |
138 | 166 | if ( $wgDBtype == 'postgres' ) { // postgresql |
139 | 167 | foreach ( $fields as $name => $type ) { |
140 | 168 | $keypos = strpos( $type, ' PRIMARY KEY' ); |
| 169 | + |
141 | 170 | if ( $keypos > 0 ) { |
142 | 171 | $type = substr( $type, 0, $keypos ); |
143 | 172 | } |
| 173 | + |
144 | 174 | if ( !array_key_exists( $name, $curfields ) ) { |
145 | 175 | SMWSQLHelpers::reportProgress( " ... creating column $name ... ", $reportTo ); |
146 | | - $db->query( "ALTER TABLE $table ADD \"" . $name . "\" $type", $fname ); |
| 176 | + |
| 177 | + $db->query( "ALTER TABLE $tableName ADD \"" . $name . "\" $type", $fname ); |
147 | 178 | $result[$name] = 'new'; |
| 179 | + |
148 | 180 | SMWSQLHelpers::reportProgress( "done \n", $reportTo ); |
149 | 181 | } elseif ( $curfields[$name] != $type ) { |
150 | 182 | SMWSQLHelpers::reportProgress( " ... changing type of column $name from '$curfields[$name]' to '$type' ... ", $reportTo ); |
151 | 183 | $notnullposnew = strpos( $type, ' NOT NULL' ); |
| 184 | + |
152 | 185 | if ( $notnullposnew > 0 ) { |
153 | 186 | $type = substr( $type, 0, $notnullposnew ); |
154 | 187 | } |
| 188 | + |
155 | 189 | $notnullposold = strpos( $curfields[$name], ' NOT NULL' ); |
156 | 190 | $typeold = ( $notnullposold > 0 ) ? substr( $curfields[$name], 0, $notnullposold ):$curfields[$name]; |
| 191 | + |
157 | 192 | if ( $typeold != $type ) { |
158 | | - $db->query( "ALTER TABLE \"" . $table . "\" ALTER COLUMN \"" . $name . "\" TYPE " . $type, $fname ); |
| 193 | + $db->query( "ALTER TABLE \"" . $tableName . "\" ALTER COLUMN \"" . $name . "\" TYPE " . $type, $fname ); |
159 | 194 | } |
| 195 | + |
160 | 196 | if ( $notnullposold != $notnullposnew ) { |
161 | | - $db->query( "ALTER TABLE \"" . $table . "\" ALTER COLUMN \"" . $name . "\" " . ( $notnullposnew > 0 ? 'SET':'DROP' ) . " NOT NULL", $fname ); |
| 197 | + $db->query( "ALTER TABLE \"" . $tableName . "\" ALTER COLUMN \"" . $name . "\" " . ( $notnullposnew > 0 ? 'SET':'DROP' ) . " NOT NULL", $fname ); |
162 | 198 | } |
| 199 | + |
163 | 200 | $result[$name] = 'up'; |
164 | 201 | $curfields[$name] = false; |
165 | 202 | SMWSQLHelpers::reportProgress( "done.\n", $reportTo ); |
— | — | @@ -167,45 +204,56 @@ |
168 | 205 | $curfields[$name] = false; |
169 | 206 | } |
170 | 207 | } |
| 208 | + |
171 | 209 | foreach ( $curfields as $name => $value ) { |
172 | 210 | if ( $value !== false ) { |
173 | 211 | SMWSQLHelpers::reportProgress( " ... deleting obsolete column $name ... ", $reportTo ); |
174 | | - $db->query( "ALTER TABLE \"" . $table . "\" DROP COLUMN \"" . $name . "\"", $fname ); |
| 212 | + |
| 213 | + $db->query( "ALTER TABLE \"" . $tableName . "\" DROP COLUMN \"" . $name . "\"", $fname ); |
175 | 214 | $result[$name] = 'del'; |
| 215 | + |
176 | 216 | SMWSQLHelpers::reportProgress( "done.\n", $reportTo ); |
177 | 217 | } |
178 | 218 | } |
179 | 219 | } else { // mysql |
180 | 220 | $position = 'FIRST'; |
| 221 | + |
181 | 222 | foreach ( $fields as $name => $type ) { |
182 | 223 | if ( !array_key_exists( $name, $curfields ) ) { |
183 | 224 | SMWSQLHelpers::reportProgress( " ... creating column $name ... ", $reportTo ); |
184 | | - $db->query( "ALTER TABLE $table ADD `$name` $type $position", $fname ); |
| 225 | + |
| 226 | + $db->query( "ALTER TABLE $tableName ADD `$name` $type $position", $fname ); |
185 | 227 | $result[$name] = 'new'; |
| 228 | + |
186 | 229 | SMWSQLHelpers::reportProgress( "done \n", $reportTo ); |
187 | 230 | } elseif ( $curfields[$name] != $type ) { |
188 | 231 | SMWSQLHelpers::reportProgress( " ... changing type of column $name from '$curfields[$name]' to '$type' ... ", $reportTo ); |
189 | | - $db->query( "ALTER TABLE $table CHANGE `$name` `$name` $type $position", $fname ); |
| 232 | + |
| 233 | + $db->query( "ALTER TABLE $tableName CHANGE `$name` `$name` $type $position", $fname ); |
190 | 234 | $result[$name] = 'up'; |
191 | 235 | $curfields[$name] = false; |
| 236 | + |
192 | 237 | SMWSQLHelpers::reportProgress( "done.\n", $reportTo ); |
193 | 238 | } else { |
194 | 239 | SMWSQLHelpers::reportProgress( " ... column $name is fine\n", $reportTo ); |
| 240 | + |
195 | 241 | $curfields[$name] = false; |
196 | 242 | } |
197 | 243 | $position = "AFTER $name"; |
198 | 244 | } |
| 245 | + |
199 | 246 | foreach ( $curfields as $name => $value ) { |
200 | 247 | if ( $value !== false ) { // not encountered yet --> delete |
201 | 248 | SMWSQLHelpers::reportProgress( " ... deleting obsolete column $name ... ", $reportTo ); |
202 | | - $db->query( "ALTER TABLE $table DROP COLUMN `$name`", $fname ); |
| 249 | + $db->query( "ALTER TABLE $tableName DROP COLUMN `$name`", $fname ); |
203 | 250 | $result[$name] = 'del'; |
204 | 251 | SMWSQLHelpers::reportProgress( "done.\n", $reportTo ); |
205 | 252 | } |
206 | 253 | } |
207 | 254 | } |
208 | 255 | |
209 | | - SMWSQLHelpers::reportProgress( " ... table $table set up successfully.\n", $reportTo ); |
| 256 | + SMWSQLHelpers::reportProgress( " ... table $tableName set up successfully.\n", $reportTo ); |
| 257 | + |
210 | 258 | return $result; |
211 | 259 | } |
212 | 260 | } |
— | — | @@ -213,10 +261,15 @@ |
214 | 262 | /** |
215 | 263 | * Make sure that each of the column descriptions in the given array is indexed by *one* index |
216 | 264 | * in the given DB table. |
| 265 | + * |
| 266 | + * @param string $tableName The table name. Does not need to have been passed to DatabaseBase->tableName yet. |
| 267 | + * @param array $columns The field names to put indexes on |
| 268 | + * @param DatabaseBase $db |
217 | 269 | */ |
218 | | - public static function setupIndex( $table, $columns, $db, $reportTo = null ) { |
| 270 | + public static function setupIndex( $tableName, array $columns, DatabaseBase $db ) { |
219 | 271 | global $wgDBtype, $verbose; |
220 | | - $table = $db->tableName( $table ); |
| 272 | + |
| 273 | + $tableName = $db->tableName( $tableName ); |
221 | 274 | $fname = 'SMWSQLHelpers::setupIndex'; |
222 | 275 | |
223 | 276 | if ( $wgDBtype == 'postgres' ) { // postgresql |
— | — | @@ -229,53 +282,62 @@ |
230 | 283 | . " LEFT JOIN pg_namespace n ON n.oid = c.relnamespace" |
231 | 284 | . " LEFT JOIN pg_tablespace t ON t.oid = i.reltablespace" |
232 | 285 | . " WHERE c.relkind = 'r'::\"char\" AND i.relkind = 'i'::\"char\"" |
233 | | - . " AND c.relname = '" . $table . "'" |
| 286 | + . " AND c.relname = '" . $tableName . "'" |
234 | 287 | . " AND NOT pg_get_indexdef(i.oid) ~ '^CREATE UNIQUE INDEX'"; |
235 | 288 | $res = $db->query( $sql, $fname ); |
| 289 | + |
236 | 290 | if ( !$res ) { |
237 | 291 | return false; |
238 | 292 | } |
| 293 | + |
239 | 294 | $indexes = array(); |
| 295 | + |
240 | 296 | while ( $row = $db->fetchObject( $res ) ) { |
241 | | - // remove unneeded indexes, let indexes alone that already exist in the correct fashion |
| 297 | + // Remove the unneeded indexes, let indexes alone that already exist in the correct fashion. |
242 | 298 | if ( array_key_exists( $row->indexcolumns, $columns ) ) { |
243 | 299 | $columns[$row->indexcolumns] = false; |
244 | 300 | } else { |
245 | 301 | $db->query( 'DROP INDEX IF EXISTS ' . $row->indexname, $fname ); |
246 | 302 | } |
247 | 303 | } |
248 | | - foreach ( $columns as $key => $column ) { // add remaining indexes |
| 304 | + |
| 305 | + foreach ( $columns as $key => $column ) { // Ddd the remaining indexes. |
249 | 306 | if ( $column != false ) { |
250 | | - $db->query( "CREATE INDEX " . $table . "_index" . $key . " ON " . $table . " USING btree(" . $column . ")", $fname ); |
| 307 | + $db->query( "CREATE INDEX " . $tableName . "_index" . $key . " ON " . $tableName . " USING btree(" . $column . ")", $fname ); |
251 | 308 | } |
252 | 309 | } |
253 | | - } else { // mysql |
254 | | - $res = $db->query( 'SHOW INDEX FROM ' . $table , $fname ); |
| 310 | + } else { // MySQL |
| 311 | + $res = $db->query( 'SHOW INDEX FROM ' . $tableName , $fname ); |
| 312 | + |
255 | 313 | if ( !$res ) { |
256 | 314 | return false; |
257 | 315 | } |
| 316 | + |
258 | 317 | $indexes = array(); |
| 318 | + |
259 | 319 | while ( $row = $db->fetchObject( $res ) ) { |
260 | 320 | if ( !array_key_exists( $row->Key_name, $indexes ) ) { |
261 | 321 | $indexes[$row->Key_name] = array(); |
262 | 322 | } |
263 | 323 | $indexes[$row->Key_name][$row->Seq_in_index] = $row->Column_name; |
264 | 324 | } |
265 | | - foreach ( $indexes as $key => $index ) { // clean up existing indexes |
| 325 | + |
| 326 | + foreach ( $indexes as $key => $index ) { // Clean up the existing indexes. |
266 | 327 | $id = array_search( implode( ',', $index ), $columns ); |
267 | 328 | if ( $id !== false ) { |
268 | 329 | $columns[$id] = false; |
269 | | - } else { // duplicate or unrequired index |
270 | | - $db->query( 'DROP INDEX ' . $key . ' ON ' . $table, $fname ); |
| 330 | + } else { // Duplicate or unrequired index. |
| 331 | + $db->query( 'DROP INDEX ' . $key . ' ON ' . $tableName, $fname ); |
271 | 332 | } |
272 | 333 | } |
273 | 334 | |
274 | | - foreach ( $columns as $key => $column ) { // add remaining indexes |
| 335 | + foreach ( $columns as $key => $column ) { // Ddd the remaining indexes. |
275 | 336 | if ( $column != false ) { |
276 | | - $db->query( "ALTER TABLE $table ADD INDEX ( $column )", $fname ); |
| 337 | + $db->query( "ALTER TABLE $tableName ADD INDEX ( $column )", $fname ); |
277 | 338 | } |
278 | 339 | } |
279 | 340 | } |
| 341 | + |
280 | 342 | return true; |
281 | 343 | } |
282 | 344 | |