diff -r 000000000000 -r 3b714bbb1347 delinquent_files/shared.inc --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/delinquent_files/shared.inc Mon Jan 23 21:37:02 2023 +0100 @@ -0,0 +1,193 @@ +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 ; + # 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 == 'mniwiki' ) return true ; # -grin 2022-02-05 +# if ( $wiki == 'vecwiki' ) return true; # -grin 2022-10-03 + + if ( $wiki == 'mnwwiktionary' ) return true ; # -grin 2022-03-10 + if ( $wiki == 'mniwiktionary' ) return true ; # -grin 2022-03-10 + if ( $wiki == 'shnwiktionary' ) return true ; # -grin 2022-07-18 + if ( $wiki == 'niawiktionary' ) return true ; # -grin 2022-09-06 + + if ( $wiki == 'wawikisource' ) return true; # -grin 2022-05-02 + if ( $wiki == 'banwikisource' ) return true; # -grin 2022-08-08 + +# if ( $wiki == 'fiwikivoyage' ) return true ; +# if ( $wiki == 'brwikisource' ) return true ; +# if ( $wiki == 'liwikibooks' ) return true ; +# if ( $wiki == 'liwikisource' ) 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 ) ; + 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 ; + } + +} + +?>