diff --git a/2018-07-wikipedia-citation-language-migration/bot.php b/2018-07-wikipedia-citation-language-migration/bot.php index 91b69e0..8825142 100755 --- a/2018-07-wikipedia-citation-language-migration/bot.php +++ b/2018-07-wikipedia-citation-language-migration/bot.php @@ -1,99 +1,102 @@ #!/usr/bin/php . define( 'SUMMARY', 'Bot: '. '[[Discussioni modulo:Citazione#Gestione lingue composte da più di una parola|nuovo separatore per le lingue]] '. '([[Speciale:PermaLink/98190278#Gestione lingue composte da più di una parola|perm]])' ); $ALWAYS = true; // load boz-mw and configuration require 'includes/boz-mw/autoload.php'; require '../config.php'; $wit = \wm\WikipediaIt::getInstance()->login(); $members = $wit->createQuery( [ 'action' => 'query', 'generator' => 'categorymembers', 'gcmtitle' => 'Categoria:Voci con modulo citazione e valori lingua da separare con virgola', 'prop' => 'revisions', 'rvprop' => [ 'content', 'timestamp', ], ] ); foreach( $members->getGenerator() as $response ) { foreach( $response->query->pages as $page ) { cli\Log::info( "[[{$page->title}]]" ); $wikitext = $wit->createWikitext( $page->revisions[ 0 ]->{ '*' } ); $n = $wikitext->pregMatchAll( '/(\|[ \t\n]*lingua[ \t\n]*=[ \t\n]*)([a-zA-Zòàùèì,\- \t\n]+)(}}|\|)/', $matches ); $changes = []; if( $n ) { for( $i = 0; $i < $n; $i++ ) { $complete = $matches[ 0 ][ $i ]; $prefix = $matches[ 1 ][ $i ]; $languages = $matches[ 2 ][ $i ]; $suffix = $matches[ 3 ][ $i ]; if( false !== strpos( $languages, 'lingua ' ) ) { cli\Log::debug( "\t skip '$languages'") ; continue; } $n_languages = preg_match_all( '/[a-zA-Z\-òàùèì]+/', $languages ); if( $n_languages > 1 ) { if( false === strpos( $languages, ',' ) ) { $languages_new = preg_replace( '/[a-zA-Z\-òàùèì]+/', '$0,', $languages, $n_languages - 1 ); $wikitext->strReplace( $prefix . $languages . $suffix, $prefix . $languages_new . $suffix ); $changes[] = "'$languages' → '$languages_new'"; } else { cli\Log::debug( "\t nothing to do on '$languages'" ); } } } } else { - cli\Log::warn( "\t unmatch" ); + cli\Log::warn( "\t unmatch (try nulledit)" ); } - if( $wikitext->getSobstitutions() ) { - $changes = array_unique( $changes ); - foreach( $changes as $change ) { - cli\Log::info( "\t Change: $change" ); - } - if( $ALWAYS || 'y' === cli\Input::yesNoQuestion( "Save?" ) ) { + if( $ALWAYS || 'y' === cli\Input::yesNoQuestion( "Save?" ) ) { + if( $changes ) { + $changes = array_unique( $changes ); + foreach( $changes as $change ) { + cli\Log::info( "\t Change: $change" ); + } $summary = SUMMARY . ': ' . implode( '; ', $changes ); - $wit->post( [ - 'action' => 'edit', - 'pageid' => $page->pageid, - 'basetimestamp' => $page->revisions[ 0 ]->timestamp, - 'text' => $wikitext->getWikitext(), - 'token' => $wit->getToken( \mw\Tokens::CSRF ), - 'summary' => $summary, - 'minor' => true, - 'bot' => true, - ] ); + } else { + cli\Log::info( "\t null edit" ); + $summary = '[[Aiuto:Null edit|null edit]]'; } + $wit->post( [ + 'action' => 'edit', + 'pageid' => $page->pageid, + 'basetimestamp' => $page->revisions[ 0 ]->timestamp, + 'text' => $wikitext->getWikitext(), + 'token' => $wit->getToken( \mw\Tokens::CSRF ), + 'summary' => $summary, + 'minor' => true, + 'bot' => true, + ] ); } } }