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 7160094..f2a0d02 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,134 +1,138 @@ #!/usr/bin/php . # boz-mw require '../includes/boz-mw/autoload.php'; # Auth tokens require '../config.php'; # Local tools require 'functions.php'; require 'classes.php'; define('SUMMARY', '[[Commons:Bots/Requests/Valerio Bozzolan bot (2)|uniforming italian sport icons]]'); -############################# -# Commons CSRF token (logged) -############################# - -$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; } +$sport_i = 0; + foreach( $SPORTS as $sport ) { + $sport_i++; + // Upper case $ensport_uc = ucfirst( $sport->ensport ); + $nation_i = 0; + foreach( $NATIONS as $nation ) { + $nation_i++; + // Retrieve a Wikitext object $commons_title = "File:{$ensport_uc} {$nation->ennation}.png"; + $commons_wikitext = commons_wikitext( $commons_title ); + if( ! $commons_wikitext ) { + echo "Skip [[$commons_title]]: empty/unexisting \n"; + continue; + } + // 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 some nation-related categories... if they exist... $categories = [ $nation_category_icons, $nation_category_png, $nation_category_variations ]; $wtf = true; foreach( $categories as $category ) { $done = wikitext_add_category_if_exists( $commons_wikitext, $category, $summary ); if( $done ) { $wtf = false; } } // 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->pregMatch( $description_pattern ) ) { $description_pattern = '/(\| *Description *= *).*?(\n\|)/s'; $commons_wikitext->pregReplace( $description_pattern, "\\1$description\\2", 1 ); $summary .= "; updated en/it description"; } // Save the whole stuff if( $summary ) { - commons_save( $commons_title, $commons_wikitext->getWikitext(), SUMMARY . $summary ); + commons_save( $commons_title, $commons_wikitext, SUMMARY . $summary ); } echo "OK $commons_title! Yeah.\n"; } } diff --git a/2017-12-commons-stub-sport-logos-importer/functions.php b/2017-12-commons-stub-sport-logos-importer/functions.php index 92f3116..8caa951 100644 --- a/2017-12-commons-stub-sport-logos-importer/functions.php +++ b/2017-12-commons-stub-sport-logos-importer/functions.php @@ -1,143 +1,205 @@ . +/** + * Save a page in Wikimedia Commons. + * + * @param $title string Page title to be saved + * @param $wikitext mw\Wikitext Wikitext to be saved + * @param $summary string Summary + */ +function commons_save( $title, $wikitext, $summary ) { + static $csrf; + if( ! $csrf ) { + $csrf = commons_csrf(); + } + try { + return wiki_save( + \wm\Commons::getInstance(), + $csrf, + $title, + $wikitext, + $summary + ); + } catch( Exception $e ) { + if( $e->getCode() === 'badtoken' ) { + $csrf = null; + return commons_save( $title, $content, $summary ); + } + } +} + +/** + * Commons CSRF + * + * @return string + */ +function commons_csrf() { + return \wm\Commons::getInstance()->login()->fetch( [ + 'action' => 'query', + 'meta' => 'tokens', + 'type' => 'csrf' + ] )->query->tokens->csrftoken; +} + /** * 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 + * @param $wiki mw\API or mw\Site + * @param $csrf string The CSRF token + * @param $title string Page title to be created + * @param $wikitext mw\Wikitext Page title to be created + * @param $summary string Summary */ -function wiki_save( $wiki, $csrf, $title, $content, $summary ) { +function wiki_save( $wiki, $csrf, $title, $wikitext, $summary ) { + + static $read = false; + echo "\n"; - echo "########### Saving [[$title]]: ##########\n"; - echo $content; + foreach( explode( "\n", $wikitext->getPrepended() ) as $line ) { + if( $line ) { + echo "+$line\n"; + } + } echo "\n"; echo "#########################################\n"; echo "Confirm summary: |$summary|\n"; - read(); + echo "#########################################\n"; + foreach( $wikitext->getSobstitutions() as $sobstitution ) { + list( $a, $b ) = $sobstitution; + foreach( explode( "\n", $a ) as $line ) { + if( ! empty( $line ) ) { + echo "-$line\n"; + } + } + foreach( explode( "\n", $b ) as $line ) { + if( ! empty( $line ) ) { + echo "+$line\n"; + } + } + echo "\n"; + } + echo "\n"; + foreach( explode( "\n", $wikitext->getAppended() ) as $line ) { + if( $line ) { + echo "+$line\n"; + } + } + echo "########### Saving [[$title]]: ##########\n"; + + if( ! $read ) { + $read = true; + read(); + } else { + sleep(3); + } + return $wiki->post( [ 'action' => 'edit', 'title' => $title, 'summary' => $summary, - 'text' => $content, + 'text' => $wikitext->getWikitext(), '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 wm\Commons::getInstance()->createWikitext( - $page->revisions[0]->{'*'} - ); + if( isset( $page->revisions ) ) { + return wm\Commons::getInstance()->createWikitext( + $page->revisions[0]->{'*'} + ); + } } - throw new Exception("missing page $page"); + return false; } /** * 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; } + +/** + * 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; +}