diff --git a/2019-05-commons-volleyball-players-upload/bot.php b/2019-05-commons-volleyball-players-upload/bot.php index 280a4d4..0f9941e 100755 --- a/2019-05-commons-volleyball-players-upload/bot.php +++ b/2019-05-commons-volleyball-players-upload/bot.php @@ -1,119 +1,120 @@ #!/usr/bin/php . // autoload framework require 'include/boz-mw/autoload.php'; -// load credentials -require '../config.php'; - use \web\MediaWikis; use \cli\Log; +use \cli\ConfigWizard; use \network\ContentDisposition; +// load configuration file or create one +ConfigWizard::requireOrCreate( __DIR__ . '/../config.php' ); + // login in MediaWiki Commons $wiki = MediaWikis::findFromUID( 'commonswiki' ); $wiki->login(); // fingerprint of the default pic $default_pic_md5 = md5( file_get_contents( 'data/default-pic.jpg' ) ); $header = true; $handle = fopen( 'data/2019-volleyball-players.csv', 'r' ); while( ( $data = fgetcsv( $handle ) ) !== false ) { // skip header if( $header ) { $header = false; continue; } // populate arguments list( $id, $surname, $name, $h, $birth, $birth_nation, $birth_where, $nation, $last_team, $last_season, $url_photo ) = $data; // avoid nonsense files $pic = file_get_contents( $url_photo ); if( strlen( $pic ) < 300 ) { Log::warn( "skip $id $surname $name with no pic $url_photo" ); continue; } // avoid default pic if( md5( $pic ) === $default_pic_md5 ) { Log::warn( "skip $id $surname $name with default pic $url_photo" ); continue; } if( empty( $name ) || empty( $surname ) ) { Log::warn( "skip $id without name or surname" ); continue; } // check if the page already exists $filename = "$name $surname (Legavolley 2019).jpg"; $response = $wiki->fetch( [ 'action' => 'query', 'prop' => 'info', 'titles' => "File:$filename", ] ); $exists = false; foreach( $response->query as $pages ) { foreach( $pages as $page ) { $exists = empty( $page->missing ); } } if( !$exists ) { Log::warn( "skip File:$filename already existing" ); continue; } $date = date('Y-m-d H:i:s'); $text = require( 'data/content.wiki.php' ); $comment = "Bot: test [[Commons:Bots/Requests/Valerio Bozzolan bot (5)|upload 2019 volleyball players from Legavolley]]"; try { $response = $wiki->upload( [ 'comment' => $comment, 'text' => $text, 'filename' => $filename, ContentDisposition::createFromNameURLType( 'file', $url_photo, 'image/jpg' ), ] ); } catch( \mw\API\Exception $e ) { Log::error( $e->getMessage() ); continue; } // look at the response $msg = $response->upload->result; Log::info( "response: $msg" ); if( isset( $response->upload->warnings ) ) { print_r( $response->upload->warnings ); } }