Changeset View
Changeset View
Standalone View
Standalone View
wikidatablame.php
- This file was added.
Property | Old Value | New Value |
---|---|---|
File Mode | null | 100755 |
<?php | |||||
# Copyright (C) 2020 Valerio Bozzolan | |||||
# | |||||
# This program is free software: you can redistribute it and/or modify | |||||
# it under the terms of the GNU Affero 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 Affero 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 <http://www.gnu.org/licenses/>. | |||||
// exit if not CLI | |||||
$argv or exit( 1 ); | |||||
// load the framework | |||||
require __DIR__ . '/../autoload.php'; | |||||
use \cli\Log; | |||||
use \cli\Input; | |||||
use \cli\Opts; | |||||
use \mw\API\PageMatcher; | |||||
use \cli\ConfigWizard; | |||||
use \cli\ParamFlag; | |||||
\mw\API::$DEFAULT_MAXLAG = 150; | |||||
// load configuration or create it | |||||
ConfigWizard::requireOrCreate( __DIR__ . '/config.php' ); | |||||
// register all CLI parameters | |||||
$opts = new Opts( [ | |||||
new ParamFlag( 'help', 'h', "Show this help and quit" ), | |||||
] ); | |||||
// error messages to be displayed | |||||
$messages = []; | |||||
// command line arguments | |||||
$arguments = Opts::unnamedArguments(); | |||||
if( count( $arguments ) < 2 ) { | |||||
$messages[] = "Please specify both the SPARQL file and the user name to blame"; | |||||
} | |||||
// take the SPARQL file pathname | |||||
$sparql_file = array_shift( $arguments ); | |||||
if( !file_exists( $sparql_file ) ) { | |||||
$messages[] = sprintf( | |||||
"The file %s does not exist", | |||||
$sparql_file | |||||
); | |||||
} | |||||
// read the SPARQL query | |||||
$sparql_query = file_get_contents( $sparql_file ); | |||||
if( !$sparql_query ) { | |||||
$messages[] = sprintf( | |||||
"Cannot read a non-empty SPARQL query from the file %s", | |||||
$sparql_file | |||||
); | |||||
} | |||||
// user names to blame | |||||
$user_names = $arguments; | |||||
// show the help | |||||
$show_help = $opts->getArg( 'help' ); | |||||
if( $show_help ) { | |||||
$messages = []; | |||||
} else { | |||||
$show_help = $messages; | |||||
} | |||||
// eventually show the help message | |||||
if( $show_help ) { | |||||
echo "Usage:\n {$argv[ 0 ]} FILE.sparql USER_NAME\n"; | |||||
echo "Allowed OPTIONS:\n"; | |||||
foreach( $opts->getParams() as $param ) { | |||||
$commands = []; | |||||
if( $param->hasLongName() ) { | |||||
$commands[] = '--' . $param->getLongName(); | |||||
} | |||||
if( $param->hasShortName() ) { | |||||
$commands[] = '-' . $param->getShortName(); | |||||
} | |||||
$command = implode( '|', $commands ); | |||||
if( $command && ! $param->isFlag() ) { | |||||
$command .= $param->isValueOptional() | |||||
? '=[VALUE]' | |||||
: '=VALUE'; | |||||
} | |||||
printf( ' % -20s ', $command ); | |||||
if( $param->hasDescription() ) { | |||||
echo ' ' . $param->getDescription(); | |||||
} | |||||
echo "\n"; | |||||
} | |||||
foreach( $messages as $msg ) { | |||||
echo "\nError: $msg"; | |||||
} | |||||
echo "\n"; | |||||
exit( $opts->getArg( 'help' ) ? 0 : 1 ); | |||||
} | |||||
// activate Wikidata power and login | |||||
$wikidata = \wm\Wikidata::instance(); | |||||
$wikidata->login(); | |||||
// an array of ids | |||||
$ids = []; | |||||
// execs the query | |||||
$rows = $wikidata->querySPARQL( $sparql_query ); | |||||
$results = 0; | |||||
$contribs = 0; | |||||
foreach ($rows as $row) { | |||||
// add id to array; | |||||
$ids[] = basename($row->item->value); | |||||
} | |||||
echo "Starting looking for contributions...\n"; | |||||
// and now the magic begins - looks for usercontribs | |||||
// https://www.wikidata.org/w/api.php?action=help&modules=query%2Busercontribs | |||||
$requests = $wikidata->createQuery( [ | |||||
'action' => 'query', | |||||
'list' => 'usercontribs', | |||||
'ucuser' => $user_names, | |||||
'ucprop' => ['ids', 'title', 'timestamp'], | |||||
'ucdir' => 'newer', | |||||
'uclimit' => 'max', | |||||
] ); | |||||
foreach ($requests as $request) { | |||||
$items = $request->query->usercontribs; | |||||
foreach($items as $item) { | |||||
if(in_array($item->title, $ids, true)) { | |||||
echo("\n \n \033[1m" . $item->timestamp . ' - https://www.wikidata.org/w/index.php?diff='. $item->revid . "\033[0m \n \n"); | |||||
$results += 1; | |||||
} | |||||
$contribs += 1; | |||||
$lastcontrib = $item->timestamp; | |||||
} | |||||
echo '- Read contributions: ' . $contribs . "\n"; | |||||
echo '- Last contributions read: ' . $lastcontrib . "\n"; | |||||
} | |||||
echo "\033[1m - Total RESULTS: " . $results . "\033[0m \n"; | |||||
echo '- Read contributions: ' . $contribs . "\n"; | |||||
echo '- Last contributions read: ' . $lastcontrib . "\n"; | |||||
if ($results === 0) { | |||||
echo "Sorry, no results found!\n"; | |||||
} | |||||
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register