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