Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
67.65% covered (warning)
67.65%
23 / 34
20.00% covered (danger)
20.00%
1 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageRerenderSerializer
67.65% covered (warning)
67.65%
23 / 34
20.00% covered (danger)
20.00%
1 / 5
5.85
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 eventDataForPage
100.00% covered (success)
100.00%
23 / 23
100.00% covered (success)
100.00%
1 / 1
1
 getCirrusConnection
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDomain
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCanonicalPageURL
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace CirrusSearch\Event;
4
5use CirrusSearch\Connection;
6use CirrusSearch\SearchConfig;
7use MediaWiki\Config\Config;
8use MediaWiki\Extension\EventBus\StreamNameMapper;
9use MediaWiki\MainConfigNames;
10use MediaWiki\Page\PageRecord;
11use MediaWiki\Page\PageReference;
12use MediaWiki\Title\TitleFormatter;
13use Wikimedia\Assert\Assert;
14use Wikimedia\UUID\GlobalIdGenerator;
15
16class PageRerenderSerializer {
17    public const STREAM = 'mediawiki.cirrussearch.page_rerender.v1';
18    public const SCHEMA = '/mediawiki/cirrussearch/page_rerender/1.0.0';
19    public const LINKS_UPDATE_REASON = 'links_update';
20    private TitleFormatter $titleFormatter;
21    private SearchConfig $searchConfig;
22    private GlobalIdGenerator $globalIdGenerator;
23    private Config $mainConfig;
24    private Connection $cirrusConnection;
25    private StreamNameMapper $streamNameMapper;
26
27    /**
28     * @param Config $mainConfig
29     * @param TitleFormatter $titleFormatter
30     * @param SearchConfig $searchConfig
31     * @param GlobalIdGenerator $globalIdGenerator
32     * @param StreamNameMapper $streamNameMapper
33     */
34    public function __construct(
35        Config $mainConfig,
36        TitleFormatter $titleFormatter,
37        SearchConfig $searchConfig,
38        GlobalIdGenerator $globalIdGenerator,
39        StreamNameMapper $streamNameMapper
40    ) {
41        $this->titleFormatter = $titleFormatter;
42        $this->searchConfig = $searchConfig;
43        $this->globalIdGenerator = $globalIdGenerator;
44        $this->mainConfig = $mainConfig;
45        $this->streamNameMapper = $streamNameMapper;
46    }
47
48    public function eventDataForPage( PageRecord $page, string $reason, string $requestId, ?string $dt = null ): array {
49        Assert::parameter( $page->isRedirect() === false, '$page', 'Redirects are not supported' );
50        $connection = $this->getCirrusConnection();
51        $attrs = [
52            '$schema' => self::SCHEMA,
53            'meta' => [
54                'stream' => $this->streamNameMapper->resolve( self::STREAM ),
55                'uid' => $this->globalIdGenerator->newUUIDv4(),
56                'request_id' => $requestId,
57                'domain' => $this->getDomain(),
58                'uri' => $this->getCanonicalPageURL( $page )
59            ],
60            'reason' => $reason,
61            'dt' => $dt ?? wfTimestamp( TS_ISO_8601 ),
62            'wiki_id' => $this->searchConfig->getWikiId(),
63            'cirrussearch_index_name' => $connection->getIndexName( $this->searchConfig->get( SearchConfig::INDEX_BASE_NAME ),
64                        $connection->getIndexSuffixForNamespace( $page->getNamespace() ) ),
65            'cirrussearch_cluster_group' => $this->searchConfig->getClusterAssignment()->getCrossClusterName() ?? "default",
66            'page_id' => $page->getId(),
67            'page_title' => $this->titleFormatter->getPrefixedDBkey( $page ),
68            'namespace_id' => $page->getNamespace(),
69            'is_redirect' => $page->isRedirect()
70        ];
71        return $attrs;
72    }
73
74    private function getCirrusConnection(): Connection {
75        $this->cirrusConnection ??= new Connection( $this->searchConfig );
76        return $this->cirrusConnection;
77    }
78
79    private function getDomain(): string {
80        return $this->mainConfig->get( MainConfigNames::ServerName );
81    }
82
83    private function getCanonicalPageURL( PageReference $wikiPage ): string {
84        $titleURL = wfUrlencode( $this->titleFormatter->getPrefixedDBkey( $wikiPage ) );
85        // The ArticlePath contains '$1' string where the article title should appear.
86        return $this->mainConfig->get( MainConfigNames::CanonicalServer ) .
87               str_replace( '$1', $titleURL, $this->mainConfig->get( MainConfigNames::ArticlePath ) );
88    }
89}