Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ApiCrud | |
0.00% |
0 / 50 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| execute | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
30 | |||
| getAllowedParams | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| getExamplesMessages | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\WikispeechSpeechDataCollector\Api; |
| 4 | |
| 5 | /** |
| 6 | * @file |
| 7 | * @ingroup Extensions |
| 8 | * @license GPL-2.0-or-later |
| 9 | */ |
| 10 | |
| 11 | use ApiBase; |
| 12 | use ApiMain; |
| 13 | use ApiUsageException; |
| 14 | use Exception; |
| 15 | use FormatJson; |
| 16 | use MediaWiki\Logger\LoggerFactory; |
| 17 | use MediaWiki\Revision\RevisionStore; |
| 18 | use MediaWiki\WikispeechSpeechDataCollector\Crud\CrudContext; |
| 19 | use MediaWiki\WikispeechSpeechDataCollector\Crud\Transaction\CrudTransactionExecutor; |
| 20 | use MediaWiki\WikispeechSpeechDataCollector\Crud\Transaction\CrudTransactionMWAssociativeArrayMarshaller; |
| 21 | use Psr\Log\LoggerInterface; |
| 22 | use Wikimedia\ParamValidator\ParamValidator; |
| 23 | use Wikimedia\Rdbms\ILoadBalancer; |
| 24 | |
| 25 | /** |
| 26 | * @todo This is part of an internal development API. It should be removed before deployment. |
| 27 | * |
| 28 | * ?action=wikispeech-sdc-crud&transaction=json content |
| 29 | * |
| 30 | * Insecure and unlimited generic API access for |
| 31 | * creating, updating, reading and deleting persistent objects. |
| 32 | * |
| 33 | * This is for development purposes. |
| 34 | * It allows for communicating with the backend in a way that |
| 35 | * hopefully is easy to implement as secure ad hoc API features |
| 36 | * without having to make too many changes in the client code. |
| 37 | * |
| 38 | * @see CrudTransactionExecutor |
| 39 | * @since 0.1.0 |
| 40 | */ |
| 41 | class ApiCrud extends ApiBase { |
| 42 | |
| 43 | /** @var CrudContext */ |
| 44 | private $context; |
| 45 | |
| 46 | /** @var LoggerInterface */ |
| 47 | private $logger; |
| 48 | |
| 49 | /** |
| 50 | * @since 0.1.0 |
| 51 | * @param ApiMain $mainModule |
| 52 | * @param string $moduleName Name of this module |
| 53 | * @param ILoadBalancer $dbLoadBalancer |
| 54 | * @param RevisionStore $revisionStore |
| 55 | * @param string $modulePrefix Prefix to use for parameter names |
| 56 | */ |
| 57 | public function __construct( |
| 58 | ApiMain $mainModule, |
| 59 | $moduleName, |
| 60 | ILoadBalancer $dbLoadBalancer, |
| 61 | RevisionStore $revisionStore, |
| 62 | $modulePrefix = '' |
| 63 | ) { |
| 64 | parent::__construct( $mainModule, $moduleName, $modulePrefix ); |
| 65 | $this->context = new CrudContext( |
| 66 | $dbLoadBalancer, |
| 67 | $this->getUser(), |
| 68 | $revisionStore |
| 69 | ); |
| 70 | // @todo inject |
| 71 | $this->logger = LoggerFactory::getInstance( "Wikispeech-SDC-ApiCrud" ); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @since 0.1.0 |
| 76 | * @throws ApiUsageException |
| 77 | */ |
| 78 | public function execute() { |
| 79 | $inputParameters = $this->extractRequestParams(); |
| 80 | $jsonStatus = FormatJson::parse( |
| 81 | $inputParameters['transaction'], |
| 82 | FormatJson::FORCE_ASSOC |
| 83 | ); |
| 84 | if ( !$jsonStatus->isOK() ) { |
| 85 | $this->logger->error( __METHOD__ . ': Failed to deserialize JSON. ' . $jsonStatus ); |
| 86 | $this->dieWithError( 'apierror-wikispeech-sdc-crud-deserialize-transaction-json' ); |
| 87 | } |
| 88 | |
| 89 | $transactionMarshaller = new CrudTransactionMWAssociativeArrayMarshaller(); |
| 90 | |
| 91 | try { |
| 92 | $transactionRequest = $transactionMarshaller->deserializeRequest( |
| 93 | $jsonStatus->getValue() |
| 94 | ); |
| 95 | } catch ( Exception $e ) { |
| 96 | $this->logger->error( __METHOD__ . ': Failed to deserialize transaction. ' . $e ); |
| 97 | $this->dieWithError( 'apierror-wikispeech-sdc-crud-deserialize-transaction' ); |
| 98 | } |
| 99 | |
| 100 | $transactionExecutor = new CrudTransactionExecutor( $this->context ); |
| 101 | try { |
| 102 | $transactionResponse = $transactionExecutor->execute( $transactionRequest ); |
| 103 | } catch ( Exception $e ) { |
| 104 | $this->logger->error( __METHOD__ . ': Failed to execute transaction. ' . $e ); |
| 105 | $this->dieWithError( 'apierror-wikispeech-sdc-crud-execute-transaction' ); |
| 106 | } |
| 107 | |
| 108 | try { |
| 109 | $serializedResponse = $transactionMarshaller->serializeResponse( $transactionResponse ); |
| 110 | } catch ( Exception $e ) { |
| 111 | $this->logger->error( __METHOD__ . ': Failed to serialize response. ' . $e ); |
| 112 | $this->dieWithError( 'apierror-wikispeech-sdc-crud-serialize-response' ); |
| 113 | } |
| 114 | |
| 115 | $this->getResult()->addValue( |
| 116 | null, |
| 117 | $this->getModuleName(), |
| 118 | $serializedResponse |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Specify what parameters the API accepts. |
| 124 | * |
| 125 | * @since 0.1.0 |
| 126 | * @return array |
| 127 | */ |
| 128 | protected function getAllowedParams() { |
| 129 | return [ |
| 130 | 'transaction' => [ |
| 131 | ParamValidator::PARAM_TYPE => 'string', |
| 132 | ParamValidator::PARAM_REQUIRED => true |
| 133 | ] |
| 134 | ]; |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Give examples of usage. |
| 139 | * |
| 140 | * @since 0.1.0 |
| 141 | * @return array |
| 142 | */ |
| 143 | public function getExamplesMessages() { |
| 144 | return [ |
| 145 | // phpcs:ignore Generic.Files.LineLength |
| 146 | 'action=wikispeech-sdc-crud&transaction={"create":{"Recording":[{"identity":null,"voiceOf":"61b25777-a277-406d-96c0-f1206ba64c69","recorded":"2020-11-02T11:15:44.000000Z","spokenDialect":"657c7d19-c871-4c59-82e6-45a7d3d00692","manuscriptPrompt":"4cba603f-ec8a-46ca-bde1-f44ae1a8d415"}]}}' |
| 147 | => 'apihelp-wikispeech-sdc-crud-example-1', |
| 148 | // phpcs:ignore Generic.Files.LineLength |
| 149 | 'action=wikispeech-sdc-crud&transaction={"read":{"Recording":["bf8b38e3-12d4-49c0-be3a-ce52e0bfefcc"]}}' |
| 150 | => 'apihelp-wikispeech-sdc-crud-example-2', |
| 151 | // phpcs:ignore Generic.Files.LineLength |
| 152 | 'action=wikispeech-sdc-crud&transaction={"delete":{"Recording":["bf8b38e3-12d4-49c0-be3a-ce52e0bfefcc"]}}' |
| 153 | => 'apihelp-wikispeech-sdc-crud-example-3', |
| 154 | ]; |
| 155 | } |
| 156 | |
| 157 | } |