This repository was archived by the owner on Feb 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathW8yAPI.php
More file actions
176 lines (160 loc) · 3.96 KB
/
W8yAPI.php
File metadata and controls
176 lines (160 loc) · 3.96 KB
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
<?php
use WikiApiary\data\Structure;
use WikiApiary\data\Utils;
use WikiApiary\TagHooks;
use Wikimedia\ParamValidator\ParamValidator;
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
class W8yAPI extends ApiBase {
/**
* @var Structure
*/
private Structure $structure;
/**
* @param mixed $failure
*
* @return void
*/
private function returnFailure( $failure ) {
$ret = [];
$ret['message'] = $failure;
$this->getResult()->addValue(
null,
$this->getModuleName(),
[ 'error' => $ret ]
);
}
/**
* @param mixed $code
* @param mixed $result
*
* @return array
*/
private function createResult( $code, $result ): array {
$ret = [];
$ret['status'] = $code;
$ret['data'] = $result;
return $ret;
}
/**
* @param array $params
*
* @return array
*/
private function convertParams( array $params ): array {
if ( isset( $params['extension-name'] ) ) {
$params['Extension name'] = $params['extension-name'];
unset( $params['extension-name'] );
}
if ( isset( $params['output'] ) ) {
$params['format'] = $params['output'];
unset( $params['output'] );
}
if ( isset( $params['pageid'] ) ) {
$params['pageId'] = $params['pageid'];
unset( $params['pageid'] );
}
if ( isset( $params['what'] ) ) {
$params['action'] = $params['what'];
unset( $params['what'] );
}
return $params;
}
public function execute() {
$params = $this->extractRequestParams();
$this->structure = new Structure();
$params = $this->convertParams( $params );
$action = $params['action'];
$output = '';
if ( !$action || $action === null ) {
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
} else {
switch ( $action ) {
case "query":
case "wiki":
case "stats":
case "extension":
$result = TagHooks::handleIt( $params );
if ( $result['status'] === 'error' ) {
$this->returnFailure( $result['data'] );
}
$output = $result['data'];
break;
default:
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
break;
}
}
$this->getResult()->addValue(
null,
$this->getModuleName(),
[ 'result' => $output ]
);
return true;
}
public function needsToken() {
return false;
}
public function isWriteMode() {
return false;
}
/**
* @return array
*/
public function getAllowedParams() {
return [
'what' => [
ParamValidator::PARAM_TYPE => [ 'query', 'stats', 'extension', 'wiki' ],
ParamValidator::PARAM_REQUIRED => true
],
'return' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false
],
'from' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false
],
'where' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false
],
'limit' => [
ParamValidator::PARAM_DEFAULT => 10,
ParamValidator::PARAM_TYPE => 'limit',
IntegerDef::PARAM_MIN => 1,
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
],
'output' => [
ParamValidator::PARAM_TYPE => [ 'csv', 'table', 'json' ],
ParamValidator::PARAM_DEFAULT => 'json',
ParamValidator::PARAM_REQUIRED => false
],
'pageid' => [
ParamValidator::PARAM_TYPE => 'integer',
ParamValidator::PARAM_REQUIRED => false
],
'for' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false
],
'extension-name' => [
ParamValidator::PARAM_TYPE => 'string',
ParamValidator::PARAM_REQUIRED => false
],
'type' => [
ParamValidator::PARAM_ISMULTI => false,
ParamValidator::PARAM_TYPE => [ 'version', 'documentation', 'usedby' ],
ParamValidator::PARAM_REQUIRED => false
]
];
}
/**
* @return array
*/
protected function getExamplesMessages(): array {
return [
'action=wikiapiary&what=wiki&pageid=4' => 'apihelp-w8y-example-1'
];
}
}