diff --git a/tools/wikidata-blame.php b/tools/wikidata-blame.php new file mode 100755 --- /dev/null +++ b/tools/wikidata-blame.php @@ -0,0 +1,138 @@ +#!/usr/bin/php +. + +// exit if not CLI +$argv or exit( 1 ); + +// load the framework +require __DIR__ . '/../autoload.php'; + +// load configuration or create it +ConfigWizard::requireOrCreate( __DIR__ . '/config.php' ); + +use \cli\Log; +use \cli\Input; +use \cli\Opts; +use \mw\API\PageMatcher; + +// 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 ); +} + +// try to open the file +$file = fopen( $filename, 'w' ); +if( !$file ) { + Log::error( "Can't open file '$filename'" ); + exit( 1 ); +} + +$wikidata = \wm\Wikidata::instance(); +$wikidata->login(); + +https://www.wikidata.org/w/api.php?action=help&modules=query%2Busercontribs + +$requests = $wikidata->createQuery( [ + 'action' => 'query', + 'titles' => $page_titles, + 'prop' => 'revisions', + 'rvprop' => [ + 'ids', + 'flags', + 'timestamp', + 'user', + 'userid', + 'size', + 'slotsize', + 'sha1', + 'comment', + 'content', + ], + 'rvslots' => 'main', + 'rvlimit' => $limit, +] ); + +