Skip to content
This repository was archived by the owner on Feb 7, 2024. It is now read-only.

Commit 4c5bab6

Browse files
committed
Started on added the API functions
1 parent 6f212c7 commit 4c5bab6

File tree

3 files changed

+173
-1
lines changed

3 files changed

+173
-1
lines changed

W8yAPI.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
3+
use Wikimedia\ParamValidator\ParamValidator;
4+
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
5+
6+
class W8yAPI extends ApiBase {
7+
8+
/**
9+
* @param mixed $failure
10+
*
11+
* @return void
12+
*/
13+
private function returnFailure( $failure ) {
14+
$ret = [];
15+
$ret['message'] = $failure;
16+
$this->getResult()->addValue(
17+
null,
18+
$this->getModuleName(),
19+
[ 'error' => $ret ]
20+
);
21+
}
22+
23+
/**
24+
* @param mixed $code
25+
* @param mixed $result
26+
*
27+
* @return array
28+
*/
29+
private function createResult( $code, $result ): array {
30+
$ret = [];
31+
$ret['status'] = $code;
32+
$ret['data'] = $result;
33+
34+
return $ret;
35+
}
36+
37+
public function execute() {
38+
$params = $this->extractRequestParams();
39+
$action = $params['what'];
40+
$output = '';
41+
if ( !$action || $action === null ) {
42+
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
43+
} else {
44+
switch ( $action ) {
45+
case "query":
46+
$output = "gelukt!";
47+
/*
48+
if ( !empty( $params['data'] ) ) {
49+
$ai = new \WSai\QueryAi();
50+
$output = $ai->ask( $params['data'] );
51+
if ( $output === false ) {
52+
$this->returnFailure( wfMessage( 'wsai-error-general', "cannot contact Open API" )->params() );
53+
break;
54+
}
55+
} else {
56+
$this->returnFailure( wfMessage( 'wsai-api-error-unknown-2nd-parameter' ) );
57+
break;
58+
}*/
59+
break;
60+
case "wiki":
61+
$output = "gelukt!";
62+
break;
63+
case "stats":
64+
$output = "gelukt!";
65+
break;
66+
case "extension":
67+
$output = "gelukt!";
68+
break;
69+
default :
70+
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
71+
break;
72+
}
73+
}
74+
75+
$this->getResult()->addValue(
76+
null,
77+
$this->getModuleName(),
78+
[ 'result' => $output ]
79+
);
80+
81+
return true;
82+
}
83+
84+
public function needsToken() {
85+
return false;
86+
}
87+
88+
public function isWriteMode() {
89+
return false;
90+
}
91+
92+
/**
93+
* @return array
94+
*/
95+
public function getAllowedParams() {
96+
return [
97+
'what' => [
98+
ParamValidator::PARAM_TYPE => [ 'query', 'stats', 'extension', 'wiki' ],
99+
ParamValidator::PARAM_REQUIRED => true
100+
],
101+
'return' => [
102+
ParamValidator::PARAM_TYPE => 'string',
103+
ParamValidator::PARAM_REQUIRED => false
104+
],
105+
'from' => [
106+
ParamValidator::PARAM_TYPE => 'string',
107+
ParamValidator::PARAM_REQUIRED => false
108+
],
109+
'where' => [
110+
ParamValidator::PARAM_TYPE => 'string',
111+
ParamValidator::PARAM_REQUIRED => false
112+
],
113+
'limit' => [
114+
ParamValidator::PARAM_DEFAULT => 10,
115+
ParamValidator::PARAM_TYPE => 'limit',
116+
IntegerDef::PARAM_MIN => 1,
117+
IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
118+
IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
119+
],
120+
'output' => [
121+
ParamValidator::PARAM_TYPE => [ 'csv', 'table', 'json' ],
122+
ParamValidator::PARAM_REQUIRED => false
123+
],
124+
'pageid' => [
125+
ParamValidator::PARAM_TYPE => 'integer',
126+
ParamValidator::PARAM_REQUIRED => false
127+
],
128+
'for' => [
129+
ParamValidator::PARAM_TYPE => 'string',
130+
ParamValidator::PARAM_REQUIRED => false
131+
],
132+
'extension-name' => [
133+
ParamValidator::PARAM_TYPE => 'string',
134+
ParamValidator::PARAM_REQUIRED => false
135+
],
136+
'type' => [
137+
ParamValidator::PARAM_ISMULTI => false,
138+
ParamValidator::PARAM_TYPE => [ 'version', 'documentation', 'usedby' ],
139+
ParamValidator::PARAM_REQUIRED => false
140+
]
141+
];
142+
}
143+
144+
/**
145+
* @return array
146+
*/
147+
protected function getExamplesMessages(): array {
148+
return [
149+
'action=wikiapiary&what=wiki&pageId=4' => 'apihelp-w8y-example-1'
150+
];
151+
}
152+
153+
}

extension.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@
1919
"ExtensionMessagesFiles": {
2020
"WikiApiaryMagic": "i18n/WikiApiary.i18n.php"
2121
},
22+
"AutoloadClasses": {
23+
"W8yAPI": "W8yAPI.php"
24+
},
25+
"APIModules": {
26+
"wikiapiary": "W8yAPI"
27+
},
2228
"AutoloadNamespaces": {
2329
"WikiApiary\\": "includes/"
2430
},

i18n/en.json

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,18 @@
3636
"w8y_ed_ed_id" : "Id",
3737
"w8y_ed_name" : "Name",
3838
"w8y_ed_version" : "Version",
39-
"w8y_ed_doc_url" : "Documentation url"
39+
"w8y_ed_doc_url" : "Documentation url",
40+
"w8y-api-error-unknown-what-parameter" : "What parameter missing",
41+
"apihelp-wikiapiary-summary": "Api functions to query WikiApiairy",
42+
"apihelp-w8y-example-1": "Get information for Wiki by Page ID",
43+
"apihelp-wikiapiary-param-what": "The query action",
44+
"apihelp-wikiapiary-param-return": "Used with what=query. Consult the readme for information",
45+
"apihelp-wikiapiary-param-from": "Used with what=query. Consult the readme for information",
46+
"apihelp-wikiapiary-param-where": "Used with what=query and what=stats. Consult the readme for information",
47+
"apihelp-wikiapiary-param-limit": "Limit the API results",
48+
"apihelp-wikiapiary-param-output": "The format needed to return",
49+
"apihelp-wikiapiary-param-pageid": "Used with what=wiki. Consult the readme for information",
50+
"apihelp-wikiapiary-param-for": "Used with what=stats. Consult the readme for information",
51+
"apihelp-wikiapiary-param-extension-name": "Used with what=extension. Consult the readme for information",
52+
"apihelp-wikiapiary-param-type": "Used with what=extension. Consult the readme for information"
4053
}

0 commit comments

Comments
 (0)