User:SVG/Maintenance scripts/removeRedundantUserGroup.php

From MediaWiki.org
Jump to navigation Jump to search

This maintenance script can be executed in commandline. It allows removing an user group from members of another group or just removing all members from this group. The actions will be logged in user rights log (Special:Log/rights). This script is general usable.

php removeRedundantUserGroup.php
> -u "User who is logged in rights log as executive user"
> -g "Group that should be removed"
> -m "Just remove group -g from members of this group (-m). Leave it empty if you want to remove all members from group -g"
> -r "Reason that is shown in rights log. Leave it empty if you don't want to reasoning your action"

It could look like:

cd /var/www/wiki/w/maintenance
php removeRedundantUserGroup.php -u "Maintenance script" -g patroller -m sysop -r "rights already included in administrator"
<?php
/**
* Maintenance script removeRedundantUserGroup.php
*
* @package MediaWiki
* @subpackage Maintenance scripts
*
* @author: Tim 'SVG' Weyer <SVG@Wikiunity.com>
*
* @copyright Copyright (C) 2012 Tim Weyer, Wikiunity
* @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
*
* @ingroup Maintenance
*/

require_once( dirname( __FILE__ ) . '/Maintenance.php' );

class RemoveRedundantUserGroup extends Maintenance {

	public function __construct() {
		parent::__construct();
		$this->mDescription = "Remove an user group from members of another group or just remove all current members from this group with rights log entries";
		$this->addOption( 'u', "User to perform right changes", false, true );
		$this->addOption( 'g', "Group to remove", false, true );
		$this->addOption( 'm', "Remove group -g from members of another group?", false, true );
		$this->addOption( 'r', "Reason to remove membership", false, true );
	}

	public function execute() {

		$dbr = wfGetDB( DB_MASTER );
		$res = $dbr->select( 'user_groups',
			array( 'ug_user' ),
			array( 'ug_group' => $this->getOption( 'm', $this->getOption( 'g', 0 ) ) ),
			 __METHOD__ );

		$selectedusers = array();
		foreach ( $res as $row ) {
			if ( $row->ug_user != 0 ) {
				$selectedusers[$row->ug_user] = User::newFromId( $row->ug_user );
			}
		}

		foreach ( $selectedusers as $selecteduser ) {
			if ( in_array( $this->getOption( 'g', 0 ), $selecteduser->getGroups() ) ) {
				$oldgroups = $selecteduser->getGroups();

				$selecteduser->removeGroup( $this->getOption( 'g', 0 ) );

				// log it
				$log = new LogPage( 'rights' );

				asort( $oldgroups );
				$newgroups = $selecteduser->getGroups();
				asort( $newgroups );

				$log->addEntry( 'rights',
								$selecteduser->getUserPage(),
								$this->getOption( 'r', '' ),
								array(
									implode( ', ', $oldgroups ),
									implode( ', ', $newgroups ) ? implode( ', ', $newgroups ) : ''
								),
								User::newFromName( $this->getOption( 'u', 'Maintenance script' ) )
							);
			}
		}

	}

}

$maintClass = "RemoveRedundantUserGroup";
require_once( RUN_MAINTENANCE_IF_MAIN );