Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SectionTranslationBeforePublishHandler
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 4
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 onSectionTranslationBeforePublish
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
 doAPIRequest
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 getPageContent
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2namespace ContentTranslation;
3
4use FormatJson;
5use MediaWiki\CommentStore\CommentStoreComment;
6use MediaWiki\Config\Config;
7use MediaWiki\Content\ContentHandlerFactory;
8use MediaWiki\Http\HttpRequestFactory;
9use MediaWiki\Page\WikiPageFactory;
10use MediaWiki\Revision\SlotRecord;
11use MediaWiki\Title\Title;
12use MediaWiki\User\User;
13
14class SectionTranslationBeforePublishHandler implements SectionTranslationBeforePublishHook {
15
16    /** @var ContentHandlerFactory */
17    private $contentHandlerFactory;
18
19    /** @var HttpRequestFactory */
20    private $httpRequestFactory;
21
22    /** @var WikiPageFactory */
23    private $wikiPageFactory;
24
25    /** @var Config */
26    private $config;
27
28    /**
29     * @param ContentHandlerFactory $contentHandlerFactory
30     * @param HttpRequestFactory $httpRequestFactory
31     * @param WikiPageFactory $wikiPageFactory
32     * @param Config $config
33     */
34    public function __construct(
35        ContentHandlerFactory $contentHandlerFactory,
36        HttpRequestFactory $httpRequestFactory,
37        WikiPageFactory $wikiPageFactory,
38        Config $config
39    ) {
40        $this->contentHandlerFactory = $contentHandlerFactory;
41        $this->httpRequestFactory = $httpRequestFactory;
42        $this->wikiPageFactory = $wikiPageFactory;
43        $this->config = $config;
44    }
45
46    /** @inheritDoc */
47    public function onSectionTranslationBeforePublish( Title $title, string $language, User $user ): void {
48        if ( !$this->config->get( 'ContentTranslationContentImportForSectionTranslation' ) ) {
49            return;
50        }
51
52        $pageContent = $this->getPageContent( $title->getPrefixedDBkey(), $language );
53        if ( $pageContent === null ) {
54            return;
55        }
56        $contentHandler = $this->contentHandlerFactory->getContentHandler( CONTENT_MODEL_WIKITEXT );
57        $content = $contentHandler->unserializeContent( $pageContent );
58        $editMessage = 'Auto-created by SectionTranslation:onSectionTranslationBeforePublish';
59
60        $page = $this->wikiPageFactory->newFromTitle( $title->toPageIdentity() );
61        $updater = $page->newPageUpdater( $user );
62        $updater->setContent( SlotRecord::MAIN, $content );
63        $updater->saveRevision( CommentStoreComment::newUnsavedComment( $editMessage ) );
64    }
65
66    protected function doAPIRequest( string $url ): ?string {
67        $response = $this->httpRequestFactory->create( $url, [], __METHOD__ );
68        $status = $response->execute();
69        if ( !$status->isOK() ) {
70            return null;
71        }
72        return $response->getContent();
73    }
74
75    public function getPageContent( string $title, string $language ): ?string {
76        $baseUrl = SiteMapper::getApiURL( $language );
77        $query = [
78            'action' => 'query',
79            'format' => 'json',
80            'formatversion' => 2,
81            'prop' => 'revisions',
82            'rvprop' => 'content',
83            'titles' => $title
84        ];
85        $url = wfAppendQuery( $baseUrl, $query );
86        $resp = $this->doAPIRequest( $url );
87
88        if ( $resp === null ) {
89            return null;
90        }
91
92        $json = FormatJson::decode( $resp, true );
93        $page = null;
94        if ( isset( $json['query']['pages'] ) ) {
95            $pages = array_values( $json['query']['pages'] );
96            if ( count( $pages ) === 1 ) {
97                $page = $pages[0];
98            }
99        }
100
101        if ( isset( $page['missing'] ) ) {
102            // Page does not exist. Nothing to do.
103            return null;
104        }
105
106        return $page['revisions'][0]['content'] ?? null;
107    }
108}