Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
96.00% |
24 / 25 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
ApiZObjectEditor | |
96.00% |
24 / 25 |
|
66.67% |
2 / 3 |
11 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
4 | |||
mustBePosted | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
isWriteMode | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
needsToken | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getAllowedParams | n/a |
0 / 0 |
n/a |
0 / 0 |
1 | |||||
getExamplesMessages | n/a |
0 / 0 |
n/a |
0 / 0 |
1 |
1 | <?php |
2 | /** |
3 | * WikiLambda ZObject creating/editing API |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt |
8 | * @license MIT |
9 | */ |
10 | |
11 | namespace MediaWiki\Extension\WikiLambda\ActionAPI; |
12 | |
13 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
14 | use MediaWiki\Extension\WikiLambda\WikiLambdaServices; |
15 | use Wikimedia\ParamValidator\ParamValidator; |
16 | |
17 | class ApiZObjectEditor extends WikiLambdaApiBase { |
18 | |
19 | /** |
20 | * @inheritDoc |
21 | */ |
22 | public function __construct( $query, $moduleName ) { |
23 | parent::__construct( $query, $moduleName ); |
24 | |
25 | $this->setUp(); |
26 | } |
27 | |
28 | /** |
29 | * @inheritDoc |
30 | */ |
31 | public function execute(): void { |
32 | $user = $this->getUser(); |
33 | $params = $this->extractRequestParams(); |
34 | |
35 | $summary = $params[ 'summary' ]; |
36 | $zobject = $params[ 'zobject' ]; |
37 | |
38 | // If zid is set, we should be editing it, if empty or Z0, we are creating a new zobject |
39 | // Shall we add an aditional flag to confirm creation/edition? |
40 | $zid = $params[ 'zid' ]; |
41 | |
42 | $zObjectStore = WikiLambdaServices::getZObjectStore(); |
43 | |
44 | if ( !$zid || $zid === ZTypeRegistry::Z_NULL_REFERENCE ) { |
45 | // Create a new ZObject |
46 | $response = $zObjectStore->createNewZObject( $zobject, $summary, $user ); |
47 | } else { |
48 | // Edit an existing ZObject |
49 | $response = $zObjectStore->updateZObject( $zid, $zobject, $summary, $user ); |
50 | } |
51 | |
52 | if ( !$response->isOK() ) { |
53 | $this->dieWithZError( $response->getErrors(), 400 ); |
54 | } |
55 | |
56 | $title = $response->getTitle(); |
57 | $this->getResult()->addValue( |
58 | null, |
59 | $this->getModuleName(), |
60 | [ |
61 | 'success' => true, |
62 | 'articleId' => $title->getArticleID(), |
63 | 'title' => $title->getBaseText(), |
64 | 'page' => $title->getBaseTitle() |
65 | ] |
66 | ); |
67 | } |
68 | |
69 | /** |
70 | * @inheritDoc |
71 | * @codeCoverageIgnore |
72 | */ |
73 | public function mustBePosted() { |
74 | return true; |
75 | } |
76 | |
77 | /** |
78 | * @inheritDoc |
79 | * @codeCoverageIgnore |
80 | */ |
81 | public function isWriteMode() { |
82 | return true; |
83 | } |
84 | |
85 | /** |
86 | * @see ApiBase::needsToken |
87 | * @return string |
88 | * @codeCoverageIgnore |
89 | */ |
90 | public function needsToken(): string { |
91 | return 'csrf'; |
92 | } |
93 | |
94 | /** |
95 | * Mark as internal. This isn't meant to be user-facing, and can change at any time. |
96 | * @return bool |
97 | */ |
98 | public function isInternal() { |
99 | return true; |
100 | } |
101 | |
102 | /** |
103 | * @inheritDoc |
104 | * @codeCoverageIgnore |
105 | */ |
106 | protected function getAllowedParams(): array { |
107 | return [ |
108 | 'summary' => [ |
109 | ParamValidator::PARAM_TYPE => 'string', |
110 | ParamValidator::PARAM_REQUIRED => false, |
111 | ParamValidator::PARAM_DEFAULT => '', |
112 | ], |
113 | 'zid' => [ |
114 | ParamValidator::PARAM_TYPE => 'string', |
115 | ParamValidator::PARAM_REQUIRED => false, |
116 | ParamValidator::PARAM_DEFAULT => null, |
117 | ], |
118 | 'zobject' => [ |
119 | ParamValidator::PARAM_TYPE => 'text', |
120 | ParamValidator::PARAM_REQUIRED => true, |
121 | ] |
122 | ]; |
123 | } |
124 | |
125 | /** |
126 | * @see ApiBase::getExamplesMessages() |
127 | * @return array |
128 | * @codeCoverageIgnore |
129 | */ |
130 | protected function getExamplesMessages() { |
131 | return [ |
132 | 'action=wikilambda_edit&format=json&summary=New%20zobject&zobject=' |
133 | . urlencode( '{"Z1K1":"Z2","Z2K1":"Z0","Z2K2":"string value",' |
134 | . '"Z2K3":{"Z1K1":"Z12","Z12K1":["Z11", {"Z1K1":"Z11","Z11K1":"Z1002","Z11K2":"label"}]}}' ) |
135 | => 'apihelp-wikilambda_edit-example-create', |
136 | 'action=wikilambda_edit&format=json&summary=Edit%20zobject&zid=Z01&zobject=' |
137 | . urlencode( '{"Z1K1":"Z2","Z2K1":"Z01","Z2K2":"string value"}' ) |
138 | => 'apihelp-wikilambda_edit-example-edit-incorrect' |
139 | ]; |
140 | } |
141 | } |