00001 <?php
00007 if ( !defined( 'MEDIAWIKI' ) ) {
00008 echo "This file is not a valid entry point\n";
00009 exit( 1 );
00010 }
00011
00012 require_once 'convertLinks.inc';
00013 require_once 'userDupes.inc';
00014 require_once 'deleteDefaultMessages.php';
00015 # Extension updates
00016 require_once( "$IP/includes/Hooks.php" );
00017
00022 $wgUpdates = array(
00023 'mysql' => array(
00024
00025
00026 array( 'add_field', 'ipblocks', 'ipb_id', 'patch-ipblocks.sql' ),
00027 array( 'add_field', 'ipblocks', 'ipb_expiry', 'patch-ipb_expiry.sql' ),
00028 array( 'do_interwiki_update' ),
00029 array( 'do_index_update' ),
00030
00031 array( 'add_table', 'hitcounter', 'patch-hitcounter.sql' ),
00032 array( 'add_field', 'recentchanges', 'rc_type', 'patch-rc_type.sql' ),
00033
00034
00035 array( 'add_field', 'user', 'user_real_name', 'patch-user-realname.sql' ),
00036 array( 'add_table', 'querycache', 'patch-querycache.sql' ),
00037 array( 'add_table', 'objectcache', 'patch-objectcache.sql' ),
00038 array( 'add_table', 'categorylinks', 'patch-categorylinks.sql' ),
00039
00040 array( 'do_old_links_update' ),
00041 array( 'fix_ancient_imagelinks' ),
00042 array( 'add_field', 'recentchanges', 'rc_ip', 'patch-rc_ip.sql' ),
00043
00044
00045 array( 'do_image_name_unique_update' ),
00046 array( 'add_field', 'recentchanges', 'rc_id', 'patch-rc_id.sql' ),
00047 array( 'add_field', 'recentchanges', 'rc_patrolled', 'patch-rc-patrol.sql' ),
00048 array( 'add_table', 'logging', 'patch-logging.sql' ),
00049
00050 array( 'add_field', 'user', 'user_token', 'patch-user_token.sql' ),
00051
00052
00053
00054 array( 'do_watchlist_update' ),
00055 array( 'do_user_update' ),
00056
00057
00058
00059 array( 'do_schema_restructuring' ),
00060 array( 'add_field', 'logging', 'log_params', 'patch-log_params.sql' ),
00061 array( 'check_bin', 'logging', 'log_title', 'patch-logging-title.sql', ),
00062 array( 'add_field', 'archive', 'ar_rev_id', 'patch-archive-rev_id.sql' ),
00063 array( 'add_field', 'page', 'page_len', 'patch-page_len.sql' ),
00064 array( 'do_inverse_timestamp' ),
00065 array( 'do_text_id' ),
00066 array( 'add_field', 'revision', 'rev_deleted', 'patch-rev_deleted.sql' ),
00067 array( 'add_field', 'image', 'img_width', 'patch-img_width.sql' ),
00068 array( 'add_field', 'image', 'img_metadata', 'patch-img_metadata.sql' ),
00069 array( 'add_field', 'user', 'user_email_token', 'patch-user_email_token.sql' ),
00070 array( 'add_field', 'archive', 'ar_text_id', 'patch-archive-text_id.sql' ),
00071 array( 'do_namespace_size' ),
00072 array( 'add_field', 'image', 'img_media_type', 'patch-img_media_type.sql' ),
00073 array( 'do_pagelinks_update' ),
00074 array( 'do_drop_img_type' ),
00075 array( 'do_user_unique_update' ),
00076 array( 'do_user_groups_update' ),
00077 array( 'add_field', 'site_stats', 'ss_total_pages', 'patch-ss_total_articles.sql' ),
00078 array( 'add_table', 'user_newtalk', 'patch-usernewtalk2.sql' ),
00079 array( 'add_table', 'transcache', 'patch-transcache.sql' ),
00080 array( 'add_field', 'interwiki', 'iw_trans', 'patch-interwiki-trans.sql' ),
00081 array( 'add_table', 'trackbacks', 'patch-trackbacks.sql' ),
00082
00083
00084 array( 'do_watchlist_null' ),
00085
00086 array( 'do_logging_timestamp_index' ),
00087 array( 'add_field', 'ipblocks', 'ipb_range_start', 'patch-ipb_range_start.sql' ),
00088 array( 'do_page_random_update' ),
00089 array( 'add_field', 'user', 'user_registration','patch-user_registration.sql' ),
00090 array( 'do_templatelinks_update' ),
00091 array( 'add_table', 'externallinks', 'patch-externallinks.sql' ),
00092 array( 'add_table', 'job', 'patch-job.sql' ),
00093 array( 'add_field', 'site_stats', 'ss_images', 'patch-ss_images.sql' ),
00094 array( 'add_table', 'langlinks', 'patch-langlinks.sql' ),
00095 array( 'add_table', 'querycache_info', 'patch-querycacheinfo.sql' ),
00096 array( 'add_table', 'filearchive', 'patch-filearchive.sql' ),
00097 array( 'add_field', 'ipblocks', 'ipb_anon_only', 'patch-ipb_anon_only.sql' ),
00098 array( 'do_rc_indices_update' ),
00099
00100
00101 array( 'add_field', 'user', 'user_newpass_time', 'patch-user_newpass_time.sql' ),
00102 array( 'add_table', 'redirect', 'patch-redirect.sql' ),
00103 array( 'add_table', 'querycachetwo', 'patch-querycachetwo.sql' ),
00104 array( 'add_field', 'ipblocks', 'ipb_enable_autoblock', 'patch-ipb_optional_autoblock.sql' ),
00105 array( 'do_backlinking_indices_update' ),
00106 array( 'add_field', 'recentchanges', 'rc_old_len', 'patch-rc_len.sql' ),
00107 array( 'add_field', 'user', 'user_editcount', 'patch-user_editcount.sql' ),
00108
00109
00110 array( 'do_restrictions_update' ),
00111 array( 'add_field', 'logging', 'log_id', 'patch-log_id.sql' ),
00112 array( 'add_field', 'revision', 'rev_parent_id', 'patch-rev_parent_id.sql' ),
00113 array( 'add_field', 'page_restrictions', 'pr_id', 'patch-page_restrictions_sortkey.sql' ),
00114 array( 'add_field', 'revision', 'rev_len', 'patch-rev_len.sql' ),
00115 array( 'add_field', 'recentchanges', 'rc_deleted', 'patch-rc_deleted.sql' ),
00116 array( 'add_field', 'logging', 'log_deleted', 'patch-log_deleted.sql' ),
00117 array( 'add_field', 'archive', 'ar_deleted', 'patch-ar_deleted.sql' ),
00118 array( 'add_field', 'ipblocks', 'ipb_deleted', 'patch-ipb_deleted.sql' ),
00119 array( 'add_field', 'filearchive', 'fa_deleted', 'patch-fa_deleted.sql' ),
00120 array( 'add_field', 'archive', 'ar_len', 'patch-ar_len.sql' ),
00121
00122
00123 array( 'add_field', 'ipblocks', 'ipb_block_email', 'patch-ipb_emailban.sql' ),
00124 array( 'do_categorylinks_indices_update' ),
00125 array( 'add_field', 'oldimage', 'oi_metadata', 'patch-oi_metadata.sql'),
00126 array( 'do_archive_user_index' ),
00127 array( 'do_image_user_index' ),
00128 array( 'do_oldimage_user_index' ),
00129 array( 'add_field', 'archive', 'ar_page_id', 'patch-archive-page_id.sql'),
00130 array( 'add_field', 'image', 'img_sha1', 'patch-img_sha1.sql' ),
00131
00132
00133 array( 'add_table', 'protected_titles', 'patch-protected_titles.sql' ),
00134
00135
00136 array( 'add_field', 'ipblocks', 'ipb_by_text', 'patch-ipb_by_text.sql' ),
00137 array( 'add_table', 'page_props', 'patch-page_props.sql' ),
00138 array( 'add_table', 'updatelog', 'patch-updatelog.sql' ),
00139 array( 'add_table', 'category', 'patch-category.sql' ),
00140 array( 'do_category_population' ),
00141 array( 'add_field', 'archive', 'ar_parent_id', 'patch-ar_parent_id.sql'),
00142 array( 'add_field', 'user_newtalk', 'user_last_timestamp', 'patch-user_last_timestamp.sql'),
00143 array( 'do_populate_parent_id' ),
00144 array( 'check_bin', 'protected_titles', 'pt_title', 'patch-pt_title-encoding.sql', ),
00145 array( 'maybe_do_profiling_memory_update' ),
00146 array( 'do_filearchive_indices_update' ),
00147
00148
00149 array( 'add_field', 'site_stats', 'ss_active_users', 'patch-ss_active_users.sql' ),
00150 array( 'do_active_users_init' ),
00151 array( 'add_field', 'ipblocks', 'ipb_allow_usertalk', 'patch-ipb_allow_usertalk.sql' ),
00152 array( 'do_unique_pl_tl_il' ),
00153
00154
00155 array( 'add_table', 'change_tag', 'patch-change_tag.sql' ),
00156 array( 'add_table', 'tag_summary', 'patch-change_tag.sql' ),
00157 array( 'add_table', 'valid_tag', 'patch-change_tag.sql' ),
00158 ),
00159
00160 'sqlite' => array(
00161
00162 array( 'add_field', 'site_stats', 'ss_active_users', 'patch-ss_active_users.sql' ),
00163 array( 'do_active_users_init' ),
00164 array( 'add_field', 'ipblocks', 'ipb_allow_usertalk', 'patch-ipb_allow_usertalk.sql' ),
00165 array( 'sqlite_initial_indexes' ),
00166
00167
00168 array( 'add_table', 'change_tag', 'patch-change_tag.sql' ),
00169 array( 'add_table', 'tag_summary', 'patch-change_tag.sql' ),
00170 array( 'add_table', 'valid_tag', 'patch-change_tag.sql' ),
00171 ),
00172 );
00173
00174
00175 # For extensions only, should be populated via hooks
00176 # $wgDBtype should be checked to specifiy the proper file
00177 $wgExtNewTables = array();
00178 $wgExtNewFields = array();
00179 $wgExtPGNewFields = array();
00180 $wgExtNewIndexes = array();
00181
00182 # Helper function: check if the given key is present in the updatelog table.
00183 # Obviously, only use this for updates that occur after the updatelog table was
00184 # created!
00185 function update_row_exists( $key ) {
00186 $dbr = wfGetDB( DB_SLAVE );
00187 $row = $dbr->selectRow(
00188 'updatelog',
00189 '1',
00190 array( 'ul_key' => $key ),
00191 __FUNCTION__
00192 );
00193 return (bool)$row;
00194 }
00195
00196 function rename_table( $from, $to, $patch ) {
00197 global $wgDatabase;
00198 if ( $wgDatabase->tableExists( $from ) ) {
00199 if ( $wgDatabase->tableExists( $to ) ) {
00200 wfOut( "...can't move table $from to $to, $to already exists.\n" );
00201 } else {
00202 wfOut( "Moving table $from to $to..." );
00203 dbsource( archive($patch), $wgDatabase );
00204 wfOut( "ok\n" );
00205 }
00206 } else {
00207
00208
00209
00210 }
00211 }
00212
00213 function add_table( $name, $patch, $fullpath=false ) {
00214 global $wgDatabase;
00215 if ( $wgDatabase->tableExists( $name ) ) {
00216 wfOut( "...$name table already exists.\n" );
00217 } else {
00218 wfOut( "Creating $name table..." );
00219 if( $fullpath ) {
00220 dbsource( $patch, $wgDatabase );
00221 } else {
00222 dbsource( archive($patch), $wgDatabase );
00223 }
00224 wfOut( "ok\n" );
00225 }
00226 }
00227
00228 function add_field( $table, $field, $patch, $fullpath=false ) {
00229 global $wgDatabase;
00230 if ( !$wgDatabase->tableExists( $table ) ) {
00231 wfOut( "...$table table does not exist, skipping new field patch\n" );
00232 } elseif ( $wgDatabase->fieldExists( $table, $field ) ) {
00233 wfOut( "...have $field field in $table table.\n" );
00234 } else {
00235 wfOut( "Adding $field field to table $table..." );
00236 if( $fullpath ) {
00237 dbsource( $patch, $wgDatabase );
00238 } else {
00239 dbsource( archive($patch), $wgDatabase );
00240 }
00241 wfOut( "ok\n" );
00242 }
00243 }
00244
00245 function add_index( $table, $index, $patch, $fullpath=false ) {
00246 global $wgDatabase;
00247 if( $wgDatabase->indexExists( $table, $index ) ) {
00248 wfOut( "...$index key already set on $table table.\n" );
00249 } else {
00250 wfOut( "Adding $index key to table $table... " );
00251 if( $fullpath ) {
00252 dbsource( $patch, $wgDatabase );
00253 } else {
00254 dbsource( archive($patch), $wgDatabase );
00255 }
00256 wfOut( "ok\n" );
00257 }
00258 }
00259
00260 function do_interwiki_update() {
00261 # Check that interwiki table exists; if it doesn't source it
00262 global $wgDatabase, $IP;
00263 if( $wgDatabase->tableExists( "interwiki" ) ) {
00264 wfOut( "...already have interwiki table\n" );
00265 return true;
00266 }
00267 wfOut( "Creating interwiki table: " );
00268 dbsource( archive("patch-interwiki.sql") );
00269 wfOut( "ok\n" );
00270 wfOut( "Adding default interwiki definitions: " );
00271 dbsource( "$IP/maintenance/interwiki.sql" );
00272 wfOut( "ok\n" );
00273 }
00274
00275 function do_index_update() {
00276 # Check that proper indexes are in place
00277 global $wgDatabase;
00278 $meta = $wgDatabase->fieldInfo( "recentchanges", "rc_timestamp" );
00279 if( !$meta->isMultipleKey() ) {
00280 wfOut( "Updating indexes to 20031107: " );
00281 dbsource( archive("patch-indexes.sql") );
00282 wfOut( "ok\n" );
00283 return true;
00284 }
00285 wfOut( "...indexes seem up to 20031107 standards\n" );
00286 return false;
00287 }
00288
00289 function do_image_index_update() {
00290 global $wgDatabase;
00291
00292 $meta = $wgDatabase->fieldInfo( "image", "img_major_mime" );
00293 if( !$meta->isMultipleKey() ) {
00294 wfOut( "Updating indexes to 20050912: " );
00295 dbsource( archive("patch-mimesearch-indexes.sql") );
00296 wfOut( "ok\n" );
00297 return true;
00298 }
00299 wfOut( "...indexes seem up to 20050912 standards\n" );
00300 return false;
00301 }
00302
00303 function do_image_name_unique_update() {
00304 global $wgDatabase;
00305 if( $wgDatabase->indexExists( 'image', 'PRIMARY' ) ) {
00306 wfOut( "...image primary key already set.\n" );
00307 } else {
00308 wfOut( "Making img_name the primary key... " );
00309 dbsource( archive("patch-image_name_primary.sql"), $wgDatabase );
00310 wfOut( "ok\n" );
00311 }
00312 }
00313
00314 function do_logging_timestamp_index() {
00315 global $wgDatabase;
00316 if( $wgDatabase->indexExists( 'logging', 'times' ) ) {
00317 wfOut( "...timestamp key on logging already exists.\n" );
00318 } else {
00319 wfOut( "Adding timestamp key on logging table... " );
00320 dbsource( archive("patch-logging-times-index.sql"), $wgDatabase );
00321 wfOut( "ok\n" );
00322 }
00323 }
00324
00325 function do_archive_user_index() {
00326 global $wgDatabase;
00327 if( $wgDatabase->indexExists( 'archive', 'usertext_timestamp' ) ) {
00328 wfOut( "...usertext,timestamp key on archive already exists.\n" );
00329 } else {
00330 wfOut( "Adding usertext,timestamp key on archive table... " );
00331 dbsource( archive("patch-archive-user-index.sql"), $wgDatabase );
00332 wfOut( "ok\n" );
00333 }
00334 }
00335
00336 function do_image_user_index() {
00337 global $wgDatabase;
00338 if( $wgDatabase->indexExists( 'image', 'img_usertext_timestamp' ) ) {
00339 wfOut( "...usertext,timestamp key on image already exists.\n" );
00340 } else {
00341 wfOut( "Adding usertext,timestamp key on image table... " );
00342 dbsource( archive("patch-image-user-index.sql"), $wgDatabase );
00343 wfOut( "ok\n" );
00344 }
00345 }
00346
00347 function do_oldimage_user_index() {
00348 global $wgDatabase;
00349 if( $wgDatabase->indexExists( 'oldimage', 'oi_usertext_timestamp' ) ) {
00350 wfOut( "...usertext,timestamp key on oldimage already exists.\n" );
00351 } else {
00352 wfOut( "Adding usertext,timestamp key on oldimage table... " );
00353 dbsource( archive("patch-oldimage-user-index.sql"), $wgDatabase );
00354 wfOut( "ok\n" );
00355 }
00356 }
00357
00358 function do_watchlist_update() {
00359 global $wgDatabase;
00360 $fname = 'do_watchlist_update';
00361 if( $wgDatabase->fieldExists( 'watchlist', 'wl_notificationtimestamp' ) ) {
00362 wfOut( "The watchlist table is already set up for email notification.\n" );
00363 } else {
00364 wfOut( "Adding wl_notificationtimestamp field for email notification management." );
00365
00366 dbsource( archive( 'patch-email-notification.sql' ), $wgDatabase );
00367 wfOut( "ok\n" );
00368 }
00369 # Check if we need to add talk page rows to the watchlist
00370 $talk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'wl_namespace & 1', $fname );
00371 $nontalk = $wgDatabase->selectField( 'watchlist', 'count(*)', 'NOT (wl_namespace & 1)', $fname );
00372 if ( $talk != $nontalk ) {
00373 wfOut( "Adding missing watchlist talk page rows... " );
00374 flush();
00375
00376 $wgDatabase->insertSelect( 'watchlist', 'watchlist',
00377 array(
00378 'wl_user' => 'wl_user',
00379 'wl_namespace' => 'wl_namespace | 1',
00380 'wl_title' => 'wl_title',
00381 'wl_notificationtimestamp' => 'wl_notificationtimestamp'
00382 ), array( 'NOT (wl_namespace & 1)' ), $fname, 'IGNORE' );
00383 wfOut( "ok\n" );
00384 } else {
00385 wfOut( "...watchlist talk page rows already present\n" );
00386 }
00387 }
00388
00389 function do_copy_newtalk_to_watchlist() {
00390 global $wgDatabase;
00391 global $wgCommandLineMode; # this needs to be saved while getID() and getName() are called
00392
00393 $res = $wgDatabase->safeQuery( 'SELECT user_id, user_ip FROM !',
00394 $wgDatabase->tableName( 'user_newtalk' ) );
00395 $num_newtalks=$wgDatabase->numRows($res);
00396 wfOut( "Now converting $num_newtalks user_newtalk entries to watchlist table entries ... \n" );
00397
00398 $user = new User();
00399 for ( $i = 1; $i <= $num_newtalks; $i++ ) {
00400 $wluser = $wgDatabase->fetchObject( $res );
00401 if ($wluser->user_id == 0) { # anonymous users ... have IP numbers as "names"
00402 if ($user->isIP($wluser->user_ip)) { # do only if it really looks like an IP number (double checked)
00403 $wgDatabase->replace( 'watchlist',
00404 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
00405 array('wl_user' => 0,
00406 'wl_namespace' => NS_USER_TALK,
00407 'wl_title' => $wluser->user_ip,
00408 'wl_notificationtimestamp' => '19700101000000'
00409 ), 'updaters.inc::do_watchlist_update2'
00410 );
00411 }
00412 } else { # normal users ... have user_ids
00413 $user->setID($wluser->user_id);
00414 $wgDatabase->replace( 'watchlist',
00415 array(array('wl_user','wl_namespace', 'wl_title', 'wl_notificationtimestamp' )),
00416 array('wl_user' => $user->getID(),
00417 'wl_namespace' => NS_USER_TALK,
00418 'wl_title' => $user->getName(),
00419 'wl_notificationtimestamp' => '19700101000000'
00420 ), 'updaters.inc::do_watchlist_update3'
00421 );
00422 }
00423 }
00424 wfOut( "Done.\n" );
00425 }
00426
00427
00428 function do_user_update() {
00429 global $wgDatabase;
00430 if( $wgDatabase->fieldExists( 'user', 'user_emailauthenticationtimestamp' ) ) {
00431 wfOut( "User table contains old email authentication field. Dropping... " );
00432 dbsource( archive( 'patch-email-authentication.sql' ), $wgDatabase );
00433 wfOut( "ok\n" );
00434 } else {
00435 wfOut( "...user table does not contain old email authentication field.\n" );
00436 }
00437 }
00438
00443 function check_bin( $table, $field, $patchFile ) {
00444 global $wgDatabase, $wgDBtype;
00445 if ($wgDBtype != 'mysql')
00446 return;
00447 $tableName = $wgDatabase->tableName( $table );
00448 $res = $wgDatabase->query( "SELECT $field FROM $tableName LIMIT 0" );
00449 $flags = explode( ' ', mysql_field_flags( $res->result, 0 ) );
00450 $wgDatabase->freeResult( $res );
00451
00452 if( in_array( 'binary', $flags ) ) {
00453 wfOut( "$table table has correct $field encoding.\n" );
00454 } else {
00455 wfOut( "Fixing $field encoding on $table table... " );
00456 dbsource( archive( $patchFile ), $wgDatabase );
00457 wfOut( "ok\n" );
00458 }
00459 }
00460
00461 function do_schema_restructuring() {
00462 global $wgDatabase;
00463 $fname="do_schema_restructuring";
00464 if ( $wgDatabase->tableExists( 'page' ) ) {
00465 wfOut( "...page table already exists.\n" );
00466 } else {
00467 wfOut( "...converting from cur/old to page/revision/text DB structure.\n" );
00468 wfOut( wfTimestamp( TS_DB ) );
00469 wfOut( "......checking for duplicate entries.\n" );
00470
00471 list ($cur, $old, $page, $revision, $text) = $wgDatabase->tableNamesN( 'cur', 'old', 'page', 'revision', 'text' );
00472
00473 $rows = $wgDatabase->query( "SELECT cur_title, cur_namespace, COUNT(cur_namespace) AS c
00474 FROM $cur GROUP BY cur_title, cur_namespace HAVING c>1", $fname );
00475
00476 if ( $wgDatabase->numRows( $rows ) > 0 ) {
00477 wfOut( wfTimestamp( TS_DB ) );
00478 wfOut( "......<b>Found duplicate entries</b>\n" );
00479 wfOut( sprintf( "<b> %-60s %3s %5s</b>\n", 'Title', 'NS', 'Count' ) );
00480 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
00481 if ( ! isset( $duplicate[$row->cur_namespace] ) ) {
00482 $duplicate[$row->cur_namespace] = array();
00483 }
00484 $duplicate[$row->cur_namespace][] = $row->cur_title;
00485 wfOut( sprintf( " %-60s %3s %5s\n", $row->cur_title, $row->cur_namespace, $row->c ) );
00486 }
00487 $sql = "SELECT cur_title, cur_namespace, cur_id, cur_timestamp FROM $cur WHERE ";
00488 $firstCond = true;
00489 foreach ( $duplicate as $ns => $titles ) {
00490 if ( $firstCond ) {
00491 $firstCond = false;
00492 } else {
00493 $sql .= ' OR ';
00494 }
00495 $sql .= "( cur_namespace = {$ns} AND cur_title in (";
00496 $first = true;
00497 foreach ( $titles as $t ) {
00498 if ( $first ) {
00499 $sql .= $wgDatabase->addQuotes( $t );
00500 $first = false;
00501 } else {
00502 $sql .= ', ' . $wgDatabase->addQuotes( $t );
00503 }
00504 }
00505 $sql .= ") ) \n";
00506 }
00507 # By sorting descending, the most recent entry will be the first in the list.
00508 # All following entries will be deleted by the next while-loop.
00509 $sql .= 'ORDER BY cur_namespace, cur_title, cur_timestamp DESC';
00510
00511 $rows = $wgDatabase->query( $sql, $fname );
00512
00513 $prev_title = $prev_namespace = false;
00514 $deleteId = array();
00515
00516 while ( $row = $wgDatabase->fetchObject( $rows ) ) {
00517 if ( $prev_title == $row->cur_title && $prev_namespace == $row->cur_namespace ) {
00518 $deleteId[] = $row->cur_id;
00519 }
00520 $prev_title = $row->cur_title;
00521 $prev_namespace = $row->cur_namespace;
00522 }
00523 $sql = "DELETE FROM $cur WHERE cur_id IN ( " . join( ',', $deleteId ) . ')';
00524 $rows = $wgDatabase->query( $sql, $fname );
00525 wfOut( wfTimestamp( TS_DB ) );
00526 wfOut( "......<b>Deleted</b> ".$wgDatabase->affectedRows()." records.\n" );
00527 }
00528
00529
00530 wfOut( wfTimestamp( TS_DB ) );
00531 wfOut( "......Creating tables.\n" );
00532 $wgDatabase->query("CREATE TABLE $page (
00533 page_id int(8) unsigned NOT NULL auto_increment,
00534 page_namespace int NOT NULL,
00535 page_title varchar(255) binary NOT NULL,
00536 page_restrictions tinyblob NOT NULL,
00537 page_counter bigint(20) unsigned NOT NULL default '0',
00538 page_is_redirect tinyint(1) unsigned NOT NULL default '0',
00539 page_is_new tinyint(1) unsigned NOT NULL default '0',
00540 page_random real unsigned NOT NULL,
00541 page_touched char(14) binary NOT NULL default '',
00542 page_latest int(8) unsigned NOT NULL,
00543 page_len int(8) unsigned NOT NULL,
00544
00545 PRIMARY KEY page_id (page_id),
00546 UNIQUE INDEX name_title (page_namespace,page_title),
00547 INDEX (page_random),
00548 INDEX (page_len)
00549 ) ENGINE=InnoDB", $fname );
00550 $wgDatabase->query("CREATE TABLE $revision (
00551 rev_id int(8) unsigned NOT NULL auto_increment,
00552 rev_page int(8) unsigned NOT NULL,
00553 rev_comment tinyblob NOT NULL,
00554 rev_user int(5) unsigned NOT NULL default '0',
00555 rev_user_text varchar(255) binary NOT NULL default '',
00556 rev_timestamp char(14) binary NOT NULL default '',
00557 rev_minor_edit tinyint(1) unsigned NOT NULL default '0',
00558 rev_deleted tinyint(1) unsigned NOT NULL default '0',
00559 rev_len int(8) unsigned,
00560 rev_parent_id int(8) unsigned default NULL,
00561 PRIMARY KEY rev_page_id (rev_page, rev_id),
00562 UNIQUE INDEX rev_id (rev_id),
00563 INDEX rev_timestamp (rev_timestamp),
00564 INDEX page_timestamp (rev_page,rev_timestamp),
00565 INDEX user_timestamp (rev_user,rev_timestamp),
00566 INDEX usertext_timestamp (rev_user_text,rev_timestamp)
00567 ) ENGINE=InnoDB", $fname );
00568
00569 wfOut( wfTimestamp( TS_DB ) );
00570 wfOut( "......Locking tables.\n" );
00571 $wgDatabase->query( "LOCK TABLES $page WRITE, $revision WRITE, $old WRITE, $cur WRITE", $fname );
00572
00573 $maxold = intval( $wgDatabase->selectField( 'old', 'max(old_id)', '', $fname ) );
00574 wfOut( wfTimestamp( TS_DB ) );
00575 wfOut( "......maxold is {$maxold}\n" );
00576
00577 wfOut( wfTimestamp( TS_DB ) );
00578 global $wgLegacySchemaConversion;
00579 if( $wgLegacySchemaConversion ) {
00580
00581
00582 wfOut( "......Moving metadata from cur; using blob references to text in cur table.\n" );
00583 $cur_text = "concat('O:18:\"historyblobcurstub\":1:{s:6:\"mCurId\";i:',cur_id,';}')";
00584 $cur_flags = "'object'";
00585 } else {
00586
00587
00588 wfOut( "......Moving text from cur.\n" );
00589 $cur_text = 'cur_text';
00590 $cur_flags = "''";
00591 }
00592 $wgDatabase->query( "INSERT INTO $old (old_namespace, old_title, old_text, old_comment, old_user, old_user_text,
00593 old_timestamp, old_minor_edit, old_flags)
00594 SELECT cur_namespace, cur_title, $cur_text, cur_comment, cur_user, cur_user_text, cur_timestamp, cur_minor_edit, $cur_flags
00595 FROM $cur", $fname );
00596
00597 wfOut( wfTimestamp( TS_DB ) );
00598 wfOut( "......Setting up revision table.\n" );
00599 $wgDatabase->query( "INSERT INTO $revision (rev_id, rev_page, rev_comment, rev_user, rev_user_text, rev_timestamp,
00600 rev_minor_edit)
00601 SELECT old_id, cur_id, old_comment, old_user, old_user_text,
00602 old_timestamp, old_minor_edit
00603 FROM $old,$cur WHERE old_namespace=cur_namespace AND old_title=cur_title", $fname );
00604
00605 wfOut( wfTimestamp( TS_DB ) );
00606 wfOut( "......Setting up page table.\n" );
00607 $wgDatabase->query( "INSERT INTO $page (page_id, page_namespace, page_title, page_restrictions, page_counter,
00608 page_is_redirect, page_is_new, page_random, page_touched, page_latest, page_len)
00609 SELECT cur_id, cur_namespace, cur_title, cur_restrictions, cur_counter, cur_is_redirect, cur_is_new,
00610 cur_random, cur_touched, rev_id, LENGTH(cur_text)
00611 FROM $cur,$revision
00612 WHERE cur_id=rev_page AND rev_timestamp=cur_timestamp AND rev_id > {$maxold}", $fname );
00613
00614 wfOut( wfTimestamp( TS_DB ) );
00615 wfOut( "......Unlocking tables.\n" );
00616 $wgDatabase->query( "UNLOCK TABLES", $fname );
00617
00618 wfOut( wfTimestamp( TS_DB ) );
00619 wfOut( "......Renaming old.\n" );
00620 $wgDatabase->query( "ALTER TABLE $old RENAME TO $text", $fname );
00621
00622 wfOut( wfTimestamp( TS_DB ) );
00623 wfOut( "...done.\n" );
00624 }
00625 }
00626
00627 function do_inverse_timestamp() {
00628 global $wgDatabase;
00629 if( $wgDatabase->fieldExists( 'revision', 'inverse_timestamp' ) ) {
00630 wfOut( "Removing revision.inverse_timestamp and fixing indexes... " );
00631 dbsource( archive( 'patch-inverse_timestamp.sql' ), $wgDatabase );
00632 wfOut( "ok\n" );
00633 } else {
00634 wfOut( "revision timestamp indexes already up to 2005-03-13\n" );
00635 }
00636 }
00637
00638 function do_text_id() {
00639 global $wgDatabase;
00640 if( $wgDatabase->fieldExists( 'revision', 'rev_text_id' ) ) {
00641 wfOut( "...rev_text_id already in place.\n" );
00642 } else {
00643 wfOut( "Adding rev_text_id field... " );
00644 dbsource( archive( 'patch-rev_text_id.sql' ), $wgDatabase );
00645 wfOut( "ok\n" );
00646 }
00647 }
00648
00649 function do_namespace_size() {
00650 $tables = array(
00651 'page' => 'page',
00652 'archive' => 'ar',
00653 'recentchanges' => 'rc',
00654 'watchlist' => 'wl',
00655 'querycache' => 'qc',
00656 'logging' => 'log',
00657 );
00658 foreach( $tables as $table => $prefix ) {
00659 do_namespace_size_on( $table, $prefix );
00660 flush();
00661 }
00662 }
00663
00664 function do_namespace_size_on( $table, $prefix ) {
00665 global $wgDatabase, $wgDBtype;
00666 if ($wgDBtype != 'mysql')
00667 return;
00668 $field = $prefix . '_namespace';
00669
00670 $tablename = $wgDatabase->tableName( $table );
00671 $result = $wgDatabase->query( "SHOW COLUMNS FROM $tablename LIKE '$field'" );
00672 $info = $wgDatabase->fetchObject( $result );
00673 $wgDatabase->freeResult( $result );
00674
00675 if( substr( $info->Type, 0, 3 ) == 'int' ) {
00676 wfOut( "...$field is already a full int ($info->Type).\n" );
00677 } else {
00678 wfOut( "Promoting $field from $info->Type to int... " );
00679
00680 $sql = "ALTER TABLE $tablename MODIFY $field int NOT NULL";
00681 $wgDatabase->query( $sql );
00682
00683 wfOut( "ok\n" );
00684 }
00685 }
00686
00687 function do_pagelinks_update() {
00688 global $wgDatabase;
00689 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
00690 wfOut( "...already have pagelinks table.\n" );
00691 } else {
00692 wfOut( "Converting links and brokenlinks tables to pagelinks... " );
00693 dbsource( archive( 'patch-pagelinks.sql' ), $wgDatabase );
00694 wfOut( "ok\n" );
00695 flush();
00696
00697 global $wgCanonicalNamespaceNames;
00698 foreach( $wgCanonicalNamespaceNames as $ns => $name ) {
00699 if( $ns != 0 ) {
00700 do_pagelinks_namespace( $ns );
00701 }
00702 }
00703 }
00704 }
00705
00706 function do_pagelinks_namespace( $namespace ) {
00707 global $wgDatabase, $wgContLang;
00708
00709 $ns = intval( $namespace );
00710 wfOut( "Cleaning up broken links for namespace $ns... " );
00711
00712 $pagelinks = $wgDatabase->tableName( 'pagelinks' );
00713 $name = $wgContLang->getNsText( $ns );
00714 $prefix = $wgDatabase->strencode( $name );
00715 $likeprefix = str_replace( '_', '\\_', $prefix);
00716
00717 $sql = "UPDATE $pagelinks
00718 SET pl_namespace=$ns,
00719 pl_title=TRIM(LEADING '$prefix:' FROM pl_title)
00720 WHERE pl_namespace=0
00721 AND pl_title LIKE '$likeprefix:%'";
00722
00723 $wgDatabase->query( $sql, 'do_pagelinks_namespace' );
00724 wfOut( "ok\n" );
00725 }
00726
00727 function do_drop_img_type() {
00728 global $wgDatabase;
00729
00730 if( $wgDatabase->fieldExists( 'image', 'img_type' ) ) {
00731 wfOut( "Dropping unused img_type field in image table... " );
00732 dbsource( archive( 'patch-drop_img_type.sql' ), $wgDatabase );
00733 wfOut( "ok\n" );
00734 } else {
00735 wfOut( "No img_type field in image table; Good.\n" );
00736 }
00737 }
00738
00739 function do_old_links_update() {
00740 global $wgDatabase;
00741 if( $wgDatabase->tableExists( 'pagelinks' ) ) {
00742 wfOut( "Already have pagelinks; skipping old links table updates.\n" );
00743 } else {
00744 convertLinks(); flush();
00745 }
00746 }
00747
00748 function fix_ancient_imagelinks() {
00749 global $wgDatabase;
00750 $info = $wgDatabase->fieldInfo( 'imagelinks', 'il_from' );
00751 if ( $info && $info->type() === 'string' ) {
00752 wfOut( "Fixing ancient broken imagelinks table.\n" );
00753 wfOut( "NOTE: you will have to run maintenance/refreshLinks.php after this.\n" );
00754 dbsource( archive( 'patch-fix-il_from.sql' ) );
00755 wfOut( "ok\n" );
00756 } else {
00757 wfOut( "...il_from OK\n" );
00758 }
00759 }
00760
00761 function do_user_unique_update() {
00762 global $wgDatabase;
00763 $duper = new UserDupes( $wgDatabase );
00764 if( $duper->hasUniqueIndex() ) {
00765 wfOut( "Already have unique user_name index.\n" );
00766 } else {
00767 if( !$duper->clearDupes() ) {
00768 wfOut( "WARNING: This next step will probably fail due to unfixed duplicates...\n" );
00769 }
00770 wfOut( "Adding unique index on user_name... " );
00771 dbsource( archive( 'patch-user_nameindex.sql' ), $wgDatabase );
00772 wfOut( "ok\n" );
00773 }
00774 }
00775
00776 function do_user_groups_update() {
00777 $fname = 'do_user_groups_update';
00778 global $wgDatabase;
00779
00780 if( $wgDatabase->tableExists( 'user_groups' ) ) {
00781 wfOut( "...user_groups table already exists.\n" );
00782 return do_user_groups_reformat();
00783 }
00784
00785 wfOut( "Adding user_groups table... " );
00786 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
00787 wfOut( "ok\n" );
00788
00789 if( !$wgDatabase->tableExists( 'user_rights' ) ) {
00790 if( $wgDatabase->fieldExists( 'user', 'user_rights' ) ) {
00791 wfOut( "Upgrading from a 1.3 or older database? Breaking out user_rights for conversion..." );
00792 dbsource( archive( 'patch-user_rights.sql' ), $wgDatabase );
00793 wfOut( "ok\n" );
00794 } else {
00795 wfOut( "*** WARNING: couldn't locate user_rights table or field for upgrade.\n" );
00796 wfOut( "*** You may need to manually configure some sysops by manipulating\n" );
00797 wfOut( "*** the user_groups table.\n" );
00798 return;
00799 }
00800 }
00801
00802 wfOut( "Converting user_rights table to user_groups... " );
00803 $result = $wgDatabase->select( 'user_rights',
00804 array( 'ur_user', 'ur_rights' ),
00805 array( "ur_rights != ''" ),
00806 $fname );
00807
00808 while( $row = $wgDatabase->fetchObject( $result ) ) {
00809 $groups = array_unique(
00810 array_map( 'trim',
00811 explode( ',', $row->ur_rights ) ) );
00812
00813 foreach( $groups as $group ) {
00814 $wgDatabase->insert( 'user_groups',
00815 array(
00816 'ug_user' => $row->ur_user,
00817 'ug_group' => $group ),
00818 $fname );
00819 }
00820 }
00821 $wgDatabase->freeResult( $result );
00822 wfOut( "ok\n" );
00823 }
00824
00825 function do_user_groups_reformat() {
00826 # Check for bogus formats from previous 1.5 alpha code.
00827 global $wgDatabase;
00828 $info = $wgDatabase->fieldInfo( 'user_groups', 'ug_group' );
00829
00830 if( $info->type() == 'int' ) {
00831 $oldug = $wgDatabase->tableName( 'user_groups' );
00832 $newug = $wgDatabase->tableName( 'user_groups_bogus' );
00833 wfOut( "user_groups is in bogus intermediate format. Renaming to $newug... " );
00834 $wgDatabase->query( "ALTER TABLE $oldug RENAME TO $newug" );
00835 wfOut( "ok\n" );
00836
00837 wfOut( "Re-adding fresh user_groups table... " );
00838 dbsource( archive( 'patch-user_groups.sql' ), $wgDatabase );
00839 wfOut( "ok\n" );
00840
00841 wfOut( "***\n" );
00842 wfOut( "*** WARNING: You will need to manually fix up user permissions in the user_groups\n" );
00843 wfOut( "*** table. Old 1.5 alpha versions did some pretty funky stuff...\n" );
00844 wfOut( "***\n" );
00845 } else {
00846 wfOut( "...user_groups is in current format.\n" );
00847 }
00848
00849 }
00850
00851 function do_watchlist_null() {
00852 # Make sure wl_notificationtimestamp can be NULL,
00853 # and update old broken items.
00854 global $wgDatabase;
00855 $info = $wgDatabase->fieldInfo( 'watchlist', 'wl_notificationtimestamp' );
00856
00857 if( !$info->nullable() ) {
00858 wfOut( "Making wl_notificationtimestamp nullable... " );
00859 dbsource( archive( 'patch-watchlist-null.sql' ), $wgDatabase );
00860 wfOut( "ok\n" );
00861 } else {
00862 wfOut( "...wl_notificationtimestamp is already nullable.\n" );
00863 }
00864
00865 }
00866
00870 function do_page_random_update() {
00871 global $wgDatabase;
00872
00873 wfOut( "Setting page_random to a random value on rows where it equals 0..." );
00874
00875 $page = $wgDatabase->tableName( 'page' );
00876 $wgDatabase->query( "UPDATE $page SET page_random = RAND() WHERE page_random = 0", 'do_page_random_update' );
00877 $rows = $wgDatabase->affectedRows();
00878
00879 wfOut( "changed $rows rows\n" );
00880 }
00881
00882 function do_templatelinks_update() {
00883 global $wgDatabase, $wgLoadBalancer;
00884 $fname = 'do_templatelinks_update';
00885
00886 if ( $wgDatabase->tableExists( 'templatelinks' ) ) {
00887 wfOut( "...templatelinks table already exists\n" );
00888 return;
00889 }
00890 wfOut( "Creating templatelinks table...\n" );
00891 dbsource( archive('patch-templatelinks.sql'), $wgDatabase );
00892 wfOut( "Populating...\n" );
00893 if ( isset( $wgLoadBalancer ) && $wgLoadBalancer->getServerCount() > 1 ) {
00894
00895 $res = $wgDatabase->select( 'pagelinks', array( 'pl_from', 'pl_namespace', 'pl_title' ),
00896 array( 'pl_namespace' => NS_TEMPLATE ), $fname );
00897 $count = 0;
00898 while ( $row = $wgDatabase->fetchObject( $res ) ) {
00899 $count = ($count + 1) % 100;
00900 if ( $count == 0 ) {
00901 if ( function_exists( 'wfWaitForSlaves' ) ) {
00902 wfWaitForSlaves( 10 );
00903 } else {
00904 sleep( 1 );
00905 }
00906 }
00907 $wgDatabase->insert( 'templatelinks',
00908 array(
00909 'tl_from' => $row->pl_from,
00910 'tl_namespace' => $row->pl_namespace,
00911 'tl_title' => $row->pl_title,
00912 ), $fname
00913 );
00914
00915 }
00916 $wgDatabase->freeResult( $res );
00917 } else {
00918
00919 $wgDatabase->insertSelect( 'templatelinks', 'pagelinks',
00920 array(
00921 'tl_from' => 'pl_from',
00922 'tl_namespace' => 'pl_namespace',
00923 'tl_title' => 'pl_title'
00924 ), array(
00925 'pl_namespace' => 10
00926 ), $fname
00927 );
00928 }
00929 wfOut( "Done. Please run maintenance/refreshLinks.php for a more thorough templatelinks update.\n" );
00930 }
00931
00932
00933
00934 function do_rc_indices_update() {
00935 global $wgDatabase;
00936 wfOut( "Checking for additional recent changes indices...\n" );
00937
00938 $indexes = array(
00939 'rc_ns_usertext' => 'patch-recentchanges-utindex.sql',
00940 'rc_user_text' => 'patch-rc_user_text-index.sql',
00941 );
00942
00943 foreach( $indexes as $index => $patch ) {
00944 $info = $wgDatabase->indexInfo( 'recentchanges', $index, __METHOD__ );
00945 if( !$info ) {
00946 wfOut( "...index `{$index}` not found; adding..." );
00947 dbsource( archive( $patch ) );
00948 wfOut( "done.\n" );
00949 } else {
00950 wfOut( "...index `{$index}` seems ok.\n" );
00951 }
00952 }
00953 }
00954
00955 function index_has_field($table, $index, $field) {
00956 global $wgDatabase;
00957 wfOut( "Checking if $table index $index includes field $field...\n" );
00958 $info = $wgDatabase->indexInfo( $table, $index, __METHOD__ );
00959 if( $info ) {
00960 foreach($info as $row) {
00961 if($row->Column_name == $field) {
00962 wfOut( "...index $index on table $table seems to be ok\n" );
00963 return true;
00964 }
00965 }
00966 }
00967 wfOut( "...index $index on table $table has no field $field; adding\n" );
00968 return false;
00969 }
00970
00971 function do_backlinking_indices_update() {
00972 wfOut( "Checking for backlinking indices...\n" );
00973 if (!index_has_field('pagelinks', 'pl_namespace', 'pl_from') ||
00974 !index_has_field('templatelinks', 'tl_namespace', 'tl_from') ||
00975 !index_has_field('imagelinks', 'il_to', 'il_from'))
00976 {
00977 dbsource( archive( 'patch-backlinkindexes.sql' ) );
00978 wfOut( "...backlinking indices updated\n" );
00979 }
00980 }
00981
00982 function do_categorylinks_indices_update() {
00983 wfOut( "Checking for categorylinks indices...\n" );
00984 if (!index_has_field('categorylinks', 'cl_sortkey', 'cl_from'))
00985 {
00986 dbsource( archive( 'patch-categorylinksindex.sql' ) );
00987 wfOut( "...categorylinks indices updated\n" );
00988 }
00989 }
00990
00991 function do_filearchive_indices_update() {
00992 global $wgDatabase;
00993 wfOut( "Checking filearchive indices...\n" );
00994 $info = $wgDatabase->indexInfo( 'filearchive', 'fa_user_timestamp', __METHOD__ );
00995 if ( !$info )
00996 {
00997 dbsource( archive( 'patch-filearhive-user-index.sql' ) );
00998 wfOut( "...filearchive indices updated\n" );
00999 }
01000 }
01001
01002 function maybe_do_profiling_memory_update() {
01003 global $wgDatabase;
01004 if ( !$wgDatabase->tableExists( 'profiling' ) ) {
01005
01006 } elseif ( $wgDatabase->fieldExists( 'profiling', 'pf_memory' ) ) {
01007 wfOut( "profiling table has pf_memory field.\n" );
01008 } else {
01009 wfOut( "Adding pf_memory field to table profiling..." );
01010 dbsource( archive( 'patch-profiling-memory.sql' ), $wgDatabase );
01011 wfOut( "ok\n" );
01012 }
01013 }
01014
01015 function do_stats_init() {
01016
01017 global $wgDatabase;
01018 wfOut( "Checking site_stats row..." );
01019 $row = $wgDatabase->selectRow( 'site_stats', '*', array( 'ss_row_id' => 1 ), __METHOD__ );
01020 if( $row === false ) {
01021 wfOut( "data is missing! rebuilding...\n" );
01022 } elseif ( isset( $row->site_stats ) && $row->ss_total_pages == -1 ) {
01023 wfOut( "missing ss_total_pages, rebuilding...\n" );
01024 } else {
01025 wfOut( "ok.\n" );
01026 return;
01027 }
01028
01029 global $IP;
01030 require_once "$IP/maintenance/initStats.inc";
01031 wfInitStats();
01032 }
01033
01034 function do_active_users_init() {
01035 global $wgDatabase;
01036 $activeUsers = $wgDatabase->selectField( 'site_stats', 'ss_active_users', false, __METHOD__ );
01037 if( $activeUsers == -1 ) {
01038 $activeUsers = $wgDatabase->selectField( 'recentchanges',
01039 'COUNT( DISTINCT rc_user_text )',
01040 array( 'rc_user != 0', 'rc_bot' => 0, "rc_log_type != 'newusers'" ), __METHOD__
01041 );
01042 $wgDatabase->update( 'site_stats',
01043 array( 'ss_active_users' => intval($activeUsers) ),
01044 array( 'ss_row_id' => 1 ), __METHOD__, array( 'LIMIT' => 1 )
01045 );
01046 }
01047 wfOut( "...ss_active_users user count set...\n" );
01048 }
01049
01050 function purge_cache() {
01051 global $wgDatabase;
01052 # We can't guarantee that the user will be able to use TRUNCATE,
01053 # but we know that DELETE is available to us
01054 wfOut( "Purging caches..." );
01055 $wgDatabase->delete( 'objectcache', '*', __METHOD__ );
01056 wfOut( "done.\n" );
01057 }
01058
01059 function do_all_updates( $shared = false, $purge = true ) {
01060 global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgSharedTables, $wgDatabase, $wgDBtype, $IP;
01061
01062 wfRunHooks('LoadExtensionSchemaUpdates');
01063
01064 $doUser = $shared ? $wgSharedDB && in_array('user', $wgSharedTables) : !$wgSharedDB || !in_array('user', $wgSharedTables);
01065
01066 if ($wgDBtype === 'postgres') {
01067 do_postgres_updates();
01068 return;
01069 }
01070
01071 # Run core updates in sequence...
01072 global $wgUpdates;
01073 if ( isset( $wgUpdates[$wgDBtype] ) ) {
01074 foreach( $wgUpdates[$wgDBtype] as $params ) {
01075 $func = array_shift( $params );
01076 call_user_func_array( $func, $params );
01077 flush();
01078 }
01079 }
01080
01082 global $wgExtNewTables, $wgExtNewFields, $wgExtNewIndexes;
01083 # Add missing extension tables
01084 foreach ( $wgExtNewTables as $tableRecord ) {
01085 add_table( $tableRecord[0], $tableRecord[1], true );
01086 flush();
01087 }
01088 # Add missing extension fields
01089 foreach ( $wgExtNewFields as $fieldRecord ) {
01090 if ( $fieldRecord[0] != 'user' || $doUser ) {
01091 add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2], true );
01092 }
01093 flush();
01094 }
01095 # Add missing extension indexes
01096 foreach ( $wgExtNewIndexes as $fieldRecord ) {
01097 add_index( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2], true );
01098 flush();
01099 }
01100
01101
01102 wfOut( "Deleting old default messages (this may take a long time!)..." );
01103 deleteDefaultMessages();
01104 wfOut( "Done\n" );
01105
01106 do_stats_init();
01107
01108 if( $purge ) {
01109 purge_cache();
01110 }
01111 }
01112
01113 function archive($name) {
01114 global $wgDBtype, $IP;
01115 if ( file_exists( "$IP/maintenance/$wgDBtype/archives/$name" ) ) {
01116 return "$IP/maintenance/$wgDBtype/archives/$name";
01117 } else {
01118 return "$IP/maintenance/archives/$name";
01119 }
01120 }
01121
01122 function do_restrictions_update() {
01123 # Adding page_restrictions table, obsoleting page.page_restrictions.
01124 # Migrating old restrictions to new table
01125 # -- Andrew Garrett, January 2007.
01126
01127 global $wgDatabase;
01128
01129 $name = 'page_restrictions';
01130 $patch = 'patch-page_restrictions.sql';
01131 $patch2 = 'patch-page_restrictions_sortkey.sql';
01132
01133 if ( $wgDatabase->tableExists( $name ) ) {
01134 wfOut( "...$name table already exists.\n" );
01135 } else {
01136 wfOut( "Creating $name table..." );
01137 dbsource( archive($patch), $wgDatabase );
01138 dbsource( archive($patch2), $wgDatabase );
01139 wfOut( "ok\n" );
01140
01141 wfOut( "Migrating old restrictions to new table..." );
01142
01143 $res = $wgDatabase->select( 'page', array( 'page_id', 'page_restrictions' ), array("page_restrictions!=''", "page_restrictions!='edit=:move='"), __METHOD__ );
01144
01145 $count = 0;
01146
01147 while ($row = $wgDatabase->fetchObject($res) ) {
01148 $count = ($count + 1) % 100;
01149
01150 if ($count == 0) {
01151 if ( function_exists( 'wfWaitForSlaves' ) ) {
01152 wfWaitForSlaves( 10 );
01153 } else {
01154 sleep( 1 );
01155 }
01156 }
01157
01158 # Figure out what the restrictions are..
01159 $id = $row->page_id;
01160 $flatrestrictions = explode( ':', $row->page_restrictions );
01161
01162 $restrictions = array ();
01163 foreach( $flatrestrictions as $restriction ) {
01164 $thisrestriction = explode( '=', $restriction, 2 );
01165 if( count( $thisrestriction ) == 1 ) {
01166
01167
01168 list( $level ) = $thisrestriction;
01169 if( $level ) {
01170 $restrictions['edit'] = $level;
01171 $restrictions['move'] = $level;
01172 }
01173 } else {
01174 list( $type, $level ) = $thisrestriction;
01175 if( $level ) {
01176 $restrictions[$type] = $level;
01177 }
01178 }
01179
01180 $wgDatabase->update( 'page', array ( 'page_restrictions' => ''), array( 'page_id' => $id ), __METHOD__ );
01181
01182 }
01183
01184 foreach( $restrictions as $type => $level ) {
01185 $wgDatabase->insert( 'page_restrictions', array ( 'pr_page' => $id,
01186 'pr_type' => $type,
01187 'pr_level' => $level,
01188 'pr_cascade' => 0,
01189 'pr_expiry' => 'infinity' ),
01190 __METHOD__ );
01191 }
01192 }
01193 wfOut( "ok\n" );
01194 }
01195 }
01196
01197 function do_category_population() {
01198 if( update_row_exists( 'populate category' ) ) {
01199 wfOut( "...category table already populated.\n" );
01200 return;
01201 }
01202 require_once( 'populateCategory.inc' );
01203 wfOut( "Populating category table, printing progress markers. " ).
01204 "For large databases, you\n".
01205 "may want to hit Ctrl-C and do this manually with maintenance/\n".
01206 "populateCategory.php.\n";
01207 populateCategory( '', 10, 0, true );
01208 wfOut( "Done populating category table.\n" );
01209 }
01210
01211 function do_populate_parent_id() {
01212 if( update_row_exists( 'populate rev_parent_id' ) ) {
01213 wfOut( "...rev_parent_id column already populated.\n" );
01214 return;
01215 }
01216 require_once( 'populateParentId.inc' );
01217
01218 global $wgDatabase;
01219 populate_rev_parent_id( $wgDatabase );
01220 }
01221
01222 function sqlite_initial_indexes() {
01223 global $wgDatabase;
01224 if ( update_row_exists( 'initial_indexes' ) ) {
01225 wfOut( "...have initial indexes\n" );
01226 return;
01227 }
01228 wfOut( "Adding initial indexes..." );
01229 $wgDatabase->sourceFile( archive( 'initial-indexes.sql' ) );
01230 wfOut( "done\n" );
01231 }
01232
01233 function do_unique_pl_tl_il() {
01234 global $wgDatabase;
01235 $info = $wgDatabase->indexInfo( 'pagelinks', 'pl_namespace' );
01236 if( is_array($info) && !$info[0]->Non_unique ) {
01237 wfOut( "...pl_namespace, tl_namespace, il_to indices are already UNIQUE.\n" );
01238 } else {
01239 wfOut( "Making pl_namespace, tl_namespace and il_to indices UNIQUE... " );
01240 dbsource( archive( 'patch-pl-tl-il-unique.sql' ), $wgDatabase );
01241 wfOut( "ok\n" );
01242 }
01243 }
01244
01245
01246
01247
01248
01249
01250 function
01251 pg_describe_table($table)
01252 {
01253 global $wgDatabase, $wgDBmwschema;
01254 $q = <<<END
01255 SELECT attname, attnum FROM pg_namespace, pg_class, pg_attribute
01256 WHERE pg_class.relnamespace = pg_namespace.oid
01257 AND attrelid=pg_class.oid AND attnum > 0
01258 AND relname=%s AND nspname=%s
01259 END;
01260 $res = $wgDatabase->query(sprintf($q,
01261 $wgDatabase->addQuotes($table),
01262 $wgDatabase->addQuotes($wgDBmwschema)));
01263 if (!$res)
01264 return null;
01265
01266 $cols = array();
01267 while ($r = $wgDatabase->fetchRow($res)) {
01268 $cols[] = array(
01269 "name" => $r[0],
01270 "ord" => $r[1],
01271 );
01272 }
01273 return $cols;
01274 }
01275
01276 function
01277 pg_describe_index($idx)
01278 {
01279 global $wgDatabase, $wgDBmwschema;
01280
01281
01282
01283 $q = <<<END
01284 SELECT indkey, indrelid FROM pg_namespace, pg_class, pg_index
01285 WHERE nspname=%s
01286 AND pg_class.relnamespace = pg_namespace.oid
01287 AND relname=%s
01288 AND indexrelid=pg_class.oid
01289 END;
01290 $res = $wgDatabase->query(sprintf($q,
01291 $wgDatabase->addQuotes($wgDBmwschema),
01292 $wgDatabase->addQuotes($idx)));
01293 if (!$res)
01294 return null;
01295 if (!($r = $wgDatabase->fetchRow($res))) {
01296 $wgDatabase->freeResult($res);
01297 return null;
01298 }
01299
01300 $indkey = $r[0];
01301 $relid = intval($r[1]);
01302 $indkeys = explode(" ", $indkey);
01303 $wgDatabase->freeResult($res);
01304
01305 $colnames = array();
01306 foreach ($indkeys as $rid) {
01307 $query = <<<END
01308 SELECT attname FROM pg_class, pg_attribute
01309 WHERE attrelid=$relid
01310 AND attnum=%d
01311 AND attrelid=pg_class.oid
01312 END;
01313 $r2 = $wgDatabase->query(sprintf($query, $rid));
01314 if (!$r2)
01315 return null;
01316 if (!($row2 = $wgDatabase->fetchRow($r2))) {
01317 $wgDatabase->freeResult($r2);
01318 return null;
01319 }
01320 $colnames[] = $row2[0];
01321 $wgDatabase->freeResult($r2);
01322 }
01323
01324 return $colnames;
01325 }
01326
01327 function
01328 pg_index_exists($table, $index)
01329 {
01330 global $wgDatabase, $wgDBmwschema;
01331 $exists = $wgDatabase->selectField("pg_indexes", "indexname",
01332 array( "indexname" => $index,
01333 "tablename" => $table,
01334 "schemaname" => $wgDBmwschema));
01335 return $exists === $index;
01336 }
01337
01338 function
01339 pg_fkey_deltype($fkey)
01340 {
01341 global $wgDatabase, $wgDBmwschema;
01342 $q = <<<END
01343 SELECT confdeltype FROM pg_constraint, pg_namespace
01344 WHERE connamespace=pg_namespace.oid
01345 AND nspname=%s
01346 AND conname=%s;
01347 END;
01348 $r = $wgDatabase->query(sprintf($q,
01349 $wgDatabase->addQuotes($wgDBmwschema),
01350 $wgDatabase->addQuotes($fkey)));
01351 if (!($row = $wgDatabase->fetchRow($r)))
01352 return null;
01353 return $row[0];
01354 }
01355
01356 function
01357 pg_rule_def($table, $rule)
01358 {
01359 global $wgDatabase, $wgDBmwschema;
01360 $q = <<<END
01361 SELECT definition FROM pg_rules
01362 WHERE schemaname = %s
01363 AND tablename = %s
01364 AND rulename = %s
01365 END;
01366 $r = $wgDatabase->query(sprintf($q,
01367 $wgDatabase->addQuotes($wgDBmwschema),
01368 $wgDatabase->addQuotes($table),
01369 $wgDatabase->addQuotes($rule)));
01370 $row = $wgDatabase->fetchRow($r);
01371 if (!$row)
01372 return null;
01373 $d = $row[0];
01374 $wgDatabase->freeResult($r);
01375 return $d;
01376 }
01377
01378 function do_postgres_updates() {
01379 global $wgDatabase, $wgVersion, $wgDBmwschema, $wgDBts2schema, $wgShowExceptionDetails, $wgDBuser;
01380
01381 ## Gather version numbers in case we need them
01382 $version = $wgDatabase->getServerVersion(); ## long string
01383 $numver = $wgDatabase->numeric_version; ## X.Y e.g. 8.3
01384
01385 $wgShowExceptionDetails = 1;
01386
01387 # Just in case their LocalSettings.php does not have this:
01388 if ( !isset( $wgDBmwschema ))
01389 $wgDBmwschema = 'mediawiki';
01390
01391 # Verify that this user is configured correctly
01392 $safeuser = $wgDatabase->addQuotes($wgDBuser);
01393 $SQL = "SELECT array_to_string(useconfig,'*') FROM pg_catalog.pg_user WHERE usename = $safeuser";
01394 $config = pg_fetch_result( $wgDatabase->doQuery( $SQL ), 0, 0 );
01395 $conf = array();
01396 foreach( explode( '*', $config ) as $c ) {
01397 list( $x,$y ) = explode( '=', $c );
01398 $conf[$x] = $y;
01399 }
01400 if( !array_key_exists( 'search_path', $conf ) ) {
01401 $search_path = '';
01402 }
01403 else {
01404 $search_path = $conf['search_path'];
01405 }
01406 if( strpos( $search_path, $wgDBmwschema ) === false ) {
01407 wfOut( "Adding in schema \"$wgDBmwschema\" to search_path for user \"$wgDBuser\"\n" );
01408 $search_path = "$wgDBmwschema, $search_path";
01409 }
01410 if( strpos( $search_path, $wgDBts2schema ) === false ) {
01411 wfOut( "Adding in schema \"$wgDBts2schema\" to search_path for user \"$wgDBuser\"\n" );
01412 $search_path = "$search_path, $wgDBts2schema";
01413 }
01414 $search_path = str_replace( ', ,', ',', $search_path);
01415 if( array_key_exists( 'search_path', $conf ) === false || $search_path != $conf['search_path'] ) {
01416 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET search_path = $search_path" );
01417 $wgDatabase->doQuery( "SET search_path = $search_path" );
01418 }
01419 else {
01420 $path = $conf['search_path'];
01421 wfOut( "... search_path for user \"$wgDBuser\" looks correct ($path)\n" );
01422 }
01423 $goodconf = array(
01424 'client_min_messages' => 'error',
01425 'DateStyle' => 'ISO, YMD',
01426 'TimeZone' => 'GMT'
01427 );
01428 foreach( array_keys( $goodconf ) AS $key ) {
01429 $value = $goodconf[$key];
01430 if( !array_key_exists( $key, $conf ) or $conf[$key] !== $value ) {
01431 wfOut( "Setting $key to '$value' for user \"$wgDBuser\"\n" );
01432 $wgDatabase->doQuery( "ALTER USER $wgDBuser SET $key = '$value'" );
01433 $wgDatabase->doQuery( "SET $key = '$value'" );
01434 }
01435 else {
01436 wfOut( "... default value of \"$key\" is correctly set to \"$value\" for user \"$wgDBuser\"\n" );
01437 }
01438 }
01439
01440 $newsequences = array(
01441 "log_log_id_seq",
01442 "pr_id_val",
01443 );
01444
01445 $newtables = array(
01446 array("category", "patch-category.sql"),
01447 array("mediawiki_version", "patch-mediawiki_version.sql"),
01448 array("mwuser", "patch-mwuser.sql"),
01449 array("pagecontent", "patch-pagecontent.sql"),
01450 array("querycachetwo", "patch-querycachetwo.sql"),
01451 array("page_props", "patch-page_props.sql"),
01452 array("page_restrictions", "patch-page_restrictions.sql"),
01453 array("profiling", "patch-profiling.sql"),
01454 array("protected_titles", "patch-protected_titles.sql"),
01455 array("redirect", "patch-redirect.sql"),
01456 array("updatelog", "patch-updatelog.sql"),
01457 array('change_tag', 'patch-change_tag.sql'),
01458 array('tag_summary', 'patch-change_tag.sql'),
01459 array('valid_tag', 'patch-change_tag.sql'),
01460 );
01461
01462 $newcols = array(
01463 array("archive", "ar_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01464 array("archive", "ar_len", "INTEGER"),
01465 array("archive", "ar_page_id", "INTEGER"),
01466 array("archive", "ar_parent_id", "INTEGER"),
01467 array("image", "img_sha1", "TEXT NOT NULL DEFAULT ''"),
01468 array("ipblocks", "ipb_allow_usertalk", "SMALLINT NOT NULL DEFAULT 0"),
01469 array("ipblocks", "ipb_anon_only", "SMALLINT NOT NULL DEFAULT 0"),
01470 array("ipblocks", "ipb_by_text", "TEXT NOT NULL DEFAULT ''"),
01471 array("ipblocks", "ipb_block_email", "SMALLINT NOT NULL DEFAULT 0"),
01472 array("ipblocks", "ipb_create_account", "SMALLINT NOT NULL DEFAULT 1"),
01473 array("ipblocks", "ipb_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01474 array("ipblocks", "ipb_enable_autoblock", "SMALLINT NOT NULL DEFAULT 1"),
01475 array("filearchive", "fa_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01476 array("logging", "log_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01477 array("logging", "log_id", "INTEGER NOT NULL PRIMARY KEY DEFAULT nextval('log_log_id_seq')"),
01478 array("logging", "log_params", "TEXT"),
01479 array("mwuser", "user_editcount", "INTEGER"),
01480 array("mwuser", "user_hidden", "SMALLINT NOT NULL DEFAULT 0"),
01481 array("mwuser", "user_newpass_time", "TIMESTAMPTZ"),
01482 array("oldimage", "oi_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01483 array("oldimage", "oi_major_mime", "TEXT NOT NULL DEFAULT 'unknown'"),
01484 array("oldimage", "oi_media_type", "TEXT"),
01485 array("oldimage", "oi_metadata", "BYTEA NOT NULL DEFAULT ''"),
01486 array("oldimage", "oi_minor_mime", "TEXT NOT NULL DEFAULT 'unknown'"),
01487 array("oldimage", "oi_sha1", "TEXT NOT NULL DEFAULT ''"),
01488 array("page_restrictions", "pr_id", "INTEGER NOT NULL UNIQUE DEFAULT nextval('pr_id_val')"),
01489 array("profiling", "pf_memory", "NUMERIC(18,10) NOT NULL DEFAULT 0"),
01490 array("recentchanges", "rc_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01491 array("recentchanges", "rc_log_action", "TEXT"),
01492 array("recentchanges", "rc_log_type", "TEXT"),
01493 array("recentchanges", "rc_logid", "INTEGER NOT NULL DEFAULT 0"),
01494 array("recentchanges", "rc_new_len", "INTEGER"),
01495 array("recentchanges", "rc_old_len", "INTEGER"),
01496 array("recentchanges", "rc_params", "TEXT"),
01497 array("revision", "rev_deleted", "SMALLINT NOT NULL DEFAULT 0"),
01498 array("revision", "rev_len", "INTEGER"),
01499 array("revision", "rev_parent_id", "INTEGER DEFAULT NULL"),
01500 array("site_stats", "ss_active_users", "INTEGER DEFAULT '-1'"),
01501 array("user_newtalk", "user_last_timestamp", "TIMESTAMPTZ"),
01502 );
01503
01504
01505 # table, column, desired type, USING clause if needed (with new default if needed)
01506 $typechanges = array(
01507 array("archive", "ar_deleted", "smallint", ""),
01508 array("archive", "ar_minor_edit", "smallint", "ar_minor_edit::smallint DEFAULT 0"),
01509 array("filearchive", "fa_deleted", "smallint", ""),
01510 array("filearchive", "fa_height", "integer", ""),
01511 array("filearchive", "fa_metadata", "bytea", "decode(fa_metadata,'escape')"),
01512 array("filearchive", "fa_size", "integer", ""),
01513 array("filearchive", "fa_width", "integer", ""),
01514 array("filearchive", "fa_storage_group","text", ""),
01515 array("filearchive", "fa_storage_key", "text", ""),
01516 array("image", "img_metadata", "bytea", "decode(img_metadata,'escape')"),
01517 array("image", "img_size", "integer", ""),
01518 array("image", "img_width", "integer", ""),
01519 array("image", "img_height", "integer", ""),
01520 array("interwiki", "iw_local", "smallint", "iw_local::smallint DEFAULT 0"),
01521 array("interwiki", "iw_trans", "smallint", "iw_trans::smallint DEFAULT 0"),
01522 array("ipblocks", "ipb_auto", "smallint", "ipb_auto::smallint DEFAULT 0"),
01523 array("ipblocks", "ipb_anon_only", "smallint", "CASE WHEN ipb_anon_only=' ' THEN 0 ELSE ipb_anon_only::smallint END DEFAULT 0"),
01524 array("ipblocks", "ipb_create_account", "smallint", "CASE WHEN ipb_create_account=' ' THEN 0 ELSE ipb_create_account::smallint END DEFAULT 1"),
01525 array("ipblocks", "ipb_enable_autoblock", "smallint", "CASE WHEN ipb_enable_autoblock=' ' THEN 0 ELSE ipb_enable_autoblock::smallint END DEFAULT 1"),
01526 array("ipblocks", "ipb_block_email", "smallint", "CASE WHEN ipb_block_email=' ' THEN 0 ELSE ipb_block_email::smallint END DEFAULT 0"),
01527 array("ipblocks", "ipb_address", "text", "ipb_address::text"),
01528 array("ipblocks", "ipb_deleted", "smallint", "ipb_deleted::smallint DEFAULT 0"),
01529 array("math", "math_inputhash", "bytea", "decode(math_inputhash,'escape')"),
01530 array("math", "math_outputhash", "bytea", "decode(math_outputhash,'escape')"),
01531 array("mwuser", "user_token", "text", ""),
01532 array("mwuser", "user_email_token","text", ""),
01533 array("objectcache", "keyname", "text", ""),
01534 array("oldimage", "oi_height", "integer", ""),
01535 array("oldimage", "oi_metadata", "bytea", "decode(img_metadata,'escape')"),
01536 array("oldimage", "oi_size", "integer", ""),
01537 array("oldimage", "oi_width", "integer", ""),
01538 array("page", "page_is_redirect","smallint", "page_is_redirect::smallint DEFAULT 0"),
01539 array("page", "page_is_new", "smallint", "page_is_new::smallint DEFAULT 0"),
01540 array("querycache", "qc_value", "integer", ""),
01541 array("querycachetwo","qcc_value", "integer", ""),
01542 array("recentchanges","rc_bot", "smallint", "rc_bot::smallint DEFAULT 0"),
01543 array("recentchanges","rc_deleted", "smallint", ""),
01544 array("recentchanges","rc_minor", "smallint", "rc_minor::smallint DEFAULT 0"),
01545 array("recentchanges","rc_new", "smallint", "rc_new::smallint DEFAULT 0"),
01546 array("recentchanges","rc_type", "smallint", "rc_type::smallint DEFAULT 0"),
01547 array("recentchanges","rc_patrolled", "smallint", "rc_patrolled::smallint DEFAULT 0"),
01548 array("revision", "rev_deleted", "smallint", "rev_deleted::smallint DEFAULT 0"),
01549 array("revision", "rev_minor_edit", "smallint", "rev_minor_edit::smallint DEFAULT 0"),
01550 array("templatelinks","tl_namespace", "smallint", "tl_namespace::smallint"),
01551 array("user_newtalk", "user_ip", "text", "host(user_ip)"),
01552 );
01553
01554 # table, column, nullability
01555 $nullchanges = array(
01556 array("oldimage", "oi_bits", "NULL"),
01557 array("oldimage", "oi_timestamp", "NULL"),
01558 array("oldimage", "oi_major_mime", "NULL"),
01559 array("oldimage", "oi_minor_mime", "NULL"),
01560 );
01561
01562 $newindexes = array(
01563 array("archive", "archive_user_text", "(ar_user_text)"),
01564 array("image", "img_sha1", "(img_sha1)"),
01565 array("oldimage", "oi_sha1", "(oi_sha1)"),
01566 array("revision", "rev_text_id_idx", "(rev_text_id)"),
01567 array("recentchanges", "rc_timestamp_bot", "(rc_timestamp) WHERE rc_bot = 0"),
01568 array("templatelinks", "templatelinks_from", "(tl_from)"),
01569 array("watchlist", "wl_user", "(wl_user)"),
01570 );
01571
01572 $newrules = array(
01573 );
01574
01575 foreach ($newsequences as $ns) {
01576 if ($wgDatabase->sequenceExists($ns)) {
01577 wfOut( "... sequence \"$ns\" already exists\n" );
01578 continue;
01579 }
01580
01581 wfOut( "Creating sequence \"$ns\"\n" );
01582 $wgDatabase->query("CREATE SEQUENCE $ns");
01583 }
01584
01585 foreach ($newtables as $nt) {
01586 if ($wgDatabase->tableExists($nt[0])) {
01587 wfOut( "... table \"$nt[0]\" already exists\n" );
01588 continue;
01589 }
01590
01591 wfOut( "Creating table \"$nt[0]\"\n" );
01592 dbsource(archive($nt[1]));
01593 }
01594
01595 ## Needed before newcols
01596 if ($wgDatabase->tableExists("archive2")) {
01597 wfOut( "Converting \"archive2\" back to normal archive table\n" );
01598 if ($wgDatabase->ruleExists("archive", "archive_insert")) {
01599 wfOut( "Dropping rule \"archive_insert\"\n" );
01600 $wgDatabase->query("DROP RULE archive_insert ON archive");
01601 }
01602 if ($wgDatabase->ruleExists("archive", "archive_delete")) {
01603 wfOut( "Dropping rule \"archive_delete\"\n" );
01604 $wgDatabase->query("DROP RULE archive_delete ON archive");
01605 }
01606 dbsource(archive("patch-remove-archive2.sql"));
01607 }
01608 else
01609 wfOut( "... obsolete table \"archive2\" does not exist\n" );
01610
01611 foreach ($newcols as $nc) {
01612 $fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
01613 if (!is_null($fi)) {
01614 wfOut( "... column \"$nc[0].$nc[1]\" already exists\n" );
01615 continue;
01616 }
01617
01618 wfOut( "Adding column \"$nc[0].$nc[1]\"\n" );
01619 $wgDatabase->query("ALTER TABLE $nc[0] ADD $nc[1] $nc[2]");
01620 }
01621
01622 foreach ($typechanges as $tc) {
01623 $fi = $wgDatabase->fieldInfo($tc[0], $tc[1]);
01624 if (is_null($fi)) {
01625 wfOut( "... error: expected column $tc[0].$tc[1] to exist\n" );
01626 exit(1);
01627 }
01628
01629 if ($fi->type() === $tc[2])
01630 wfOut( "... column \"$tc[0].$tc[1]\" is already of type \"$tc[2]\"\n" );
01631 else {
01632 wfOut( "Changing column type of \"$tc[0].$tc[1]\" from \"{$fi->type()}\" to \"$tc[2]\"\n" );
01633 $sql = "ALTER TABLE $tc[0] ALTER $tc[1] TYPE $tc[2]";
01634 if (strlen($tc[3])) {
01635 $default = array();
01636 if (preg_match( '/DEFAULT (.+)/', $tc[3], $default)) {
01637 $sqldef = "ALTER TABLE $tc[0] ALTER $tc[1] SET DEFAULT $default[1]";
01638 $wgDatabase->query($sqldef);
01639 $tc[3] = preg_replace( '/\s*DEFAULT .+/', '', $tc[3]);
01640 }
01641 $sql .= " USING $tc[3]";
01642 }
01643 $sql .= ";\nCOMMIT;\n";
01644 $wgDatabase->query($sql);
01645 }
01646 }
01647
01648 foreach ($nullchanges as $nc) {
01649 $fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
01650 if (is_null($fi)) {
01651 wfOut( "... error: expected column $nc[0].$nc[1] to exist\n" );
01652 exit(1);
01653 }
01654 if ($fi->nullable()) {
01655 ## It's NULL - does it need to be NOT NULL?
01656 if ('NOT NULL' === $nc[2]) {
01657 wfOut( "Changing \"$nc[0].$nc[1]\" to not allow NULLs\n" );
01658 $wgDatabase->query( "ALTER TABLE $nc[0] ALTER $nc[1] SET NOT NULL" );
01659 }
01660 else {
01661 wfOut( "... column \"$nc[0].$nc[1]\" is already set as NULL\n" );
01662 }
01663 }
01664 else {
01665 ## It's NOT NULL - does it need to be NULL?
01666 if ('NULL' === $nc[2]) {
01667 wfOut( "Changing \"$nc[0].$nc[1]\" to allow NULLs\n" );
01668 $wgDatabase->query( "ALTER TABLE $nc[0] ALTER $nc[1] DROP NOT NULL" );
01669 }
01670 else {
01671 wfOut( "... column \"$nc[0].$nc[1]\" is already set as NOT NULL\n" );
01672 }
01673 }
01674 }
01675
01676 if ($wgDatabase->fieldInfo('oldimage','oi_deleted')->type() !== 'smallint') {
01677 wfOut( "Changing \"oldimage.oi_deleted\" to type \"smallint\"\n" );
01678 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted DROP DEFAULT" );
01679 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted TYPE SMALLINT USING (oi_deleted::smallint)" );
01680 $wgDatabase->query( "ALTER TABLE oldimage ALTER oi_deleted SET DEFAULT 0" );
01681 }
01682 else
01683 wfOut( "... column \"oldimage.oi_deleted\" is already of type \"smallint\"\n" );
01684
01685
01686 foreach ($newindexes as $ni) {
01687 if (pg_index_exists($ni[0], $ni[1])) {
01688 wfOut( "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n" );
01689 continue;
01690 }
01691 wfOut( "Creating index \"$ni[1]\" on table \"$ni[0]\" $ni[2]\n" );
01692 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
01693 }
01694
01695 foreach ($newrules as $nr) {
01696 if ($wgDatabase->ruleExists($nr[0], $nr[1])) {
01697 wfOut( "... rule \"$nr[1]\" on table \"$nr[0]\" already exists\n" );
01698 continue;
01699 }
01700 wfOut( "Adding rule \"$nr[1]\" to table \"$nr[0]\"\n" );
01701 dbsource(archive($nr[2]));
01702 }
01703
01704 if ($wgDatabase->hasConstraint("oldimage_oi_name_fkey")) {
01705 wfOut( "Making foreign key on table \"oldimage\" (to image) a cascade delete\n" );
01706 $wgDatabase->query( "ALTER TABLE oldimage DROP CONSTRAINT oldimage_oi_name_fkey" );
01707 $wgDatabase->query( "ALTER TABLE oldimage ADD CONSTRAINT oldimage_oi_name_fkey_cascade ".
01708 "FOREIGN KEY (oi_name) REFERENCES image(img_name) ON DELETE CASCADE" );
01709 }
01710 else
01711 wfOut( "... table \"oldimage\" has correct cascade delete foreign key to image\n" );
01712
01713 if (!$wgDatabase->triggerExists("page", "page_deleted")) {
01714 wfOut( "Adding function and trigger \"page_deleted\" to table \"page\"\n" );
01715 dbsource(archive('patch-page_deleted.sql'));
01716 }
01717 else
01718 wfOut( "... table \"page\" has \"page_deleted\" trigger\n" );
01719
01720 $fi = $wgDatabase->fieldInfo("recentchanges", "rc_cur_id");
01721 if (!$fi->nullable()) {
01722 wfOut( "Removing NOT NULL constraint from \"recentchanges.rc_cur_id\"\n" );
01723 dbsource(archive('patch-rc_cur_id-not-null.sql'));
01724 }
01725 else
01726 wfOut( "... column \"recentchanges.rc_cur_id\" has a NOT NULL constraint\n" );
01727
01728 $pu = pg_describe_index("pagelink_unique");
01729 if (!is_null($pu) && ($pu[0] != "pl_from" || $pu[1] != "pl_namespace" || $pu[2] != "pl_title")) {
01730 wfOut( "Dropping obsolete version of index \"pagelink_unique index\"\n" );
01731 $wgDatabase->query("DROP INDEX pagelink_unique");
01732 $pu = null;
01733 }
01734 else
01735 wfOut( "... obsolete version of index \"pagelink_unique index\" does not exist\n" );
01736
01737 if (is_null($pu)) {
01738 wfOut( "Creating index \"pagelink_unique index\"\n" );
01739 $wgDatabase->query("CREATE UNIQUE INDEX pagelink_unique ON pagelinks (pl_from,pl_namespace,pl_title)");
01740 }
01741 else
01742 wfOut( "... index \"pagelink_unique_index\" already exists\n" );
01743
01744 if (pg_fkey_deltype("revision_rev_user_fkey") == 'r') {
01745 wfOut( "... constraint \"revision_rev_user_fkey\" is ON DELETE RESTRICT\n" );
01746 }
01747 else {
01748 wfOut( "Changing constraint \"revision_rev_user_fkey\" to ON DELETE RESTRICT\n" );
01749 dbsource(archive('patch-revision_rev_user_fkey.sql'));
01750 }
01751
01752 # Fix ipb_address index
01753 if (pg_index_exists('ipblocks', 'ipb_address' )) {
01754 wfOut( "Removing deprecated index 'ipb_address'...\n" );
01755 $wgDatabase->query('DROP INDEX ipb_address');
01756 }
01757 if (pg_index_exists('ipblocks', 'ipb_address_unique' )) {
01758 wfOut( "... have ipb_address_unique\n" );
01759 }
01760 else {
01761 wfOut( "Adding ipb_address_unique index\n" );
01762 dbsource(archive('patch-ipb_address_unique.sql'));
01763 }
01764
01765 global $wgExtNewTables, $wgExtPGNewFields, $wgExtNewIndexes;
01766 # Add missing extension tables
01767 foreach ( $wgExtNewTables as $nt ) {
01768 if ($wgDatabase->tableExists($nt[0])) {
01769 wfOut( "... table \"$nt[0]\" already exists\n" );
01770 continue;
01771 }
01772 wfOut( "Creating table \"$nt[0]\"\n" );
01773 dbsource($nt[1]);
01774 }
01775 # Add missing extension fields
01776 foreach ( $wgExtPGNewFields as $nc ) {
01777 $fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
01778 if (!is_null($fi)) {
01779 wfOut( "... column \"$nc[0].$nc[1]\" already exists\n" );
01780 continue;
01781 }
01782 wfOut( "Adding column \"$nc[0].$nc[1]\"\n" );
01783 $wgDatabase->query( "ALTER TABLE $nc[0] ADD $nc[1] $nc[2]" );
01784 }
01785 # Add missing extension indexes
01786 foreach ( $wgExtNewIndexes as $ni ) {
01787 if (pg_index_exists($ni[0], $ni[1])) {
01788 wfOut( "... index \"$ni[1]\" on table \"$ni[0]\" already exists\n" );
01789 continue;
01790 }
01791 wfOut( "Creating index \"$ni[1]\" on table \"$ni[0]\"\n" );
01792 if ( preg_match( '/^\(/', $ni[2] ) ) {
01793 $wgDatabase->query( "CREATE INDEX $ni[1] ON $ni[0] $ni[2]" );
01794 }
01795 else {
01796 dbsource($ni[2]);
01797 }
01798 }
01799
01800 # Tweak the page_title tsearch2 trigger to filter out slashes
01801 # This is create or replace, so harmless to call if not needed
01802 dbsource(archive('patch-ts2pagetitle.sql'));
01803
01804 ## If the server is 8.3 or higher, rewrite the tsearch2 triggers
01805 ## in case they have the old 'default' versions
01806 if ( $numver >= 8.3 )
01807 dbsource(archive('patch-tsearch2funcs.sql'));
01808
01809 ## Put a new row in the mediawiki_version table
01810 $wgDatabase->insert( 'mediawiki_version',
01811 array(
01812 'type' => 'Update',
01813 'ctype' => 'U',
01814 'mw_version' => $wgVersion,
01815 'pg_version' => $version,
01816 'sql_version' => '$LastChangedRevision: 51640 $',
01817 'sql_date' => '$LastChangedDate: 2009-06-09 22:58:05 +1000 (Tue, 09 Jun 2009) $',
01818 ) );
01819 return;
01820 }