Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
DefaultPageProperties
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
5 / 5
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 initialize
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 finishInitializeBatch
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 finalize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 loadCreateTimestamp
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace CirrusSearch\BuildDocument;
4
5use CirrusSearch\Util;
6use Elastica\Document;
7use MediaWiki\Revision\RevisionRecord;
8use MediaWiki\Title\Title;
9use MediaWiki\Utils\MWTimestamp;
10use MediaWiki\WikiMap\WikiMap;
11use Wikimedia\Rdbms\IReadableDatabase;
12use WikiPage;
13
14/**
15 * Default properties attached to all page documents.
16 */
17class DefaultPageProperties implements PagePropertyBuilder {
18    /** @var IReadableDatabase Wiki database to query additional page properties from. */
19    private $db;
20
21    /**
22     * @param IReadableDatabase $db Wiki database to query additional page properties from.
23     */
24    public function __construct( IReadableDatabase $db ) {
25        $this->db = $db;
26    }
27
28    /**
29     * {@inheritDoc}
30     *
31     * @param Document $doc The document to be populated
32     * @param WikiPage $page The page to scope operation to
33     */
34    public function initialize( Document $doc, WikiPage $page, RevisionRecord $revision ): void {
35        $title = $page->getTitle();
36        $doc->set( 'wiki', WikiMap::getCurrentWikiId() );
37        $doc->set( 'page_id', $page->getId() );
38        $doc->set( 'namespace',
39            $title->getNamespace() );
40        $doc->set( 'namespace_text',
41            Util::getNamespaceText( $title ) );
42        $doc->set( 'title', $title->getText() );
43        $doc->set( 'timestamp',
44            wfTimestamp( TS_ISO_8601, $revision->getTimestamp() ) );
45        $createTs = $this->loadCreateTimestamp(
46            $page->getId(), TS_ISO_8601 );
47        if ( $createTs !== false ) {
48            $doc->set( 'create_timestamp', $createTs );
49        }
50    }
51
52    /**
53     * {@inheritDoc}
54     */
55    public function finishInitializeBatch(): void {
56        // NOOP
57    }
58
59    /**
60     * {@inheritDoc}
61     */
62    public function finalize( Document $doc, Title $title, RevisionRecord $revision ): void {
63        // NOOP
64    }
65
66    /**
67     * Timestamp the oldest revision of this page was created.
68     * @param int $pageId
69     * @param int $style TS_* output format constant
70     * @return string|bool Formatted timestamp or false on failure
71     */
72    private function loadCreateTimestamp( int $pageId, int $style ) {
73        $row = $this->db->newSelectQueryBuilder()
74            ->select( 'rev_timestamp' )
75            ->from( 'revision' )
76            ->where( [ 'rev_page' => $pageId ] )
77            ->orderBy( 'rev_timestamp' )
78            ->caller( __METHOD__ )
79            ->fetchRow();
80        if ( !$row ) {
81            return false;
82        }
83        return MWTimestamp::convert( $style, $row->rev_timestamp );
84    }
85}