Robert Vogel has uploaded a new change for review. https://gerrit.wikimedia.org/r/247520
Change subject: maintenance: Added user im- and export scripts ...................................................................... maintenance: Added user im- and export scripts There are many cases where customers want to pre populate the user database of a new wiki. E.g. when migrating from another system. Here are two scripts that can ease the process. Needs merge to REL1_23 Change-Id: Id5069939902e59a936bbc24d5e83df67ae7d69a4 --- A maintenance/BSExportUsers.php A maintenance/BSImportUsers.php 2 files changed, 179 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceFoundation refs/changes/20/247520/1 diff --git a/maintenance/BSExportUsers.php b/maintenance/BSExportUsers.php new file mode 100644 index 0000000..3cbcc9f --- /dev/null +++ b/maintenance/BSExportUsers.php @@ -0,0 +1,59 @@ +<?php + +class BSExportUsers extends BSMaintenance { + public function execute() { + $oDOM = new DOMDocument(); + $oDOM->formatOutput = true; + $oUsersNode = $oDOM->createElement( 'users' ); + $oDOM->appendChild( $oUsersNode ); + + $dbr = $this->getDB( DB_SLAVE ); + $res = $dbr->select( 'user', '*' ); + + foreach( $res as $row ) { + $oUserNode = $oDOM->createElement( 'user' ); + $oUsersNode->appendChild( $oUserNode ); + + $oUserNode->appendChild( $oDOM->createElement( 'name', $row->user_name ) ); + $oUserNode->appendChild( $oDOM->createElement( 'id', $row->user_id ) ); + $oUserNode->appendChild( $oDOM->createElement( 'realname', $row->user_real_name ) ); + $oUserNode->appendChild( $oDOM->createElement( 'email', $row->user_email ) ); + $oUserNode->appendChild( $oDOM->createElement( 'touched', wfTimestamp( TS_ISO_8601, $row->user_touched ) ) ); + $oUserNode->appendChild( $oDOM->createElement( 'registration', wfTimestamp( TS_ISO_8601, $row->user_registration ) ) ); + $oUserNode->appendChild( $oDOM->createElement( 'editcount', $row->user_editcount ) ); + + $res2 = $dbr->select( 'user_groups', '*', array( 'ug_user' => $row->user_id ) ); + if( $dbr->numRows( $res2 ) > 0 ) { + $oGroupsNode = $oDOM->createElement( 'groups' ); + $oUserNode->appendChild( $oGroupsNode ); + foreach( $res2 as $row2 ) { + $oGroupNode = $oDOM->createElement( 'group' ); + $oGroupNode->setAttribute( 'name', $row2->ug_group ); + $oGroupsNode->appendChild( $oGroupNode ); + } + } + + $res3 = $dbr->select( 'user_properties', '*', array( 'up_user' => $row->user_id ) ); + if( $dbr->numRows( $res3 ) > 0 ) { + $oPropertiesNode = $oDOM->createElement( 'properties' ); + $oUserNode->appendChild( $oPropertiesNode ); + foreach( $res3 as $row3 ) { + $oPropertyNode = $oDOM->createElement( 'property' ); + $oPropertyNode->setAttribute( 'name', $row3->up_property ); + $oPropertyNode->setAttribute( 'value', $row3->up_value ); + $oPropertiesNode->appendChild( $oPropertyNode ); + } + } + } + + $this->output( $oDOM->saveXML() ); + } + +} + +$maintClass = 'BSExportUsers'; +if (defined('RUN_MAINTENANCE_IF_MAIN')) { + require_once( RUN_MAINTENANCE_IF_MAIN ); +} else { + require_once( DO_MAINTENANCE ); # Make this work on versions before 1.17 +} \ No newline at end of file diff --git a/maintenance/BSImportUsers.php b/maintenance/BSImportUsers.php new file mode 100644 index 0000000..2f46675 --- /dev/null +++ b/maintenance/BSImportUsers.php @@ -0,0 +1,120 @@ +<?php + +require_once( 'BSMaintenance.php' ); + +class BSImportUsers extends BSMaintenance { + public function __construct() { + $this->addOption('src', 'The path to the source file', true, true); + $this->addOption('defaultpw', 'A password that should be set for any new user', false, true); + $this->addOption('createuserpage', 'Wether or not a user page should be created (<userpage> element needs to be available)', false, false); + + parent::__construct(); + } + + public function execute() { + $oDOM = new DOMDocument(); + $oDOM->load( $this->getOption( 'src' ) ); + $oDOM->recover = true; + + $oUserNodes = $oDOM->getElementsByTagName( 'user' ); + foreach ( $oUserNodes as $oUserNode ) { + $sUserName = $this->getChildNodeValue( $oUserNode, 'name' ); + $oUser = User::newFromName( $sUserName ); + if( $oUser instanceof User === false ) { + $this->output( $sUserName.' is not a valid username' ); + continue; + } + + if( $oUser->getId() !== 0 ) { + $this->output( $oUser->getName().'already exists. UserID: '.$oUser->getId() ); + $this->output( 'Skipping!' ); //TODO: make optional + continue; + } + + $sUserRealName = $this->getChildNodeValue( $oUserNode, 'realname' ); + if( !empty($sUserRealName ) ) { + $oUser->setRealName( $sUserRealName ); + } + + $sUserEmail = $this->getChildNodeValue( $oUserNode, 'email' ); + if( !empty($sUserEmail ) ) { + $oUser->setEmail( $sUserEmail ); + } + + if( !empty ( $this->getOption( 'defaultpw', '' ) ) ) { + $oUser->setPassword( $this->getOption( 'defaultpw', '' ) ); + } + + //TODO: maybe write 'touched', 'registration', etc. directly to DB? + + $oProperties = $oUserNode->getElementsByTagName('property'); + foreach( $oProperties as $oProperty ) { + $oUser->setOption( + $oProperty->getAttribute( 'name' ), + $oProperty->getAttribute( 'value' ) + ); + } + + $oStatus = $oUser->addToDatabase(); + if( $oStatus->isOK() ) { + $this->output( $oUser->getName().' successfully added to database. UserID: '.$oUser->getId() ); + } + else { + $this->error( $oUser->getName().' could not be added to database. Message '.$oStatus->getMessage()->plain() ); + continue; + } + + $oGroups = $oUserNode->getElementsByTagName('group'); + foreach( $oGroups as $oGroup ) { + $oUser->addGroup( $oGroup->getAttribute( 'name' ) ); + } + + if( $this->getOption( 'createuserpage', false ) ) { + if( $oUser->getUserPage()->exists() ) { + continue; + } + $sContent = $this->getChildNodeValue( $oUserNode, 'userpage' ); + if( empty( $sContent ) ) { + continue; + } + + $oContent = ContentHandler::makeContent( + $sContent, + $oUser->getUserPage() + ); + + $oWikiPage = WikiPage::factory( $oUser->getUserPage() ); + $oEditStatus = $oWikiPage->doEditContent( $oContent, __CLASS__ ); + if( $oEditStatus->isOK() ) { + $this->output( 'Page '.$oUser->getUserPage()->getPrefixedText().' successfully created.' ); + } + else { + $this->error( 'Page '.$oUser->getUserPage()->getPrefixedText().' could not be created. Message: '.$oEditStatus->getMessage()->plain() ); + } + } + } + } + + /** + * + * @param DOMElement $oNode + * @param string $param1 + * @return string + */ + public function getChildNodeValue( $oNode, $sChildNodeName ) { + $oChildNode = $oNode->getElementsByTagName( $sChildNodeName )->item( 0 ); + if( $oChildNode instanceof DOMElement === false ) { + return ''; + } + + return $oChildNode->nodeValue; + } + +} + +$maintClass = 'BSImportUsers'; +if (defined('RUN_MAINTENANCE_IF_MAIN')) { + require_once( RUN_MAINTENANCE_IF_MAIN ); +} else { + require_once( DO_MAINTENANCE ); # Make this work on versions before 1.17 +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/247520 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Id5069939902e59a936bbc24d5e83df67ae7d69a4 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation Gerrit-Branch: master Gerrit-Owner: Robert Vogel <vo...@hallowelt.biz> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits