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+ }
0 commit comments