diff --git a/.gitmodules b/.gitmodules index a0693e9..267844b 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,12 +1,15 @@ [submodule "includes/boz-mw"] path = includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw.git [submodule "2018-06-wikidata-volleyball-players/includes/boz-mw"] path = 2018-06-wikidata-volleyball-players/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw [submodule "2018-06-wikidata-soccer-serie-a/includes/boz-mw"] path = 2018-06-wikidata-soccer-serie-a/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw.git [submodule "2018-02-italian-parks/includes/boz-mw"] path = 2018-02-italian-parks/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw.git +[submodule "2018-07-wikipedia-citation-language-migration/includes/boz-mw"] + path = 2018-07-wikipedia-citation-language-migration/includes/boz-mw + url = https://github.com/valerio-bozzolan/boz-mw diff --git a/2018-07-wikipedia-citation-language-migration/bot.php b/2018-07-wikipedia-citation-language-migration/bot.php new file mode 100755 index 0000000..eef6c2e --- /dev/null +++ b/2018-07-wikipedia-citation-language-migration/bot.php @@ -0,0 +1,93 @@ +#!/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 ]; + + $n_languages = preg_match_all( '/[a-zA-Z\-òàùèì]+/', $languages ); + if( $n_languages > 1 ) { + if( false === strpos( $languages, ',' ) ) { + $languages_new = preg_replace( '/\w+/', '$0,', $languages, $n_languages - 1 ); + $wikitext->strReplace( + $prefix . $languages . $suffix, + $prefix . $languages_new . $suffix + ); + $changes[] = "'$languages' → '$languages_new'"; + } else { + cli\Log::debug( "nothing to do on '$languages'" ); + } + } + } + } else { + cli\Log::warn( "[[{$page->title}]] unmatch" ); + } + + if( $wikitext->getSobstitutions() ) { + $changes = array_unique( $changes ); + foreach( $changes as $change ) { + cli\Log::info( "\tChange: $change" ); + } + if( $ALWAYS || 'y' === cli\Input::yesNoQuestion( "Save?" ) ) { + $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, + 'bot' => true, + ] ); + } + } + } +} diff --git a/2018-07-wikipedia-citation-language-migration/includes/boz-mw b/2018-07-wikipedia-citation-language-migration/includes/boz-mw new file mode 160000 index 0000000..948a7c9 --- /dev/null +++ b/2018-07-wikipedia-citation-language-migration/includes/boz-mw @@ -0,0 +1 @@ +Subproject commit 948a7c9b30897fe80a447c0bf069b5b43c559efb