Read Class List
From Eckerd Academic Wiki
I use this program to load in a class list of students as users to the Wiki and assign them to a group. Before using this code you must
- Have the correct Wiki Extensions includes ( see here)
- Create the group in LocalSettings.php ( see here)
New Version MediaWiki 1.12.0
Installed software
Product Version MediaWiki 1.12.0 PHP 5.1.6 (apache2handler) MySQL 4.1.14
Extension functions
wfNamespacePermissions
Hooks
Hook name Subscribed by
userCan namespacePermissionsCheckNamespace
<?php /** * Main automatic wiki script extension to read a class list in * Written by S. Weppner, Bill Junkin, Eckerd College * EACH SEMESTER ONE MUST CHANGE TO PRESENT SEMESTER BY CHANGING $term * AND legend.txt * @package MediaWiki */ # This is run from the command line in an extensions directory /***************************************** BEGIN REQUIRED BY WIKI SOFTWARE (INIT) -- S. Weppner ******************************************/ echo 'Welcome!'; # Initialise common code $preIP = dirname( __FILE__ ); #gainst register_globals # This must be done before any globals are set by the code if ( ini_get( 'register_globals' ) ) { if ( isset( $_REQUEST['GLOBALS'] ) ) { die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>'); } $verboten = array( 'GLOBALS', '_SERVER', 'HTTP_SERVER_VARS', '_GET', 'HTTP_GET_VARS', '_POST', 'HTTP_POST_VARS', '_COOKIE', 'HTTP_COOKIE_VARS', '_FILES', 'HTTP_POST_FILES', '_ENV', 'HTTP_ENV_VARS', '_REQUEST', '_SESSION', 'HTTP_SESSION_VARS' ); foreach ( $_REQUEST as $name => $value ) { if( in_array( $name, $verboten ) ) { header( "HTTP/1.x 500 Internal Server Error" ); echo "register_globals security paranoia: trying to overwrite superglobals, aborting."; die( -1 ); } unset( $GLOBALS[$name] ); } } $wgRequestTime = microtime(true); # getrusage() does not exist on the Microsoft Windows platforms, catching this if ( function_exists ( 'getrusage' ) ) { $wgRUstart = getrusage(); } else { $wgRUstart = array(); } unset( $IP ); @ini_set( 'allow_url_fopen', 0 ); # For security # Valid web server entry point, enable includes. # Please don't move this line to includes/Defines.php. This line essentially # defines a valid entry point. If you put it in includes/Defines.php, then # any script that includes it becomes an entry point, thereby defeating # its purpose. define( 'MEDIAWIKI', true ); # Start profiler require_once( '../StartProfiler.php' ); # Load up some global defines. require_once( '../includes/Defines.php' ); require_once( '../LocalSettings.php' ); # Add stats require_once("../includes/SiteStats.php"); require_once( '../includes/Setup.php' ); $wgCommandLineMode =true; #add User require_once( "../includes/User.php" ); /***************************************** END REQUIRED BY WIKI SOFTWARE (INIT) -- S. Weppner ******************************************/ /*********************************************** This section reads in a formatted file classlist which in Eckerd Colleges Case is read from a banner database Lines are missing in this section because they are institutional dependent **********************************************************/ # Open a connection to BAnner database $output = 1;//Turn printing on or off. $output =0 turns printing off # Now read The list of classes found in the file legend.txt the form is CRN (tab) wikiName # file() loads the contents of classlist.txt into an array, $lines # each line in the file becomes a seperate element of the array. $lines = file('legend.txt'); #legend conains the CRN of the course and its Wiki Group # This defines the group name: $wikiName $groupexist=true; foreach($lines as $index=>$line){ list($crn,$wikiName) = preg_split("/\D/",trim($line),2); $crn = trim($crn); $wikiName = trim($wikiName); print "\n$crn: $wikiName\n"; if($output){ echo "\nName of group: " . htmlspecialchars($wikiName) . "\n";} #Get all students in the class from Banner # Get $rrealname (first and last name) , $nname (email name) from the Database using the CRN # # The students log into the wiki using their Eckerd email username and password # using an LDAP server /************************ End Institutional Dependent Section **********************************/ **********************************/ # add each student to the Wiki using Internal Wiki commands $u = User::newFromName($nname); $testuser = true; if ( 0 != $u->idForName() ) { echo "A user already exists, only adding group\n"; $testuser= false; }//End of if user aleady exists if ($testuser&&$groupexist){ # Update user count $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); $ssUpdate->doUpdate(); #This section adds to the SQL Wiki database through higher level # Wiki functions - S. Weppner $u->addToDatabase(); $np = $u->randomPassword(); $u->setPassword( $np ); $u->setEmail( $eemail ); $u->setRealName( $rrealnname ); $u->setToken(); global $wgAuth; $wgAuth->initUser( $u ); $u->setOption( 'rememberpassword', 1 ); $wgUser = $u; $wgUser->saveSettings(); wfRunHooks( 'AddNewAccount' ); }//Ebd of if testusers or groupexists # NOW ADD GROUP if ($groupexist){ $oldGroups = $u->getGroups(); $newGroups = $oldGroups; // add groups $newGroups[] = $wikiName; $u->addGroup( $wikiName ); $newGroups = array_unique( $newGroups ); if ($newGroups != $oldGroups ) { wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) ); wfDebug( 'newGroups: ' . print_r( $newGroups, true ) ); wfRunHooks( 'UserRights', array( &$u, $wikiName, '' ) ); } else{echo 'Group already exists';} } //End of if group exists }//End of if to check that the $nname variable exists }//End of loop over students in once class echo "\n"; if (!$groupexist) {echo 'Group does not exist, no users added';} }//End of loop over classes echo "\nBYE!!!\n"; ?>
Older Version MediWiki 1.6.8
I run this program from a browser, it reads in a file classlist.txt which has as a format
- first line: Group to add class to (for example PH241-weppner)
- remaining N lines: information (name, email address) of all N students which needs to be modified for your institution's format.
It is rather robust, it will only add a student if new, it will only add a student to a group which exists, it will not add student if student already belongs to group so it can be run many times if there are formatting problems in file.
It has been tested on this version:
- MediaWiki: 1.6.8
- PHP: 4.3.9 (apache2handler)
- MySQL: 4.1.14-log
- Extensions:
o Extension functions: + wfNamespacePermissions
- Hooks:
o userCan: namespacePermissionsCheckNamespace
Feel free to contact me with questions--Weppnesp 22:35, 17 February 2007 (EST)
<nowiki> <?php /** * Main wiki script extension to read a class list in * Written by S. Weppner, Eckerd College * @package MediaWiki */ /***************************************** BEGIN REQUIRED BY WIKI SOFTWARE (INIT) -- S. Weppner ******************************************/ $wgRequestTime = microtime(); # This is the beginning of input.php to load the Wiki Environment # getrusage() does not exist on the Microsoft Windows platforms, catching this if ( function_exists ( 'getrusage' ) ) { $wgRUstart = getrusage(); } else { $wgRUstart = array(); } unset( $IP ); @ini_set( 'allow_url_fopen', 0 ); # For security... if ( isset( $_REQUEST['GLOBALS'] ) ) { die( '<a href="http://www.hardened-php.net/index.76.html">$GLOBALS overwrite vulnerability</a>'); } # Valid web server entry point, enable includes. # Please don't move this line to includes/Defines.php. This line essentially # defines a valid entry point. If you put it in includes/Defines.php, then # any script that includes it becomes an entry point, thereby defeating # its purpose. define( 'MEDIAWIKI', true ); ## The location of this file has to be one level off the Main Wiki directory -- S. Weppner # Load up some global defines. require_once( '../includes/Defines.php' ); # Include this site setttings require_once( '../LocalSettings.php' ); # Prepare MediaWiki require_once( '../includes/Setup.php' ); # Initialize MediaWiki base class require_once( "../includes/Wiki.php" ); $mediaWiki = new MediaWiki(); # bAdd Groups require_once( "../includes/Group.php" ); # Add stats require_once("../includes/SiteStatsUpdate.php"); wfProfileIn( 'main-misc-setup' ); OutputPage::setEncodings(); # Not really used yet # Query string fields $action = $wgRequest->getVal( 'action', 'view' ); $title = $wgRequest->getVal( 'title' ); $wgTitle = NULL; unset( $wgTitle ); wfProfileOut( 'main-misc-setup' ); # Setting global variables in mediaWiki $mediaWiki->setVal( 'Server', $wgServer ); $mediaWiki->setVal( 'DisableInternalSearch', $wgDisableInternalSearch ); $mediaWiki->setVal( 'action', $action ); $mediaWiki->setVal( 'SquidMaxage', $wgSquidMaxage ); $mediaWiki->setVal( 'EnableDublinCoreRdf', $wgEnableDublinCoreRdf ); $mediaWiki->setVal( 'EnableCreativeCommonsRdf', $wgEnableCreativeCommonsRdf ); $mediaWiki->setVal( 'CommandLineMode', $wgCommandLineMode ); $mediaWiki->setVal( 'UseExternalEditor', $wgUseExternalEditor ); $mediaWiki->setVal( 'DisabledActions', $wgDisabledActions ); /***************************************** END REQUIRED BY WIKI SOFTWARE (INIT) -- S. Weppner ******************************************/ /*********************************************** This section needs to read in a formatted file classlist and saved in classlist.txt. The group name that the course belongs too has been added as the first line of the file. If the student already exists then the group is added. If both student and group already exist then nothing is added. **********************************************************/ # Now read The Class File with a default name # file() loads the contents of classlist.txt into an array, $lines # each line in the file becomes a seperate element of the array. $lines = file('classlist.txt'); ### Change below # First line contains the name of the group the users should be added to (their class name) # The Group must already have been created within the Wiki (Localsettings.php). # If it does not exist nothing happens $GG= new Group(); $groupexist=false; foreach($lines as $index=>$line){ if ($index==0){ echo '<br />'; echo 'Name of group: "' . htmlspecialchars($line) . '"<br />'; echo '<br />'; $ssize= strlen($line); echo 'SIZE:'.$ssize; $addgroup = substr($line,0,ssize-1); echo 'Name of group: "' . $addgroup . '"<br />'; $groupexist=true; if ( $GG->getId() != 0 ) {$groupexist=true;} } ### change above else { # This section is specific to the file setup you have. The file should contain # the group name on the first line then student information on the remaining lines. # Students first and last name and email address (which becomes the user name before the @ sign, # each student has its own line of information. With N students there should be N+1 lines, # the extra line is the first one which is the group name (see above). # variables which need to be set are: # (1) nname -- username # (2) rrealname --users full real name (first and last) # (3) ppassword --users password, set to a bogus value # (4) eemail -- users email # For password what Eckerd College does is assign a "bogus" password which is later # changed (see below) to a random password using $np = $u->randomPassword(); $u->setNewpassword( $np ); # a random letter and number generator, which is then encrypted by the MediaWiki software # This password is actually never known or used by anyone. # At Eckerd the function confirmUser (in confirmUser.php) was rewritten to connect to our LDAP server # which tests if user, when logging in, is using their default Eckerd College email password. # You could, alternately, modify the code below to email the user this random password # by using the function addNewAccountMailPassword() which is found in SpecialUserlogin.php ## RIGHT NOW A STATIC READ IN THIS SECTION, GOAL TO MAKE DYNAMIC ### INSTITUTION SPECIFIC BEGIN echo '<br />'; echo 'Next User in the file: "' . htmlspecialchars($line) . '"<br />'; echo $index; echo '<br />'; $pos1 = strpos($line,','); $pos2 = strpos($line,' ',$pos1+2); $pos3 = strpos($line,'@'); $pos3a = strpos($line,'Box',$pos3-19); $pos4 = strpos($line,' ',$pos3a+4); $nname = substr($line,$pos4+2,$pos3-$pos4-2); $rrealnname = substr($line,$pos1+2,$pos2-$pos1-1).substr($line,0,$pos1); $ppassword = '#bogus#'; // See above, you need to create a password which will be changed later $eemail = $nname.'@eckerd.edu'; echo $nname.' '; echo $rrealnname; echo ' '.$ppassword; echo '<br />'; ### INSTITUTION SPECIFIC END $u = User::newFromName($nname); $testuser = true; if ( 0 != $u->idForName() ) { echo 'A user already exists, only adding group<br/>'; $testuser= false; } ## This section below adds to the MYSQL Database the USER if needed ( a new user) if ($testuser&&$groupexist){ # Update user count $ssUpdate = new SiteStatsUpdate( 0, 0, 0, 0, 1 ); $ssUpdate->doUpdate(); #This section adds to the SQL Wiki database through higher level # Wiki functions - S. Weppner $u->addToDatabase(); $u->setPassword( $ppassword ); $np = $u->randomPassword(); //Here Password is changed! $u->setNewpassword( $np ); $u->setEmail( $eemail ); $u->setRealName( $rrealnname ); $u->setToken(); global $wgAuth; $wgAuth->initUser( $u ); $wgUser = $u; $wgUser->saveSettings(); wfRunHooks( 'AddNewAccount' ); } # NOW ADD GROUP DESCRIPTION to the USER if ($groupexist){ $oldGroups = $u->getGroups(); $newGroups = $oldGroups; // add groups $newGroups = array_merge($newGroups, $addgroup); $u->addGroup( $addgroup ); $newGroups = array_unique( $newGroups ); if ($newGroups != $oldGroups ) { wfDebug( 'oldGroups: ' . print_r( $oldGroups, true ) ); wfDebug( 'newGroups: ' . print_r( $newGroups, true ) ); wfRunHooks( 'UserRights', array( &$u, $addgroup, '' ) ); } } } } if (!$groupexist) {echo 'Group does not exist, no users added';} ## DONE READING IN INFORMATION /***************************************** BEGIN REQUIRED BY WIKI SOFTWARE (CLOSE) -- S. Weppner ******************************************/ $mediaWiki->finalCleanup ( $wgDeferredUpdateList, $wgLoadBalancer, $wgOut ); $mediaWiki->doUpdates( $wgPostCommitUpdateList ); $mediaWiki->restInPeace( $wgLoadBalancer ); /***************************************** END REQUIRED BY WIKI SOFTWARE (CLOSE) -- S. Weppner ******************************************/ ?> </nowiki></pre>
