26 private $loadBalancer;
30 public function __construct(
33 ILoadBalancer $loadBalancer,
34 UserFactory $userFactory
36 parent::__construct( $mainModule, $moduleName );
37 $this->loadBalancer = $loadBalancer;
38 $this->userFactory = $userFactory;
41 public function execute():
void {
42 $params = $this->extractRequestParams();
45 $user = $this->getUser();
47 if ( isset( $params[
'username'] ) ) {
48 if ( $this->getUser()->isAllowed(
'translate-sandboxmanage' ) ) {
49 $user = $this->userFactory->newFromName( $params[
'username'] );
51 $this->dieWithError( [
'apierror-badparameter',
'username' ],
'invalidparam' );
54 $this->dieWithError( [
'apierror-badparameter',
'username' ],
'invalidparam' );
59 $action = $params[
'subaction'];
61 if ( $action ===
'add' ) {
62 if ( !isset( $params[
'title'] ) ) {
63 $this->dieWithError( [
'apierror-missingparam',
'title' ] );
65 if ( !isset( $params[
'translation'] ) ) {
66 $this->dieWithError( [
'apierror-missingparam',
'translation' ] );
72 Title::newFromText( $params[
'title'] ),
73 $params[
'translation'],
74 FormatJson::decode( $params[
'metadata'],
true )
76 $stash->addTranslation( $translation );
80 if ( $action ===
'query' ) {
81 $output[
'translations'] = [];
83 $translations = $stash->getTranslations( $user );
84 foreach ( $translations as $translation ) {
85 $output[
'translations'][] = $this->formatTranslation( $translation );
90 $output[
'result'] =
'ok';
91 $this->getResult()->addValue(
null, $this->getModuleName(), $output );
95 $title = $translation->getTitle();
101 if ( $handle->isValid() ) {
102 $groupId = MessageIndex::getPrimaryGroupId( $handle );
103 $group = MessageGroups::getGroup( $groupId );
105 $key = $handle->getKey();
107 $definition = $group->getMessage( $key, $group->getSourceLanguage() );
108 $comparison = $group->getMessage( $key, $handle->getCode() );
112 'title' => $title->getPrefixedText(),
113 'definition' => $definition,
114 'translation' => $translation->getValue(),
115 'comparison' => $comparison,
116 'metadata' => $translation->getMetadata(),
120 public function isWriteMode():
bool {
124 public function needsToken():
string {
128 protected function getAllowedParams(): array {
131 ParamValidator::PARAM_TYPE => [
'add',
'query' ],
132 ParamValidator::PARAM_REQUIRED =>
true,
135 ParamValidator::PARAM_TYPE =>
'string',
138 ParamValidator::PARAM_TYPE =>
'string',
141 ParamValidator::PARAM_TYPE =>
'string',
144 ParamValidator::PARAM_TYPE =>
'string',
145 ParamValidator::PARAM_REQUIRED =>
true,
148 ParamValidator::PARAM_TYPE =>
'string',
153 protected function getExamplesMessages(): array {
155 'action=translationstash&subaction=add&title=MediaWiki:Jan/fi&translation=tammikuu&metadata={}'
156 =>
'apihelp-translationstash-example-1',
157 'action=translationstash&subaction=query'
158 =>
'apihelp-translationstash-example-2',