Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 268
0.00% covered (danger)
0.00%
0 / 13
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiSectionTranslationPublish
0.00% covered (danger)
0.00%
0 / 268
0.00% covered (danger)
0.00%
0 / 13
1056
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getPublishSummary
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 submitEditAction
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 1
56
 publish
0.00% covered (danger)
0.00%
0 / 112
0.00% covered (danger)
0.00%
0 / 1
110
 saveWikitext
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 1
12
 storeTags
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 isSandbox
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 updateTranslation
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
2
 needsToken
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isWriteMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * Saving a section to an existing wikipage, created using Section translation feature of ContentTranslation.
4 * The following special things happen when the page is created:
5 * - HTML from the translation editor's contenteditable is converted to wiki syntax using Parsoid.
6 * - A change tag is added.
7 * - The edit summary shows a link to the revision from which the translation was made.
8 *
9 * @copyright See AUTHORS.txt
10 * @license GPL-2.0-or-later
11 */
12
13namespace ContentTranslation\ActionApi;
14
15use ContentTranslation\ContentTranslationHookRunner;
16use ContentTranslation\Exception\HtmlToWikitextConversionException;
17use ContentTranslation\Exception\SectionWikitextRetrievalException;
18use ContentTranslation\LogNames;
19use ContentTranslation\SectionAction;
20use ContentTranslation\Service\SandboxTitleMaker;
21use ContentTranslation\Service\SectionContentEvaluator;
22use ContentTranslation\Service\SectionPositionCalculator;
23use ContentTranslation\Service\TranslationTargetUrlCreator;
24use ContentTranslation\Service\UserPermissionChecker;
25use ContentTranslation\Store\SectionTranslationStore;
26use ContentTranslation\Store\TranslationStore;
27use ContentTranslation\Translation;
28use Exception;
29use MediaWiki\Api\ApiBase;
30use MediaWiki\Api\ApiMain;
31use MediaWiki\Api\ApiUsageException;
32use MediaWiki\ChangeTags\ChangeTagsStore;
33use MediaWiki\Deferred\DeferredUpdates;
34use MediaWiki\HookContainer\HookContainer;
35use MediaWiki\Language\LanguageNameUtils;
36use MediaWiki\Logger\LoggerFactory;
37use MediaWiki\Request\DerivativeRequest;
38use MediaWiki\Title\Title;
39use MediaWiki\Title\TitleFactory;
40use MediaWiki\User\User;
41use Psr\Log\LoggerInterface;
42use Wikimedia\ParamValidator\ParamValidator;
43
44class ApiSectionTranslationPublish extends ApiBase {
45    private LoggerInterface $logger;
46
47    public function __construct(
48        ApiMain $main,
49        string $action,
50        private readonly TitleFactory $titleFactory,
51        private readonly HookContainer $hookContainer,
52        private readonly LanguageNameUtils $languageNameUtils,
53        private readonly SectionContentEvaluator $sectionContentEvaluator,
54        private readonly SectionPositionCalculator $sectionPositionCalculator,
55        private readonly SandboxTitleMaker $sandboxTitleMaker,
56        private readonly SectionTranslationStore $sectionTranslationStore,
57        private readonly TranslationStore $translationStore,
58        private readonly TranslationTargetUrlCreator $targetUrlCreator,
59        private readonly UserPermissionChecker $userPermissionChecker,
60        private readonly ChangeTagsStore $changeTagsStore
61    ) {
62        parent::__construct( $main, $action );
63        $this->logger = LoggerFactory::getInstance( LogNames::MAIN );
64    }
65
66    /**
67     * @param string $sourceLanguage
68     * @param string $sourceRevId
69     * @param string $sourceTitle
70     * @param SectionAction $sectionAction
71     * @param string $sourceSectionTitle
72     * @return string
73     */
74    private function getPublishSummary(
75        string $sourceLanguage,
76        string $sourceRevId,
77        string $sourceTitle,
78        SectionAction $sectionAction,
79        string $sourceSectionTitle
80    ): string {
81        $sourceLink = "[[:{$sourceLanguage}:Special:Redirect/revision/{$sourceRevId}|{$sourceTitle}]]";
82        // if the published section is a lead section, the summary should be slightly different
83        if ( $sectionAction->isNewLeadSection() ) {
84            $message = $this->msg( 'cx-sx-publish-lead-section-summary', $sourceLink );
85        } else {
86            $message = $this->msg( 'cx-sx-publish-summary', $sourceSectionTitle, $sourceLink );
87        }
88
89        return $message->inContentLanguage()->text();
90    }
91
92    /**
93     * Attempt to save a given page's wikitext to MediaWiki's storage layer via its API
94     *
95     * @param Title $title The title of the page to write
96     * @param string $wikitext The wikitext to write
97     * @param int|string $sectionNumber
98     * @return mixed The result of the save attempt
99     * @throws ApiUsageException
100     */
101    protected function submitEditAction( Title $title, string $wikitext, $sectionNumber ) {
102        $params = $this->extractRequestParams();
103        [ 'sourcelanguage' => $from, 'sourcerevid' => $sourceRevId, 'sourcetitle' => $sourceTitle ] = $params;
104        $publishTarget = $params['publishtarget'];
105        $sectionAction = SectionAction::fromData( $sectionNumber, $publishTarget );
106        $summary = $this->getPublishSummary(
107            $from,
108            $sourceRevId,
109            $sourceTitle,
110            $sectionAction,
111            $params['sourcesectiontitle']
112        );
113
114        $apiParams = [
115            'action' => 'edit',
116            'title' => $title->getPrefixedDBkey(),
117            'summary' => $summary,
118            'sectiontitle' => $params['targetsectiontitle'],
119            'section' => $sectionNumber,
120            'captchaid' => $params['captchaid'],
121            'captchaword' => $params['captchaword'],
122        ];
123
124        if ( $sectionAction->isNewNumberedSection() ) {
125            $apiParams['prependtext'] = $wikitext;
126        } else {
127            $apiParams['text'] = $wikitext;
128        }
129
130        // Pass any unrecognized query parameters to the internal action=edit API request.
131        $allowedParams = array_diff_key(
132            $this->getRequest()->getValues(),
133            $this->getAllowedParams(),
134            $this->getMain()->getAllowedParams()
135        );
136        $api = new ApiMain(
137            new DerivativeRequest(
138                $this->getRequest(),
139                $apiParams + $allowedParams,
140                /* was posted? */ true
141            ),
142            /* enable write? */ true
143        );
144        $api->execute();
145        return $api->getResult()->getResultData();
146    }
147
148    /**
149     * @throws ApiUsageException
150     */
151    public function execute() {
152        $params = $this->extractRequestParams();
153
154        $block = $this->getUser()->getBlock();
155        if ( $block && $block->isSitewide() ) {
156            $this->dieBlocked( $block );
157        }
158
159        if ( !$this->languageNameUtils->isKnownLanguageTag( $params['sourcelanguage'] ) ) {
160            $this->dieWithError( 'apierror-cx-invalidsourcelanguage', 'invalidsourcelanguage' );
161        }
162
163        if ( !$this->languageNameUtils->isKnownLanguageTag( $params['targetlanguage'] ) ) {
164            $this->dieWithError( 'apierror-cx-invalidtargetlanguage', 'invalidtargetlanguage' );
165        }
166
167        if ( trim( $params['html'] ) === '' ) {
168            $this->dieWithError( [ 'apierror-paramempty', 'html' ], 'invalidhtml' );
169        }
170
171        // Check user group publishing requirements
172        if (
173            !$this->userPermissionChecker->checkUserCanPublish(
174                $this->getUser(),
175                $params['title'],
176                $this->isSandbox()
177            )
178        ) {
179            $this->dieWithError( 'apierror-cx-publish-usergroup-required', 'usergroup-required' );
180        }
181
182        $this->publish();
183    }
184
185    public function publish() {
186        $params = $this->extractRequestParams();
187
188        $targetTitleRaw = $params['title'];
189        $isSandbox = $this->isSandbox();
190        $user = $this->getUser();
191        if ( $isSandbox ) {
192            $targetTitle = $this->sandboxTitleMaker->makeSandboxTitle( $user, $targetTitleRaw );
193        } else {
194            $targetTitle = $this->titleFactory->newFromText( $targetTitleRaw );
195        }
196
197        if ( !$targetTitle ) {
198            $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $targetTitleRaw ) ] );
199        }
200
201        $hookRunner = new ContentTranslationHookRunner( $this->hookContainer );
202        $targetLanguage = $params['targetlanguage'];
203        '@phan-var Title $targetTitle';
204        $hookRunner->onSectionTranslationBeforePublish( $targetTitle, $targetLanguage, $user );
205
206        $existingTargetSectionTitle = $params['existingsectiontitle'];
207        $sourceSectionTitle = $params['sourcesectiontitle'];
208        $sourceLanguage = $params['sourcelanguage'];
209        $sourceTitle = $params['sourcetitle'];
210        $sectionNumber = $this->sectionPositionCalculator->calculateSectionPosition(
211            $targetTitle,
212            $targetLanguage,
213            $isSandbox,
214            $sourceLanguage,
215            $sourceTitle,
216            $sourceSectionTitle,
217            $existingTargetSectionTitle,
218        );
219
220        $targetSectionTitle = $params['targetsectiontitle'];
221        try {
222            $editResult = $this->saveWikitext(
223                $params['html'],
224                $targetTitle,
225                $sectionNumber,
226                $targetSectionTitle,
227                $params['publishtarget'],
228                $existingTargetSectionTitle
229            );
230        } catch ( ApiUsageException $e ) {
231            throw $e;
232        } catch ( Exception $e ) {
233            $this->logger->error(
234                'Error when publishing section {targetTitle}, {exception}',
235                [
236                    'targetTitle' => $targetTitle->getPrefixedDBkey(),
237                    'exception' => $e->getMessage(),
238                ]
239            );
240
241            throw $e;
242        }
243
244        $editStatus = $editResult['result'];
245
246        if ( $editStatus === 'Success' ) {
247            [ 'sourcelanguage' => $sourceLanguage, 'sourcetitle' => $sourceTitle ] = $params;
248            $result = [
249                'result' => 'success',
250                'edit' => $editResult,
251                'targettitle' => $targetTitle->getPrefixedDBkey(),
252                'targeturl' => $this->targetUrlCreator->createUrlForSXRedirection(
253                    $targetTitle->getPrefixedDBkey(),
254                    $targetLanguage,
255                    $sourceLanguage,
256                    $sourceTitle,
257                    $targetSectionTitle
258                )
259            ];
260            // newrevid can be unset when publishing already present sections with the exact same
261            // contents as the current revision
262            if ( isset( $editResult['newrevid'] ) ) {
263                // Add the tags post-send, after RC row insertion
264                $newRevId = intval( $editResult['newrevid'] );
265                $result['newrevid'] = $newRevId;
266                $this->storeTags( $newRevId );
267
268                $translation = $this->translationStore->findTranslationByUser(
269                    $user,
270                    $sourceTitle,
271                    $sourceLanguage,
272                    $targetLanguage,
273                );
274
275                if ( $translation === null ) {
276                    $this->dieWithError( 'apierror-cxpublishsection-translationnotfound', 'translationnotfound' );
277                }
278
279                // if translation exists update the "translation_target_revision_id" field for this row
280                '@phan-var Translation $translation';
281                $this->updateTranslation(
282                    $translation,
283                    $user,
284                    $newRevId,
285                    $targetTitle->getPrefixedDBkey(),
286                    $targetLanguage
287                );
288
289                $publishedStatusIndex = SectionTranslationStore::getStatusIndexByStatus(
290                    SectionTranslationStore::TRANSLATION_STATUS_PUBLISHED
291                );
292                $this->sectionTranslationStore->updateTranslationStatusById(
293                    $params['sectiontranslationid'],
294                    $publishedStatusIndex
295                );
296
297                $hookRunner->onContentTranslationAfterPublish(
298                    $user,
299                    $targetTitle,
300                    $newRevId,
301                    $sourceLanguage,
302                    $sourceTitle,
303                    $targetLanguage,
304                    true
305                );
306            }
307
308            if ( isset( $editResult['pageid'] ) ) {
309                $result['pageid'] = intval( $editResult['pageid'] );
310            }
311        } else {
312            $result = [
313                'result' => 'error',
314                'edit' => $editResult
315            ];
316
317            // Don't bother logging captcha related errors
318            // ApiContentTranslationPublish::saveWikiText returns the API response directly, whereas
319            // ApiSectionTranslationPublish::saveWikiText returns 'edit' property from API response.
320            if ( !isset( $editResult['captcha'] ) ) {
321                $this->logger->error(
322                    'Error when publishing section {targetTitle}',
323                    [
324                        'targetTitle' => $targetTitle->getPrefixedDBkey(),
325                        'editResult' => json_encode( $editResult, JSON_PRETTY_PRINT ),
326                    ]
327                );
328            }
329        }
330
331        $this->getResult()->addValue( null, $this->getModuleName(), $result );
332    }
333
334    /**
335     * Given the HTML of the published translation and the target page title (as a Title instance),
336     * this method uses the ParsoidClient service to convert the HTML to wikitext and calls
337     * the "submitEditAction" method to save the wikitext to MediaWiki's storage layer via its API.
338     *
339     * @param string $html
340     * @param Title $targetTitle
341     * @param int|string $sectionNumber
342     * @param string $targetSectionTitle
343     * @param string $publishTarget
344     * @param string|null $existingSectionTitle
345     * @return mixed
346     * @throws ApiUsageException
347     */
348    private function saveWikitext(
349        string $html,
350        Title $targetTitle,
351        $sectionNumber,
352        string $targetSectionTitle,
353        string $publishTarget,
354        ?string $existingSectionTitle = null
355    ) {
356        $sectionAction = SectionAction::fromData( $sectionNumber, $publishTarget );
357        try {
358            $wikitext = $this->sectionContentEvaluator->calculateSectionContent(
359                $html,
360                $targetTitle,
361                $sectionAction,
362                $targetSectionTitle,
363                $existingSectionTitle
364            );
365        } catch ( HtmlToWikitextConversionException $e ) {
366            $this->logger->error(
367                'Error when converting section HTML to Wikitext using ParsoidClient for {targetTitle}, {errorMessage}',
368                [
369                    'errorMessage' => $e->getMessage(),
370                    'targetTitle' => $targetTitle->getPrefixedDBkey(),
371                ]
372            );
373            $this->dieWithError(
374                [ 'apierror-cx-docserverexception', wfEscapeWikiText( $e->getMessage() ) ], 'docserver'
375            );
376        } catch ( SectionWikitextRetrievalException $e ) {
377            $this->logger->error(
378                'Error while retrieving wikitext of {targetTitle}:{sectionTitle}. {errorMessage}.',
379                [
380                    'targetTitle' => $targetTitle->getPrefixedDBkey(),
381                    'sectionTitle' => $existingSectionTitle,
382                    'errorMessage' => $e->getMessage(),
383                ]
384            );
385            $this->dieWithError(
386                [ 'apierror-cx-wikitext-retrieval-exception', wfEscapeWikiText( $e->getMessage() ) ], 'docserver'
387            );
388        }
389        $editResult = $this->submitEditAction( $targetTitle, $wikitext, $sectionNumber );
390        return $editResult['edit'];
391    }
392
393    /**
394     * Given a new target revision id, this method adds a deferred update to be executed at the end
395     * of the current request. This update adds the "contenttranslation" and "sectiontranslation" tags
396     * to the given revision.
397     *
398     * @param int $newRevId
399     * @return void
400     */
401    private function storeTags( int $newRevId ) {
402        $tags = [
403            'contenttranslation',
404            'sectiontranslation'
405        ];
406        DeferredUpdates::addCallableUpdate( function () use ( $newRevId, $tags ) {
407            $this->changeTagsStore->addTags( $tags, null, $newRevId, null );
408        } );
409    }
410
411    private function isSandbox(): bool {
412        $params = $this->extractRequestParams();
413        // section number is not used for sandbox publishing
414        $sectionAction = SectionAction::fromData( '', $params['publishtarget'] );
415
416        return $sectionAction->isSandboxAction();
417    }
418
419    /**
420     * Given an existing Translation model and a new target revision id, this method updates the target
421     * revision id for this model and saves the corresponding row inside "cx_translations" table.
422     *
423     * @param Translation $translation
424     * @param User $user
425     * @param int $newRevId
426     * @param string $targetTitle
427     * @param string $targetLanguage
428     * @return void
429     */
430    private function updateTranslation(
431        Translation $translation,
432        User $user,
433        int $newRevId,
434        string $targetTitle,
435        string $targetLanguage
436    ): void {
437        $translation->translation['status'] = TranslationStore::TRANSLATION_STATUS_PUBLISHED;
438        $translation->translation['targetURL'] = $this->targetUrlCreator->createTargetUrl(
439            $targetTitle,
440            $targetLanguage
441        );
442        $translation->translation['targetRevisionId'] = $newRevId;
443        $this->translationStore->saveTranslation( $translation, $user );
444    }
445
446    /** @inheritDoc */
447    public function getAllowedParams() {
448        return [
449            'title' => [
450                ParamValidator::PARAM_REQUIRED => true,
451            ],
452            'html' => [
453                ParamValidator::PARAM_TYPE => 'string',
454                ParamValidator::PARAM_REQUIRED => true,
455            ],
456            'sourcelanguage' => [
457                ParamValidator::PARAM_REQUIRED => true,
458            ],
459            'targetlanguage' => [
460                ParamValidator::PARAM_REQUIRED => true,
461            ],
462            'sourcetitle' => [
463                ParamValidator::PARAM_REQUIRED => true,
464            ],
465            'sourcerevid' => [
466                ParamValidator::PARAM_REQUIRED => true,
467            ],
468            'sourcesectiontitle' => [
469                ParamValidator::PARAM_REQUIRED => true,
470            ],
471            'targetsectiontitle' => [
472                ParamValidator::PARAM_REQUIRED => true,
473            ],
474            'sectiontranslationid' => [
475                ParamValidator::PARAM_TYPE => 'integer',
476                ParamValidator::PARAM_REQUIRED => true,
477            ],
478            'publishtarget' => [
479                ParamValidator::PARAM_TYPE => [ 'NEW_SECTION', 'EXPAND', 'REPLACE', 'SANDBOX' ],
480                ParamValidator::PARAM_REQUIRED => false,
481            ],
482            'existingsectiontitle' => [
483                ParamValidator::PARAM_TYPE => 'string',
484                ParamValidator::PARAM_REQUIRED => false,
485            ],
486            'captchaid' => null,
487            'captchaword' => null,
488        ];
489    }
490
491    /** @inheritDoc */
492    public function needsToken() {
493        return 'csrf';
494    }
495
496    /** @inheritDoc */
497    public function isWriteMode() {
498        return true;
499    }
500
501    /** @inheritDoc */
502    public function isInternal() {
503        return true;
504    }
505}