Debian Codegraph @

extensions/Echo/includes/Api/ApiCrossWiki.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Extension\Notifications\Api;
4
5 // @phan-file-suppress PhanUndeclaredMethod This is a trait, and phan is confused by $this
6 use Exception;
7 use MediaWiki\Extension\Notifications\ForeignNotifications;
8 use MediaWiki\Extension\Notifications\ForeignWikiRequest;
9 use MediaWiki\WikiMap\WikiMap;
10 use Wikimedia\ParamValidator\ParamValidator;
11
12 /**
13 * Trait that adds cross-wiki functionality to an API module. For mixing into ApiBase subclasses.
14 *
15 * In addition to mixing in this trait, you have to do the following in your API module:
16 * - In your getAllowedParams() method, merge in the return value of getCrossWikiParams()
17 * - In your execute() method, call getFromForeign() somewhere and do something with the result
18 * - Optionally, override getForeignQueryParams() to customize what is sent to the foreign wikis
19 */
20 trait ApiCrossWiki {
21
22 protected ?ForeignNotifications $foreignNotifications = null;
23
24 /**
25 * This will take the current API call (with all of its params) and execute
26 * it on all foreign wikis, returning an array of results per wiki.
27 *
28 * @param array|null $wikis List of wikis to query. Defaults to the result of getRequestedForeignWikis().
29 * @param array $paramOverrides Request parameter overrides
30 * @return array[]
31 * @throws Exception
32 */
33 protected function getFromForeign( ?array $wikis = null, array $paramOverrides = [] ) {
34 $wikis ??= $this->getRequestedForeignWikis();
35 if ( $wikis === [] ) {
36 return [];
37 }
38 $tokenType = $this->needsToken();
39 $foreignReq = new ForeignWikiRequest(
40 $this->getUser(),
41 $paramOverrides + $this->getForeignQueryParams(),
42 $wikis,
43 $this->getModulePrefix() . 'wikis',
44 $tokenType !== false ? $tokenType : null
45 );
46 return $foreignReq->execute( $this->getRequest() );
47 }
48
49 /**
50 * Get the query parameters to use for the foreign API requests.
51 * Implementing classes should override this if they need to customize
52 * the parameters.
53 * @return array Query parameters
54 */
55 protected function getForeignQueryParams() {
56 return $this->getRequest()->getValues();
57 }
58
59 /**
60 * @return bool
61 */
62 protected function allowCrossWikiNotifications() {
63 global $wgEchoCrossWikiNotifications;
64 return $wgEchoCrossWikiNotifications;
65 }
66
67 /**
68 * This is basically equivalent to $params['wikis'], but some added checks:
69 * - `*` will expand to "all wikis with unread notifications"
70 * - if `$wgEchoCrossWikiNotifications` is off, foreign wikis will be excluded
71 *
72 * @return string[]
73 */
74 protected function getRequestedWikis(): array {
75 $params = $this->extractRequestParams();
76
77 // if wiki is omitted from params, that's because crosswiki is/was not
78 // available, and it'll default to current wiki
79 $wikis = $params['wikis'] ?? [ WikiMap::getCurrentWikiId() ];
80
81 if ( in_array( '*', $wikis ) ) {
82 // expand `*` to all foreign wikis with unread notifications + local
83 $wikis = array_merge(
84 [ WikiMap::getCurrentWikiId() ],
85 $this->getForeignWikisWithUnreadNotifications()
86 );
87 }
88
89 if ( !$this->allowCrossWikiNotifications() ) {
90 // exclude foreign wikis if x-wiki is not enabled
91 $wikis = array_intersect_key( [ WikiMap::getCurrentWikiId() ], $wikis );
92 }
93
94 return $wikis;
95 }
96
97 /**
98 * @return string[] Wiki names
99 */
100 protected function getRequestedForeignWikis(): array {
101 return array_diff( $this->getRequestedWikis(), [ WikiMap::getCurrentWikiId() ] );
102 }
103
104 protected function getForeignNotifications(): ForeignNotifications {
105 $this->foreignNotifications ??= new ForeignNotifications( $this->getUser() );
106 return $this->foreignNotifications;
107 }
108
109 /**
110 * @return string[] Wiki names
111 */
112 protected function getForeignWikisWithUnreadNotifications(): array {
113 return $this->getForeignNotifications()->getWikis();
114 }
115
116 /**
117 * @return array[]
118 */
119 public function getCrossWikiParams(): array {
120 global $wgConf;
121
122 $params = [];
123
124 if ( $this->allowCrossWikiNotifications() ) {
125 $params += [
126 // fetch notifications from multiple wikis
127 'wikis' => [
128 ParamValidator::PARAM_ISMULTI => true,
129 ParamValidator::PARAM_DEFAULT => WikiMap::getCurrentWikiId(),
130 // `*` will let you immediately fetch from all wikis that have
131 // unread notifications, without having to look them up first
132 ParamValidator::PARAM_TYPE => array_unique(
133 array_merge(
134 $wgConf->wikis,
135 [ WikiMap::getCurrentWikiId(), '*' ]
136 )
137 ),
138 ],
139 ];
140 }
141
142 return $params;
143 }
144 }