User:Leucosticte/MPushB

 * version 1.0.1 * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along * with this program; if not, write to the Free Software Foundation, Inc., * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * http://www.gnu.org/copyleft/gpl.html */

// "q" (queue) Three options: -qrev, qus // "r" (repeat) Three options: -ro (onetime), -rd (continuous, using defaults), // r $usage = 'Usage: php mirrorpushbot.php -q' . '[-r]' . "\n"; $options = getopt( 'q:r:'); $allowableOptions['q'] = array( 'rc', 'rev', 'us', 'all' ); $allowableOptions['r'] = array( 'o', 'd', ); if ( !isset ( $options['q'] ) ) {     $options['q'] = 'all'; } if ( !in_array ( $options[ 'q' ], $allowableOptions['q'] ) ) {      die ( $usage ); } if ( !isset ( $options['r'] ) ) {      $options['r'] = 'd'; // Default to continuous, using defaults } elseif ( !in_array ( $options[ 'r' ], $allowableOptions['r'] ) ) {      if ( !is_int ( $options['r'] ) ) { // Microseconds option            die ( $usage );      } else {            $sleepMicroseconds = $options['r'];      } }

/* Set up bot classes. */ include('botclasses.php'); // Get the passwords $passwordFile = 'inclubot_passwords.php'; if ( !file_exists ( $passwordFile ) ) { die ( "File $passwordFile does not exist\n" ); } include( 'inclubot_passwords.php' ); // Get the defaults $defaultsFile = 'inclubot_defaults.php'; if ( !file_exists ( $defaultsFile ) ) { die ( "File $defaultsFile does not exist\n" ); } include( 'inclubot_defaults.php' ); $wiki     = new wikipedia; $wiki->url = $localWikiUrl;

// Database $db = new mysqli( $host, $dbUser, $dbPass ); if ( !$db ) { die( 'Could not connect: ' . mysql_error); } $db->select_db ( "$dbName" ); if ( !$db ) { die( "Could not select $dbName" ); } $wiki->login( $pushUser, $pushPass ); $token = urlencode ( $wiki->getedittoken );

if ( !$mbRet ) { die ( "No unpushed edits with text were found\n" ); } $keepGoing = true; while ( $keepGoing ) { $mbRet = $db->query( "SELECT * FROM mb_rc_queue WHERE mbrcq_push_result=''" ); $row = $mbRet->fetch_assoc; if ( $row['mbrcq_rc_logtype'] == 'newusers' ) { $pushMapping = getNewusersPushMapping( $row ); $pushMapping = array(                 'pageid' => 'mbrcq_rc_cur_id',                  'text' => 'mbrcq_text',                  'summary' => 'mbrcq_rc_comment',                  'minor' => 'mbrcq_rc_minor',                  'bot' => 'mbrcq_rc_bot',                  'title' => 'mbrcq_rc_title',                  'namespace' => 'mbrcq_rc_namespace',                  'contentmodel' => 'mbrcq_content_model',                  'rcid' => 'mbrcq_rc_id',                  'revid' => 'mbrcq_rc_thisoldid',                  'user' => 'mbrcq_rc_user_text',                  'userid' => 'mbrcq_rc_user',                  'timestamp' => 'mbrcq_rc_timestamp',                  'comment' => 'mbrcq_rc_comment',                  'tags' => 'mbrcq_tags',            ); }           $pushMapping = array(                  'pageid' => 'mbrcq_rc_cur_id',                  'text' => 'mbrcq_text',                  'summary' => 'mbrcq_rc_comment',                  'minor' => 'mbrcq_rc_minor',                  'bot' => 'mbrcq_rc_bot',                  'title' => 'mbrcq_rc_title',                  'namespace' => 'mbrcq_rc_namespace',                  'contentmodel' => 'mbrcq_content_model',                  'rcid' => 'mbrcq_rc_id',                  'revid' => 'mbrcq_rc_thisoldid',                  'user' => 'mbrcq_rc_user_text',                  'userid' => 'mbrcq_rc_user',                  'timestamp' => 'mbrcq_rc_timestamp',                  'comment' => 'mbrcq_rc_comment',                  'tags' => 'mbrcq_tags',            ); $query = "?action=mirroredit&format=php&token=$token"; foreach( $pushMapping as $pushMappingKey => $pushMappingValue ) { // TODO: The API should reject it if we don't have that user. And if rejected, we shouldn't                 // mark it as having been successfully pushed. if ( $row[$pushMappingValue] || $pushMappingKey == 'namespace' ) { // Namespace can be zero $query .= "&$pushMappingKey=". ( urlencode( $row[$pushMappingValue] ) ); }           }            $localRet = $wiki->query ( $query, true,                  'Content-Type: application/x-www-form-urlencoded' ); // POST var_dump ( $localRet ); if ( !$localRet ) { die ( "Nothing was returned\n" ); }           if ( isset( $localRet['error' ] ) ) { die( "The $localWikiName API returned an error message!\n" ); }           // TODO: Loop $db->query ( "UPDATE mb_rc_queue SET mbrcq_push_result=" . $localRet['result']                 . ", mbrcq_push_user="                  . $localRet['userid'] . " WHERE mbrcq_rcid=" . $row['mbrcq_rc_id'] ); break; $mbRet = $db->query( "SELECT * FROM mb_rc_queue WHERE mbrcq_rc_log_action='create' "                 . "AND mbrcq_push_result='' LIMIT 1000" ); if ( !$mbRet || $mbRet.num_rows == 0 ) { die ( "No unpushed user create events were found\n" ); }           $row = $mbRet->fetch_assoc; // Just do one row for testing purposes $pushMapping = array(                 'user' => 'mbrcq_rc_user_text',                  'userid' => 'mbrcq_rc_user',                  'timestamp' => 'mbrcq_rc_timestamp',                  'rcid' => 'mbrcq_rc_id',                  'logid' => 'mbrcq_rc_logid',            ); $query = "?action=mirroredit&format=php&token=$token"; foreach( $pushMapping as $pushMappingKey => $pushMappingValue ) { if ( $row[$pushMappingValue] || $pushMappingKey == 'namespace' ) { // Namespace can be zero $query .= "&$pushMappingKey=". ( urlencode( $row[$pushMappingValue] ) ); }           }            $localRet = $wiki->query ( $query, true,                  'Content-Type: application/x-www-form-urlencoded' ); // POST var_dump ( $localRet ); if ( !$localRet ) { die ( "Nothing was returned\n" ); }           if ( isset( $localRet['error' ] ) ) { die( "The $localWikiName API returned an error message!\n" ); }           // TODO: Loop $db->query ( "UPDATE mb_rc_queue SET mbrcq_push_result=" . $localRet['result']                 . ", mbrcq_push_user="                  . $localRet['userid'] . " WHERE mbrcq_rcid=" . $row['mbrcq_rc_id'] ); function getNewusersPushMapping( $row ) { $pushMapping = array(           'userid' => 'mbrq_user',            'username' => 'mbrcq_rc_user_text',	    'usertouched' => 'mbrcq_rc_timestamp',            'userregistration' => 'mbrcq_rc_timestamp',	    'logid' => 'mbrcq_rc_logid',	    'logtype' => 'mbrcq_rc_logtype',	    'logaction' => 'mbrcq_rc_logaction',	    'logtimestamp' => 'mbrcq_rc_timestamp',	    'loguser' => 'mbrcq_rc_user',	    'lognamespace' => 'mbrcq_rc_namespace',	    'logusertext' => 'mbrcq_rc_user_text',	    'logtitle' => 'User:' . 'mbrcq_rc_user'],			'log_comment' => $params['logcomment'],			'log_params' => $params['logparams'],			'log_page' => $params['logpage']      ); return $pushMapping; }