| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Api; |
| 4 |
|
| 5 |
|
| 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 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
trait ApiCrossWiki { |
| 21 |
|
| 22 |
protected ?ForeignNotifications $foreignNotifications = null; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
protected function getForeignQueryParams() { |
| 56 |
return $this->getRequest()->getValues(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
protected function allowCrossWikiNotifications() { |
| 63 |
global $wgEchoCrossWikiNotifications; |
| 64 |
return $wgEchoCrossWikiNotifications; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected function getRequestedWikis(): array { |
| 75 |
$params = $this->extractRequestParams(); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
$wikis = $params['wikis'] ?? [ WikiMap::getCurrentWikiId() ]; |
| 80 |
|
| 81 |
if ( in_array( '*', $wikis ) ) { |
| 82 |
|
| 83 |
$wikis = array_merge( |
| 84 |
[ WikiMap::getCurrentWikiId() ], |
| 85 |
$this->getForeignWikisWithUnreadNotifications() |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
if ( !$this->allowCrossWikiNotifications() ) { |
| 90 |
|
| 91 |
$wikis = array_intersect_key( [ WikiMap::getCurrentWikiId() ], $wikis ); |
| 92 |
} |
| 93 |
|
| 94 |
return $wikis; |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 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 |
|
| 111 |
|
| 112 |
protected function getForeignWikisWithUnreadNotifications(): array { |
| 113 |
return $this->getForeignNotifications()->getWikis(); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
public function getCrossWikiParams(): array { |
| 120 |
global $wgConf; |
| 121 |
|
| 122 |
$params = []; |
| 123 |
|
| 124 |
if ( $this->allowCrossWikiNotifications() ) { |
| 125 |
$params += [ |
| 126 |
|
| 127 |
'wikis' => [ |
| 128 |
ParamValidator::PARAM_ISMULTI => true, |
| 129 |
ParamValidator::PARAM_DEFAULT => WikiMap::getCurrentWikiId(), |
| 130 |
|
| 131 |
|
| 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 |
} |