Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
98.00% |
49 / 50 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApiZObjectEditor | |
98.00% |
49 / 50 |
|
75.00% |
3 / 4 |
18 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| run | |
100.00% |
35 / 35 |
|
100.00% |
1 / 1 |
6 | |||
| updateWatchlist | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
5 | |||
| 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\Api\ApiWatchlistTrait; |
| 15 | use MediaWiki\Extension\WikiLambda\HttpStatus; |
| 16 | use MediaWiki\Extension\WikiLambda\Registry\ZTypeRegistry; |
| 17 | use MediaWiki\Extension\WikiLambda\WikiLambdaServices; |
| 18 | use MediaWiki\Extension\WikiLambda\ZErrorFactory; |
| 19 | use MediaWiki\MainConfigNames; |
| 20 | use MediaWiki\Title\Title; |
| 21 | use MediaWiki\User\Options\UserOptionsLookup; |
| 22 | use MediaWiki\User\User; |
| 23 | use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 24 | use MediaWiki\Watchlist\WatchlistManager; |
| 25 | use Wikimedia\ParamValidator\ParamValidator; |
| 26 | use Wikimedia\Stats\StatsFactory; |
| 27 | |
| 28 | class ApiZObjectEditor extends WikiLambdaApiBase { |
| 29 | |
| 30 | use ApiWatchlistTrait; |
| 31 | |
| 32 | public function __construct( |
| 33 | ApiMain $mainModule, |
| 34 | string $moduleName, |
| 35 | StatsFactory $statsFactory, |
| 36 | WatchlistManager $watchlistManager, |
| 37 | WatchedItemStoreInterface $watchedItemStore, |
| 38 | UserOptionsLookup $userOptionsLookup, |
| 39 | ) { |
| 40 | parent::__construct( $mainModule, $moduleName, $statsFactory ); |
| 41 | |
| 42 | $this->watchlistExpiryEnabled = $this->getConfig()->get( MainConfigNames::WatchlistExpiry ); |
| 43 | $this->watchlistMaxDuration = $this->getConfig()->get( MainConfigNames::WatchlistExpiryMaxDuration ); |
| 44 | $this->watchlistManager = $watchlistManager; |
| 45 | $this->watchedItemStore = $watchedItemStore; |
| 46 | $this->userOptionsLookup = $userOptionsLookup; |
| 47 | |
| 48 | $this->setUp(); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * @inheritDoc |
| 53 | */ |
| 54 | protected function run(): void { |
| 55 | $user = $this->getUser(); |
| 56 | $params = $this->extractRequestParams(); |
| 57 | |
| 58 | $summary = $params[ 'summary' ]; |
| 59 | $zobject = $params[ 'zobject' ]; |
| 60 | |
| 61 | // If zid is set, we should be editing it, if empty or Z0, we are creating a new zobject |
| 62 | $zid = $params[ 'zid' ]; |
| 63 | |
| 64 | // If baserevid is set, the edit fails when the ZObject has a different latest revision |
| 65 | $baseRevId = $params[ 'baserevid' ]; |
| 66 | |
| 67 | $zObjectStore = WikiLambdaServices::getZObjectStore(); |
| 68 | |
| 69 | $creating = !$zid || $zid === ZTypeRegistry::Z_NULL_REFERENCE; |
| 70 | if ( $creating ) { |
| 71 | // Create a new ZObject |
| 72 | $response = $zObjectStore->createNewZObject( $this, $zobject, $summary, $user ); |
| 73 | } else { |
| 74 | // Check if the ZObject exists (i.e. someone's making an edit), and pass the correct edit flag |
| 75 | if ( Title::newFromText( $zid )->exists() ) { |
| 76 | $editFlag = EDIT_UPDATE; |
| 77 | } else { |
| 78 | // … but only for very-priviledged users, as this can cause major issues if e.g. Z99999999 was created |
| 79 | if ( !$user->isAllowed( 'wikilambda-create-arbitrary-zid' ) ) { |
| 80 | $zError = ZErrorFactory::createAuthorizationZError( 'wikilambda-edit', EDIT_NEW ); |
| 81 | WikiLambdaApiBase::dieWithZError( $zError, HttpStatus::FORBIDDEN ); |
| 82 | } |
| 83 | $editFlag = EDIT_NEW; |
| 84 | $creating = true; |
| 85 | } |
| 86 | |
| 87 | // Edit an existing ZObject |
| 88 | $response = $zObjectStore->updateZObject( |
| 89 | $this, $zid, $zobject, $summary, $user, $editFlag, $baseRevId, $params['undo'], $params['undoafter'] |
| 90 | ); |
| 91 | } |
| 92 | |
| 93 | if ( !$response->isOK() ) { |
| 94 | WikiLambdaApiBase::dieWithZError( $response->getErrors(), HttpStatus::BAD_REQUEST ); |
| 95 | } |
| 96 | |
| 97 | $title = $response->getTitle(); |
| 98 | $this->updateWatchlist( $params, $title, $user, $creating ); |
| 99 | |
| 100 | $this->getResult()->addValue( |
| 101 | null, |
| 102 | $this->getModuleName(), |
| 103 | [ |
| 104 | 'success' => true, |
| 105 | 'articleId' => $title->getArticleID(), |
| 106 | 'title' => $title->getBaseText(), |
| 107 | 'page' => $title->getBaseTitle(), |
| 108 | 'revisionId' => $response->getWikiPage()->getLatest() |
| 109 | ] |
| 110 | ); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Watch or unwatch the saved page, as the request and the user preferences specify. |
| 115 | * |
| 116 | * @param array $params |
| 117 | * @param Title $title |
| 118 | * @param User $user |
| 119 | * @param bool $creating |
| 120 | */ |
| 121 | private function updateWatchlist( array $params, Title $title, User $user, bool $creating ): void { |
| 122 | // Core decides before the save, but a new ZID is known only after it, so apply watchcreations here. |
| 123 | $watch = $this->getWatchlistValue( $params['watchlist'], $title, $user ) || ( |
| 124 | $creating && $this->getWatchlistValue( $params['watchlist'], $title, $user, 'watchcreations' ) |
| 125 | ); |
| 126 | $expiry = $watch ? $this->getExpiryFromParams( |
| 127 | $params, $title, $user, $creating ? 'watchcreations-expiry' : 'watchdefault-expiry' |
| 128 | ) : null; |
| 129 | $this->watchlistManager->setWatch( $watch, $user, $title, $expiry ); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * @inheritDoc |
| 134 | * @codeCoverageIgnore |
| 135 | */ |
| 136 | public function mustBePosted() { |
| 137 | return true; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * @inheritDoc |
| 142 | * @codeCoverageIgnore |
| 143 | */ |
| 144 | public function isWriteMode() { |
| 145 | return true; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @inheritDoc |
| 150 | * @codeCoverageIgnore |
| 151 | */ |
| 152 | public function needsToken(): string { |
| 153 | return 'csrf'; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Mark as internal. This isn't meant to be user-facing, and can change at any time. |
| 158 | * @return bool |
| 159 | */ |
| 160 | public function isInternal() { |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | /** |
| 165 | * @inheritDoc |
| 166 | * @codeCoverageIgnore |
| 167 | */ |
| 168 | protected function getAllowedParams(): array { |
| 169 | return [ |
| 170 | 'summary' => [ |
| 171 | ParamValidator::PARAM_TYPE => 'string', |
| 172 | ParamValidator::PARAM_REQUIRED => false, |
| 173 | ParamValidator::PARAM_DEFAULT => '', |
| 174 | ], |
| 175 | 'zid' => [ |
| 176 | ParamValidator::PARAM_TYPE => 'string', |
| 177 | ParamValidator::PARAM_REQUIRED => false, |
| 178 | ParamValidator::PARAM_DEFAULT => null, |
| 179 | ], |
| 180 | 'zobject' => [ |
| 181 | ParamValidator::PARAM_TYPE => 'text', |
| 182 | ParamValidator::PARAM_REQUIRED => true, |
| 183 | ], |
| 184 | 'baserevid' => [ |
| 185 | ParamValidator::PARAM_TYPE => 'integer', |
| 186 | ParamValidator::PARAM_REQUIRED => false, |
| 187 | ParamValidator::PARAM_DEFAULT => null, |
| 188 | ], |
| 189 | 'undo' => [ |
| 190 | ParamValidator::PARAM_TYPE => 'integer', |
| 191 | ParamValidator::PARAM_REQUIRED => false, |
| 192 | ParamValidator::PARAM_DEFAULT => 0, |
| 193 | ], |
| 194 | 'undoafter' => [ |
| 195 | ParamValidator::PARAM_TYPE => 'integer', |
| 196 | ParamValidator::PARAM_REQUIRED => false, |
| 197 | ParamValidator::PARAM_DEFAULT => 0, |
| 198 | ] |
| 199 | ] + $this->getWatchlistParams(); |
| 200 | } |
| 201 | |
| 202 | /** |
| 203 | * @inheritDoc |
| 204 | * @codeCoverageIgnore |
| 205 | */ |
| 206 | protected function getExamplesMessages() { |
| 207 | return [ |
| 208 | 'action=wikilambda_edit&format=json&summary=New%20zobject&zobject=' |
| 209 | . urlencode( '{"Z1K1":"Z2","Z2K1":{"Z1K1":"Z6","Z6K1":"Z0"},"Z2K2":"string value",' |
| 210 | . '"Z2K3":{"Z1K1":"Z12","Z12K1":["Z11", {"Z1K1":"Z11","Z11K1":"Z1002","Z11K2":"label"}]}}' ) |
| 211 | => 'apihelp-wikilambda_edit-example-create', |
| 212 | 'action=wikilambda_edit&format=json&summary=Edit%20zobject&zid=Z01&zobject=' |
| 213 | . urlencode( '{"Z1K1":"Z2","Z2K1":{"Z1K1":"Z6","Z6K1":"Z01"},"Z2K2":"string value"}' ) |
| 214 | => 'apihelp-wikilambda_edit-example-edit-incorrect' |
| 215 | ]; |
| 216 | } |
| 217 | } |