FANDOM


MediaWiki extensions manual - list
UserExport

Release status: beta

Implementation Special page, Data extraction
Description Export Mediawiki user name and email to a CSV file
Author(s) Rodrigo Sampaio Primo (RodrigoSampaioPrimoTalk)
Last version 1.0 (2009-08-05)
MediaWiki 1.14+
License GPL
Download see here
Parameters

$wgUserExportProtectedGroups

Added rights

userexport

Check usage and version matrix

User Export is a very simple extension to generate a CSV file with the user name and email of all Mediawiki users. It can be easily extended to support more user fields. It creates a new Special Page called UserExport where you can generate the CSV file.

Installation Edit

  1. Create a new folder in the following location:
    mediawiki-install-folder/extensions/userexport
  2. Take the code, create the respective files and put the files to the recently created folder
  3. Add the following code at the bottom of your LocalSettings.php
require_once( "$IP/extensions/userexport/UserExport.php" );

Usage Edit

  1. Follow Installation Instructions
  2. You will have a new restricted Special Page called Special:UserExport with the title User Export.
  3. This page has only a button to generate the CSV file. Click on the button and download the file.

Code Edit

UserExport.php Edit

<?php
/** \file
* \brief Contains setup code for the User Export Extension.
*/
 
# Not a valid entry point, skip unless MEDIAWIKI is defined
if (!defined('MEDIAWIKI')) {
    echo "User Export extension";
    exit(1);
}
 
$wgExtensionCredits['specialpage'][] = array(
    'path'           => __FILE__,
    'name'           => 'User Export',
    'version'        => '1.0',
    'url'            => 'https://www.mediawiki.org/wiki/Extension:UserExport',
    'author'         => 'Rodrigo Sampaio Primo',
    'descriptionmsg' => 'userexport-desc',
);
 
$wgAvailableRights[] = 'userexport';
$wgGroupPermissions['bureaucrat']['userexport'] = true;
 
$dir = dirname(__FILE__) . '/';
$wgAutoloadClasses['UserExport'] = $dir . 'UserExport.body.php';
$wgExtensionMessagesFiles['UserExport'] = $dir . 'UserExport.i18n.php';
$wgExtensionAliasesFiles['UserExport'] = $dir . 'UserExport.i18n.alias.php';
 
$wgSpecialPages['UserExport'] = 'UserExport';
$wgSpecialPageGroups['UserExport'] = 'users';
$wgUserExportProtectedGroups = array( "sysop" );
 
# Add a new log type
$wgLogTypes[]                         = 'userexport';
$wgLogNames['userexport']              = 'userexport-logpage';
$wgLogHeaders['userexport']            = 'userexport-logpagetext';
$wgLogActions['userexport/exportuser']  = 'userexport-success-log';

UserExport.body.php Edit

<?php
/** \file
* \brief Contains code for the UserExport Class (extends SpecialPage).
*/
 
///Special page class for the User Export extension
/**
 * Special page that allows sysops to export the username and
 * user email to a CSV file
 * 
 * @addtogroup Extensions
 * @author Rodrigo Sampaio Primo <rodrigo@utopia.org.br>
 */
class UserExport extends SpecialPage {
	function __construct() {
		parent::__construct( 'UserExport', 'userexport' );
	}
 
	function execute( $par ) {
		global $wgRequest, $wgOut, $wgUser;
 
		wfLoadExtensionMessages( 'UserExport' );
 
		$this->setHeaders();
 
		if ( !$wgUser->isAllowed( 'userexport' ) ) {
			$wgOut->permissionRequired( 'userexport' );
			return;
		}
 
        if ( $wgRequest->getText( 'exportusers' ) ) {
            if ( !$wgUser->matchEditToken( $wgRequest->getVal( 'token' ) ) ) {
                // bad edit token
                $wgOut->addHtml( "<span style=\"color: red;\">" . wfMsg( 'userexport-badtoken' ) . "</span><br />\n" );
            } else {
                $this->exportUsers();
            }
        }
 
        $wgOut->addHTML(
            Xml::openElement('p') .
            wfMsg( 'userexport-description' ) .
            Xml::closeElement('p') . 
            Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getTitle()->getLocalUrl(), 'id' => 'userexportform' ) ) .
            Xml::submitButton( wfMsg( 'userexport-submit' ) ) .
			Html::hidden( 'token', $wgUser->editToken() ) .
	    	Html::hidden( 'exportusers', true ) .
		    Xml::closeElement( 'form' ) . "\n"
		);
	}
 
	/**
     * Function to query the database and generate the CVS file
     *
	 * @return Always returns true - throws exceptions on failure.
	 */
    private function exportUsers()
    {
        $filePath = tempnam(sys_get_temp_dir(), '');
        $file = fopen($filePath, 'w');
 
        $db = wfGetDB( DB_MASTER );
        $users = $db->select('user', array('user_name', 'user_email'));
 
        fputcsv($file, array('login', 'email'));
 
        while ( $user = $db->fetchObject( $users ) ) {
            fputcsv($file, array($user->user_name, $user->user_email));
        }
 
        fclose($file);
 
        header("Pragma:  no-cache");
        header("Expires: 0");
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-type: text/csv");
        header("Content-Transfer-Encoding: binary"); 
        header("Content-Disposition: attachment; filename=\"mediawiki_users.csv\"");
        header("Content-Length: " . filesize($filePath));  
        header("Accept-Ranges: bytes");  
 
        readfile($filePath);
        unlink($filePath);
        die;
    }
}

UserExport.i18n.php Edit

<?php
/**
 * Internationalisation file for the User Export Extension.
 *
 * @addtogroup Extensions
 */
 
$messages = array();
 
$messages['en'] = array(
    'userexport'                     => 'User Export',
    'userexport-submit'              => 'Export users to CSV file',
    'userexport-badtoken' 		     => 'Invalid edit token',
    'userexport-description'         => 'This is a very simple extension to export Mediawiki users to a CSV file.<br />It only export the user name and user email, but it can be easily extended to support more user fields.',
    'userexport-desc'         		 => 'Export the Mediawiki users to a CSV file',
);
 
$messages['de'] = array(
    'userexport'                     => 'Benutzerdatenexport',
    'userexport-submit'              => 'Benutzerdaten exportieren',
    'userexport-badtoken' 		     => 'Der Bearbeitungstoken ist ungültig.',
    'userexport-description'         => 'Dies ist eine einfache Programmerweiterung mit der man die Benutzerdaten (Name und E-Mail-Adresse) der auf dem Wiki registrierten Benutzer in eine CSV-Datei exportieren kann.',
    'userexport-desc'        		 => 'Ergänzt eine [[Spezial:UserExport|Spezialseite]] zum Exportieren von Benutzerdaten im CSV-Format',
);


UserExport.i18n.alias.php Edit

<?php
/**
 * Aliases for the special page UserExport
 *
 */
 
$aliases = array();
 
$aliases['en'] = array(
	'userexport' => array( 'userexport' ),
);
 
$aliases['de'] = array(
	'userexport' => array( 'Benutzerdatenexport' ),
);

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.