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

Commit 076593c

Browse files
committed
v0.5.2 Start on MW API support. action=query is currently tested.
1 parent 4c5bab6 commit 076593c

File tree

8 files changed

+112
-94
lines changed

8 files changed

+112
-94
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,7 @@ You can then call the module as follows :
118118
```
119119

120120
The result will be a Lua table
121+
122+
## MediaWiki API
123+
Go to the sandbox for Wikiapiary.
124+
currently only "query" is supported.

W8yAPI.php

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
<?php
22

3+
use WikiApiary\data\Structure;
4+
use WikiApiary\data\Utils;
5+
use WikiApiary\TagHooks;
36
use Wikimedia\ParamValidator\ParamValidator;
47
use Wikimedia\ParamValidator\TypeDef\IntegerDef;
58

69
class W8yAPI extends ApiBase {
710

11+
12+
/**
13+
* @var Structure
14+
*/
15+
private Structure $structure;
16+
817
/**
918
* @param mixed $failure
1019
*
@@ -34,40 +43,53 @@ private function createResult( $code, $result ): array {
3443
return $ret;
3544
}
3645

46+
/**
47+
* @param array $params
48+
*
49+
* @return array
50+
*/
51+
private function convertParams( array $params ): array {
52+
if ( isset( $params['extension-name'] ) ) {
53+
$params['Extension name'] = $params['extension-name'];
54+
unset( $params['extension-name'] );
55+
}
56+
if ( isset( $params['output'] ) ) {
57+
$params['format'] = $params['output'];
58+
unset( $params['output'] );
59+
}
60+
if ( isset( $params['pageid'] ) ) {
61+
$params['pageId'] = $params['pageid'];
62+
unset( $params['pageid'] );
63+
}
64+
if ( isset( $params['what'] ) ) {
65+
$params['action'] = $params['what'];
66+
unset( $params['what'] );
67+
}
68+
return $params;
69+
}
70+
3771
public function execute() {
3872
$params = $this->extractRequestParams();
39-
$action = $params['what'];
73+
$this->structure = new Structure();
74+
$params = $this->convertParams( $params );
75+
$action = $params['action'];
4076
$output = '';
4177
if ( !$action || $action === null ) {
4278
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
4379
} else {
4480
switch ( $action ) {
4581
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;
6082
case "wiki":
61-
$output = "gelukt!";
62-
break;
6383
case "stats":
64-
$output = "gelukt!";
65-
break;
6684
case "extension":
67-
$output = "gelukt!";
85+
$result = TagHooks::handleIt( $params );
86+
if ( $result['status'] === 'error' ) {
87+
$this->returnFailure( $result['data'] );
88+
}
89+
$output = $result['data'];
6890
break;
69-
default :
70-
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
91+
default:
92+
$this->returnFailure( $this->msg( 'w8y-api-error-unknown-what-parameter' ) );
7193
break;
7294
}
7395
}
@@ -119,6 +141,7 @@ public function getAllowedParams() {
119141
],
120142
'output' => [
121143
ParamValidator::PARAM_TYPE => [ 'csv', 'table', 'json' ],
144+
ParamValidator::PARAM_DEFAULT => 'json',
122145
ParamValidator::PARAM_REQUIRED => false
123146
],
124147
'pageid' => [
@@ -146,7 +169,7 @@ public function getAllowedParams() {
146169
*/
147170
protected function getExamplesMessages(): array {
148171
return [
149-
'action=wikiapiary&what=wiki&pageId=4' => 'apihelp-w8y-example-1'
172+
'action=wikiapiary&what=wiki&pageid=4' => 'apihelp-w8y-example-1'
150173
];
151174
}
152175

extension.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "WikiApiary",
3-
"version": "0.5.1",
3+
"version": "0.5.2",
44
"author": [
55
"[https://www.mediawiki.org/wiki/User:Cindy.cicalese Cindy Cicalese]"
66
],

includes/TagHooks.php

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,20 @@ class TagHooks {
2727
private array $parameters;
2828

2929
/**
30-
* @param Parser &$parser
30+
* @param array $parameters
3131
*
32-
* @return mixed
32+
* @return array|string[]
3333
*/
34-
public function w8y( Parser &$parser ): mixed {
35-
// First set global debug status
36-
$config = MediaWikiServices::getInstance()->getMainConfig();
37-
if ( $config->has( 'WikiApiary' ) ) {
38-
$w8yConfig = $config->get( 'WikiApiary' );
39-
DBHooks::$debug = $w8yConfig['debug'];
40-
} else {
41-
DBHooks::$debug = false;
42-
}
43-
34+
public static function handleIt( array $parameters ): array {
35+
$action = Utils::getOptionSetting( 'action', true, $parameters );
36+
$limit = Utils::getOptionSetting( 'limit', true, $parameters );
37+
$format = Utils::getOptionSetting( 'format', true, $parameters );
4438
$result = '';
45-
Utils::$parameters = $this->extractOptions( array_slice( func_get_args(),
46-
1 ) );
47-
$action = Utils::getOptionSetting( 'action' );
48-
$limit = Utils::getOptionSetting( 'limit' );
49-
$format = Utils::getOptionSetting( 'format' );
5039
switch ( $action ) {
5140
case "extension":
52-
$eName = Utils::getOptionSetting( 'Extension name' );
53-
$eType = Utils::getOptionSetting( 'type' );
54-
$limit = Utils::getOptionSetting( 'limit' );
41+
$eName = Utils::getOptionSetting( 'Extension name', true, $parameters );
42+
$eType = Utils::getOptionSetting( 'type', true, $parameters );
43+
$limit = Utils::getOptionSetting( 'limit', true, $parameters );
5544
if ( $eName === null || $eType === null ) {
5645
break;
5746
}
@@ -65,19 +54,17 @@ public function w8y( Parser &$parser ): mixed {
6554
break;
6655
case "query":
6756
$query = new Query();
68-
$get = Utils::getOptionSetting( 'return' );
69-
$table = Utils::getOptionSetting( 'from' );
70-
$where = Utils::getOptionSetting( 'where' );
57+
$get = Utils::getOptionSetting( 'return', true, $parameters );
58+
$table = Utils::getOptionSetting( 'from', true, $parameters );
59+
$where = Utils::getOptionSetting( 'where', true, $parameters );
7160
$result = $query->doQuery( $get,
7261
$table,
7362
$where,
7463
$limit,
7564
$format );
76-
ResponseHandler::printDebugMessage( $result,
77-
"sql result" );
7865
break;
7966
case "wiki":
80-
$pId = Utils::getOptionSetting( 'pageId' );
67+
$pId = Utils::getOptionSetting( 'pageId', true, $parameters );
8168
if ( $pId === null ) {
8269
ResponseHandler::addMessage( wfMessage( 'w8y_missing-page-id' )->text() );
8370
} else {
@@ -86,13 +73,11 @@ public function w8y( Parser &$parser ): mixed {
8673
$format = 'table';
8774
}
8875
$result = $query->doQuery( intval( $pId ), $format );
89-
ResponseHandler::printDebugMessage( $result,
90-
"sql result" );
9176
}
9277
break;
9378
case "stats":
94-
$type = Utils::getOptionSetting( 'for' );
95-
$where = Utils::getOptionSetting( 'where' );
79+
$type = Utils::getOptionSetting( 'for', true, $parameters );
80+
$where = Utils::getOptionSetting( 'where', true, $parameters );
9681
if ( $limit === null ) {
9782
$limit = 10;
9883
}
@@ -105,23 +90,41 @@ public function w8y( Parser &$parser ): mixed {
10590
$format = 'table';
10691
}
10792
$result = $query->doQuery( $type, $where, $limit, $format );
108-
ResponseHandler::printDebugMessage( $result,
109-
"sql result" );
11093
}
11194
break;
11295
case "addToDB":
11396
break;
11497
}
11598
if ( !empty( ResponseHandler::getMessages() ) ) {
116-
return ResponseHandler::getMessages();
117-
} elseif ( is_array( $result ) ) {
118-
return "<pre>" . print_r( $result,
119-
true ) . "</pre>";
99+
return [ 'status' => 'error', 'data' => ResponseHandler::getMessages() ];
100+
} elseif ( is_array( $result ) && $format !== 'json' ) {
101+
return [ 'status' => 'error', 'data' => "<pre>" . print_r( $result, true ) . "</pre>" ];
120102
} else {
121-
return $result;
103+
return [ 'status' => 'ok', 'data' => $result ];
122104
}
123105
}
124106

107+
/**
108+
* @param Parser &$parser
109+
*
110+
* @return mixed
111+
*/
112+
public function w8y( Parser &$parser ): mixed {
113+
// First set global debug status
114+
$config = MediaWikiServices::getInstance()->getMainConfig();
115+
if ( $config->has( 'WikiApiary' ) ) {
116+
$w8yConfig = $config->get( 'WikiApiary' );
117+
DBHooks::$debug = $w8yConfig['debug'];
118+
} else {
119+
DBHooks::$debug = false;
120+
}
121+
122+
$parameters = $this->extractOptions( array_slice( func_get_args(), 1 ) );
123+
Utils::$parameters = $parameters;
124+
$result = self::handleIt( $parameters );
125+
return $result['data'];
126+
}
127+
125128
/**
126129
* Converts an array of values in form [0] => "name=value" into a real associative array in form [name] => value.
127130
* If no "=" is provided, true is assumed like this: [name] => true.

includes/data/ResponseHandler.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
namespace WikiApiary\data;
1212

13-
use WikiApiary\DBHooks;
14-
1513
class ResponseHandler {
1614

1715
/**
@@ -37,18 +35,4 @@ public static function getMessages(): string {
3735
return "";
3836
}
3937
}
40-
41-
/**
42-
* @param mixed $message
43-
* @param string $title
44-
* @return void
45-
*/
46-
public static function printDebugMessage( mixed $message, string $title = '' ): void {
47-
if ( DBHooks::$debug ) {
48-
echo "<pre>$title\n";
49-
var_dump( $message );
50-
echo "</pre>";
51-
}
52-
}
53-
5438
}

includes/data/Utils.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,11 @@ public static function checkForMultiple( string $option, bool $extract = false )
5050
$k = $itemExploded[0];
5151
$v = $itemExploded[1];
5252
$ret[$k] = $v;
53+
} else {
54+
$ret = $option;
5355
}
5456
} else {
55-
return $option;
57+
$ret = $option;
5658
}
5759
return $ret;
5860
}

includes/data/query/Query.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,12 @@ public function __construct() {
3636
* @return mixed
3737
*/
3838
public function doQuery( ?string $get, ?string $from, ?string $where, ?string $limit, ?string $format ): mixed {
39-
ResponseHandler::printDebugMessage( $get, "return" );
40-
ResponseHandler::printDebugMessage( $from, "table" );
41-
ResponseHandler::printDebugMessage( $where, "where" );
42-
ResponseHandler::printDebugMessage( $limit, "limit" );
43-
ResponseHandler::printDebugMessage( $format, "format" );
4439
if ( $from === null ) {
4540
ResponseHandler::addMessage( wfMessage( 'w8y_missing-table-argument' )->text() );
4641
return "";
4742
}
4843
if ( !$this->structure->tableExists( $from ) ) {
49-
ResponseHandler::addMessage( wfMessage( 'w8y_not-a-valid-table', $from )->text() );
44+
ResponseHandler::addMessage( wfMessage( 'w8y_not-a-valid-table', $from )->parse() );
5045
return "";
5146
}
5247
if ( $get === null ) {
@@ -87,7 +82,7 @@ public function doQuery( ?string $get, ?string $from, ?string $where, ?string $l
8782
/**
8883
* @param string|array $select
8984
* @param string $table
90-
* @param array|null $selectWhere
85+
* @param string|array|null $selectWhere
9186
* @param int $limit
9287
* @param string $format
9388
*
@@ -96,7 +91,7 @@ public function doQuery( ?string $get, ?string $from, ?string $where, ?string $l
9691
private function query(
9792
string|array $select,
9893
string $table,
99-
?array $selectWhere,
94+
null|string|array $selectWhere,
10095
int $limit,
10196
string $format
10297
): mixed {
@@ -107,22 +102,29 @@ private function query(
107102
if ( $selectWhere === null ) {
108103
$selectWhere = '';
109104
}
110-
ResponseHandler::printDebugMessage( $selectWhere, '$selectWhere' );
111-
ResponseHandler::printDebugMessage( $table, '$table' );
112-
ResponseHandler::printDebugMessage( $select, '$select' );
113-
ResponseHandler::printDebugMessage( $selectOptions, '$selectOptions' );
114-
ResponseHandler::printDebugMessage( $format, '$format' );
115-
116105
$res = $dbr->select( $table, $select, $selectWhere, __METHOD__, $selectOptions );
117106
$result = [];
107+
$t = 0;
118108
if ( $res->numRows() > 0 ) {
119109
while ( $row = $res->fetchRow() ) {
120-
$result[] = $row;
110+
if ( $format === 'json' ) {
111+
foreach ( $row as $k => $v ) {
112+
if ( !is_int( $k ) ) {
113+
$result[$t][Structure::w8yMessage( $k )] = $v;
114+
}
115+
}
116+
} else {
117+
$result[] = $row;
118+
}
119+
$t++;
121120
}
122121
}
123122
if ( $format === 'csv' ) {
124123
return Utils::formatCSV( $result );
125124
}
125+
if ( $format === 'json' ) {
126+
return $result;
127+
}
126128

127129
return $result;
128130
}

includes/data/query/Stats.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ public function doQuery( string $action, string $where, int $limit = 10, string
206206
}
207207

208208
$tables = [ Structure::w8yMessage( 'w8y_count' ) ];
209-
210209
switch ( $export ) {
211210
case "table":
212211
if ( $action === "extensions" ) {
@@ -231,6 +230,7 @@ public function doQuery( string $action, string $where, int $limit = 10, string
231230
case "arrayfunctions":
232231
return [ Utils::exportArrayFunction( $result ), 'nowiki' => true ];
233232
case "lua":
233+
case "json":
234234
return $result;
235235
default:
236236
return "";

0 commit comments

Comments
 (0)