Творчество:DarpaBot

Материал из Мегапедии
Перейти к: навигация, поиск

<source lang="php"> <?php /*

* PHP framework for MediaWiki bots
*/

/* string */ $wbUserAgent = 'VladykaBot/0.1'; /* string */ $wbLogMode = 'file'; /* bool */ $wbAllowAnonymousEdits = false;

class DarpaBot { /* string */ public $user = null; /* string */ private $file; /* APIRequest */ private $handler;

/* void */ public function __construct( $url ) { $this->handler = new APIRequest( $url );

global $wbLogMode; if( $wbLogMode == 'file' ) { $this->file = fopen( 'vlbot-' . time() . '.log', 'w' ); } }

/* void */ public function login( $user, $pass ) { $key = $this->handler->request( 'login', array( 'lgname' => $this->user, 'lgpassword' => $pass ) );

$request = $this->handler->request( 'login', array( 'lgname' => $this->user, 'lgpassword' => $pass, 'lgtoken' => $key['login']['token'] ) ); $result = $request['login']['result'];

switch( $result ) { case 'Success': $this->user = $user; if( $wbLogMode !== false ) { $this->log( "Successfully logged-in as $user." ); } break;

default: $this->fatal_error( $result, __METHOD__ ); } }

/* array */ public function query( array $options ) { return $this->handler->request( 'query', $options ); }

/* void */ public function process_list( $list, array $options, $cycles, $callback ) { $end = false; $options = array_merge( array( 'list' => $list ), $options ); while( $end !== true && $cycles !== 0 ) { $resource = $this->query( $options ); call_user_func( $callback, $resource );

$offset = $resource['query-continue']; if( $offset ) { $module = array_shift( array_keys( $offset ) ); $key = array_shift( array_keys( $offset[$module] ) ); $options = array_merge( array( $key => $offset[$module][$key] ), $options ); --$cycles; } else { $end = true; } } }

/* void */ public function edit( $page, $text, $summary = , $minor = false, $options = array() ) { global $wbAllowAnonymousEdits; $this->check_login( $wbAllowAnonymousEdits );

$token = $this->token( 'edit' ); $params = array_merge( $options, array( 'title' => $page, 'text' => $text, 'summary' => $summary, 'minor' => $minor ? 1 : 0, 'bot' => 1, 'token' => $token ) ); $exec = $this->handler->request( 'edit', $params ); if( $exec['edit']['result'] === 'Success' && $wbLogMode !== false ) { $this->log("$page has been succesfully edited."); } elseif( $wbLogMode !== false ) { $this->warning( $exec['edit']['result'], __METHOD__ ); } }

/* void */ public function rollback( /* FIXME */ ) {

}

/* void */ public function delete( /* FIXME */ ) {

}

/* void */ public function undelete( /* FIXME */ ) {

}

/* void */ public function protect( /* FIXME */ ) {

}

/* void */ public function block( /* FIXME */ ) {

}

/* void */ public function unblock( /* FIXME */ ) {

}

/* void */ public function email( /* FIXME */ ) {

}

/* void */ public function upload( /* FIXME */ ) {

}

/* void */ public function rights( $user, array $rights, $reason ) { $token = $this->token( 'userrights' ); $params = array( 'user' => $user, 'token' => $token, 'reason' => $reason ); if( isset( $rights['add'] ) ) { $params['add'] = $rights['add']; } if( isset( $rights['remove'] ) ) { $params['remove'] = $rights['remove']; } $exec = $this->handler->request( 'userrights', $params ); # FIXME }

/* string */ private function token( $action, $param = 'foo' ) { switch( $action ) { case 'userrights': $token = $this->query( array( 'list' => 'users', 'ususers' => $param, 'ustoken' => 'userrights' ); return $token['query']['users'][0]['userrightstoken'];

default: $token = $this->query( array( 'prop' => 'info', 'intoken' => $action, 'titles' => $param ) ); $key = array_shift( array_keys( $token['query']['pages'] ) ); return $token['query']['pages'][$key][$action.'token']; } }

/* void */ private function check_login( $anon_mode ) { $userinfo = $this->query( array( 'meta' => 'userinfo' ) );

if( $anon_mode && $this->user !== $userinfo['query']['userinfo']['name'] ) { $this->fatal_error( 'NoLogin', __METHOD__ ); } }

/* void */ private function warning( $code, $method ) { echo("Error: $code (called from $method).\n"); }

/* void */ private function fatal_error( $code, $method ) { exit("Fatal error: $code (called from $method).\n"); }

/* void */ private function log( $message ) { global $wbLogMode;

switch( $wbLogMode ) { case 'stdout': echo date("G:i:s") . ': ' . (string)$message . "\n"; break;

case 'file': fwrite( $this->file, date("G:i:s") . ': ' . (string)$message . "\n" ); break; # FIXME } } }

class APIRequest { /* string */ private $url; /* string */ private $curl; /* string */ private $jar;

/* void */ public function __construct( $api ) { global $wbUserAgent;

$this->url = $api; $this->curl = curl_init(); $this->jar = 'vlbot-' . time() . '.jar';

curl_setopt( $this->curl, CURLOPT_COOKIEJAR, $this->jar ); curl_setopt( $this->curl, CURLOPT_COOKIEFILE, $this->jar ); curl_setopt( $this->curl, CURLOPT_USERAGENT, $wbUserAgent ); curl_setopt( $this->curl, CURLOPT_MAXREDIRS, 5 ); curl_setopt( $this->curl, CURLOPT_RETURNTRANSFER, 1 ); curl_setopt( $this->curl, CURLOPT_TIMEOUT, 40 ); curl_setopt( $this->curl, CURLOPT_CONNECTTIMEOUT, 12 ); }

/* array */ public function request( $action, array $options ) { $method = $this->action_method( $action ); $result = $this->$method( $this->url, array_merge( array( 'action' => $action, 'format' => 'php' ), $options ) );

return unserialize( $result ); }

/* void */ public function change_agent( $useragent ) { curl_setopt( $this->curl, CURLOPT_USERAGENT, $useragent ); }

/* void */ public function set_cookie( $cookie ) { curl_setopt( $this->curl, CURLOPT_COOKIE, $cookie ); }

/* string */ private function curl_request( $url ) { curl_setopt( $this->curl, CURLOPT_URL, $url );

return curl_exec( $this->curl ); }

/* string */ private function get( $url, array $request ) { curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt( $this->curl, CURLOPT_HTTPGET, 1 ); $url .= '?' . http_build_query( $request );

return $this->curl_request( $url ); }

/* string */ private function post( $url, array $request ) { curl_setopt( $this->curl, CURLOPT_FOLLOWLOCATION, 1 ); curl_setopt( $this->curl, CURLOPT_POST, 1 ); curl_setopt( $this->curl, CURLOPT_POSTFIELDS, $request );

return $this->curl_request( $url ); }

/* string */ private function action_method( $action ) { # FIXME $get_actions = array( 'query' ); return ( in_array( $action, $get_actions ) ) ? 'get' : 'post'; } }

class TH { /* void */ static public function str( $i ) { if( !is_string( $i ) || is_numeric( $i ) ) { exit("TypeHinting error: $i is not string.\n"); } }

/* void */ static public function num( $i ) { if( !is_numeric( $i ) ) { exit("TypeHinting error: $i is not number.\n"); } } } </source>