Thu, 22 Feb 2024 20:21:22 +0100
Skipping {{nobots}} pages
#$Id$ # <?PHP # Wikipedia bot components require_once ( __DIR__.'/vendor/mediawiki-api/vendor/autoload.php' ) ; # "the usual" tools routines, mostly database opening ones; these should probably be replaced by local ones! require_once ( __DIR__.'/public_html/php/common.php' ) ; class CommonsDelinquent { var $debugging = false ; function __construct () { $this->config = parse_ini_file ( __DIR__.'/bot.cnf' ) ; } // Runs a MySQL query. Optional debugging output, and output of query on error function runQuery ( $db , $sql ) { if ( $this->debugging ) print "$sql\n" ; if(!$result = $db->query($sql)) throw new Exception('There was an error running the query [' . $db->error . ']:'."\n$sql\n"); return $result ; } function getCommonsDB () { $this->last_db = openDB ( 'commons' , 'wikimedia' ) ; return $this->last_db ; } function getToolDB () { $this->last_db = openToolDB ( 'commonsdelinquent_p' ) ; return $this->last_db ; } function getDBsafe ( $s ) { if ( !isset ( $this->last_db ) ) die ( "getDBsafe called before database was opened!" ) ; return $this->last_db->real_escape_string ( $s ) ; } function isBadWiki ( $wiki ) { if ( $wiki == 'ukwikimedia' ) return true ; if ( preg_match ( '/^wikimania/' , $wiki ) ) return true ; if ( preg_match ( '/strategy/' , $wiki ) ) return true ; if ( preg_match ( '/foundation/' , $wiki ) ) return true ; # if ( preg_match ( '/outreach/' , $wiki ) ) return true ; if ( preg_match ( '/mxwikimedia/' , $wiki ) ) return true ; if ( preg_match ( '/rswikimedia/' , $wiki ) ) return true ; if ( preg_match ( '/tenwiki/' , $wiki ) ) return true ; if ( preg_match ( '/stqwiki/' , $wiki ) ) return true ; if ( preg_match ( '/enwikinews/' , $wiki ) ) return true ; if ( preg_match ( '/testwikidatawiki/' , $wiki ) ) return true ; # if ( preg_match ( '/^suwiki$/' , $wiki ) ) return true ; if ( preg_match ( '/usability/' , $wiki ) ) return true ; # wikisource if ( preg_match ( '/sourceswiki/' , $wiki ) ) return true ; # SUL LOGIN not working if ( $wiki == 'donatewiki' ) return true ; if ( $wiki == 'idwikimedia' ) return true ; if ( $wiki == 'bdwikimedia' ) return true ; if ( $wiki == 'maiwikimedia' ) return true ; if ( $wiki == 'amwikimedia' ) return true ; if ( $wiki == 'gewikimedia' ) return true ; if ( $wiki == 'azwikimedia' ) return true ; if ( $wiki == 'gurwiki' ) return true ; if ( $wiki == 'gomwiktionary' ) return true; return false ; // Wiki is OK } function hasLocalFile ( $wiki , $file ) { $ret = false ; # print "OPENING 1: $wiki\n" ; $db = openDBwiki ( $wiki ) ; if ( $db === false ) { print "FAILED TO OPEN $wiki - returning false\n" ; return false ; } $this->last_db = $db ; /// hack by grin, 2021-03-01; getDBsafe may return empty! $sql_name = $this->getDBsafe(str_replace(' ','_',$file)); if( $sql_name == "" ) return false; /// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ $sql = "SELECT * FROM image WHERE img_name='" . $sql_name . "' LIMIT 1" ; try { $result = $this->runQuery ( $this->last_db , $sql ) ; while($o = $result->fetch_object()) $ret = true ; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } return $ret ; } function setDone ( $id , $value , $meta = null) { $db = $this->getToolDB() ; $sql = "UPDATE event SET done=" . ($value*1) ; if ( isset ( $meta ) ) { if ( !is_array ( $meta ) ) $meta = array ( 'note' => $meta ) ; # clip note to varchar(64) if( isset($meta['note']) ) $meta['note'] = substr($meta['note'], 0, 63); foreach ( $meta AS $k => $v ) { $sql .= ",$k='" . $this->getDBsafe($v) . "'" ; } } $sql .= " WHERE id=" . ($id*1) ; if ( $value != 1 ) print "$sql\n" ; $this->runQuery ( $db , $sql ) ; $db->close() ; } function wiki2server ( $wiki ) { if ( $wiki == 'wikidatawiki' ) return 'www.wikidata.org' ; if ( $wiki == 'commonswiki' ) return 'commons.wikimedia.org' ; if ( $wiki == 'mediawikiwiki' ) return 'www.mediawiki.org' ; if ( $wiki == 'metawiki' ) return 'meta.wikimedia.org' ; if ( $wiki == 'outreachwiki' ) return 'outreach.wikimedia.org' ; if ( $wiki == 'incubatorwiki' ) return 'incubator.wikimedia.org' ; if ( $wiki == 'sourceswiki' ) return 'wikisource.org' ; if ( $wiki == 'specieswiki' ) return 'species.wikimedia.org' ; /// fix by grin 2021-03-01: missing close re separator if ( preg_match ( '/(.+)wikimedia/' , $wiki , $m ) ) return $m[1] . ".wikimedia.org" ; if ( preg_match ( '/^(wikimania\d+)wiki$/' , $wiki , $m ) ) return $m[1] . ".wikimedia.org" ; if ( preg_match ( '/^(.+?)(wik.+)$/' , $wiki , $m ) ) { $server = str_replace('_','-',$m[1]) . "." ; if ( $server == 'be-x-old.' ) $server = 'be-tarask.' ; if ( $m[2] == 'wiki' ) $server .= 'wikipedia' ; else $server .= $m[2] ; $server .= '.org' ; return $server ; } return false ; } function getAPI ( $wiki ) { # TODO check if re-opening same API, cache in object $server = $this->wiki2server ( $wiki ) ; if ( $server === false ) return false ; $api = new \Mediawiki\Api\MediawikiApi( 'https://'.$server.'/w/api.php' ); if ( !$api->isLoggedin() ) { print "Logging into https://$server/w/api.php\n"; # --grin 2022-02-05 $x = $api->login( new \Mediawiki\Api\ApiUser( $this->config['name'], $this->config['password'] ) ); if ( !$x ) return false ; } return $api ; } function editWiki ( $wiki , $action , $params ) { $api = $this->getAPI ( $wiki ) ; if ( $api === false ) return false ; $params['token'] = $api->getToken() ; $params['bot'] = 1 ; $x = false ; if ( $this->debugging ) print_r ( $params ) ; try { $x = $api->postRequest( new \Mediawiki\Api\SimpleRequest( $action, $params ) ); } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; $this->last_exception = $e->getMessage() ; $x = false ; } if ( $this->debugging ) print_r ( $x ) ; //$api->logout() ; $params['token'] = $api->getToken() ; $api->postRequest( new \Mediawiki\Api\SimpleRequest( "logout", $params ) ); return $x ; } function editWikidata ( $action , $params ) { $ret = $this->editWiki ( 'wikidatawiki' , $action , $params ) ; if ( $this->debugging ) { if ( is_array($ret) ) print "RET:" . $ret['success'] . "\n" ; else print "RET: FALSE\n" ; } if ( is_array($ret) ) return $ret['success'] ; return false ; } } ?>