| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Api; |
| 4 |
|
| 5 |
use MediaWiki\Api\ApiQuery; |
| 6 |
use MediaWiki\Api\ApiQueryBase; |
| 7 |
use MediaWiki\Api\ApiUsageException; |
| 8 |
use MediaWiki\Extension\Notifications\DbFactory; |
| 9 |
use MediaWiki\Extension\Notifications\NotifUser; |
| 10 |
use MediaWiki\Extension\Notifications\Services; |
| 11 |
use MediaWiki\Logger\LoggerFactory; |
| 12 |
use MediaWiki\Page\PageRecord; |
| 13 |
use MediaWiki\Page\PageStore; |
| 14 |
use MediaWiki\Title\Title; |
| 15 |
use MediaWiki\Title\TitleFactory; |
| 16 |
use MediaWiki\WikiMap\WikiMap; |
| 17 |
use Wikimedia\ParamValidator\ParamValidator; |
| 18 |
use Wikimedia\ParamValidator\TypeDef\IntegerDef; |
| 19 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 20 |
|
| 21 |
class ApiEchoUnreadNotificationPages extends ApiQueryBase { |
| 22 |
use ApiCrossWiki; |
| 23 |
use ApiEchoPermissionsTrait; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
protected $crossWikiSummary = false; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private $pageStore; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
private $titleFactory; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public function __construct( $query, $moduleName, PageStore $pageStore, TitleFactory $titleFactory ) { |
| 47 |
parent::__construct( $query, $moduleName, 'unp' ); |
| 48 |
$this->pageStore = $pageStore; |
| 49 |
$this->titleFactory = $titleFactory; |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public function execute() { |
| 56 |
$this->checkReadNotificationsPermissions(); |
| 57 |
|
| 58 |
|
| 59 |
$this->getMain()->getVal( '_' ); |
| 60 |
|
| 61 |
$params = $this->extractRequestParams(); |
| 62 |
|
| 63 |
$result = []; |
| 64 |
if ( in_array( WikiMap::getCurrentWikiId(), $this->getRequestedWikis() ) ) { |
| 65 |
$result[WikiMap::getCurrentWikiId()] = $this->getFromLocal( $params['limit'], $params['grouppages'] ); |
| 66 |
} |
| 67 |
|
| 68 |
if ( $this->getRequestedForeignWikis() ) { |
| 69 |
$result += $this->getUnreadNotificationPagesFromForeign(); |
| 70 |
} |
| 71 |
|
| 72 |
$apis = $this->getForeignNotifications()->getApiEndpoints( $this->getRequestedWikis() ); |
| 73 |
foreach ( $result as $wiki => $data ) { |
| 74 |
$result[$wiki]['source'] = $apis[$wiki]; |
| 75 |
$result[$wiki]['pages'] = $data['pages'] ?: []; |
| 76 |
} |
| 77 |
|
| 78 |
$this->getResult()->addValue( 'query', $this->getModuleName(), $result ); |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
protected function getFromLocal( $limit, $groupPages ) { |
| 88 |
$attributeManager = Services::getInstance()->getAttributeManager(); |
| 89 |
$enabledTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' ); |
| 90 |
|
| 91 |
$dbr = DbFactory::newFromDefault()->getEchoDb( DB_REPLICA ); |
| 92 |
$queryBuilder = $dbr->newSelectQueryBuilder() |
| 93 |
->select( [ 'event_page_id', 'count' => 'COUNT(*)' ] ) |
| 94 |
->from( 'echo_event' ) |
| 95 |
->join( 'echo_notification', null, 'notification_event = event_id' ) |
| 96 |
->where( [ |
| 97 |
'notification_user' => $this->getUser()->getId(), |
| 98 |
'notification_read_timestamp' => null, |
| 99 |
'event_deleted' => 0, |
| 100 |
'event_type' => $enabledTypes, |
| 101 |
] ) |
| 102 |
->groupBy( 'event_page_id' ) |
| 103 |
->caller( __METHOD__ ); |
| 104 |
|
| 105 |
|
| 106 |
if ( !$groupPages ) { |
| 107 |
$queryBuilder |
| 108 |
->orderBy( 'count', SelectQueryBuilder::SORT_DESC ) |
| 109 |
->limit( $limit ); |
| 110 |
} |
| 111 |
$rows = $queryBuilder->fetchResultSet(); |
| 112 |
|
| 113 |
$nullCount = 0; |
| 114 |
$pageCounts = []; |
| 115 |
foreach ( $rows as $row ) { |
| 116 |
if ( $row->event_page_id !== null ) { |
| 117 |
$pageCounts[(int)$row->event_page_id] = intval( $row->count ); |
| 118 |
} else { |
| 119 |
$nullCount = intval( $row->count ); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
$titles = $this->pageStore |
| 124 |
->newSelectQueryBuilder() |
| 125 |
->wherePageIds( array_keys( $pageCounts ) ) |
| 126 |
->caller( __METHOD__ ) |
| 127 |
->fetchPageRecords(); |
| 128 |
|
| 129 |
$groupCounts = []; |
| 130 |
|
| 131 |
foreach ( $titles as $title ) { |
| 132 |
$title = $this->titleFactory->castFromPageIdentity( $title ); |
| 133 |
if ( $groupPages ) { |
| 134 |
|
| 135 |
$pageName = $title->getSubjectPage()->getPrefixedText(); |
| 136 |
} else { |
| 137 |
$pageName = $title->getPrefixedText(); |
| 138 |
} |
| 139 |
|
| 140 |
$count = $pageCounts[$title->getArticleID()] ?? 0; |
| 141 |
if ( isset( $groupCounts[$pageName] ) ) { |
| 142 |
$groupCounts[$pageName] += $count; |
| 143 |
} else { |
| 144 |
$groupCounts[$pageName] = $count; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$userPageName = $this->getUser()->getUserPage()->getPrefixedText(); |
| 149 |
if ( $nullCount > 0 && $groupPages ) { |
| 150 |
|
| 151 |
if ( isset( $groupCounts[$userPageName] ) ) { |
| 152 |
$groupCounts[$userPageName] += $nullCount; |
| 153 |
} else { |
| 154 |
$groupCounts[$userPageName] = $nullCount; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
arsort( $groupCounts ); |
| 159 |
if ( $groupPages ) { |
| 160 |
$groupCounts = array_slice( $groupCounts, 0, $limit ); |
| 161 |
} |
| 162 |
|
| 163 |
$result = []; |
| 164 |
foreach ( $groupCounts as $pageName => $count ) { |
| 165 |
if ( $groupPages ) { |
| 166 |
$title = Title::newFromText( $pageName ); |
| 167 |
$pages = [ $title->getSubjectPage()->getPrefixedText() ]; |
| 168 |
if ( $title->canHaveTalkPage() ) { |
| 169 |
$pages[] = $title->getTalkPage()->getPrefixedText(); |
| 170 |
} |
| 171 |
if ( $pageName === $userPageName ) { |
| 172 |
$pages[] = null; |
| 173 |
} |
| 174 |
$pageDescription = [ |
| 175 |
'ns' => $title->getNamespace(), |
| 176 |
'title' => $title->getPrefixedText(), |
| 177 |
'unprefixed' => $title->getText(), |
| 178 |
'pages' => $pages, |
| 179 |
]; |
| 180 |
} else { |
| 181 |
$pageDescription = [ 'title' => $pageName ]; |
| 182 |
} |
| 183 |
$result[] = $pageDescription + [ |
| 184 |
'count' => $count, |
| 185 |
]; |
| 186 |
} |
| 187 |
if ( !$groupPages && $nullCount > 0 ) { |
| 188 |
$result[] = [ |
| 189 |
'title' => null, |
| 190 |
'count' => $nullCount, |
| 191 |
]; |
| 192 |
} |
| 193 |
|
| 194 |
return [ |
| 195 |
'pages' => $result, |
| 196 |
'totalCount' => NotifUser::newFromUser( $this->getUser() )->getLocalNotificationCount(), |
| 197 |
]; |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
protected function getUnreadNotificationPagesFromForeign() { |
| 204 |
$result = []; |
| 205 |
foreach ( $this->getFromForeign() as $wiki => $data ) { |
| 206 |
if ( isset( $data['query'][$this->getModuleName()][$wiki] ) ) { |
| 207 |
$result[$wiki] = $data['query'][$this->getModuleName()][$wiki]; |
| 208 |
} else { |
| 209 |
|
| 210 |
|
| 211 |
LoggerFactory::getInstance( 'Echo' )->warning( |
| 212 |
__METHOD__ . ': Unexpected API response from {wiki}', |
| 213 |
[ |
| 214 |
'wiki' => $wiki, |
| 215 |
'data' => $data, |
| 216 |
] |
| 217 |
); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
return $result; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function getAllowedParams() { |
| 228 |
$maxUpdateCount = $this->getConfig()->get( 'EchoMaxUpdateCount' ); |
| 229 |
|
| 230 |
return $this->getCrossWikiParams() + [ |
| 231 |
'grouppages' => [ |
| 232 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 233 |
ParamValidator::PARAM_DEFAULT => false, |
| 234 |
], |
| 235 |
'limit' => [ |
| 236 |
ParamValidator::PARAM_TYPE => 'limit', |
| 237 |
ParamValidator::PARAM_DEFAULT => 10, |
| 238 |
IntegerDef::PARAM_MIN => 1, |
| 239 |
IntegerDef::PARAM_MAX => $maxUpdateCount, |
| 240 |
IntegerDef::PARAM_MAX2 => $maxUpdateCount, |
| 241 |
], |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
]; |
| 248 |
} |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
protected function getExamplesMessages() { |
| 255 |
return [ |
| 256 |
'action=query&meta=unreadnotificationpages' => 'apihelp-query+unreadnotificationpages-example-1', |
| 257 |
]; |
| 258 |
} |
| 259 |
|
| 260 |
public function getHelpUrls() { |
| 261 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Echo_(Notifications)/API'; |
| 262 |
} |
| 263 |
} |