25 private IConnectionProvider $connectionProvider;
26 private UserFactory $userFactory;
29 public function __construct(
32 IConnectionProvider $connectionProvider,
33 UserFactory $userFactory,
36 parent::__construct( $mainModule, $moduleName );
37 $this->connectionProvider = $connectionProvider;
38 $this->userFactory = $userFactory;
39 $this->messageIndex = $messageIndex;
42 public function execute():
void {
43 $params = $this->extractRequestParams();
46 $user = $this->getUser();
48 if ( isset( $params[
'username'] ) ) {
49 if ( $user->isAllowed(
'translate-sandboxmanage' ) ) {
50 $user = $this->userFactory->newFromName( $params[
'username'] );
52 $this->dieWithError( [
'apierror-badparameter',
'username' ],
'invalidparam' );
55 $this->dieWithError( [
'apierror-badparameter',
'username' ],
'invalidparam' );
60 $action = $params[
'subaction'];
62 if ( $action ===
'add' ) {
63 if ( !isset( $params[
'title'] ) ) {
64 $this->dieWithError( [
'apierror-missingparam',
'title' ] );
66 if ( !isset( $params[
'translation'] ) ) {
67 $this->dieWithError( [
'apierror-missingparam',
'translation' ] );
70 $metadata = $params[
'metadata'] ? FormatJson::decode( $params[
'metadata'],
true ) :
null;
74 Title::newFromText( $params[
'title'] ),
75 $params[
'translation'],
78 $stash->addTranslation( $translation );
82 if ( $action ===
'query' ) {
83 $output[
'translations'] = [];
85 $translations = $stash->getTranslations( $user );
86 foreach ( $translations as $translation ) {
87 $output[
'translations'][] = $this->formatTranslation( $translation );
92 $output[
'result'] =
'ok';
93 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
97 $title = $translation->getTitle();
103 if ( $handle->isValid() ) {
104 $groupId = $this->messageIndex->getPrimaryGroupId( $handle );
105 $group = MessageGroups::getGroup( $groupId );
107 $key = $handle->getKey();
109 $definition = $group->getMessage( $key, $group->getSourceLanguage() );
110 $comparison = $group->getMessage( $key, $handle->getCode() );
114 'title' => $title->getPrefixedText(),
115 'definition' => $definition,
116 'translation' => $translation->getValue(),
117 'comparison' => $comparison,
118 'metadata' => $translation->getMetadata(),
122 public function isWriteMode():
bool {
126 public function needsToken():
string {
130 protected function getAllowedParams(): array {
133 ParamValidator::PARAM_TYPE => [
'add',
'query' ],
134 ParamValidator::PARAM_REQUIRED =>
true,
137 ParamValidator::PARAM_TYPE =>
'string',
140 ParamValidator::PARAM_TYPE =>
'string',
143 ParamValidator::PARAM_TYPE =>
'string',
146 ParamValidator::PARAM_TYPE =>
'string',
147 ParamValidator::PARAM_REQUIRED =>
true,
150 ParamValidator::PARAM_TYPE =>
'string',
155 protected function getExamplesMessages(): array {
157 'action=translationstash&subaction=add&title=MediaWiki:Jan/fi&translation=tammikuu&metadata={}'
158 =>
'apihelp-translationstash-example-1',
159 'action=translationstash&subaction=query'
160 =>
'apihelp-translationstash-example-2',