User:Robchurch/Reset user skin preference

From mediawiki.org
<?php

/**
 * Maintenance script to reset all users' skin preferences to a specified default
 *
 * @package MediaWiki
 * @subpackage Maintenance
 * @author Rob Church <robchur@gmail.com>
 */
 
require_once( 'commandLine.inc' );
echo( "Reset User Skins\n\n" );

if( count( $args ) > 0 ) {

	$fname = 'resetUserSkins';
	$skin = trim( $args[0] );

	$dbw =& wfGetDB( DB_MASTER );
	$dbw->begin();
	$res = $dbw->select( 'user', array( 'user_id', 'user_options' ), array(), $fname );
	if( $res && $dbw->numRows( $res ) > 0 ) {
	
		echo( $dbw->numRows( $res ) . " records to process. Updating..." );
		while( $row = $dbw->fetchObject( $res ) ) {
			$prefs = explode( "\n", $row->user_options );
			for( $i = 0; $i < count( $prefs ); $i++ ) {
				if( preg_match( '/^skin=(.*)$/', $prefs[$i] ) )
					$prefs[$i] = 'skin=' . $skin;
			}
			$values['user_options'] = implode( "\n", $prefs );
			$values['user_touched'] = $dbw->timestamp();
			$dbw->update( 'user', $values, array( 'user_id' => $row->user_id ), $fname );
		}
		
		$dbw->commit();
		echo( "done.\n\n" );
		exit( 0 );
	
	} else {
		$dbw->commit();
		echo( "No users found.\n\n" );
		exit( 0 );
	}

} else {
	showUsage( 'Please provide the internal name of a skin to use.' );
	exit( 1 );
}

function showUsage( $error = false ) {
	if( $error )
		echo( "{$error}\n\n" );
	echo( "Resets all user skin preferences to a specified value. Useful for deploying\n" );
	echo( "a standard/corporate skin selection.\n\n" );
	echo( "\tUSAGE: php resetUserSkins.php <skin>\n" );
	echo( "\t<skin> : Internal name of the skin to use\n\n" );
}
 
?>