App-wdq-0.4.4000755001750001750 012701414674 11615 5ustar00vojvoj000000000000README100644001750001750 30712701414674 12536 0ustar00vojvoj000000000000App-wdq-0.4.4NAME App::wdq - command line access to Wikidata Query Service DESCRIPTION See command line script wdq for documentation. COPYRIGHT AND LICENSE Copyright Jakob Voss, 2015- GPL 2.0 Changes100644001750001750 554612701414674 13203 0ustar00vojvoj000000000000App-wdq-0.4.4Revision history for App-wdq 0.4.4 2016-04-07 10:26:33 CEST - New options "label", "description", and "monochrome" - Allow repeated options label, description, and text - Renamed output format "pretty" to "text" - Fix and extend help output (#23, #24) - Fix text template format with uppercase field names 0.4.3 2015-11-14 22:18:06 CET - New option text to easily get names and descriptions - Show count in text format when counting 0.4.2 2015-11-10 15:10:09 CET - New option count - Minor fixes and cleanup 0.4.1 2015-10-27 13:44:50 CET - Fixed test - New option enumerate 0.4.0 2015-10-26 11:55:47 CET - Implemented search mode - Added colorized XML output format - Refactoring 0.3.6 2015-10-22 20:05:48 CEST - More condense simple output format with color (#10) - New output format pretty based on string templates - Extended and reorganized help 0.3.5 2015-10-20 12:25:46 CEST - Use MediaWiki API to resolve URLs to Wikidata items - New option no-mediawiki to disable MediaWiki API requests - Validate option language - SPARQL Query Results JSON format with color (#18) - refactoring 0.3.4 2015-10-19 16:10:08 CEST - New option no-header to omit header in CSV or export format - Extend option ids to more types of Wikidata URIs 0.3.3 2015-10-17 21:11:41 CEST - Better abbreviated URIs as qnames - Fix lookup of "Property:P_" 0.3.2 2015-10-16 14:14:24 CEST - Fix zero-result-bug in lookup mode - Support limit in lookup mode - Enable streaming output of simple JSON format 0.3.1 2015-10-16 11:13:18 CEST - Simplify lookup by passing ids as command line arguments - Implement proper exit codes and option to ignore errors 0.3.0 2015-10-15 16:17:33 CEST - Add lookup mode to look up Wikidata ids by URL/ID 0.2.2 2015-10-12 13:19:57 CEST - Added options to limit number of results - Make brackets in abbreviated query optional - Extended ontology help - Add Blazegraph namespaces bd: and hint: 0.2.1 2015-10-08 12:27:02 CEST - Added option to show Wikibase ontology in a nutshell - Improved colors 0.2.0 2015-10-07 12:32:52 CEST - Made simple format the default - Fixed SPARQL csv format, including colors - Added output format ldjson (Line Delimited JSON) - Support export via Catmandu for more export formats 0.1.2 2015-10-06 15:50:29 CEST - Added option ids to abbreviate Wikidata identifiers 0.1.1 2015-10-06 12:43:48 CEST - Added output formats csv and simple 0.1.0 2015-10-04 17:20:41 CEST - Renamed option --sparql to --query - Renamed option --sparql to --query - Renamed option --dry to --no-execute - Validate SPARQL with RDF::Query - Support output format tsv - Test with correct perl executable (issuee #3) 0.0.1 2015-10-02 22:29:07 CEST - Inital release at CPAN t000755001750001750 012701414674 12001 5ustar00vojvoj000000000000App-wdq-0.4.4bin.t100644001750001750 167312701414674 13105 0ustar00vojvoj000000000000App-wdq-0.4.4/tuse v5.14; use Test::More; use Test::Output; my $exit; sub wdq { system($^X, 'script/wdq', @_); $exit = $? >> 8 } sub slurp { local (@ARGV,$/) = shift; <> } foreach my $opt (qw(--help -h -?)) { output_like { wdq $opt } qr/^wdq /, qr/^$/, 'help'; is $exit, 0; } foreach my $opt (qw(--version -V)) { output_like { wdq $opt } qr/^wdq \d+\.\d+\.\d+/, qr/^$/, 'version'; is $exit, 0; } output_like { wdq '--foo' } qr/^$/, qr/^Unknown option: foo/, 'unknown option'; is $exit, 1; stderr_is { wdq '--no-color', '--query', '{ ?s ?p x:foo }' } "invalid SPARQL query\n", "validate SPARQL"; is $exit, 1; stderr_is { wdq '--no-color', '--format', 'x' } "unknown format: x\n", "unknown format"; is $exit, 1; stdout_is { wdq qw(-nq t/examples/stackexchange.query) } slurp('t/examples/stackexchange.sparql'), 'no-execute'; stderr_is { wdq '-g123' } "invalid language(s): 123\n", "invalid language(s)"; is $exit, 1; done_testing; LICENSE100644001750001750 4347012701414674 12733 0ustar00vojvoj000000000000App-wdq-0.4.4This software is Copyright (c) 2015- by -. This is free software, licensed under: The GNU General Public License, Version 2, June 1991 GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Lesser General Public License instead of this License. cpanfile100644001750001750 43412701414674 13363 0ustar00vojvoj000000000000App-wdq-0.4.4requires 'perl', '>= 5.14.0'; requires 'HTTP::Tiny','0.039'; # libhttp-tiny-perl requires 'RDF::Query','2.902'; # librdf-query-perl requires 'RDF::Trine','1.010'; # implies JSON and URI test_requires 'Test::More'; test_requires 'Test::Output'; test_requires 'Test::PerlTidy'; dist.ini100644001750001750 23612701414674 13323 0ustar00vojvoj000000000000App-wdq-0.4.4author = Jakob Voß name = App-wdq [@Milla] [ReadmeAnyFromPod / ReadmeMarkdownInRoot] source_filename = script/wdq filename = README.md Build.PL100644001750001750 25412701414674 13153 0ustar00vojvoj000000000000App-wdq-0.4.4# This Build.PL for App-wdq was generated by Dist::Zilla::Plugin::ModuleBuildTiny 0.014. use strict; use warnings; use v5.14.0; use Module::Build::Tiny 0.034; Build_PL(); META.yml100644001750001750 152412701414674 13151 0ustar00vojvoj000000000000App-wdq-0.4.4--- abstract: 'command line access to Wikidata Query Service' author: - '-' build_requires: Test::More: '0' Test::Output: '0' Test::PerlTidy: '0' configure_requires: Module::Build::Tiny: '0.034' dynamic_config: 0 generated_by: 'Dist::Zilla version 5.035, Dist::Milla version v1.0.8, CPAN::Meta::Converter version 2.150001' license: gpl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: App-wdq no_index: directory: - t - xt - inc - share - eg - examples requires: HTTP::Tiny: '0.039' RDF::Query: '2.902' RDF::Trine: '1.010' perl: v5.14.0 resources: bugtracker: https://github.com/nichtich/wdq/issues homepage: https://github.com/nichtich/wdq repository: https://github.com/nichtich/wdq.git version: 0.4.4 x_contributors: - 'Marius Hoch ' MANIFEST100644001750001750 100212701414674 13020 0ustar00vojvoj000000000000App-wdq-0.4.4# This file was automatically generated by Dist::Zilla::Plugin::Manifest v5.035. Build.PL CONTRIBUTING.md Changes LICENSE MANIFEST META.json META.yml README cpanfile dist.ini lib/App/wdq.pm script/wdq t/bin.t t/examples/count.json t/examples/label.sparql t/examples/lookup t/examples/query.query t/examples/response.json t/examples/search.json t/examples/stackexchange.query t/examples/stackexchange.sparql t/format-text.t t/format.t t/lookup.t t/options.t t/perltidy.t t/query.t t/release-pod-syntax.t t/search.t query.t100644001750001750 301412701414674 13471 0ustar00vojvoj000000000000App-wdq-0.4.4/tuse v5.14; use Test::More; use Test::Output; sub wdq { system( $^X, 'script/wdq', '-n', @_ ) } sub slurp { local (@ARGV,$/) = shift; <> } my $sparql = < PREFIX wdt: SELECT * WHERE { ?c wdt:P361 wd:Q544 . } SPARQL foreach my $query ( '?c wdt:P361 wd:Q544', '{ ?c wdt:P361 wd:Q544 }', 'SELECT * WHERE { ?c wdt:P361 wd:Q544 }', $sparql ) { stdout_is { wdq -q => $query } $sparql, substr $query, 0, 20; stdout_is { wdq $query } $sparql, substr $query, 0, 20; } stdout_is { wdq( -q => '?a ?b ?c', '--limit' => 1 ) } "SELECT * WHERE {\n ?a ?b ?c .\n}\nLIMIT 1\n", '--limit'; stdout_is { wdq( -q => 'SELECT * WHERE { ?a ?b ?c } LIMIT 2', '--limit' => 3 ) } "SELECT * WHERE {\n ?a ?b ?c .\n}\nLIMIT 3\n", '--limit (override)'; stdout_is { wdq( '-53q' => '?a ?b ?c', '--limit' => 6 ) } "SELECT * WHERE {\n ?a ?b ?c .\n}\nLIMIT 3\n", 'limit -3'; stdout_is { wdq( '-cx', '?x wdt:P31 wd:Q5633421' ) } < PREFIX wdt: SELECT (COUNT(DISTINCT ?x) AS ?count) WHERE { ?x wdt:P31 wd:Q5633421 . } SPARQL stdout_is { system( $^X, 'script/wdq', '-cx', '?x wdt:P31 wd:Q5633421', '--response', 't/examples/count.json', '-ftext' ) } "63107\n", 'text format for count'; stdout_is { wdq( '-lid,x', '-tx', '-gde', '?id ?p ?x' ) } slurp('t/examples/label.sparql'), '--label/description'; done_testing; META.json100644001750001750 316512701414674 13324 0ustar00vojvoj000000000000App-wdq-0.4.4{ "abstract" : "command line access to Wikidata Query Service", "author" : [ "-" ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 5.035, Dist::Milla version v1.0.8, CPAN::Meta::Converter version 2.150001", "license" : [ "gpl_2" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "App-wdq", "no_index" : { "directory" : [ "t", "xt", "inc", "share", "eg", "examples" ] }, "prereqs" : { "configure" : { "requires" : { "Module::Build::Tiny" : "0.034" } }, "develop" : { "requires" : { "Dist::Milla" : "v1.0.8", "Test::Pod" : "1.41" } }, "runtime" : { "requires" : { "HTTP::Tiny" : "0.039", "RDF::Query" : "2.902", "RDF::Trine" : "1.010", "perl" : "v5.14.0" } }, "test" : { "requires" : { "Test::More" : "0", "Test::Output" : "0", "Test::PerlTidy" : "0" } } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/nichtich/wdq/issues" }, "homepage" : "https://github.com/nichtich/wdq", "repository" : { "type" : "git", "url" : "https://github.com/nichtich/wdq.git", "web" : "https://github.com/nichtich/wdq" } }, "version" : "0.4.4", "x_contributors" : [ "Marius Hoch " ] } script000755001750001750 012701414674 13042 5ustar00vojvoj000000000000App-wdq-0.4.4wdq100755001750001750 12117212701414674 13767 0ustar00vojvoj000000000000App-wdq-0.4.4/script#!/usr/bin/env perl use v5.14; use Getopt::Long; use Scalar::Util qw(blessed); use Encode qw(decode_utf8 encode_utf8); use JSON; our $VERSION = '0.4.4'; # Assume everything outside is UTF-8 binmode( STDIN, ":encoding(UTF-8)" ); binmode( STDOUT, ":encoding(UTF-8)" ); binmode( STDERR, ":encoding(UTF-8)" ); @ARGV = map { decode_utf8($_) } @ARGV; # get command line options my %OPT; Getopt::Long::Configure('bundling'); GetOptions( \%OPT, 'help|h|?', 'version|V', 'man', 'ontology', 'api=s', 'format|f=s', 'query|q=s', 'ids|i!', 'language|g=s', 'label|l=s@', 'description|d=s@', 'text|t=s@', 'enumerate|e!', 'color|C!', 'monochrome|M', 'count|c=s', 'header!', 'H!', 'no-execute|n!', 'no-mediawiki|m!', 'N!', 'ignore!', 'limit=i', '1!', '2!', '3!', '4!', '5!', '6!', '7!', '8!', '9!', 'default-prefixes!', 'response=s', # not documented, does not respect --limit 'export=s', 'force!', ) or exit 1; # use color by default if output is terminal $OPT{color} = 0 if $OPT{monochrome}; $OPT{color_stderr} = $OPT{color} // -t STDERR; ## no critic $OPT{color} //= -t STDOUT; ## no critic my %COLORS = ( t => "\e[1;39m", # title : bold v => "\e[0;32m", # value: green n => "\e[0;34m", # name: blue i => "\e[0;33m", # identifier: yellow e => "\e[1;31m", # error: bold red ); sub cBold { $OPT{color} ? "$COLORS{t}$_[0]\e[0m" : $_[0]; } sub cValue { $OPT{color} ? "$COLORS{v}$_[0]\e[0m" : $_[0]; } sub cName { $OPT{color} ? "$COLORS{n}$_[0]\e[0m" : $_[0]; } sub cIdentifier { $OPT{color} ? "$COLORS{i}$_[0]\e[0m" : $_[0]; } sub warning { say STDERR $OPT{color_stderr} ? "$COLORS{e}$_[0]\e[0m" : $_[0]; } my %NAMESPACES = ( # standard ontologies rdf => 'http://www.w3.org/1999/02/22-rdf-syntax-ns#', xsd => 'http://www.w3.org/2001/XMLSchema#', rdfs => 'http://www.w3.org/2000/01/rdf-schema#', owl => 'http://www.w3.org/2002/07/owl#', skos => 'http://www.w3.org/2004/02/skos/core#', schema => 'http://schema.org/', geo => 'http://www.opengis.net/ont/geosparql#', prov => 'http://www.w3.org/ns/prov#', # Wikibase ontology wikibase => 'http://wikiba.se/ontology#', wd => 'http://www.wikidata.org/entity/', wdt => 'http://www.wikidata.org/prop/direct/', wds => 'http://www.wikidata.org/entity/statement/', p => 'http://www.wikidata.org/prop/', wdref => 'http://www.wikidata.org/reference/', wdv => 'http://www.wikidata.org/value/', ps => 'http://www.wikidata.org/prop/statement/', psv => 'http://www.wikidata.org/prop/statement/value/', pq => 'http://www.wikidata.org/prop/qualifier/', pqv => 'http://www.wikidata.org/prop/qualifier/value/', pr => 'http://www.wikidata.org/prop/reference/', prv => 'http://www.wikidata.org/prop/reference/value/', wdno => 'http://www.wikidata.org/prop/novalue/', # blazegraph SPARQL extensions hint => 'http://www.bigdata.com/queryHints#', bd => 'http://www.bigdata.com/rdf#', bds => 'http://www.bigdata.com/rdf/search#', fts => 'http://www.bigdata.com/rdf/fts#', # not used in Wikidata Query Service wdata => 'http://www.wikidata.org/wiki/Special:EntityData/', cc => 'http://creativecommons.org/ns#', ); my $LANGUAGE_PATTERN = qr{^([a-z]+(-[a-zA-Z0-9]+)*)$}; my $ENTITY_PATTERN = qr{^ (https?://www\.wikidata\.org/ (wiki|entity|wiki/Special:EntityData)/)? (?(q|(Property:)?p)\d+) $}ix; my $WIKIDATA_ID_PATTERN = qr{^ http://www\.wikidata\.org/ ( entity (/statement)? | reference | value | prop (/(statement|qualifier|reference)/value)?) /(?.+) $}x; my $SITELINK_PATTERN = qr{^ https?:// (? ( [^.]+. ( wikipedia | wiktionary | wikibooks | wikiquote | wikisource | wikinews | wikiversity | wikivoyage ) | ( commons | species )\.wikimedia )\.org) /wiki/ (?.+) }x; sub pod_text { require Pod::Usage; my $text; open my $out, '>', \$text; Pod::Usage::pod2usage( -exit => 'NOEXIT', -output => $out, -verbose => 99, indent => 2, @_ ); $text =~ s/^(.[^\s].+:)$/cBold($1)/mge; # headers $text =~ s/("[^"\n]+")/cValue($1)/mge; # strings $text =~ s/(<[^->\n][^>\n]+>)/cIdentifier($1)/mge; # URLs $text; } # help mode if ( $OPT{help} ) { @ARGV = qw(help); } elsif ( $OPT{version} ) { @ARGV = qw(help version); } elsif ( $OPT{namespaces} ) { warning("--namespaces is deprecated. Use 'wdq help prefixes' instead!"); @ARGV = qw(help prefixes); } elsif ( $OPT{ontology} ) { warning("--ontology is deprecated. Use 'wdq help ontology' instead!"); @ARGV = qw(help ontology); } if ( @ARGV and $ARGV[0] eq 'help' ) { shift @ARGV; my $topic = lc( shift @ARGV ); my $help; if ( $topic =~ /^ver(sion)?$/ ) { $help = cBold('wdq') . " $VERSION\n"; } elsif ( $topic =~ /^pref?(ix(es)?)?$/ ) { $help = join( "\n", map { sprintf "%8s: %s", $_, cIdentifier( $NAMESPACES{$_} ); } sort keys %NAMESPACES ) . "\n"; $help =~ s/^([^:]+):/cName($1).":"/mge; } elsif ( $topic =~ /^out(put)?$/ ) { $help = pod_text( -sections => ['OUTPUT'] ); $help =~ s/( (name|value|identifier|title|error))/$COLORS{substr $2, 0,1}.$1."\e[0m"/mge if $OPT{color}; } elsif ( $topic eq 'pretty' ) { $help = pod_text( -sections => ['OUTPUT/Pretty'] ); $help =~ s/( (name|value|identifier|title|error))/$COLORS{substr $2, 0,1}.$1."\e[0m"/mge if $OPT{color}; } elsif ( $topic =~ /^formats?$/ ) { $help = pod_text( -sections => ['OUTPUT/Formats'] ); } elsif ( $topic =~ /^modes?$/ ) { $help = pod_text( -sections => ['MODES'] ); } elsif ( $topic =~ /^exp?(ort)?$/ ) { $help = pod_text( -sections => ['OUTPUT/Export'] ); } elsif ( $topic =~ /^ont(ology)?$/ ) { $help = pod_text( -sections => ['WIKIDATA ONTOLOGY'] ); $help =~ s/^.*Wikidata Ontology:.*\n//; $help =~ s/^( | )//mg; $help =~ s/^([a-z]+)/cBold($1)/mgei; $help =~ s/ ([A-Z][A-Za-z]+)/" ".cBold($1)/mge; $help =~ s/ ([a-z]+:([a-zA-Z_]+)?|[a-z][a-zA-Z]+)/" ".cName($1)/mge; $help =~ s/(@[a-z_]+)/cIdentifier($1)/mge; } elsif ( $topic =~ /^opt(ions?)?$/ ) { $help = pod_text( -sections => ['OPTIONS'] ); $help =~ s/^( *--?[^ ]+( [^ ].*)?)$/cName($1)/mge; # options $help =~ s/^\n//gm; # remove empty lines } else { $help = pod_text( -msg => 'wdq' . ' [ ' . join( ' ] [ ', cName('MODE'), cName('OPTIONS'), cName('REQUEST') . ' | ' . cName('< REQUEST_FILE') ) . " ]\n", -sections => ['SYNOPSIS'], ); $help =~ s/\n\n -/\n -/gm; $help =~ s/^ / /mg; } print $help; exit; } if ( $OPT{man} ) { my $module = $OPT{color} ? 'Pod::Text::Color' : 'Pod::Text'; # may fail if pure script installed by hand eval "require $module; require App::wdq"; ## no critic $module->new->parse_from_file( $INC{'App/wdq.pm'} // $0 ); exit; } # default SPARQL endpoint $OPT{api} //= 'https://query.wikidata.org/bigdata/namespace/wdq/sparql'; # add default prefixes by default $OPT{'default-prefixes'} //= 1; # include header in output $OPT{header} //= $OPT{H} ? 0 : 1; # limit given as single digit option foreach ( grep { $OPT{$_} } 1 .. 9 ) { $OPT{limit} = $_ if !$OPT{limit} or $OPT{limit} > $_; } # validate language and set default value if missing $OPT{language} //= do { my $l = $ENV{LANG} // 'en'; $l =~ s/_.*//; $l }; $OPT{language} = lc( $OPT{language} ); if ( grep { $_ !~ $LANGUAGE_PATTERN } split ",", $OPT{language} ) { warning("invalid language(s): $OPT{language}"); exit 1; } # disable all requests if ( $OPT{N} ) { $OPT{'no-mediawiki'} = 1; $OPT{'no-execute'} = 1; } my $MODE = !@ARGV ? 'query' : do { my $arg = $ARGV[0]; $arg =~ s/^\s*|\s*$//g; if ( $arg =~ /^(query|lookup|p?search)$/ ) { lc( shift @ARGV ); } elsif ( $arg =~ $ENTITY_PATTERN or $arg =~ $SITELINK_PATTERN ) { 'lookup'; } else { my $guess = () = $arg =~ /[a-z]+:[^\s]/gi; $guess += () = $arg =~ /<[^>]+>/g; $guess += () = $arg =~ /[?\$][^\s]/g; if ( $guess > 2 ) { warning("ignoring additional command line argument") if $OPT{query} or @ARGV > 1; 'query'; } else { 'search'; } } }; # default output format if ( $OPT{format} =~ /{[^}]+}/ ) { $OPT{pretty} = $OPT{format}; $OPT{format} = 'text'; } else { $OPT{format} = lc( $OPT{format} // ( $MODE =~ /^p?search$/ ? 'text' : 'simple' ) ); if ( $OPT{format} eq 'text' ) { $OPT{pretty} //= $OPT{count} ? "{count|style=v}" : "{id|style=i}{label|style=v|pre=: }" . "{alias|pre= (|post=)|style=v}" . "{description|length=78|pre=\n }"; $OPT{ids} //= 1; } } # require only if actually needed require RDF::Query; # monkey-patch RDF::Query to keep minimum required version at Ubuntu 14.04 LTS require version; if ( version->parse($RDF::Query::VERSION) < version->parse('2.915_01') ) { require RDF::Query::Parser::SPARQL; *RDF::Query::Node::Resource::as_sparql = sub { my $self = shift; my $context = shift || {}; my $uri = $self->uri_value; my $ns = $context->{namespaces} || {}; my %ns = %$ns; foreach my $k ( keys %ns ) { no warnings 'uninitialized'; if ( $k eq '__DEFAULT__' ) { $k = ''; } my $v = $ns{$k}; if ( index( $uri, $v ) == 0 ) { my $local = substr( $uri, length($v) ); if ( $local =~ $RDF::Query::Parser::SPARQL::r_PN_LOCAL ) { my $qname = join( ':', $k, $local ); return $qname; } } } '<' . URI->new( encode_utf8( $self->uri_value ) )->canonical . '>'; } } my $EXPORTER; if ( $OPT{export} || $OPT{format} eq 'export' ) { $EXPORTER = eval { require Catmandu; Catmandu->exporter( $OPT{export} // 'JSON', header => $OPT{header} ); }; if ($@) { warning("option export requires Perl module " . "Catmandu::Exporter::$OPT{export}" ); exit 1; } elsif ( $OPT{format} !~ /^(ldjson|simple)$/ ) { warning("option export overrides option format"); Catmandu->load(); } $OPT{format} = 'export'; } # output formats package App::wdq::Output { sub add { my ( $self, $row ) = @_; $self->{format}{row}->( $self, $row ); } sub end { my ($self) = @_; return unless $self->{format}{post}; $self->{format}{post}->($self); } sub print { my $self = shift; print { $self->{out} } @_; } sub say { my $self = shift; say { $self->{out} } @_; } } package App::wdq::Format { sub new { my $class = shift; bless {@_}, $class; } sub start { my ( $self, $vars, $out ) = @_; my $output = bless { format => $self, vars => $vars, out => $out }, 'App::wdq::Output'; $self->{pre}->($output) if $self->{pre}; return $output; } } sub simple_node { if ( !blessed( $_[0] ) ) { ''; } elsif ( $_[0]->is_resource ) { $_[0]->uri_value; } elsif ( $_[0]->is_literal ) { $_[0]->literal_value; } else { $_[0]->sse; } } sub simple_row { my ( $row, $vars ) = @_; my $simple = { map { $_ => simple_node( $row->{$_} ) } grep { defined $row->{$_} } @$vars }; # TODO: add language #if (blessed $row->{label} && $row->{label}->is_literal) { # $simple->{language} = $row->{label}->literal_value_language; #} $simple; } sub json { ( state $JSON= JSON->new->canonical->allow_nonref )->encode( $_[0] ); } sub pretty_json { ( state $JSON= JSON->new->pretty->canonical )->encode( $_[0] ); } sub xml { my $text = shift; $text =~ s/&/&/gm; $text =~ s/</</gm; $text =~ s/>/>/gm; $text; } sub xmlattr { my $text = xml(shift); $text =~ s/"/"/gm; $text; } sub expand_format_string { my ( $string, $vars ) = @_; $string =~ s!{\s*([^}]+)}! my @a = split /\|/, $1; my $s = $vars->{shift @a}; my %o = map { /([^=]+)(=(.*))?/s; ($1 => $3) } @a; pretty_variable( $s, \%o ); !eg; $string; } sub pretty_variable { my ( $value, $p ) = @_; if ( !defined $value or $value eq '' ) { $value = $p->{default} // return ''; } if ( defined $p->{length} ) { if ( length $value > $p->{length} ) { $value = substr( $value, 0, $p->{length} - 1 ) . "\x{2026}"; } elsif ( defined $p->{align} ) { my $f = $p->{length} . 's'; $value = sprintf( $p->{align} eq 'right' ? "%$f" : "%-$f", $value ); } } if ( $OPT{color} && $COLORS{ $p->{style} } ) { $value = $COLORS{ $p->{style} } . $value . "\e[0m"; } $value = $p->{pre} . $value if defined $p->{pre}; $value = $value . $p->{post} if defined $p->{post}; $value; } my %FORMATS = ( # SPARQL Query Results JSON json => App::wdq::Format->new( pre => sub { my $o = shift; $o->print( "{\n \"head\": {\n \"vars\": [", join( ", ", map { cName( json($_) ) } @{ $o->{vars} } ), "]\n },\n \"results\": {\n \"bindings\": [" ); }, row => sub { my ( $o, $row ) = @_; $o->print( $o->{count}++ ? ", {\n" : " {\n" ); my $delim = 0; foreach my $v ( @{ $o->{vars} } ) { next unless defined $row->{$v}; $o->say(",") if $delim++; $o->print( " " . cName( json($v) ) . ": " ); my $node = $row->{$v}->as_hashref; $node->{'xml:lang'} = delete $node->{lang} if $node->{lang}; my $json = pretty_json($node); $json =~ s/^ / /mg; $json =~ s/}\n$/ }/; $json =~ s/^(\s+"[a-z:]+")\s*:(.*")(,)?$/"$1:".cValue($2).$3/mge; $o->print($json); } $o->print("\n }"); }, post => sub { $_[0]->print(" ]\n }\n}\n"); } ), # SPARQL Query Results XML xml => App::wdq::Format->new( pre => sub { my $o = shift; $o->say('<?xml version="1.0"?>'); $o->say('<sparql xmlns="http://www.w3.org/2005/sparql-results#">'); $o->say(' <head>'); foreach ( @{ $o->{vars} } ) { my $name = cName( xmlattr($_) ); $o->say(" <variable name=\"$name\"/>"); } $o->say(' </head>'); $o->say(' <results>'); }, row => sub { my ( $o, $row ) = @_; $o->say(' <result>'); foreach ( @{ $o->{vars} } ) { my $node = $row->{$_} // next; my $name = cName( xmlattr($_) ); $o->say(" <binding name=\"$name\">"); if ( $node->is_blank ) { $o->say( " <bnode>" . xml( $node->value ) . '</node>' ); } elsif ( $node->is_resource ) { $o->say(" <uri>" . cIdentifier( xml( $node->uri_value ) ) . '</uri>' ); } else { my $literal = " <literal"; if ( $node->literal_value_language ) { $literal .= ' xml:lang="' . cIdentifier( xmlattr( $node->literal_value_language ) ) . '"'; } elsif ( $node->literal_datatype ) { $literal .= ' datatype="' . cIdentifier( xmlattr( $node->literal_datatype ) ) . '"'; } $o->say("$literal>" . cValue( xml( $node->literal_value ) ) . "</literal>" ); } $o->say(' </binding>'); } $o->say(' </result>'); }, post => sub { $_[0]->say(' </results>'); $_[0]->say('</sparql>'); } ), # SPARQL TSV tsv => App::wdq::Format->new( pre => sub { my $o = shift; $o->say( join( "\t", map { "?$_" } @{ $o->{vars} } ) ); }, row => sub { my ( $o, $row ) = @_; $o->say( join "\t", map { blessed($_) ? $_->as_ntriples : '' } map { $row->{$_} } @{ $o->{vars} } ); } ), # SPARQL CSV csv => App::wdq::Format->new( pre => sub { my $o = shift; $o->say( join ',', map { cName($_) } @{ $o->{vars} } ) if $OPT{header}; }, row => sub { my ( $o, $row ) = @_; $o->say( join ',', map { my $s = simple_node( $row->{$_} ); if ( $s =~ /[",\x0A\x0D]/ ) { $s =~ s/"/""/g; $s = "\"$s\""; } cValue($s) } @{ $o->{vars} } ); } ), # simple JSON key-value structure simple => App::wdq::Format->new( pre => sub { my $o = shift; $o->{count} = 0; $o->print("[ "); }, row => sub { my ( $o, $row ) = @_; my $json = pretty_json( simple_row( $row, $o->{vars} ) ); chomp $json; $json =~ s/^\s+("[^"]+") : (".*")(,)?$/ ' '.cName($1).': '.cValue($2).$3/mge; $o->print(",") if $o->{count}++; $o->print($json); }, post => sub { my $o = shift; $o->say( $o->{count} ? " ]" : "]" ); } ), # simple line-delimited JSON key-value structure ldjson => App::wdq::Format->new( row => sub { my ( $o, $row ) = @_; $o->say( json( simple_row( $row, $o->{vars} ) ) ); } ), # pipe to Catmandu exporter export => App::wdq::Format->new( pre => sub { $_[0]->{exporter} = $EXPORTER; }, row => sub { my ( $o, $row ) = @_; $o->{exporter}->add( simple_row( $row, $o->{vars} ) ); }, post => sub { $_[0]->{exporter}->commit; } ), # format string text => App::wdq::Format->new( row => sub { my ( $o, $row ) = @_; $row = { map { $_ => simple_node( $row->{$_} ) } keys %$row }; $o->say( expand_format_string( $OPT{pretty}, $row ) ); } ), ); if ( $MODE =~ /^p?search$/ ) { $OPT{ids} //= 1; } my $format = $FORMATS{ $OPT{format} } // do { warning("unknown format: $OPT{format}"); exit 1; }; sub expand_query { my $query = shift; my $select = "*"; # TODO: only in query mode if ( $OPT{count} ) { fatal("cannot expand query with COUNT") if $query =~ /^\s*SELECT/i; $select = "(COUNT(DISTINCT ?$OPT{count}) AS ?count)"; } if ( $query =~ /^\s*{/m ) { $query = "SELECT $select WHERE $query"; } elsif ( $query !~ /^[^{]*SELECT/ ) { $query = "SELECT $select WHERE { $query }"; } # LIMIT and OFFSET are always last so we can safely use regexp here if ( $OPT{limit} ) { $query =~ s/\n*LIMIT\s+\d+(\s+OFFSET\s+\d+)?\s*$/$1/sm; $query .= "\nLIMIT $OPT{limit}"; } my %namespace; if ( $OPT{'default-prefixes'} ) { # Add PREFIX for actually used and known prefixes my $ps = join '|', keys %NAMESPACES; $namespace{$_} = $NAMESPACES{$_} for $query =~ /($ps):[^\/]/mg; } # TODO: only in query mode. # TODO: test this my %textvar; # allow repeated and/or comma/space separated argument foreach my $field (qw(label description text)) { $OPT{$field} = [ map { split /\s*,\s*|\s+/, $_ } @{ $OPT{$field} // [] } ]; } $textvar{$_}{label} = 1 for @{ $OPT{label} }; $textvar{$_}{description} = 1 for @{ $OPT{description} }; $textvar{$_}{label} = $textvar{$_}{description} = 1 for @{ $OPT{text} }; if (%textvar) { if ( $OPT{count} ) { warning("Option --text/label/description only slow down --count"); } my @services; foreach my $var ( sort keys %textvar ) { my $s = <<SPARQL; SERVICE wikibase:label { bd:serviceParam wikibase:language "$OPT{language}" . SPARQL if ( $textvar{$var}{label} ) { my $label = $var eq 'id' ? 'label' : $var . 'Label'; $s .= " ?$var rdfs:label ?$label .\n"; } if ( $textvar{$var}{description} ) { my $descr = $var eq 'id' ? 'description' : $var . 'Description'; $s .= " ?$var schema:description ?$descr .\n"; } push @services, "$s }"; } my $service = join "\n", @services; $query =~ s/}([^}]*)$/$service}\1/m; $namespace{$_} = $NAMESPACES{$_} for qw(bd rdfs schema wikibase); } my @prefixes = map { "PREFIX $_: <$namespace{$_}>" } sort keys %namespace; $query = join "\n", @prefixes, $query; my $sparql = RDF::Query->new($query); if ( !$sparql ) { if ( $OPT{'force'} ) { warning("SPARQL query seems invalid"); } else { warning("invalid SPARQL query"); exit 1; } } else { $query = $sparql->as_sparql; $query =~ s/^\s*{}\s*$//mg; # BUG IN RDF::Query $query =~ s/\n$//mg; $query =~ s/LANG\(LANG\(\?/LANGMATCHES(LANG(?/mg; # BUG in RDF::Query $query =~ s/\t/ /mg; } if ( $OPT{'no-execute'} ) { say $query; return; } return $sparql ? ( $query, [ map { $_->name } @{ $sparql->parsed->{variables} } ] ) : ($query); } sub http_get { my ( $url, %query ) = @_; if ( $OPT{response} ) { local ( @ARGV, $/ ) = $OPT{response}; return <>; } require HTTP::Tiny; my $http = HTTP::Tiny->new( default_headers => { agent => "wdq/$VERSION" }, timeout => 30, ); $query{format} = 'json'; $url .= '?' . $http->www_form_urlencode( \%query ); my $res = $http->get($url); if ( !$res->{success} ) { if ( $OPT{ignore} ) { return; } else { warning("HTTP request failed"); say STDERR $res->{content}; exit 1; } } $res->{content}; } sub get_qid_from_sitelink { my $api = "https://$_[0]/w/api.php"; my $title = $_[1]; my $res = http_get( $api, action => 'query', prop => 'pageprops', titles => $title, redirects => 1 ); my $data = JSON->new->decode($res); my ($page) = values %{ $data->{query}->{pages} }; return unless $page->{pageprops}; return $page->{pageprops}->{wikibase_item}; } sub get_lookup_query { my $id = shift; my $entity_id; if ( $id =~ $ENTITY_PATTERN ) { $entity_id = $+{id}; $entity_id =~ s/Property://i; } else { # URL could be percent-encoded or not, so normalize my $uri = URI->new($id); if ( $uri and $uri->canonical =~ $SITELINK_PATTERN ) { my ( $base, $title ) = ( $+{base}, $+{title} ); # unescape to UTF-8 octets $title =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; $title = decode_utf8($title); if ( $OPT{'no-mediawiki'} ) { warning("MediaWiki API disabled"); exit 1; } $entity_id = get_qid_from_sitelink( $base, $title ); unless ($entity_id) { warning("Wikidata item not found: $id"); return; } } } unless ($entity_id) { warning("unknown identifier: $id"); return; } my $uri = "http://www.wikidata.org/entity/" . uc($entity_id); return <<SPARQL; SELECT ?id ?label ?description WHERE { BIND(<$uri> AS ?id) SERVICE wikibase:label { bd:serviceParam wikibase:language "$OPT{language}" . ?id rdfs:label ?label ; schema:description ?description } } SPARQL } sub literal { RDF::Trine::Node::Literal->new(@_) } sub map_ids { my $row = shift; foreach my $name ( keys %$row ) { my $v = $row->{$name}; next unless $v->isa('RDF::Trine::Node::Resource'); my $id; $id = $+{id} if $v->uri_value =~ $ENTITY_PATTERN; $id = $+{id} if $v->uri_value =~ $WIKIDATA_ID_PATTERN; $row->{$name} = literal($id) if $id; } $row; } sub get_sparql { my $json = http_get( $OPT{api}, query => shift ); require RDF::Trine::Iterator::JSONHandler; $json ? RDF::Trine::Iterator::JSONHandler->new->parse($json) : RDF::Trine::Iterator->new( [], 'bindings', [] ); } sub map_searchresponse_to_row { my $res = shift; my $language = $OPT{language}; # TODO: only first my $row = { id => RDF::Trine::Node::Resource->new( $res->{concepturi} ), label => literal( $res->{label}, $language ), }; if ( defined $res->{description} ) { $row->{description} = literal( $res->{description}, $language ); } if ( $res->{match}{type} eq 'alias' ) { $row->{alias} = literal( $res->{match}{text}, $language ); } $row; } sub perform_search { my $query = shift; if ( $OPT{'no-mediawiki'} ) { warning("MediaWiki API disabled"); exit 1; } my $language = $OPT{language}; # TODO: only first my $api = "https://www.wikidata.org/w/api.php"; my $res = http_get( $api, action => 'wbsearchentities', language => $language, uselang => $language, strictlanguage => '1', type => $MODE eq 'search' ? 'item' : 'property', search => $query, $OPT{limit} ? ( limit => $OPT{limit} ) : (), ); my $data = JSON->new->decode($res); $data = [ map { map_searchresponse_to_row($_) } @{ $data->{search} } ]; require RDF::Trine::Iterator::Bindings; RDF::Trine::Iterator::Bindings->new($data); } sub postprocess_iterator { my ( $iterator, @map ) = @_; # abbreviate identifiers push @map, \&map_ids if $OPT{ids}; # enumerate results my $n = 1; if ( $OPT{enumerate} ) { push @map, sub { my $row = shift; $row->{n} = literal( $n++ ); $row; } } # apply postprocessing if (@map) { $iterator = RDF::Trine::Iterator::smap( sub { my $row = shift; $row = $_->($row) for @map; $row; }, $iterator ); } $iterator; } my ( $variables, $iterator, $query ); # query mode if ( $MODE eq 'query' ) { $OPT{query} //= shift @ARGV if @ARGV; $OPT{query} //= '-'; my $query = do { local $/ = undef; if ( $OPT{query} eq '-' ) { <STDIN>; } elsif ( -f $OPT{query} or $OPT{query} !~ /[$\?\{]/ ) { open my $fh, '<', $OPT{query} or die "failed to open file " . $OPT{query}; <$fh>; } else { $OPT{query}; } }; ( $query, $variables ) = expand_query($query); exit unless $query; $iterator = get_sparql($query); } # lookup mode elsif ( $MODE eq 'lookup' ) { require URI; my $output; my $lookup = sub { my $id = shift; $id =~ s/^\s+|\s+$//g; # trim whitespace return if $id eq ''; # skip empty lines my $query = get_lookup_query($id) or return; ($query) = expand_query($query); return unless $query; my $iterator = get_sparql($query); $iterator = postprocess_iterator($iterator); my $vars = [qw(id label description)]; unshift( @$vars, 'n' ) if $OPT{enumerate}; $output //= $format->start( $vars, \*STDOUT ); while ( my $row = $iterator->next ) { $output->add($row); } }; my $limit = $OPT{limit} // 0; if (@ARGV) { foreach (@ARGV) { $lookup->($_); last if !--$limit; } } else { while (<>) { $lookup->($_); last if !--$limit; } } $output->end() if $output; exit; } # search mode elsif ( $MODE =~ /^p?search$/ ) { $query = join ' ', @ARGV; $variables = [qw(id label alias description)]; $iterator = perform_search($query); } unless ( $OPT{ignore} || $iterator->peek ) { warning( $query ? "not found: $query" : "not found" ); exit 1; } # postprocess results unshift( @$variables, 'n' ) if $OPT{enumerate} and $variables; $iterator = postprocess_iterator($iterator); # emit results { my $vars = $variables // sort keys %{ $iterator->peek }; my $output = $format->start( $vars, \*STDOUT ); my $n = 0; while ( my $row = $iterator->next ) { last if $OPT{limit} and $n++ >= $OPT{limit}; $output->add($row); } $output->end; } __END__ =head1 NAME wdq - command line access to Wikidata Query Service =begin markdown # STATUS [![Build Status](https://travis-ci.org/nichtich/wdq.png)](https://travis-ci.org/nichtich/wdq) [![Coverage Status](https://coveralls.io/repos/nichtich/App-wdq/badge.png)](https://coveralls.io/r/nichtich/App-wdq) [![Kwalitee Score](http://cpants.cpanauthors.org/dist/App-wdq.png)](http://cpants.cpanauthors.org/dist/App-wdq) =end markdown =head1 SYNOPSIS Access L<Wikidata Query Service|https://query.wikidata.org/> via command line to perform SPARQL queries (C<query> mode), lookup entities (C<lookup>), or search items and properties (C<search> or C<psearch>): wdq -g en solar system # search 'solar system' in English wdq psearch -g es parte # search property 'parte' in Spanish wdq P361 Q544 # lookup properties and items wdq '?c wdt:P361 wd:Q544' # query parts of the solar system See the manual for details or get help via C<wdq help>: wdq help options # list and explain command line options wdq help modes # list and explain request modes wdq help output # explain output control wdq help formats # list and explain output formats wdq help ontology # show Wikidata ontology in a nutshell wdq help prefixes # list RDF prefixes allowed in queries wdq help version # show version of wdq =head1 DESCRIPTION The command line script C<wdq>, included in CPAN module L<App::wdq>, provides a tool to access L<Wikidata Query Service|https://query.wikidata.org/>. It supports formulation and execution of L<SPARQL SELECT queries|http://www.w3.org/TR/sparql11-query/#select> to extract selected information from Wikidata or other Wikibase instances. =head1 INSTALLATION Perl should already installed at most operating systems. Otherwise L<get Perl!|https://www.perl.org/get.html> =head2 FROM CPAN Install sources from CPAN including all dependencies: cpanm App::wdq First L<install cpanm|https://github.com/miyagawa/cpanminus/#installation> if missing. If installation of C<App::wdq> fails try cpanm option C<--notest> or install dependencies as packages as described below. =head2 PREBUILD PACKAGES Install dependencies as prebuild packages for your operating system: # Debian based systems e.g. Ubuntu (>= 14.04) sudo apt-get install libhttp-tiny-perl librdf-query-perl # Windows/ActiveState ppm install HTTP-Tiny ppm install RDF-Query Then install C<wdq> from CPAN as described above or copy the script to some place in your C<$PATH>: wget https://github.com/nichtich/wdq/raw/master/script/wdq chmod +x wdq The latter method will not install this documentation. =head1 MODES Request mode C<query> (default), C<lookup>, C<serch>, or C<psearch> can explicitly be set via first argument or it's guessed from arguments. =head2 query Read SPARQL query from STDIN, option C<--query|-q>, or argument. Namespace definitions and C<SELECT> clause are added if missing. wdq '?c wdt:P361 wd:Q544' wdq '{ ?c wdt:P361 wd:Q544 }' # equivalent wdq 'SELECT * WHERE { ?c wdt:P361 wd:Q544 }' # equivalent wdq < queryfile =head2 lookup Read Wikidata entity ids, URLs, or Wikimedia project URLs from STDIN or arguments. Result fields are C<label>, C<description>, and C<id>: wdq Q1 wdq lookup Q1 # equivalent echo Q1 | wdq lookup # equivalent wdq http://de.wikipedia.org/wiki/Universum # same result =encoding utf8 =head2 search / psearch Search for items or properties. Result fields are C<label>, C<id>, C<description>, and possibly matched C<alias>. Search and result language is read from environment or option C<--language>/C<-g>: wdq search -g sv Pippi Långstrump Default output format in search mode is C<text>. =head1 OPTIONS =over =item --query|-q QUERY Query or query file (C<-> for STDIN as default) =item --format|-f FORMAT|TEMPLATE Output format or string template. Call C<wdq help formats> for details. =item --export EXPORTER Use a L<Catmandu> exporter as output format. =item --no-header|-H Exclude header in CSV output or other exporter. =item --enumerate|-e Enumerate results by adding a counter variable C<n> =item --limit INTEGER Add or override a LIMIT clause to limitate the number of results. Single-digit options such as C<-1> can also be used to also set a limit. =item --ids|-i Abbreviate Wikidata identifier URIs as strings. =item --language|-g Language to query labels and descriptions in. Set to the locale by default. This option is currentl only used on lookup mode. =item --count|-c VARS Prepend SPARQL QUERY to count distinct values =item --label|-l VARS =item --description|-d VARS =item --text|-t VARS Add label, description, or both. Adds C<label>/C<description> for variable C<id> or C<xLabel>/C<xDescription> for any C<x>. =item --ignore Ignore empty results instead of issuing warning and exit code. =item --color|-C By default output is colored if writing to a terminal. Disable this with C<--no-color>, C<--monochrome>, or C<-M>. Force color with C<--color> or C<-C>. =item --api URL SPARQL endpoint. Default value: C<https://query.wikidata.org/bigdata/namespace/wdq/sparql> =item --no-mediawiki|-m Don't query MediaWiki API to map URLs to Wikidata items. =item --no-execute|-n Don't execute SPARQL queries but show them in expanded form. Useful to validate and pretty-print queries. MediaWiki API requests may be =item -N Don't execute any queries. Same as C<--no-mediawiki --no-execute>. =item --help|-h|-? Show usage help =item --ontology Show information about the Wikidata Ontology =item --no-default-prefixes Don't add default namespace prefixes to the SPARQL query =item --man Show detailled manual =item --version|-V Show version if this script =back =head1 OUTPUT Output can be controlled with options C<--format>/C<-f>, C<--export>, C<--header>/C<--no-header>/C<-H>, and C<--color>/C<--no-color>/C<-C>. =head2 Formats Option C<--format>/C<-f> sets an output format or string template: =over =item C<simple> (default in query and lookup mode) Flat JSON without language tags =item C<text> (default in search mode) Print C<label>, C<alias>, C<id> and C<description> or C<count> when counting. Also sets option C<--ids>. =item C<ldjson> Line delimited Flat JSON =item C<csv> SPARQL Query Results CSV Format. Suppress header with option C<--no-header>/C<-H>. Use Catmandu CSV exporter for more options =item C<tsv> SPARQL Query Results TSV Format =item C<xml> SPARQL Query Results XML Format =item C<json> SPARQL Query Results JSON Format =item C<...> String template. Call C<wdq help pretty> for details =back =head2 Pretty Option C<--format> can be set to a string template with bracket expressions with optional template parameters (for instance C<{id|pre= (|post=)}>). =over =item style Highlight C<n> name, C<v> value, C<i> identifier, C<t> title, or C<e> error =item length Abbreviate long values =item align Use C<left> or C<right> to align short values to a given C<length> =item pre/post Add string before/after value =back =head2 Export Option C<--export> sets a L<Catmandu> exporter to create output with. Given the corresponding exporter modules installed, one can write results as C<YAML>, Excel (C<XLS>), and Markdown table (C<Table>) among other formats: wdq --export YAML # short form wdq --format ldjson | catmandu convert to YAML # equivalent Use Catmandu config file (C<catmandu.yml>) to further configure export. See also tools such as L<jq|http://stedolan.github.io/jq/> and L<miller|http://johnkerl.org/miller/> for processing results. =head1 EXAMPLES # search "solar system" in English (=> Q544) wdq -g en solar system # search part-of property (=> P361) wdq psearch -g en part # get all parts of the solar system wdq '?c wdt:P361 wd:Q544' # look up label and description wdq Q42 P9 # look up German Wikipedia article and get label description in French wdq -g fr http://de.wikipedia.org/wiki/Argon # get all references used at an item wdq 'wd:Q1 ?prop [ prov:wasDerivedFrom ?ref ]' # get doctoral advisor graph (academic genealogy) as CSV wdq '?student wdt:P184 ?advisor' --ids --format csv # print expanded SPARQL query wdq -n '?c wdt:P361 wd:Q544' # execute query and return first 10 tab-separated values wdq -f tsv --limit 10 < query # print result as Markdown Table (requires Catmandu::Exporter::Table) wdq --export Table < query # count instances (P31) of books (Q571) wdq --count x '?x wdt:P31 wd:Q571' # list types (P279) of Exoplanets (Q44559) with label and description wdq '?id wdt:P279 wd:Q44559:' --text id --format text =head1 WIKIDATA ONTOLOGY Entity (item/property) wd:Q* <-- owl:sameAs --> wd:Q* --> rdfs:label, skos:altLabel, schema:description "*"@* --> schema:dateModified, schema:version --> wdt:P* "*", URI, _:blank --> p:P* Statement Item wd:Q* <-- schema:about <http://*.wikipedia.org/wiki/*> --> schema:inLanguage, wikibase:badge Property wd:P* --> wikibase:propertyType PropertyType --> wkibase:directClaim wdt:P* --> wikibase:claim p:P* --> wikibase:statementProperty ps:P* --> wikibase:statementValue psv:P* --> wikibase:qualifier pq:P* --> wikibase:qualifierValue pqv:P* --> wikibase:reference pr:P* --> wikibase:referenceValue prv:P* --> wikibase:novalue wdno:P* PropertyType wikibase: String, Url, WikibaseItem, WikibaseProperty, CommonsMedia, Math, Monolingualtext, GlobeCoordinate, Quantity, Time, ExternalId Statement wds:* --> wikibase:rank Rank --> a wdno:P* --> ps:P* "*", URI, _:blank --> psv:P* Value --> pq:P* "*", URI, _:blank --> pqv:P* Value --> prov:wasDerivedFrom Reference Reference wdref:* --> pr:P* "*", URI --> prv:P* Value Rank wikibase: NormalRank, PreferredRank, DeprecatedRank, BestRank Value (GlobecoordinateValue/QuantityValue/TimeValue) wdv:* --> wikibase: geoLatitude, geoLongitude, geoPrecision, geoGlobe URI --> wikibase: timeValue, timePrecision, timeTimezone, timeCalendarModel --> wikibase: quantityAmount, quantityUpperBound, quantityLowerBound, quantityUnit URI =head1 COPYRIGHT AND LICENSE Copyright by Jakob Voss C<voss@gbv.de> Based on a PHP script by Marius Hoch C<hoo@online.de> at L<https://github.com/mariushoch/asparagus>. Licensed under GPL 2.0+ =cut ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������format.t��������������������������������������������������������������������������������������������100644��001750��001750�� 4275�12701414674� 13626� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������use v5.14; use Test::More; use Test::Output; my $exit; sub wdq { system($^X, qw(script/wdq --response t/examples/response.json), @_) } stdout_is { wdq qw(-q t/examples/query.query -fjson -i) } <<'OUT', 'json'; { "head": { "vars": ["z", "x", "y"] }, "results": { "bindings": [ { "z": { "type": "literal", "value": "☃\n", "xml:lang": "en" }, "x": { "type": "literal", "value": "Q123" }, "y": { "type": "uri", "value": "http://www.example.org/" } } ] } } OUT stdout_is { wdq qw(-q t/examples/query.query) } <<OUT, 'simple (default)'; [ { "x": "http://www.wikidata.org/entity/Q123", "y": "http://www.example.org/", "z": "☃\\n" } ] OUT stdout_is { wdq qw(-f csv -q t/examples/query.query) } <<OUT, 'csv'; z,x,y "\x{e2}\x{98}\x{83} ",http://www.wikidata.org/entity/Q123,http://www.example.org/ OUT stdout_is { wdq qw(-f csv -H -q t/examples/query.query) } <<OUT, 'csv w/o header'; "\x{e2}\x{98}\x{83} ",http://www.wikidata.org/entity/Q123,http://www.example.org/ OUT stdout_is { wdq qw(-f tsv -q t/examples/query.query) } <<OUT, 'tsv'; ?z\t?x\t?y "\\u2603\\n"\@en\t<http://www.wikidata.org/entity/Q123>\t<http://www.example.org/> OUT stdout_is { wdq qw(-f ldjson -q t/examples/query.query) } <<OUT, 'ldjson'; {"x":"http://www.wikidata.org/entity/Q123","y":"http://www.example.org/","z":"☃\\n"} OUT stdout_is { wdq qw(--ids -f tsv -q t/examples/query.query) } <<OUT, 'ids'; ?z\t?x\t?y "\\u2603\\n"\@en\t"Q123"\t<http://www.example.org/> OUT if ( eval { require Catmandu::Exporter::Table; 1 } ) { output_is { wdq qw(--export Table -q t/examples/query.query -f csv) } <<OUT, "option export overrides option format\n", 'export'; | x | y | z | |-------------------------------------|-------------------------|----| | http://www.wikidata.org/entity/Q123 | http://www.example.org/ | ☃ | OUT stdout_is { wdq qw(--export CSV --no-header -q t/examples/query.query) } "http://www.wikidata.org/entity/Q123,http://www.example.org/,\"☃\\n\"\n", "exporter with --no-header"; } else { note "Skipping option --export" } done_testing; �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������lookup.t��������������������������������������������������������������������������������������������100644��001750��001750�� 3150�12701414674� 13636� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������use v5.14; use Test::More; use Test::Output; my $sparql = <<SPARQL; PREFIX bd: <http://www.bigdata.com/rdf#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX schema: <http://schema.org/> PREFIX wikibase: <http://wikiba.se/ontology#> SELECT * WHERE { BIND(<http://www.wikidata.org/entity/Q243972> AS ?id) SERVICE wikibase:label { bd:serviceParam wikibase:language "fr" . ?id rdfs:label ?label . ?id schema:description ?description . } } SPARQL stdout_is { system("$^X script/wdq lookup -gfr -n < t/examples/lookup") } $sparql x 8, 'lookup given qid from STDIN'; my @sitelinks = ( " http://de.wikipedia.org/wiki/Erdős-Zahl", "\thttps://de.wikipedia.org/wiki/Erd%C5%91s-Zahl\n", ); output_is { system($^X, 'script/wdq', '-N', 'lookup', @sitelinks) } "", "MediaWiki API disabled\n", "--no-mediawiki --no-execute"; SKIP: { skip 'release test requiring network', 1 unless $ENV{RELEASE_TESTING}; output_is { system($^X, 'script/wdq', '-gfr', '-n', @sitelinks) } $sparql x 2, "", "N: lookup Wikidata id via MediaWiki API"; my $cmd = "echo $sitelinks[0] | $^X script/wdq -gen lookup -f csv -Hi"; stdout_like { system($cmd) } qr{^Q243972,Erdős}, "N: lookup via MediaWiki API and SPARQL (STDIN)"; } { $sparql =~ s/Q243972/P7/m; $sparql =~ s/"fr"/"es,en-simple"/m; my $cmd = join ' ', "$^X script/wdq --language es,en-simple -n", 'http://www.wikidata.org/wiki/Property:P7', 'Property:P7', ' P7', 'p7 '; stdout_is { system $cmd } $sparql x 4, 'lookup given pid from ARGV'; } done_testing; ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������search.t��������������������������������������������������������������������������������������������100644��001750��001750�� 655�12701414674� 13561� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������use v5.14; use Test::More; use Test::Output; sub wdq { my $c = join ' ', $^X, qw(script/wdq -gde --res t/examples/search.json), @_; `$c`; } my $xml = wdq(qw(search -f xml )); like "" . $xml, qr{xml:lang="de">IPPNW</literal>}, 'contains language tag'; is( ( () = $xml =~ /<binding name="alias">/g ), 4, 'contains some alias' ); like wdq(qw(search --ids -f simple)), qr{"id": "Q256503",}, 'option --ids'; done_testing; �����������������������������������������������������������������������������������options.t�������������������������������������������������������������������������������������������100644��001750��001750�� 717�12701414674� 14006� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������use v5.14; use Test::More; use Test::Output; my $wdq = "$^X script/wdq"; my $res = "--response t/examples"; # enumerate (-e), identifier (-i) { stdout_like { system("$wdq query -eifcsv $res/response.json '?x ?y ?z'") } qr/^n,x,y,z\n1,Q123/m, '-e -i (query)' } # enumerate (-e), format (-f) { stdout_like { system("$wdq search -ef{n}:{id} $res/search.json") } qr/^1:Q256503\n2:Q39631/m, '-e -f (search)' } done_testing; �������������������������������������������������perltidy.t������������������������������������������������������������������������������������������100644��001750��001750�� 467�12701414674� 14151� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������use v5.14; use Test::More; use Test::PerlTidy; if ( $ENV{RELEASE_TESTING} ) { my @files = ('script/wdq', Test::PerlTidy::list_files( path => 'lib' )); $Test::PerlTidy::MUTE=1; ok( Test::PerlTidy::is_file_tidy($_), $_ ) for @files; } else { plan( skip_all => 'release test' ); } done_testing; ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������App�������������������������������������������������������������������������������������������������000755��001750��001750�� 0�12701414674� 13024� 5����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/lib������������������������������������������������������������������������������������������������������������������������������������������������������wdq.pm����������������������������������������������������������������������������������������������100644��001750��001750�� 432�12701414674� 14274� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/lib/App��������������������������������������������������������������������������������������������������������������������������������������������������package App::wdq; use v5.14; our $VERSION = '0.4.4'; 1; __END__ =head1 NAME App::wdq - command line access to Wikidata Query Service =head1 DESCRIPTION See command line script L<wdq> for documentation. =head1 COPYRIGHT AND LICENSE Copyright Jakob Voss, 2015- GPL 2.0 =cut ��������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������CONTRIBUTING.md�������������������������������������������������������������������������������������100644��001750��001750�� 1112�12701414674� 14122� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4����������������������������������������������������������������������������������������������������������������������������������������������������������# wdq coding guidelines **wdq** is written in Perl. The current version is one single Perl script, located in `script/wdq`. Files in `lib/` are optional for documentation only. Global variables are UPPERCASE. A future version may better be organized in modules and use `App::FatPacker` to build a single executable. Code MUST be cleaned up with standard `perltidy`: perltidy -b script/wdq lib/App/wdq.pm Please enable this git pre-commit hook in executable `.git/hooks/pre-commit` to prevent against committing failing tests: #!/bin/sh RELEASE_TESTING=1 prove -l -j8 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������format-text.t���������������������������������������������������������������������������������������100644��001750��001750�� 1524�12701414674� 14602� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������use v5.14; use Test::More; use Test::Output; sub wdq { system( $^X, qw(script/wdq search --res t/examples/search.json), @_ ); } my $text = <<TEXT; Q256503: Ärzte Wikimedia-Begriffsklärungsseite Q39631: Arzt (Ärzteschaft) Heilberuf Q49330: Ärzte ohne Grenzen Q245385: IPPNW (Ärzte für die Verhütung des Atomkrieges) Q160721: Nürnberger Ärzteprozess (Ärzteprozess) Erster der zwölf Nürnberger Nachfolgeprozesse Q256664: Ärzteverschwörung Q695416: Poliklinik (Ärztehaus) TEXT stdout_is { wdq() } $text, 'text format by default in search mode'; stdout_is { wdq('-ftext') } $text, 'text format explicitly'; my $format = '{id}{label|pre==|length=3}{alias|length=14|align=right}'; stdout_is { wdq( '--format', $format, '-3' ) } <<TEXT, 'format string'; Q256503=Är… Q39631=Ar… Ärzteschaft Q49330=Är… TEXT done_testing; ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������examples��������������������������������������������������������������������������������������������000755��001750��001750�� 0�12701414674� 13617� 5����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������lookup����������������������������������������������������������������������������������������������100644��001750��001750�� 443�12701414674� 15174� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples����������������������������������������������������������������������������������������������������������������������������������������������� http://www.wikidata.org/entity/Q243972 https://www.wikidata.org/entity/Q243972 https://www.wikidata.org/wiki/Special:EntityData/Q243972 http://www.wikidata.org/wiki/Special:EntityData/Q243972 http://www.wikidata.org/wiki/Q243972 https://www.wikidata.org/wiki/Q243972 q243972 Q243972 �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������count.json������������������������������������������������������������������������������������������100644��001750��001750�� 247�12701414674� 15765� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������{ "head": { "vars": ["count"] }, "results": { "bindings": [ { "count": { "type": "literal", "value": "63107" } } ] } } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������query.query�����������������������������������������������������������������������������������������100644��001750��001750�� 32�12701414674� 16146� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������SELECT ?z ?x ?y WHERE { } ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������search.json�����������������������������������������������������������������������������������������100644��001750��001750�� 4072�12701414674� 16122� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������{"searchinfo":{"search":"\u00c4rzte"},"search":[{"id":"Q256503","concepturi":"http://www.wikidata.org/entity/Q256503","url":"//www.wikidata.org/wiki/Q256503","title":"Q256503","pageid":248909,"label":"\u00c4rzte","description":"Wikimedia-Begriffskl\u00e4rungsseite","match":{"type":"label","language":"de","text":"\u00c4rzte"}},{"id":"Q39631","concepturi":"http://www.wikidata.org/entity/Q39631","url":"//www.wikidata.org/wiki/Q39631","title":"Q39631","pageid":42019,"label":"Arzt","description":"Heilberuf","match":{"type":"alias","language":"de","text":"\u00c4rzteschaft"},"aliases":["\u00c4rzteschaft"]},{"id":"Q49330","concepturi":"http://www.wikidata.org/entity/Q49330","url":"//www.wikidata.org/wiki/Q49330","title":"Q49330","pageid":51315,"label":"\u00c4rzte ohne Grenzen","match":{"type":"label","language":"de","text":"\u00c4rzte ohne Grenzen"}},{"id":"Q245385","concepturi":"http://www.wikidata.org/entity/Q245385","url":"//www.wikidata.org/wiki/Q245385","title":"Q245385","pageid":238328,"label":"IPPNW","match":{"type":"alias","language":"de","text":"\u00c4rzte f\u00fcr die Verh\u00fctung des Atomkrieges"},"aliases":["\u00c4rzte f\u00fcr die Verh\u00fctung des Atomkrieges"]},{"id":"Q160721","concepturi":"http://www.wikidata.org/entity/Q160721","url":"//www.wikidata.org/wiki/Q160721","title":"Q160721","pageid":161594,"label":"N\u00fcrnberger \u00c4rzteprozess","description":"Erster der zw\u00f6lf N\u00fcrnberger Nachfolgeprozesse","match":{"type":"alias","language":"de","text":"\u00c4rzteprozess"},"aliases":["\u00c4rzteprozess"]},{"id":"Q256664","concepturi":"http://www.wikidata.org/entity/Q256664","url":"//www.wikidata.org/wiki/Q256664","title":"Q256664","pageid":249062,"label":"\u00c4rzteverschw\u00f6rung","match":{"type":"label","language":"de","text":"\u00c4rzteverschw\u00f6rung"}},{"id":"Q695416","concepturi":"http://www.wikidata.org/entity/Q695416","url":"//www.wikidata.org/wiki/Q695416","title":"Q695416","pageid":654988,"label":"Poliklinik","match":{"type":"alias","language":"de","text":"\u00c4rztehaus"},"aliases":["\u00c4rztehaus"]}],"search-continue":7,"success":1} ����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������release-pod-syntax.t��������������������������������������������������������������������������������100644��001750��001750�� 456�12701414674� 16037� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t��������������������������������������������������������������������������������������������������������������������������������������������������������#!perl BEGIN { unless ($ENV{RELEASE_TESTING}) { require Test::More; Test::More::plan(skip_all => 'these tests are for release candidate testing'); } } # This file was automatically generated by Dist::Zilla::Plugin::PodSyntaxTests. use Test::More; use Test::Pod 1.41; all_pod_files_ok(); ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������label.sparql����������������������������������������������������������������������������������������100644��001750��001750�� 751�12701414674� 16245� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������PREFIX bd: <http://www.bigdata.com/rdf#> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> PREFIX schema: <http://schema.org/> PREFIX wikibase: <http://wikiba.se/ontology#> SELECT * WHERE { ?id ?p ?x . SERVICE wikibase:label { bd:serviceParam wikibase:language "de" . ?id rdfs:label ?label . } SERVICE wikibase:label { bd:serviceParam wikibase:language "de" . ?x rdfs:label ?xLabel . ?x schema:description ?xDescription . } } �����������������������response.json���������������������������������������������������������������������������������������100644��001750��001750�� 622�12701414674� 16470� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������{ "head" : { "vars" : [ "x", "y", "z" ] }, "results" : { "bindings" : [ { "x" : { "type" : "uri", "value" : "http://www.wikidata.org/entity/Q123" }, "y" : { "type" : "uri", "value" : "http://www.example.org/" }, "z" : { "xml:lang" : "en", "type" : "literal", "value" : "\u2603\n" } } ] } } ��������������������������������������������������������������������������������������������������������������stackexchange.query���������������������������������������������������������������������������������100644��001750��001750�� 215�12701414674� 17634� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������SELECT ?q ?qLabel ?tag WHERE { ?q wdt:P1482 ?tag . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } } �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������stackexchange.sparql��������������������������������������������������������������������������������100644��001750��001750�� 427�12701414674� 17776� 0����������������������������������������������������������������������������������������������������ustar�00voj�����������������������������voj�����������������������������000000��000000��App-wdq-0.4.4/t/examples�����������������������������������������������������������������������������������������������������������������������������������������������PREFIX bd: <http://www.bigdata.com/rdf#> PREFIX wdt: <http://www.wikidata.org/prop/direct/> PREFIX wikibase: <http://wikiba.se/ontology#> SELECT ?q ?qLabel ?tag WHERE { ?q wdt:P1482 ?tag . SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . } } ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������