Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
AbstractRevisionHandler | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
6 | |
100.00% |
1 / 1 |
getInfo | |
100.00% |
32 / 32 |
|
100.00% |
1 / 1 |
6 | |||
getRevision | n/a |
0 / 0 |
n/a |
0 / 0 |
0 |
1 | <?php |
2 | |
3 | namespace MediaWiki\IPInfo\Rest\Handler; |
4 | |
5 | use MediaWiki\Rest\LocalizedHttpException; |
6 | use MediaWiki\Revision\RevisionRecord; |
7 | use Wikimedia\IPUtils; |
8 | use Wikimedia\Message\MessageValue; |
9 | |
10 | abstract 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 ( $author->isRegistered() ) { |
42 | // Since the IP address only exists in CheckUser, there is no way to access it. |
43 | // @TODO Allow extensions (like CheckUser) to either pass without a value |
44 | // (which would result in a 404) or throw a fatal (which could result in a 403). |
45 | throw new LocalizedHttpException( |
46 | new MessageValue( 'ipinfo-rest-revision-registered' ), |
47 | 404 |
48 | ); |
49 | } |
50 | |
51 | if ( !IPUtils::isValid( $author->getName() ) ) { |
52 | // Not a valid IP and probably an imported edit |
53 | throw new LocalizedHttpException( |
54 | new MessageValue( 'ipinfo-rest-revision-invalid-ip' ), |
55 | 404 |
56 | ); |
57 | } |
58 | |
59 | return [ |
60 | $this->presenter->present( $this->infoManager->retrieveFromIP( $author->getName() ), |
61 | $this->getAuthority()->getUser() ) |
62 | ]; |
63 | } |
64 | |
65 | /** |
66 | * @param int $id ID of the revision |
67 | * @return RevisionRecord|null if the revision does not exist |
68 | */ |
69 | abstract protected function getRevision( int $id ): ?RevisionRecord; |
70 | } |