Debian Codegraph @

extensions/VisualEditor/editcheck/includes/ApiEditCheckReferenceUrl.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\VisualEditor\EditCheck;
4
5 use MediaWiki\Api\ApiBase;
6 use MediaWiki\Api\ApiMain;
7 use MediaWiki\Api\ApiUsageException;
8 use MediaWiki\Extension\AbuseFilter\BlockedDomainStorage;
9 use MediaWiki\Extension\SpamBlacklist\BaseBlacklist;
10 use MediaWiki\Registration\ExtensionRegistry;
11 use Wikimedia\ParamValidator\ParamValidator;
12
13 class ApiEditCheckReferenceUrl extends ApiBase {
14
15 /** @phan-suppress-next-line PhanUndeclaredTypeProperty */
16 private ?BlockedDomainStorage $blockedDomainStorage;
17
18 public function __construct(
19 ApiMain $main,
20 string $name,
21 // @phan-suppress-next-line PhanUndeclaredTypeParameter
22 ?BlockedDomainStorage $blockedDomainStorage
23 ) {
24 parent::__construct( $main, $name );
25 $this->blockedDomainStorage = $blockedDomainStorage;
26 }
27
28 /**
29 * @inheritDoc
30 * @throws ApiUsageException
31 */
32 public function execute() {
33 $params = $this->extractRequestParams();
34
35 $url = $params['url'];
36
37 if ( $this->isInBlockedExternalDomains( $url ) || $this->isInSpamBlackList( $url ) ) {
38 $result = 'blocked';
39 } else {
40 $result = 'allowed';
41 }
42
43 $this->getResult()->addValue( null, $this->getModuleName(), [ $url => $result ] );
44 }
45
46 private function isInBlockedExternalDomains( string $url ): bool {
47 if ( !$this->blockedDomainStorage ) {
48 return false;
49 }
50
51 // @phan-suppress-next-line PhanUndeclaredClassMethod
52 $domain = $this->blockedDomainStorage->validateDomain( $url );
53 // @phan-suppress-next-line PhanUndeclaredClassMethod
54 $blockedDomains = $this->blockedDomainStorage->loadComputed();
55 return !empty( $blockedDomains[ $domain ] );
56 }
57
58 private function isInSpamBlackList( string $url ): bool {
59 if ( !ExtensionRegistry::getInstance()->isLoaded( 'SpamBlacklist' ) ) {
60 return false;
61 }
62 if ( !str_contains( $url, '//' ) ) {
63 // SpamBlackist only detects full URLs
64 $url = 'https://' . $url;
65 }
66
67 // @phan-suppress-next-line PhanUndeclaredClassMethod
68 $matches = BaseBlacklist::getSpamBlacklist()->filter(
69 [ $url ],
70 null,
71 $this->getUser(),
72 true
73 );
74
75 return $matches !== false;
76 }
77
78 /**
79 * Check if the required extensions are available for this API to be usable
80 *
81 * @return bool
82 */
83 public static function isAvailable(): bool {
84 return ExtensionRegistry::getInstance()->isLoaded( 'SpamBlacklist' ) ||
85 // BlockedExternalDomains is within AbuseFilter:
86 ExtensionRegistry::getInstance()->isLoaded( 'Abuse Filter' );
87 }
88
89 /**
90 * @inheritDoc
91 */
92 public function getAllowedParams() {
93 return [
94 'url' => [
95 ParamValidator::PARAM_REQUIRED => true,
96 ParamValidator::PARAM_TYPE => 'string',
97 ],
98 ];
99 }
100
101 /**
102 * @inheritDoc
103 */
104 public function needsToken() {
105 return false;
106 }
107
108 /**
109 * @inheritDoc
110 */
111 public function isInternal() {
112 return true;
113 }
114
115 /**
116 * @inheritDoc
117 */
118 public function isWriteMode() {
119 return false;
120 }
121 }