-
Notifications
You must be signed in to change notification settings - Fork 1
/
ApiQueryWikidataProp.php
92 lines (69 loc) · 2.78 KB
/
ApiQueryWikidataProp.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
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
<?php
/**
* Timeline List-Module for the MediaWiki web API. Invoke with list=timeline
*/
class ApiQueryWikidataProp extends ApiQueryBase {
public function __construct( $query, $moduleName, $prefix = 'wd' ) {
parent :: __construct( $query, $moduleName, $prefix );
}
public function execute() {
$params = $this->extractRequestParams();
$titles = $this->getPageSet()->getTitles();
foreach ( $titles as $t ) {
$path = array( 'query', 'pages', $t->getArticleID(), $this->getModuleName() );
$this->addWikidataForPage( $t, $path, $params );
}
}
public function addWikidataForPage( Title $title, $path, $params ) {
$props = $params['prop'];
$langs = $params['lang'];
$result = $this->getResult();
#FIXME: handle indexed items correctly, especially multiple statuements for a single property, or multiple aliases for a single language!
$page = WikiPage::factory( $title ); # XXX: use a cached copy?
$content = $page->getContent();
$data = array();
if ( empty($props) or in_array('*', $props) ) $props = $content->getPropertyNames();
if ( empty($lang) or in_array('*', $lang) ) $lang = null;
foreach ( $props as $p ) {
$d = $content->getPropertyMultilang($p, $lang);
if ($d) $data[$p] = $d;
}
$result->addValue( $path, null, $data );
}
public function getDescription() {
return array ("Includes structured data for each page. ");
}
public function getExamples() {
return array (
"Get structured data of Data:Example:",
" api.php?action=query&prop=wikidata&titles=Data:Example",
);
}
public function getVersion() {
return __CLASS__ . ': $Id$';
}
public function getAllowedParams() {
$params = array(
'prop' => array(
ApiBase::PARAM_DFLT => '*',
ApiBase::PARAM_TYPE => array_keys( WikiTalkQueryTimeline::$all_properties ),
ApiBase::PARAM_ISMULTI => true,
),
'prop' => array(
ApiBase::PARAM_DFLT => '*',
ApiBase::PARAM_TYPE => array_keys( WikiTalkQueryTimeline::$all_properties ),
ApiBase::PARAM_ISMULTI => true,
),
);
return $params;
}
public function getParamDescription() {
$params = array(
'prop' => array( 'What properties to return from the structured data records',
'To get all properties, use "*" (this is the default).'),
'lang' => array( 'The languages to include for multilingual values',
'To get all languages, use "*" (this is the default).')
);
return $params;
}
}