diff --git a/2018-08-wiki-loves-monuments-switzerland/README.md b/2018-08-wiki-loves-monuments-switzerland/README.md index 327ac7f..1dee3ca 100644 --- a/2018-08-wiki-loves-monuments-switzerland/README.md +++ b/2018-08-wiki-loves-monuments-switzerland/README.md @@ -1,17 +1,23 @@ # 2018 Wiki loves monuments CH This PHP bot was developed to prepare Commons and Wikidata to the Wiki loves monuments CH event. +## Consensus + +Well the consensus for a Wikimedia Foundation event is pleonastic... anyway: + +* https://commons.wikimedia.org/wiki/Commons:Bots/Requests/Valerio_Bozzolan_bot_(4) + ## 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 index bc7837e..542e498 100755 --- a/2018-08-wiki-loves-monuments-switzerland/bot.php +++ b/2018-08-wiki-loves-monuments-switzerland/bot.php @@ -1,166 +1,160 @@ #!/usr/bin/php login(); $wikidata = \wm\Wikidata::getInstance()->login(); // existing monuments $results = \wm\Wikidata::querySPARQL( $QUERY ); foreach( $results as $result ) { // normalize stuff $entity_id = $result->item ->value; $label_en = @$result->labelEn ->value; $label_native = @$result->labelNative->value; - $label = $result->itemLabel ->value; + $label = @$result->itemLabel ->value; $status = $result->status ->value; $canton = $result->canton ->value; - $cantonLabel = $result->cantonLabel->value; + $cantonLabel = @$result->cantonLabel->value; $entity_id = str_replace( 'http://www.wikidata.org/entity/', '', $entity_id ); $status = str_replace( 'http://www.wikidata.org/entity/', '', $status ); $canton = str_replace( 'http://www.wikidata.org/entity/', '', $canton ); // create Commons canton name if( ! isset( $CANTON_NAME->{ $canton } ) ) { Log::warn( "missing canton $canton $cantonLabel" ); continue; } $canton = $CANTON_NAME->{ $canton }; // create Commons category title $category_name = ucfirst( coalesce( $label_en, $label_native, $label ) ); $category_name_prefixed = "Category:$category_name"; if( ! $category_name ) { Log::warn( "skip $entity_id no best label" ); continue; } Log::info( "Commons category: [[$category_name_prefixed]]" ); // create Commons heritage category $is_national = $status === 'Q8274529'; $is_regional = $status === 'Q12126757'; if( ! ( $is_national xor $is_regional ) ) { Log::warn( "wtf regional or national; skip" ); continue; } $commons_heritage_category = sprintf( "Cultural properties of %s significance in the canton of $canton", $is_national ? "national" : "regional", $canton ); $commons_canton_category = "Category:$commons_heritage_category"; // fetch existing entity $entity = $wikidata->fetchSingleEntity( $entity_id, [ 'props' => 'claims' ] ); // entity has images? if( $entity->hasClaimsInProperty( 'P373' ) ) { Log::warn( "already has Commons category; skip" ); continue; } // manual check asd - if( 'n' === cli\Input::yesNoQuestion( "Do you like [[$category_name_prefixed]]?" ) ) { + $url = 'https://commons.wikimedia.org/wiki/' . rawurlencode( $category_name_prefixed ); + if( 'n' === cli\Input::yesNoQuestion( "Do you like [[$category_name_prefixed]]? https://www.wikidata.org/wiki/$entity_id $url" ) ) { continue; } $category_has_images = false; // has images? if( $entity->hasClaimsInProperty( 'P18' ) ) { foreach( $entity->getClaimsInProperty( 'P18' ) as $claim ) { $image = $claim->getMainsnak()->getDataValue()->getValue(); // categorize that image with the unexisting category Log::info( "categorize [[File:$image]] under [[$category_name_prefixed]]" ); $commons->edit( [ 'title' => "File:$image", 'appendtext' => "\n[[$category_name_prefixed]]", 'summary' => "[[Commons:Wiki Loves Monuments 2018 in Switzerland]]: +[[$category_name_prefixed]]", 'bot' => 1, ] ); $category_has_images = true; } } Log::info( $category_has_images ? "has images" : "no images" ); // Commons category content $cat_content = ""; if( ! $category_has_images ) { $cat_content .= "{{Empty category|Populated by [[Commons:Wiki Loves Monuments 2018 in Switzerland|Wiki Loves Monuments 2018 in Switzerland]].}}\n"; } $cat_content .= "{{Wikidata Infobox|qid=$entity_id}}\n"; $cat_content .= "[[Category:$commons_heritage_category]]"; // confirm save in Commons echo "---\n$cat_content\n---\n"; Log::info( "save Commons [[$category_name_prefixed]]" ); try { // save in Commons without overwriting $commons->edit( [ 'title' => $category_name_prefixed, 'text' => $cat_content, - 'summary' => "[[Commons:Wiki Loves Monuments 2018 in Switzerland]]: creating category for monument [[d:$entity_id|$category_name]] $NOTES", + 'summary' => "[[Commons:Wiki Loves Monuments 2018 in Switzerland]] ([[Commons:Bots/Requests/Valerio Bozzolan bot (4)|not complete consensus]]): creating category for monument [[d:$entity_id|$category_name]] $NOTES", 'createonly' => true, - - /** - * No consensus for bot flag on Wikimedia Commons yet - * - * @see https://commons.wikimedia.org/wiki/Commons:Bots/Requests/Valerio_Bozzolan_bot_(4) - */ - // 'bot' => 1, No consensus + 'bot' => 1, ] ); } catch( mw\API\ArticleExistsException $e ) { Log::warn( "The page [[$category_name_prefixed]] already exists in Commons. skip" ); if( 'y' === cli\Input::yesNoQuestion( "Skip [[$category_name_prefixed]]?" ) ) { continue; } } // save the Commons category in Wikidata Log::info( "saving Commons category in Wikidata" ); // save the Commons category in Wikidata - $entity_data = new wb\DataModel( $wikidata ); + $entity_data = $entity->cloneEmpty(); $entity_data->addClaim( new wb\StatementCommonsCategory( 'P373', $category_name ) ); $entity_data->editEntity( [ - 'id' => $entity_id, 'summary' => "[[c:Commons:Wiki Loves Monuments 2018 in Switzerland]]: " . $entity_data->getEditSummary(), 'bot' => 1, ] ); } 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/query.sparql b/2018-08-wiki-loves-monuments-switzerland/data/query.sparql index 2657dd5..de41d81 100644 --- a/2018-08-wiki-loves-monuments-switzerland/data/query.sparql +++ b/2018-08-wiki-loves-monuments-switzerland/data/query.sparql @@ -1,27 +1,30 @@ -#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. +# Monuments in Switzerland with English label but without Commons category +SELECT DISTINCT ?item ?itemLabel ?status ?canton WHERE { + + # heritage status ?item wdt:P1435 ?status. + + # has Swiss code + ?item wdt:P381 []. + + # has English label + ?item rdfs:label ?itemLabel. + FILTER( LANG( ?itemLabel ) = "en" ). + + # canton + ?canton wdt:P31 wd:Q23058. + + # solve cantons inconsistences { ?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") + + # without commons category + MINUS { + ?item wdt:P373 []. } - 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)