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