Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractRevisionHandler
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
7
100.00% covered (success)
100.00%
1 / 1
 getInfo
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
7
 getRevision
n/a
0 / 0
n/a
0 / 0
0
1<?php
2
3namespace MediaWiki\IPInfo\Rest\Handler;
4
5use MediaWiki\Rest\LocalizedHttpException;
6use MediaWiki\Revision\RevisionRecord;
7use Wikimedia\IPUtils;
8use Wikimedia\Message\MessageValue;
9
10abstract class AbstractRevisionHandler extends IPInfoHandler {
11
12    /** @inheritDoc */
13    protected function getInfo( int $id ): array {
14        $revision = $this->getRevision( $id );
15
16        if ( !$revision ) {
17            throw new LocalizedHttpException(
18                new MessageValue( 'rest-nonexistent-revision', [ $id ] ),
19                404
20            );
21        }
22
23        $user = $this->userFactory->newFromUserIdentity( $this->getAuthority()->getUser() );
24        if ( !$this->permissionManager->userCan( 'read', $user, $revision->getPageAsLinkTarget() ) ) {
25            throw new LocalizedHttpException(
26                new MessageValue( 'rest-revision-permission-denied-revision', [ $id ] ),
27                403
28            );
29        }
30
31        $author = $revision->getUser( RevisionRecord::FOR_THIS_USER, $this->getAuthority() );
32
33        if ( !$author ) {
34            // User does not have access to author.
35            throw new LocalizedHttpException(
36                new MessageValue( 'ipinfo-rest-revision-no-author' ),
37                403
38            );
39        }
40
41        if ( $this->userIdentityUtils->isNamed( $author ) ) {
42            // By design, IPInfo currently only supports retrieving IP data for anonymous or temporary users.
43            throw new LocalizedHttpException(
44                new MessageValue( 'ipinfo-rest-revision-registered' ),
45                404
46            );
47        }
48
49        if ( !( $this->userIdentityUtils->isTemp( $author ) || IPUtils::isValid( $author->getName() ) ) ) {
50            // Not a valid IP or temporary account; may be an imported edit
51            throw new LocalizedHttpException(
52                new MessageValue( 'ipinfo-rest-revision-invalid-ip' ),
53                404
54            );
55        }
56
57        $address = $this->tempUserIPLookup->getAddressForRevision( $revision );
58
59        return [
60            $this->presenter->present(
61                $this->infoManager->retrieveFor( $author, $address ),
62                $this->getAuthority()->getUser()
63            )
64        ];
65    }
66
67    /**
68     * @param int $id ID of the revision
69     * @return RevisionRecord|null if the revision does not exist
70     */
71    abstract protected function getRevision( int $id ): ?RevisionRecord;
72}