This repository has been archived by the owner on Nov 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asparagus
executable file
·177 lines (146 loc) · 4.29 KB
/
asparagus
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#!/bin/php
<?php
/**
* CLI interface for the Wikidata query service. Build on the Wikimedia Hackathon
* in Lyon.
*
* @author Marius Hoch < [email protected] >
* @license GPL-2.0+
*/
$endPoint = 'https://wdqs-beta.wmflabs.org/bigdata/namespace/wdq/sparql?query=$1';
$defaultPrefixes = 'PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX entity: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX v: <http://www.wikidata.org/prop/statement/>
PREFIX q: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>';
/**
* @param string $query
* @param string $endPoint
* @param string $responseFormat
*
* @return string|bool Result
*/
function doQuery( $query, $endPoint, $responseFormat ) {
$url = str_replace(
'$1',
urlencode( $query ),
$endPoint
);
$opts = array();
if ( $responseFormat !== null ) {
$opts['header'] = "Accept: $responseFormat\r\n";
}
$context = stream_context_create( array( 'https' => $opts, 'http' => $opts ) );
// @TODO: Better error handling
return @file_get_contents( $url, false, $context );
}
/**
* Reads the user input and returns once a full query has been entered,
* or the user wants to exit.
*
* @return string|bool The query, or false if the user wants to exit
*/
function readInput() {
$query = '';
// Whether we saw an initial opening {. This assumes all queries contain at least one {…} section.
$sawInitialBracket = false;
// Number of open { sections
$brackC = 0;
// Number of single quotes seen
$singleQuoteC = 0;
// Number of double quotes seen
$doubleQuoteC = 0;
// Number of consecutive backslashes seen
$consecutiveBackslashC = 0;
while ( $brackC !== 0 || !$sawInitialBracket ) {
$input = readline( $query === '' ? '> ' : '… ' );
if ( $query === '' && ( $input === 'quit' || $input === 'exit' ) ) {
return false;
}
for ( $i = 0; $i < strlen( $input ); $i++ ) {
// { or }
if ( $doubleQuoteC % 2 === 0 && $singleQuoteC % 2 === 0 ) {
if ( $input[$i] === '{' ) {
$sawInitialBracket = true;
$brackC--;
continue;
} elseif ( $input[$i] === '}' ) {
$brackC++;
continue;
}
}
// Backslashes
if ( $input[$i] === '\\' ) {
$consecutiveBackslashC++;
continue;
} else {
$consecutiveBackslashC = 0;
}
// (Unescaped) quotes
if ( $input[$i] === '"' && $consecutiveBackslashC % 2 === 0 ) {
$doubleQuoteC++;
continue;
} elseif ( $input[$i] === "'" && $consecutiveBackslashC % 2 === 0 ) {
$singleQuoteC++;
continue;
}
}
if ( preg_match( '/\\\\[gG]$/', $input ) ) {
$input = preg_replace( '/\\\\[gG]$/', '', $input );
$query .= $input;
return $query;
}
if ( $input !== '' ) {
$query .= $input . "\n";
}
}
// If the query didn't end on a \g or \G, read on until an empty input line occurs
while ( true ) {
$input = readline( '… ' );
if ( $input === '' ) {
break;
}
if ( preg_match( '/\\\\[gG]$/', $input ) ) {
$input = preg_replace( '/\\\\[gG]$/', '', $input );
$query .= $input;
return $query;
}
$query .= $input . "\n";
}
return $query;
}
if ( $argc > 1 && ( $argv[1] === '--help' || $argv[1] === '-h' ) ) {
echo "BlazeGraph cli interface targeted at the Wikidata Query service.\n";
echo "Runs runs queries interactively.\n\n";
echo "Usage: " . $argv[0] . " [--response=format-mime-type] [--no-default-prefixes]\n";
exit( 1 );
}
$longopts = array(
'response::', // [--response=format]
'no-default-prefixes', // [--no-default-prefixes]
);
$opts = getopt( '', $longopts );
$response = isset( $opts['response'] ) ? $opts['response'] : 'application/sparql-results+json';
echo "Enter SPARQL queries delimited by either an empty line after the query, a \\g or a \\G.
Quit by typing 'quit' or 'exit'.\n";
while( true ) {
$query = readInput();
if ( $query === false ) {
if ( mt_rand( 0, 10 ) === 8 ) {
die( "Here, have a kitten: https://commons.wikimedia.org/wiki/File:Cat_deck.PNG\n" );
}
die( "cu\n" );
}
if ( !isset( $opts['no-default-prefixes'] ) ) {
$query = $defaultPrefixes . "\n" . $query;
}
$result = doQuery( $query, $endPoint, $response );
if ( $result === false ) {
echo "Query execution failed.\n";
} else {
echo $result . "\n";
}
}