Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageAuthorLookup
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 2
42
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
 getAuthor
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3declare( strict_types=1 );
4
5namespace MediaWiki\Extension\CampaignEvents\MWEntity;
6
7use MediaWiki\Revision\RevisionRecord;
8use MediaWiki\Revision\RevisionStoreFactory;
9use UnexpectedValueException;
10
11/**
12 * This service can be used to find the global user who created a given page.
13 */
14class PageAuthorLookup {
15    public const SERVICE_NAME = 'CampaignEventsPageAuthorLookup';
16
17    private RevisionStoreFactory $revisionStoreFactory;
18    private CampaignsCentralUserLookup $centralUserLookup;
19
20    /**
21     * @param RevisionStoreFactory $revisionStoreFactory
22     * @param CampaignsCentralUserLookup $centralUserLookup
23     */
24    public function __construct(
25        RevisionStoreFactory $revisionStoreFactory,
26        CampaignsCentralUserLookup $centralUserLookup
27    ) {
28        $this->revisionStoreFactory = $revisionStoreFactory;
29        $this->centralUserLookup = $centralUserLookup;
30    }
31
32    /**
33     * @param ICampaignsPage $page
34     * @return CentralUser|null Null if the author is not available for some reason, e.g. because
35     * the account does not exist globally.
36     * @warning This method bypasses visibility checks on the author's name.
37     */
38    public function getAuthor( ICampaignsPage $page ): ?CentralUser {
39        if ( !$page instanceof MWPageProxy ) {
40            throw new UnexpectedValueException( 'Unknown campaigns page implementation: ' . get_class( $page ) );
41        }
42        $revStore = $this->revisionStoreFactory->getRevisionStore( $page->getWikiId() );
43        $firstRev = $revStore->getFirstRevision( $page->getPageIdentity() );
44        if ( !$firstRev ) {
45            return null;
46        }
47        $userIdentity = $firstRev->getUser( RevisionRecord::RAW );
48        if ( !$userIdentity ) {
49            return null;
50        }
51        try {
52            return $this->centralUserLookup->newFromUserIdentity( $userIdentity );
53        } catch ( UserNotGlobalException $_ ) {
54            return null;
55        }
56    }
57}