diff --git a/2018-08-wiki-loves-monuments-switzerland/sectioner.php b/2018-08-wiki-loves-monuments-switzerland/sectioner.php new file mode 100755 index 0000000..8e33e48 --- /dev/null +++ b/2018-08-wiki-loves-monuments-switzerland/sectioner.php @@ -0,0 +1,62 @@ +#!/usr/bin/php +fetch( [ + 'action' => 'query', + 'prop' => 'revisions', + 'titles' => $title, + 'rvprop' => 'content', + 'rvslots' => 'main', + 'rvlimit' => 1, + ] ); + + foreach( $result->query->pages as $page ) { + $old_wikitext = $page->revisions[ 0 ]->slots->main->{ '*' }; + $old_wikitext = str_replace( "{{Cultural heritage CH header 2018}}\n", '', $old_wikitext ); + $towns = 0; + $last_town = null; + if( $old_wikitext ) { + $old_lines = explode( "\n", $old_wikitext ); + $new_lines = []; + foreach( $old_lines as $line ) { + if( 1 === preg_match( '/Town *= *([^|]+)\|/', $line, $matches ) ) { + $town = $matches[ 1 ]; + if( $town !== $last_town ) { + if( $last_town !== null ) { + $new_lines[] = '|}'; + } + $new_lines[] = "{{Cultural heritage CH header 2018|$town}}"; + $towns++; + } + $last_town = $town; + } + $new_lines[] = $line; + } + $wikitext = implode( "\n", $new_lines ); + \wm\Commons::getInstance()->login()->edit( [ + 'title' => $title, + 'summary' => sprintf( + 'split in %d town headings', + $towns + ), + 'text' => $wikitext, + + // to operate like a human and avoid Wikimedia Commons bot request for permission :| + 'bot' => 0, + ] ); + } + } +}