diff --git a/.gitmodules b/.gitmodules index 1868125..6dc6368 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,18 +1,21 @@ [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 [submodule "2017-11-wikidata-volleyball-players/includes/boz-mw"] path = 2017-11-wikidata-volleyball-players/includes/boz-mw url = https://github.com/valerio-bozzolan/boz-mw +[submodule "2018-08-wiki-loves-monuments-switzerland/boz-mw"] + path = 2018-08-wiki-loves-monuments-switzerland/includes/boz-mw + url = git@github.com:valerio-bozzolan/boz-mw.git diff --git a/2018-08-wiki-loves-monuments-switzerland/README.md b/2018-08-wiki-loves-monuments-switzerland/README.md new file mode 100644 index 0000000..327ac7f --- /dev/null +++ b/2018-08-wiki-loves-monuments-switzerland/README.md @@ -0,0 +1,17 @@ +# 2018 Wiki loves monuments CH + +This PHP bot was developed to prepare Commons and Wikidata to the Wiki loves monuments CH event. + +## Usage + + ./bot.php + +## License + +Copyright (C) 2018 Valerio Bozzolan + +This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with this program. If not, see . diff --git a/2018-08-wiki-loves-monuments-switzerland/bot.php b/2018-08-wiki-loves-monuments-switzerland/bot.php new file mode 100755 index 0000000..1331836 --- /dev/null +++ b/2018-08-wiki-loves-monuments-switzerland/bot.php @@ -0,0 +1,133 @@ +#!/usr/bin/php +login(); +$wikidata = \wm\Wikidata::getInstance()->login(); + +// existing monuments +$results = json_decode( ( new \network\HTTPRequest( 'https://query.wikidata.org/sparql' ) ) + ->fetch( [ + 'format' => 'json', + 'query' => $QUERY + ] ) ) + ->results + ->bindings; + +foreach( $results as $result ) { + + $item = $result->item ->value; + $label_en = $result->labelEn ->value; + $label_native = $result->labelNative->value; + $label = $result->itemLabel ->value; + $status = $result->status ->value; + $canton = $result->canton ->value; + $cantonLabel = $result->cantonLabel->value; + + $item = str_replace( 'http://www.wikidata.org/entity/', '', $item ); + $status = str_replace( 'http://www.wikidata.org/entity/', '', $status ); + $canton = str_replace( 'http://www.wikidata.org/entity/', '', $canton ); + + $is_national = $status === 'Q8274529'; + $is_regional = $status === 'Q12126757'; + + if( ! ( $is_national xor $is_regional ) ) { + Log::warn( "wtf regional or national; skip" ); + continue; + } + + $canton = $CANTON_NAME[ $canton ]; + if( ! $canton ) { + Log::warn( "missing canton $canton $cantonLabel" ); + continue; + } + $commons_canton_category = "Category:$commons_heritage_category"; + + $best_label = coalesce( $label_en, $label_native, $label ); + if( ! $best_label ) { + Log::warn( "skip $item no label" ); + continue; + } + $category_name = "Category:$best_label"; + + $commons_heritage_category = sprintf( + "Cultural properties of %s significance in the canton of $canton", + $is_national ? "national" : "regional", + $canton + ); + + $cat_content = []; + $cat_content[] = "{{Empty category|Populated by [[Commons:Wiki Loves Monuments 2018 in Switzerland|Wiki Loves Monuments 2018 in Switzerland]].}}"; + $cat_content[] = "{{Wikidata Infobox|qid=$item}}"; + $cat_content[] = "[[Category:$commons_canton_category]]"; + + $cat_content = implode( "\n", $cat_content ); + + // confirm save in Commons + if( $ALWAYS || 'y' === cli\Input::yesNoQuestion( "Save [[$category_name]]?" ) ) { + + // save in Commons without overwriting + $status = $commons->post( [ + 'title' => $commons_canton_category, + 'text' => $cat_content, + 'summary' => "[[Commons:Wiki Loves Monuments 2018 in Switzerland]]: creating category for monument [[d:$item]]", + 'createonly' => true, + 'token' => $commons->getToken( mw\Tokens::CSRF ), + ] ); + + if( isset( $status->error ) ) { + if( $status->error->code === 'articleexists' ) { + Log::warn( "The page $cat_content already exists in Commons. skip" ); + continue; + } + } + } + + // create item with data to be added + $data_new = new wb\DataModel(); + + // add Commons category to item + $data_new->addClaim( wb\StatementCommonsMedia( 'P373', $category_name ) ); + + // save the Commons category in Wikidata + $wikidata->post( [ + 'action' => 'wbeditentity', + 'id' => $item, + 'data' => $data_new->getJSON(), + 'summary' => "[[c:Commons:Wiki Loves Monuments 2018 in Switzerland]]: " . $data_new->getEditSummary(), + 'token' => $wikidata->getToken( mw\Tokens::CSRF ), + 'bot' => 1, + ] ) ); +} + +fclose( $out ); + +function coalesce() { + $args = func_get_args(); + foreach( $args as $arg ) { + if( ! empty( $arg ) ) { + return $arg; + } + } + return NULL; +} diff --git a/2018-08-wiki-loves-monuments-switzerland/data/cantons.json b/2018-08-wiki-loves-monuments-switzerland/data/cantons.json new file mode 100644 index 0000000..a74fb30 --- /dev/null +++ b/2018-08-wiki-loves-monuments-switzerland/data/cantons.json @@ -0,0 +1,28 @@ +{ + "Q11972": "Aargau", + "Q12094": "Appenzell Innerrhoden", + "Q12079": "Appenzell Ausserrhoden\u200e", + "Q11911": "Bern\u200e", + "Q12146": "Basel-Land", + "Q12172": "Basel-Stadt", + "Q12640": "Fribourg", + "Q11917": "Geneva\u200e", + "Q11922": "Glarus", + "Q11925": "Graub\u00fcnden", + "Q12755": "Jura", + "Q12121": "Lucerne", + "Q12738": "Neuch\u00e2tel\u200e", + "Q12592": "Nidwalden", + "Q12573": "Obwalden", + "Q12746": "St. Gallen\u200e", + "Q12697": "Schaffhausen", + "Q11929": "Solothurn", + "Q12433": "Schwyz", + "Q12713": "Thurgau", + "Q12724": "Ticino", + "Q12404": "Uri", + "Q834": "Valais", + "Q12771": "Vaud", + "Q11933": "Zug", + "Q11943": "Z\u00fcrich" +} \ No newline at end of file diff --git a/2018-08-wiki-loves-monuments-switzerland/data/query.sparql b/2018-08-wiki-loves-monuments-switzerland/data/query.sparql new file mode 100644 index 0000000..2657dd5 --- /dev/null +++ b/2018-08-wiki-loves-monuments-switzerland/data/query.sparql @@ -0,0 +1,27 @@ +#List of monuments in Switzerland(in English, local language, and fallback language), and status +SELECT DISTINCT ?item ?labelEn ?labelNative ?itemLabel ?languageCode ?itemLabelCode ?status ?statusLabel ?canton ?cantonLabel WHERE { + ?item wdt:P381 ?pbc. + ?item wdt:P1435 ?status. + { + ?item wdt:P131 ?canton. + ?canton wdt:P31 wd:Q23058. + } UNION { + ?item wdt:P131 ?city. + ?city wdt:P131 ?canton. + ?canton wdt:P31 wd:Q23058. + } + OPTIONAL { + ?item rdfs:label ?labelEn + FILTER( LANG( ?labelEn ) = "en") + } + OPTIONAL { + ?item wdt:P131 ?where. + ?where wdt:P37 ?language. + ?language wdt:P218 ?languageCode. + ?item rdfs:label ?labelNative. + FILTER( LANG( ?labelNative ) = ?languageCode ) + } + MINUS { ?item wdt:P373 ?cat. } + SERVICE wikibase:label { bd:serviceParam wikibase:language "en,rm,it,fr,de". } +} +ORDER BY DESC(?labelEn) diff --git a/2018-08-wiki-loves-monuments-switzerland/includes/boz-mw b/2018-08-wiki-loves-monuments-switzerland/includes/boz-mw new file mode 160000 index 0000000..58652cc --- /dev/null +++ b/2018-08-wiki-loves-monuments-switzerland/includes/boz-mw @@ -0,0 +1 @@ +Subproject commit 58652cc657ef2846e8777b42eca16899775d210d