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