MediaWiki r23759 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r23758‎ | r23759 (on ViewVC)‎ | r23760 >
Date:20:02, 5 July 2007
Author:aaron
Status:old
Tags:
Comment:
*Add extension schema change support
Modified paths:

Diff [purge]

Index: trunk/phase3/maintenance/updaters.inc
===================================================================
--- trunk/phase3/maintenance/updaters.inc	(revision 23758)
+++ trunk/phase3/maintenance/updaters.inc	(revision 23759)
@@ -13,6 +13,8 @@
 require_once 'convertLinks.inc';
 require_once 'userDupes.inc';
 require_once 'deleteDefaultMessages.php';
+# Extension updates
+require_once( "$IP/includes/Hooks.php" );
 
 $wgRenamedTables = array(
 #           from             to                  patch file
@@ -77,9 +79,15 @@
  	array( 'revision',	    'rev_parent_id',    'patch-rev_parent_id.sql' ),
 	array( 'page_restrictions', 'pr_id',		'patch-page_restrictions_sortkey.sql' ),
 	array( 'ipblocks',      'ipb_block_email',  'patch-ipb_emailban.sql' ),
-	array( 'oldimage',      'oi_metadata',     'patch-oi_metadata.sql'),
+	array( 'oldimage',      'oi_metadata',      'patch-oi_metadata.sql'),
 );
 
+# For extensions only, should be populated via hooks
+# $wgDBtype should be checked to specifiy the proper file
+$wgExtNewTables = array(); // table, dir
+$wgExtNewFields = array(); // table, column, dir
+$wgExtNewIndexes = array(); // table, index, dir
+
 function rename_table( $from, $to, $patch ) {
 	global $wgDatabase;
 	if ( $wgDatabase->tableExists( $from ) ) {
@@ -97,18 +105,22 @@
 	}
 }
 
-function add_table( $name, $patch ) {
+function add_table( $name, $patch, $fullpath=false ) {
 	global $wgDatabase;
 	if ( $wgDatabase->tableExists( $name ) ) {
 		echo "...$name table already exists.\n";
 	} else {
 		echo "Creating $name table...";
-		dbsource( archive($patch), $wgDatabase );
+		if( $fullpath ) {
+			dbsource( $patch, $wgDatabase );
+		} else {
+			dbsource( archive($patch), $wgDatabase );
+		}
 		echo "ok\n";
 	}
 }
 
-function add_field( $table, $field, $patch ) {
+function add_field( $table, $field, $patch, $fullpath=false ) {
 	global $wgDatabase;
 	if ( !$wgDatabase->tableExists( $table ) ) {
 		echo "...$table table does not exist, skipping new field patch\n";
@@ -116,11 +128,30 @@
 		echo "...have $field field in $table table.\n";
 	} else {
 		echo "Adding $field field to table $table...";
-		dbsource( archive($patch) , $wgDatabase );
+		if( $fullpath ) {
+			dbsource( $patch, $wgDatabase );
+		} else {
+			dbsource( archive($patch), $wgDatabase );
+		}
 		echo "ok\n";
 	}
 }
 
+function add_index( $table, $index, $patch, $fullpath=false ) {
+	global $wgDatabase;
+	if( $wgDatabase->indexExists( $table, $index ) ) {
+		echo "...$index key already set.\n";
+	} else {
+		echo "Adding $index key... ";
+		if( $fullpath ) {
+			dbsource( $patch, $wgDatabase );
+		} else {
+			dbsource( archive($patch), $wgDatabase );
+		}
+		echo "ok\n";
+	}
+}
+
 function do_revision_updates() {
 	global $wgSoftwareRevision;
 	if ( $wgSoftwareRevision < 1001 ) {
@@ -884,6 +915,8 @@
 function do_all_updates( $shared = false, $purge = true ) {
 	global $wgNewTables, $wgNewFields, $wgRenamedTables, $wgSharedDB, $wgDatabase, $wgDBtype, $IP;
 
+	wfRunHooks('LoadExtensionSchemaUpdates');
+
 	$doUser = !$wgSharedDB || $shared;
 
 	if ($wgDBtype === 'postgres') {
@@ -909,6 +942,25 @@
 		}
 		flush();
 	}
+	
+	global $wgExtNewTables, $wgExtNewFields, $wgExtNewIndexes;
+	# Add missing extension tables
+	foreach ( $wgExtNewTables as $tableRecord ) {
+		add_table( $tableRecord[0], $tableRecord[1], true );
+		flush();
+	}
+	# Add missing extension fields
+	foreach ( $wgExtNewFields as $fieldRecord ) {
+		if ( $fieldRecord[0] != 'user' || $doUser ) {
+			add_field( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2], true );
+		}
+		flush();
+	}
+	# Add missing extension indexes
+	foreach ( $wgExtNewIndexes as $fieldRecord ) {
+		add_index( $fieldRecord[0], $fieldRecord[1], $fieldRecord[2], true );
+		flush();
+	}
 
 	# Do schema updates which require special handling
 	do_interwiki_update(); flush();
@@ -1415,6 +1467,37 @@
 	} else
 		echo "... archive.ar_deleted already exists\n";
 
+	global $wgExtNewTables, $wgExtNewFields, $wgExtNewIndexes;
+	# Add missing extension tables
+	foreach ( $wgExtNewTables as $nt ) {
+		if ($wgDatabase->tableExists($nt[0])) {
+			echo "... table $nt[0] already exists\n";
+			continue;
+		}
+
+		echo "... create table $nt[0]\n";
+		dbsource($nt[1]);
+	}
+	# Add missing extension fields
+	foreach ( $wgExtNewFields as $nc ) {
+		$fi = $wgDatabase->fieldInfo($nc[0], $nc[1]);
+		if (!is_null($fi)) {
+			echo "... column $nc[0].$nc[1] already exists\n";
+			continue;
+		}
+
+		echo "... add column $nc[0].$nc[1]\n";
+		$wgDatabase->query("ALTER TABLE $nc[0] ADD $nc[1] $nc[2]");
+	}
+	# Add missing extension indexes
+	foreach ( $wgExtNewIndexes as $ni ) {
+		if (pg_index_exists($ni[0], $ni[1])) {
+			echo "... index $ni[1] on $ni[0] already exists\n";
+			continue;
+		}
+		dbsource($ni[2]);
+	}
+
 	return;
 }
 
Index: trunk/extensions/CheckUser/CheckUser.php
===================================================================
--- trunk/extensions/CheckUser/CheckUser.php	(revision 23758)
+++ trunk/extensions/CheckUser/CheckUser.php	(revision 23759)
@@ -38,6 +38,7 @@
 $wgHooks['RecentChange_save'][] = 'efUpdateCheckUserData';
 $wgHooks['ParserTestTables'][] = 'efCheckUserParserTestTables';
 $wgHooks['LoginAuthenticateAudit'][] = 'efCheckUserRecordLogin';
+$wgHooks['LoadExtensionSchemaUpdates'][] = 'efCheckUserSchemaUpdates';
 
 /**
  * Hook function for RecentChange_save
@@ -261,6 +262,25 @@
 	return $name;
 }
 
+function efCheckUserSchemaUpdates() {
+	global $wgDBtype, $wgExtNewFields, $wgExtNewIndexes;
+	
+	# Run install.php
+	require( dirname(__FILE__) . '/install.php' );
+	
+	# FIXME: do postgres index changes!
+	if ($wgDBtype == 'mysql') {	
+		$wgExtNewFields[] = array('cu_changes', 
+			'cuc_cookie_user', dirname(__FILE__) . '/Archives/patch-cuc_cookie_user.sql');
+		$wgExtNewIndexes[] = array('cu_changes', 
+			'cuc_user_time', dirname(__FILE__) . '/Archives/patch-cu_changes_indexes.sql');
+	} else {
+		$wgExtNewFields[] = array('cu_changes', 
+			'cuc_cookie_user', dirname(__FILE__) . 'TEXT');
+	}
+	return true;
+}
+
 /**
  * Tell the parser test engine to create a stub cu_changes table,
  * or temporary pages won't save correctly during the test run.
Index: trunk/extensions/CheckUser/install.php
===================================================================
--- trunk/extensions/CheckUser/install.php	(revision 23758)
+++ trunk/extensions/CheckUser/install.php	(revision 23759)
@@ -10,23 +10,24 @@
 	
 $db =& wfGetDB( DB_MASTER );
 if ( $db->tableExists( 'cu_changes' ) && !isset( $options['force'] ) ) {
-	echo "cu_changes already exists\n";
-	exit( 1 );
+	echo "...cu_changes already exists.\n";
+} else {
+	$cutoff = isset( $options['cutoff'] ) ? wfTimestamp( TS_MW, $options['cutoff'] ) : null;
+	create_cu_changes( $db, $cutoff );
 }
 
-$cutoff = isset( $options['cutoff'] ) ? wfTimestamp( TS_MW, $options['cutoff'] ) : null;
-create_cu_changes( $db, $cutoff );
-
 function create_cu_changes( $db, $cutoff = null ) {
 	global $wgDBtype;
 	if( !$db->tableExists( 'cu_changes' ) ) {
 		$sourcefile = $wgDBtype === 'postgres' ? '/cu_changes.pg.sql' : '/cu_changes.sql';
 		$db->sourceFile( dirname( __FILE__ ) . $sourcefile );
 	}
+	
+	echo "...cu_changes table added.\n";
 	// Check if the table is empty
 	$rcRows = $db->selectField( 'recentchanges', 'COUNT(*)', false, __FUNCTION__ );
 	if ( !$rcRows ) {
-		echo "recent_changes is empty; done\n";
+		echo "recent_changes is empty; nothing to add.\n";
 		exit( 1 );
 	}
 	
@@ -76,6 +77,8 @@
 		wfWaitForSlaves( 5 );
 	}
 	$db->commit();
+	
+	echo "...cu_changes table added and populated.\n";
 }
 
 

Follow-up revisions

Rev.Commit summaryAuthorDate
r23912Merged revisions 23662-23909 via svnmerge from...david18:11, 9 July 2007

Status & tagging log

  • 15:19, 12 September 2011 Meno25 (Talk | contribs) changed the status of r23759 [removed: ok added: old]
Personal tools
Namespaces
Variants
Views
Actions
Site
Support
Download
Development
Communication
Toolbox