tags:

views:

935

answers:

3

I'm trying to write a script that will create a user in MediaWiki, so that I can run a batch job to import a series of users.

I'm using mediawiki-1.12.0.

I got this code from a forum, but it doesn't look like it works with 1.12 (it's for 1.13)

$name = 'Username'; #Username (MUST start with a capital letter)
$pass = 'password'; #Password (plaintext, will be hashed later down)
$email = 'email';   #Email (automatically gets confirmed after the creation process)
$path = "/path/to/mediawiki";
putenv( "MW_INSTALL_PATH={$path}" );
require_once( "{$path}/includes/WebStart.php" );
$pass = User::crypt( $pass );
$user = User::createNew( $name, array( 'password' => $pass, 'email' => $email ) );
$user->confirmEmail();
$user->saveSettings();  
$ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
$ssUpdate->doUpdate();

Thanks!

+2  A: 

There's a createAndPromote script in maintenance/ which creates user accounts and grants administrator rights. You could adapt this to remove the permissions portion.

Alternatively, you might take a look at the ImportUsers extension.

Rob
A: 

Same problem here, I use the same piece of code and get the following error:

Fatal error: Call to a member function incr() on a non-object in /path2wiki/includes/GlobalFunctions.php on line 1709

Here the code that matters:

/** * Increment a statistics counter */ function wfIncrStats( $key ) { global $wgMemc; $key = wfMemcKey( 'stats', $key ); if ( is_null( $wgMemc->incr( $key ) ) ) { <<<<< Problem with $wgMemc $wgMemc->add( $key, 1 ); } }

I cannot find any line with "new" and "wgMemc" (case insensitive) in the source, is there a way to hack this code to make it work as expected ?

neck
A: 

I used this on Mediawiki 1.7, and it worked well for me:

#!/usr/bin/php
## Add a user to Mediawiki

<?php

    $domain = 'example.com';
    $mwpath = '/docs/www-wiki';

    if ($argc < 3) {
        die("Missing arguments.\n"
           ."Usage: $0 USER PASSWORD\n");
    }
    $user = $argv[1];
    $pass = $argv[2];

    print "Add user $user with password $pass [y/N]?\n";
    $ans = fgets(STDIN,256);
    if (!  preg_match('/^[yY]/', $ans) ) {
        print "Canceled.\n";
        exit;
    }

    $user = ucfirst(strtolower($user)); // maybe unneeded, because handled in MW functions?


    # Adapted from http://www.mwusers.com/forums/showthread.php?9788-Create-new-user-in-database&amp;p=42931&amp;viewfull=1#post42931

    $path = $mwpath;
    putenv("MW_INSTALL_PATH={$path}");

    #require_once ("{$path}/includes/WebStart.php"); // for version >= 1.14 ?

    # My version 1.7 doesn't have WebStart.php.
    # It seems to work by including the following lines found in index.php
    # Some are probably not needed, but I don't want to do more testing
    define( 'MEDIAWIKI', true );
    require_once( './includes/Defines.php' );
    require_once( './LocalSettings.php' );
    require_once( 'includes/Setup.php' );
    require_once( "includes/Wiki.php" );
    $mediaWiki = new MediaWiki();


    $mwuser=User::newFromName($user);

    if (! is_object($mwuser)) {
        die("Invalid user!\n");
    }

    $mwuser->addToDatabase(); // don't we need a return value to check?
    $mwuser->setPassword( $pass );
    $mwuser->setEmail( strtolower($user) . '@' . $domain );
    $mwuser->confirmEmail();

    #$mwuser->setRealName( $_POST["nome"] );
    #$mwuser->addGroup($_POST["grupo"]);

    $mwuser->saveSettings(); // no return value?
    $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 );
    $ssUpdate->doUpdate();
?>

I guess your problem was also the use of WebStart.php in your script, which didn't exist in your Mediawiki version.

mivk