Changeset 12752 for trunk/wp-admin/includes/upgrade.php
- Timestamp:
- 01/18/2010 08:34:48 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/wp-admin/includes/upgrade.php
r12584 r12752 268 268 return; 269 269 270 if ( ! is_blog_installed() )270 if ( ! is_blog_installed() ) 271 271 return; 272 272 … … 632 632 // populate comment_count field of posts table 633 633 $comments = $wpdb->get_results( "SELECT comment_post_ID, COUNT(*) as c FROM $wpdb->comments WHERE comment_approved = '1' GROUP BY comment_post_ID" ); 634 if ( is_array( $comments ) )634 if ( is_array( $comments ) ) 635 635 foreach ($comments as $comment) 636 636 $wpdb->update( $wpdb->posts, array('comment_count' => $comment->c), array('ID' => $comment->comment_post_ID) ); … … 1200 1200 1201 1201 // Separate individual queries into an array 1202 if ( !is_array($queries) ) {1202 if ( !is_array($queries) ) { 1203 1203 $queries = explode( ';', $queries ); 1204 if ('' == $queries[count($queries) - 1]) array_pop($queries);1204 if ('' == $queries[count($queries) - 1]) array_pop($queries); 1205 1205 } 1206 1206 … … 1211 1211 // Create a tablename index for an array ($cqueries) of queries 1212 1212 foreach($queries as $qry) { 1213 if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) {1213 if (preg_match("|CREATE TABLE ([^ ]*)|", $qry, $matches)) { 1214 1214 $cqueries[trim( strtolower($matches[1]), '`' )] = $qry; 1215 1215 $for_update[$matches[1]] = 'Created table '.$matches[1]; 1216 } 1217 else if(preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { 1216 } else if (preg_match("|CREATE DATABASE ([^ ]*)|", $qry, $matches)) { 1218 1217 array_unshift($cqueries, $qry); 1219 } 1220 else if(preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { 1218 } else if (preg_match("|INSERT INTO ([^ ]*)|", $qry, $matches)) { 1221 1219 $iqueries[] = $qry; 1222 } 1223 else if(preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { 1220 } else if (preg_match("|UPDATE ([^ ]*)|", $qry, $matches)) { 1224 1221 $iqueries[] = $qry; 1225 } 1226 else { 1222 } else { 1227 1223 // Unrecognized query type 1228 1224 } … … 1230 1226 1231 1227 // Check to see which tables and fields exist 1232 if ($tables = $wpdb->get_col('SHOW TABLES;')) {1228 if ($tables = $wpdb->get_col('SHOW TABLES;')) { 1233 1229 // For every table in the database 1234 foreach ($tables as $table) {1230 foreach ($tables as $table) { 1235 1231 // If a table query exists for the database table... 1236 if ( array_key_exists(strtolower($table), $cqueries) ) {1232 if ( array_key_exists(strtolower($table), $cqueries) ) { 1237 1233 // Clear the field and index arrays 1238 1234 unset($cfields); … … 1248 1244 1249 1245 // For every field line specified in the query 1250 foreach ($flds as $fld) {1246 foreach ($flds as $fld) { 1251 1247 // Extract the field name 1252 1248 preg_match("|^([^ ]*)|", trim($fld), $fvals); … … 1255 1251 // Verify the found field name 1256 1252 $validfield = true; 1257 switch(strtolower($fieldname)) 1258 { 1253 switch (strtolower($fieldname)) { 1259 1254 case '': 1260 1255 case 'primary': … … 1270 1265 1271 1266 // If it's a valid field, add it to the field array 1272 if ($validfield) {1267 if ($validfield) { 1273 1268 $cfields[strtolower($fieldname)] = trim($fld, ", \n"); 1274 1269 } … … 1279 1274 1280 1275 // For every field in the table 1281 foreach ($tablefields as $tablefield) {1276 foreach ($tablefields as $tablefield) { 1282 1277 // If the table field exists in the field array... 1283 if (array_key_exists(strtolower($tablefield->Field), $cfields)) {1278 if (array_key_exists(strtolower($tablefield->Field), $cfields)) { 1284 1279 // Get the field type from the query 1285 1280 preg_match("|".$tablefield->Field." ([^ ]*( unsigned)?)|i", $cfields[strtolower($tablefield->Field)], $matches); … … 1287 1282 1288 1283 // Is actual field type different from the field type in query? 1289 if ($tablefield->Type != $fieldtype) {1284 if ($tablefield->Type != $fieldtype) { 1290 1285 // Add a query to change the column type 1291 1286 $cqueries[] = "ALTER TABLE {$table} CHANGE COLUMN {$tablefield->Field} " . $cfields[strtolower($tablefield->Field)]; … … 1295 1290 // Get the default value from the array 1296 1291 //echo "{$cfields[strtolower($tablefield->Field)]}<br>"; 1297 if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) {1292 if (preg_match("| DEFAULT '(.*)'|i", $cfields[strtolower($tablefield->Field)], $matches)) { 1298 1293 $default_value = $matches[1]; 1299 if($tablefield->Default != $default_value) 1300 { 1294 if ($tablefield->Default != $default_value) { 1301 1295 // Add a query to change the column's default value 1302 1296 $cqueries[] = "ALTER TABLE {$table} ALTER COLUMN {$tablefield->Field} SET DEFAULT '{$default_value}'"; … … 1307 1301 // Remove the field from the array (so it's not added) 1308 1302 unset($cfields[strtolower($tablefield->Field)]); 1309 } 1310 else { 1303 } else { 1311 1304 // This field exists in the table, but not in the creation queries? 1312 1305 } … … 1314 1307 1315 1308 // For every remaining field specified for the table 1316 foreach ($cfields as $fieldname => $fielddef) {1309 foreach ($cfields as $fieldname => $fielddef) { 1317 1310 // Push a query line into $cqueries that adds the field to that table 1318 1311 $cqueries[] = "ALTER TABLE {$table} ADD COLUMN $fielddef"; … … 1324 1317 $tableindices = $wpdb->get_results("SHOW INDEX FROM {$table};"); 1325 1318 1326 if ($tableindices) {1319 if ($tableindices) { 1327 1320 // Clear the index array 1328 1321 unset($index_ary); 1329 1322 1330 1323 // For every index in the table 1331 foreach ($tableindices as $tableindex) {1324 foreach ($tableindices as $tableindex) { 1332 1325 // Add the index to the index data array 1333 1326 $keyname = $tableindex->Key_name; … … 1337 1330 1338 1331 // For each actual index in the index array 1339 foreach ($index_ary as $index_name => $index_data) {1332 foreach ($index_ary as $index_name => $index_data) { 1340 1333 // Build a create string to compare to the query 1341 1334 $index_string = ''; 1342 if ($index_name == 'PRIMARY') {1335 if ($index_name == 'PRIMARY') { 1343 1336 $index_string .= 'PRIMARY '; 1344 } 1345 else if($index_data['unique']) { 1337 } else if($index_data['unique']) { 1346 1338 $index_string .= 'UNIQUE '; 1347 1339 } 1348 1340 $index_string .= 'KEY '; 1349 if ($index_name != 'PRIMARY') {1341 if ($index_name != 'PRIMARY') { 1350 1342 $index_string .= $index_name; 1351 1343 } 1352 1344 $index_columns = ''; 1353 1345 // For each column in the index 1354 foreach ($index_data['columns'] as $column_data) {1355 if ($index_columns != '') $index_columns .= ',';1346 foreach ($index_data['columns'] as $column_data) { 1347 if ($index_columns != '') $index_columns .= ','; 1356 1348 // Add the field to the column list string 1357 1349 $index_columns .= $column_data['fieldname']; 1358 if ($column_data['subpart'] != '') {1350 if ($column_data['subpart'] != '') { 1359 1351 $index_columns .= '('.$column_data['subpart'].')'; 1360 1352 } … … 1362 1354 // Add the column list to the index create string 1363 1355 $index_string .= ' ('.$index_columns.')'; 1364 if (!(($aindex = array_search($index_string, $indices)) === false)) {1356 if (!(($aindex = array_search($index_string, $indices)) === false)) { 1365 1357 unset($indices[$aindex]); 1366 1358 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">{$table}:<br />Found index:".$index_string."</pre>\n"; … … 1387 1379 1388 1380 $allqueries = array_merge($cqueries, $iqueries); 1389 if ($execute) {1390 foreach ($allqueries as $query) {1381 if ($execute) { 1382 foreach ($allqueries as $query) { 1391 1383 //echo "<pre style=\"border:1px solid #ccc;margin-top:5px;\">".print_r($query, true)."</pre>\n"; 1392 1384 $wpdb->query($query);
Note: See TracChangeset
for help on using the changeset viewer.