diff --git a/2017-12-commons-stub-sport-logos-importer/classes.php b/2017-12-commons-stub-sport-logos-importer/classes.php new file mode 100644 index 0000000..f8db08d --- /dev/null +++ b/2017-12-commons-stub-sport-logos-importer/classes.php @@ -0,0 +1,57 @@ +. + +/** + * A CSV row of a Sport. + */ +class Sport { + function __construct( $itwikiprefix, $ensport, $ensportmen, $itsport, $itsportmen, $commonscat ) { + $this->itwikiprefix = $itwikiprefix; + $this->ensport = $ensport; + $this->ensportmen = $ensportmen; + $this->itsport = $itsport; + $this->itsportmen = $itsportmen; + $this->commonscat = $commonscat; + } + + static function createFromData( $data ) { + return new self( + $data[0], + $data[1], + $data[2], + $data[3], + $data[4], + $data[5] + ); + } +} + +/** + * A CSV row of a Nation. + */ +class Nation { + function __construct( $itpeople, $ennation ) { + $this->itpeople = $itpeople; + $this->ennation = $ennation; + } + + static function createFromData( $data ) { + return new self( + $data[0], + $data[1] + ); + } +} diff --git a/2017-12-commons-stub-sport-logos-importer/commons-stub-sport-logos-importer.php b/2017-12-commons-stub-sport-logos-importer/commons-stub-sport-logos-importer.php index b302a74..7160094 100755 --- a/2017-12-commons-stub-sport-logos-importer/commons-stub-sport-logos-importer.php +++ b/2017-12-commons-stub-sport-logos-importer/commons-stub-sport-logos-importer.php @@ -1,255 +1,134 @@ #!/usr/bin/php . -require '../config.php'; +# boz-mw require '../includes/boz-mw/autoload.php'; -define('SUMMARY', '[[Commons:Bots/Requests/Valerio Bozzolan bot (2)|uniforming italian sport icons]]'); - -##################### -# Commons Login token -##################### +# Auth tokens +require '../config.php'; -mw\APIRequest::$WAIT_POST = 0.2; +# Local tools +require 'functions.php'; +require 'classes.php'; -$logintoken = \wm\Commons::getInstance()->fetch( [ - 'action' => 'query', - 'meta' => 'tokens', - 'type' => 'login' -] )->query->tokens->logintoken; - -############### -# Commons login -############### - -$response = \wm\Commons::getInstance()->post( [ - 'action' => 'login', - 'lgname' => WIKI_USERNAME, - 'lgpassword' => WIKI_PASSWORD, - 'lgtoken' => $logintoken -] ); -if( ! isset( $response->login->result ) || $response->login->result !== 'Success' ) { - throw new Exception("login failed"); -} +define('SUMMARY', '[[Commons:Bots/Requests/Valerio Bozzolan bot (2)|uniforming italian sport icons]]'); ############################# # Commons CSRF token (logged) ############################# -$COMMONS_CSRF_TOKEN = \wm\Commons::getInstance()->fetch( [ +$COMMONS_CSRF_TOKEN = \wm\Commons::getInstance()->login()->fetch( [ 'action' => 'query', 'meta' => 'tokens', 'type' => 'csrf' ] )->query->tokens->csrftoken; ################ # Prepare sports ################ $SPORTS = []; $sports_data = trim( file_get_contents('sports.csv') ); $sports_data = explode( "\n", $sports_data ); array_shift( $sports_data ); foreach( $sports_data as $i => $sport_data ) { $sport_data = explode( ',', $sport_data ); $sport = Sport::createFromData( $sport_data ); $SPORTS[] = $sport; } ################# # Prepare nations ################# $NATIONS = []; $nations_data = trim( file_get_contents('nations.csv') ); $nations_data = explode( "\n", $nations_data ); array_shift( $nations_data ); foreach( $nations_data as $i => $nation_data ) { $nation_data = explode( ',', $nation_data ); $nation = Nation::createFromData( $nation_data ); $NATIONS[] = $nation; } foreach( $SPORTS as $sport ) { // Upper case $ensport_uc = ucfirst( $sport->ensport ); foreach( $NATIONS as $nation ) { - // Retrieve wikitext + // Retrieve a Wikitext object $commons_title = "File:{$ensport_uc} {$nation->ennation}.png"; $commons_wikitext = commons_wikitext( $commons_title ); - // Transform to object - $commons_wikitext_object = new \mw\Wikitext( \wm\Commons::getInstance(), $commons_wikitext ); - - // Titles + // Some categories + $sport_category = $sport->commonscat; $nation_category = "Flags of {$nation->ennation}"; $nation_category_icons = "Flags of {$nation->ennation} icons"; $nation_category_png = "PNG flags of {$nation->ennation}"; $nation_category_variations = "Variations on flags of {$nation->ennation}"; // Clear summary $summary = ''; - // Add categories + // Add some nation-related categories... if they exist... $categories = [ $nation_category_icons, $nation_category_png, $nation_category_variations ]; - $has_one = false; + $wtf = true; foreach( $categories as $category ) { - if( $commons_wikitext_object->hasCategory( $category ) ) { - $has_one = true; - } else { - if( commons_page_exists("Category:$category") ) { - $has_one = true; - $commons_wikitext_object->addCategory( $category ); - $summary .= "; [[Category:$category]]"; - } + $done = wikitext_add_category_if_exists( $commons_wikitext, $category, $summary ); + if( $done ) { + $wtf = false; } } - // Add the generic national category only if others are missing - if( ! $has_one ) { - if( ! $commons_wikitext_object->hasCategory( $nation_category ) ) { - if( commons_page_exists( "Category:$nation_category" ) ) { - $commons_wikitext_object->addCategory( $nation_category ); - $summary .= "; +[[Category:$nation_category]]"; - } - } + // WTF! No previous nation-related category exist! + if( $wtf ) { + // Last chance + wikitext_add_category_if_exists( $commons_wikitext, $nation_category, $summary ); + } + + // Add a sport-related category + wikitext_add_category_if_exists( $commons_wikitext, $sport_category, $summary ); + + // Destroy the damn {{Uncategorized}} + if( 1 === $commons_wikitext->pregMatch('/{{Uncategorized\|.*?}}/') ) { + $commons_wikitext->pregReplace( '/{{Uncategorized *\|.*?}}.*\n/', '' ); + $summary .= "; -[[Template:Uncategorized]]"; } // Update description $description = "{{en|Icon for {$sport->ensport} from {$nation->ennation}}}{{it|Icona per {$sport->itsportmen} {$nation->itpeople}}}"; $description_pattern = '/\|.*Description *= *' . preg_quote( $description ) . '/'; - if( 1 !== $commons_wikitext_object->pregMatch( $description_pattern ) ) { + if( 1 !== $commons_wikitext->pregMatch( $description_pattern ) ) { $description_pattern = '/(\| *Description *= *).*?(\n\|)/s'; - $commons_wikitext_object->pregReplace( $description_pattern, "\\1$description\\2", 1 ); + $commons_wikitext->pregReplace( $description_pattern, "\\1$description\\2", 1 ); $summary .= "; updated en/it description"; } - // Save + // Save the whole stuff if( $summary ) { - commons_save( $commons_title, $commons_wikitext_object->getWikitext(), SUMMARY . $summary ); + commons_save( $commons_title, $commons_wikitext->getWikitext(), SUMMARY . $summary ); } - echo "OK $commons_title\n"; - } -} - -class Sport { - function __construct( $itwikiprefix, $ensport, $ensportmen, $itsport, $itsportmen ) { - $this->itwikiprefix = $itwikiprefix; - $this->ensport = $ensport; - $this->ensportmen = $ensportmen; - $this->itsport = $itsport; - $this->itsportmen = $itsportmen; - } - - static function createFromData( $data ) { - return new self( - $data[0], - $data[1], - $data[2], - $data[3], - $data[4] - ); - } -} - -class Nation { - function __construct( $itpeople, $ennation ) { - $this->itpeople = $itpeople; - $this->ennation = $ennation; + echo "OK $commons_title! Yeah.\n"; } - - static function createFromData( $data ) { - return new self( - $data[0], - $data[1] - ); - } -} - -function wiki_save( $wiki, $csrf, $title, $content, $summary ) { - echo "\n"; - echo "########### Saving [[$title]]: ##########\n"; - echo $content; - echo "\n"; - echo "#########################################\n"; - echo "Confirm summary: |$summary|\n"; - read(); - return $wiki->post( [ - 'action' => 'edit', - 'title' => $title, - 'summary' => $summary, - 'text' => $content, - 'token' => $csrf, - 'bot' => '' - ] ); -} - -function read( $default = '' ) { - $v = chop( fgets(STDIN) ); - return $v ? $v : $default; -} - -function commons_save( $title, $content, $summary ) { - return wiki_save( - \wm\Commons::getInstance(), - $GLOBALS['COMMONS_CSRF_TOKEN'], - $title, - $content, - $summary - ); -} - -function commons_wikitext( $page ) { - // Wikitext - // https://it.wikipedia.org/w/api.php?action=query&prop=revisions&titles=linux+%28kernel%29&rvprop=content - $pages = \wm\Commons::getInstance()->fetch( [ - 'action' => 'query', - 'prop' => 'revisions', - 'titles' => $page, - 'rvprop' => 'content' - ] ); - foreach( $pages->query->pages as $page ) { - return $page->revisions[0]->{'*'}; - } - throw new Exception("missing page $page"); -} - -function nation_from_it_people( $it_people ) { - foreach( $GLOBALS['NATIONS'] as $nation ) { - if( $nation->itpeople === $it_people ) { - return $nation; - } - } - throw new Exception("missing it people $it_people"); -} - -function commons_page_exists( $page ) { - $response = \wm\Commons::getInstance()->fetch( [ - 'action' => 'query', - 'prop' => 'info', - 'titles' => $page - ] ); - return ! isset( $response->query->pages->{-1} ); } diff --git a/2017-12-commons-stub-sport-logos-importer/functions.php b/2017-12-commons-stub-sport-logos-importer/functions.php new file mode 100644 index 0000000..913002a --- /dev/null +++ b/2017-12-commons-stub-sport-logos-importer/functions.php @@ -0,0 +1,143 @@ +. + +/** + * Save a page. + * + * @param $wiki mw\APIRequest or mw\Site + * @param $csrf The CSRF token + * @param $title Page title to be created + * @param $summary Summary + */ +function wiki_save( $wiki, $csrf, $title, $content, $summary ) { + echo "\n"; + echo "########### Saving [[$title]]: ##########\n"; + echo $content; + echo "\n"; + echo "#########################################\n"; + echo "Confirm summary: |$summary|\n"; + read(); + return $wiki->post( [ + 'action' => 'edit', + 'title' => $title, + 'summary' => $summary, + 'text' => $content, + 'token' => $csrf, + 'bot' => '' + ] ); +} + +/** + * Wait for an input + * + * @param $default Default string to be returned if nothing is chopped. + * @return string Chopped string, or $default. + */ +function read( $default = '' ) { + $v = chop( fgets(STDIN) ); + return $v ? $v : $default; +} + +/** + * Save a page in Wikimedia Commons. + * + * @param $title Page title to be saved + * @param $content Wikitext to be saved + * @param $summary Summary + */ +function commons_save( $title, $content, $summary ) { + return wiki_save( + \wm\Commons::getInstance(), + $GLOBALS['COMMONS_CSRF_TOKEN'], + $title, + $content, + $summary + ); +} + +/** + * Get a mw\Wikitext object from a page title. + * + * @param string $page Page title with namespace prefix + * @return mw\Wikitext + */ +function commons_wikitext( $page ) { + // Wikitext + // https://it.wikipedia.org/w/api.php?action=query&prop=revisions&titles=linux+%28kernel%29&rvprop=content + $pages = \wm\Commons::getInstance()->fetch( [ + 'action' => 'query', + 'prop' => 'revisions', + 'titles' => $page, + 'rvprop' => 'content' + ] ); + foreach( $pages->query->pages as $page ) { + return Commons::getInstance()->createWikitext( + $page->revisions[0]->{'*'} + ); + } + throw new Exception("missing page $page"); +} + +/** + * Get a Nation object from a string like "italiani". + * + * @param string $it_people A string like "italiani" + * @return Nation + */ +function nation_from_it_people( $it_people ) { + foreach( $GLOBALS['NATIONS'] as $nation ) { + if( $nation->itpeople === $it_people ) { + return $nation; + } + } + throw new Exception("missing it people $it_people"); +} + +/** + * Discover if a page exists on Wikimedia Commons. + * + * @param $page Page title with namespace prefix + * @return bool + */ +function commons_page_exists( $page ) { + $response = \wm\Commons::getInstance()->fetch( [ + 'action' => 'query', + 'prop' => 'info', + 'titles' => $page + ] ); + return ! isset( $response->query->pages->{-1} ); +} + +/** + * Add a category into a mw\Wikitext object, only if it's not added yet. + * If the category is added, a summary is appended. + * + * @param $wikitext mw\Wikitext + * @param $category string Category without namespace prefix + * @param $summary + * @return int 0 if unexisting; 1 if existing; 2 if added. + */ +function wikitext_add_category_if_exists( $wikitext, $category, & $summary ) { + if( $wikitext->hasCategory( $category ) ) { + return 1; + } + if( commons_page_exists("Category:$category") ) { + $wikitext->addCategory( $category ); + $summary .= "; +[[Category:$category]]"; + return 2; + } + return 0; +} diff --git a/config-example.php b/config-example.php index ef6ef0a..1fd95c2 100644 --- a/config-example.php +++ b/config-example.php @@ -1,5 +1,5 @@