-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPIAsker.php
67 lines (62 loc) · 1.59 KB
/
APIAsker.php
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
<?php
/**
* This singleton class is for querying SMW using the SMW Ask API.
* API access happens internally (no http call).
*
* @author WME 201411
*/
class APIAsker{
const QUERYRESULTLIMIT = 5000;
private static $instance; //the singleton instance
private function __construct(){} //remember the singleton
/**
* Singleton function
*/
public static function getInstance(){
if(!isset(self::$instance))
self::$instance = new APIAsker();
return self::$instance;
}
/**
* This function executes the given SMW ask query
* API access happens internally
*
* @author WME
* @param String $query the SMW ask query
* @return associative array with the query result
*/
function ask($query)
{
$params = new FauxRequest(
array(
'action' => 'ask',
'query' => $query."|limit=" . self::QUERYRESULTLIMIT
)
);
$api = new ApiMain( $params );
$api->execute();
$result = &$api->getResultData();
return $result;
}
/**
* This function executes the given query
* api.php?action=query&titles=File:Banner-big-aquacultuur.jpg&prop=imageinfo&&iiprop=url
*/
function queryImageUrl($img){
$params = new FauxRequest(
array(
'action' => 'query',
'titles' => 'File:'.$img, //TODO: is File: namespace always recognized?
'prop' => 'imageinfo',
'iiprop' => 'url'
)
);
$api = new ApiMain( $params );
$api->execute();
$result = &$api->getResultData();
//possible since: mw1.25
//$result = $api->getResult->getResultData(array('query','pages'));
return array_pop(array_pop($result['query']['pages'])['imageinfo'])['url'];
}
}
?>