| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
s |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\Api; |
| 27 |
|
| 28 |
use MediaWiki\Revision\RevisionStore; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use MediaWiki\Title\TitleFactory; |
| 31 |
use MediaWiki\Title\TitleFormatter; |
| 32 |
use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 33 |
use Wikimedia\ParamValidator\ParamValidator; |
| 34 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 35 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class ApiSetNotificationTimestamp extends ApiBase { |
| 42 |
|
| 43 |
|
| 44 |
private $mPageSet = null; |
| 45 |
|
| 46 |
private RevisionStore $revisionStore; |
| 47 |
private IConnectionProvider $dbProvider; |
| 48 |
private WatchedItemStoreInterface $watchedItemStore; |
| 49 |
private TitleFormatter $titleFormatter; |
| 50 |
private TitleFactory $titleFactory; |
| 51 |
|
| 52 |
public function __construct( |
| 53 |
ApiMain $main, |
| 54 |
string $action, |
| 55 |
IConnectionProvider $dbProvider, |
| 56 |
RevisionStore $revisionStore, |
| 57 |
WatchedItemStoreInterface $watchedItemStore, |
| 58 |
TitleFormatter $titleFormatter, |
| 59 |
TitleFactory $titleFactory |
| 60 |
) { |
| 61 |
parent::__construct( $main, $action ); |
| 62 |
|
| 63 |
$this->dbProvider = $dbProvider; |
| 64 |
$this->revisionStore = $revisionStore; |
| 65 |
$this->watchedItemStore = $watchedItemStore; |
| 66 |
$this->titleFormatter = $titleFormatter; |
| 67 |
$this->titleFactory = $titleFactory; |
| 68 |
} |
| 69 |
|
| 70 |
public function execute() { |
| 71 |
$user = $this->getUser(); |
| 72 |
|
| 73 |
if ( !$user->isRegistered() ) { |
| 74 |
$this->dieWithError( 'watchlistanontext', 'notloggedin' ); |
| 75 |
} |
| 76 |
$this->checkUserRightsAny( 'editmywatchlist' ); |
| 77 |
|
| 78 |
$params = $this->extractRequestParams(); |
| 79 |
$this->requireMaxOneParameter( $params, 'timestamp', 'torevid', 'newerthanrevid' ); |
| 80 |
|
| 81 |
$continuationManager = new ApiContinuationManager( $this, [], [] ); |
| 82 |
$this->setContinuationManager( $continuationManager ); |
| 83 |
|
| 84 |
$pageSet = $this->getPageSet(); |
| 85 |
if ( $params['entirewatchlist'] && $pageSet->getDataSource() !== null ) { |
| 86 |
$this->dieWithError( |
| 87 |
[ |
| 88 |
'apierror-invalidparammix-cannotusewith', |
| 89 |
$this->encodeParamName( 'entirewatchlist' ), |
| 90 |
$pageSet->encodeParamName( $pageSet->getDataSource() ) |
| 91 |
], |
| 92 |
'multisource' |
| 93 |
); |
| 94 |
} |
| 95 |
|
| 96 |
$dbw = $this->dbProvider->getPrimaryDatabase(); |
| 97 |
|
| 98 |
$timestamp = null; |
| 99 |
if ( isset( $params['timestamp'] ) ) { |
| 100 |
$timestamp = $dbw->timestamp( $params['timestamp'] ); |
| 101 |
} |
| 102 |
|
| 103 |
if ( !$params['entirewatchlist'] ) { |
| 104 |
$pageSet->execute(); |
| 105 |
} |
| 106 |
|
| 107 |
if ( isset( $params['torevid'] ) ) { |
| 108 |
if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) { |
| 109 |
$this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'torevid' ) ] ); |
| 110 |
} |
| 111 |
$titles = $pageSet->getGoodPages(); |
| 112 |
$title = reset( $titles ); |
| 113 |
if ( $title ) { |
| 114 |
|
| 115 |
$timestamp = $this->revisionStore->getTimestampFromId( |
| 116 |
$params['torevid'], |
| 117 |
IDBAccessObject::READ_LATEST |
| 118 |
); |
| 119 |
if ( $timestamp ) { |
| 120 |
$timestamp = $dbw->timestamp( $timestamp ); |
| 121 |
} else { |
| 122 |
$timestamp = null; |
| 123 |
} |
| 124 |
} |
| 125 |
} elseif ( isset( $params['newerthanrevid'] ) ) { |
| 126 |
if ( $params['entirewatchlist'] || $pageSet->getGoodTitleCount() > 1 ) { |
| 127 |
$this->dieWithError( [ 'apierror-multpages', $this->encodeParamName( 'newerthanrevid' ) ] ); |
| 128 |
} |
| 129 |
$titles = $pageSet->getGoodPages(); |
| 130 |
$title = reset( $titles ); |
| 131 |
if ( $title ) { |
| 132 |
$timestamp = null; |
| 133 |
$currRev = $this->revisionStore->getRevisionById( |
| 134 |
$params['newerthanrevid'], |
| 135 |
IDBAccessObject::READ_LATEST |
| 136 |
); |
| 137 |
if ( $currRev ) { |
| 138 |
$nextRev = $this->revisionStore->getNextRevision( |
| 139 |
$currRev, |
| 140 |
IDBAccessObject::READ_LATEST |
| 141 |
); |
| 142 |
if ( $nextRev ) { |
| 143 |
$timestamp = $dbw->timestamp( $nextRev->getTimestamp() ); |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
$apiResult = $this->getResult(); |
| 150 |
$result = []; |
| 151 |
if ( $params['entirewatchlist'] ) { |
| 152 |
|
| 153 |
$this->watchedItemStore->resetAllNotificationTimestampsForUser( $user, $timestamp ); |
| 154 |
|
| 155 |
$result['notificationtimestamp'] = $timestamp === null |
| 156 |
? '' |
| 157 |
: wfTimestamp( TS_ISO_8601, $timestamp ); |
| 158 |
} else { |
| 159 |
|
| 160 |
foreach ( $pageSet->getInvalidTitlesAndReasons() as $r ) { |
| 161 |
$r['invalid'] = true; |
| 162 |
$result[] = $r; |
| 163 |
} |
| 164 |
foreach ( $pageSet->getMissingPageIDs() as $p ) { |
| 165 |
$page = []; |
| 166 |
$page['pageid'] = $p; |
| 167 |
$page['missing'] = true; |
| 168 |
$page['notwatched'] = true; |
| 169 |
$result[] = $page; |
| 170 |
} |
| 171 |
foreach ( $pageSet->getMissingRevisionIDs() as $r ) { |
| 172 |
$rev = []; |
| 173 |
$rev['revid'] = $r; |
| 174 |
$rev['missing'] = true; |
| 175 |
$rev['notwatched'] = true; |
| 176 |
$result[] = $rev; |
| 177 |
} |
| 178 |
|
| 179 |
$pages = $pageSet->getPages(); |
| 180 |
if ( $pages ) { |
| 181 |
|
| 182 |
$this->watchedItemStore->setNotificationTimestampsForUser( |
| 183 |
$user, |
| 184 |
$timestamp, |
| 185 |
$pages |
| 186 |
); |
| 187 |
|
| 188 |
|
| 189 |
$timestamps = $this->watchedItemStore->getNotificationTimestampsBatch( |
| 190 |
$user, |
| 191 |
$pages |
| 192 |
); |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
foreach ( $pages as $page ) { |
| 197 |
$ns = $page->getNamespace(); |
| 198 |
$dbkey = $page->getDBkey(); |
| 199 |
$r = [ |
| 200 |
'ns' => $ns, |
| 201 |
'title' => $this->titleFormatter->getPrefixedText( $page ), |
| 202 |
]; |
| 203 |
if ( !$page->exists() ) { |
| 204 |
$r['missing'] = true; |
| 205 |
$title = $this->titleFactory->newFromPageIdentity( $page ); |
| 206 |
if ( $title->isKnown() ) { |
| 207 |
$r['known'] = true; |
| 208 |
} |
| 209 |
} |
| 210 |
if ( isset( $timestamps[$ns] ) && array_key_exists( $dbkey, $timestamps[$ns] ) |
| 211 |
&& $timestamps[$ns][$dbkey] !== false |
| 212 |
) { |
| 213 |
$r['notificationtimestamp'] = ''; |
| 214 |
if ( $timestamps[$ns][$dbkey] !== null ) { |
| 215 |
$r['notificationtimestamp'] = wfTimestamp( TS_ISO_8601, $timestamps[$ns][$dbkey] ); |
| 216 |
} |
| 217 |
} else { |
| 218 |
$r['notwatched'] = true; |
| 219 |
} |
| 220 |
$result[] = $r; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
ApiResult::setIndexedTagName( $result, 'page' ); |
| 225 |
} |
| 226 |
$apiResult->addValue( null, $this->getModuleName(), $result ); |
| 227 |
|
| 228 |
$this->setContinuationManager( null ); |
| 229 |
$continuationManager->setContinuationIntoResult( $apiResult ); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
private function getPageSet() { |
| 237 |
$this->mPageSet ??= new ApiPageSet( $this ); |
| 238 |
|
| 239 |
return $this->mPageSet; |
| 240 |
} |
| 241 |
|
| 242 |
public function mustBePosted() { |
| 243 |
return true; |
| 244 |
} |
| 245 |
|
| 246 |
public function isWriteMode() { |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
public function needsToken() { |
| 251 |
return 'csrf'; |
| 252 |
} |
| 253 |
|
| 254 |
public function getAllowedParams( $flags = 0 ) { |
| 255 |
$result = [ |
| 256 |
'entirewatchlist' => [ |
| 257 |
ParamValidator::PARAM_TYPE => 'boolean' |
| 258 |
], |
| 259 |
'timestamp' => [ |
| 260 |
ParamValidator::PARAM_TYPE => 'timestamp' |
| 261 |
], |
| 262 |
'torevid' => [ |
| 263 |
ParamValidator::PARAM_TYPE => 'integer' |
| 264 |
], |
| 265 |
'newerthanrevid' => [ |
| 266 |
ParamValidator::PARAM_TYPE => 'integer' |
| 267 |
], |
| 268 |
'continue' => [ |
| 269 |
ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', |
| 270 |
], |
| 271 |
]; |
| 272 |
if ( $flags ) { |
| 273 |
$result += $this->getPageSet()->getFinalParams( $flags ); |
| 274 |
} |
| 275 |
|
| 276 |
return $result; |
| 277 |
} |
| 278 |
|
| 279 |
protected function getExamplesMessages() { |
| 280 |
$title = Title::newMainPage()->getPrefixedText(); |
| 281 |
$mp = rawurlencode( $title ); |
| 282 |
|
| 283 |
return [ |
| 284 |
'action=setnotificationtimestamp&entirewatchlist=&token=123ABC' |
| 285 |
=> 'apihelp-setnotificationtimestamp-example-all', |
| 286 |
"action=setnotificationtimestamp&titles={$mp}&token=123ABC" |
| 287 |
=> 'apihelp-setnotificationtimestamp-example-page', |
| 288 |
"action=setnotificationtimestamp&titles={$mp}&" . |
| 289 |
'timestamp=2012-01-01T00:00:00Z&token=123ABC' |
| 290 |
=> 'apihelp-setnotificationtimestamp-example-pagetimestamp', |
| 291 |
'action=setnotificationtimestamp&generator=allpages&gapnamespace=2&token=123ABC' |
| 292 |
=> 'apihelp-setnotificationtimestamp-example-allpages', |
| 293 |
]; |
| 294 |
} |
| 295 |
|
| 296 |
public function getHelpUrls() { |
| 297 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:SetNotificationTimestamp'; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
class_alias( ApiSetNotificationTimestamp::class, 'ApiSetNotificationTimestamp' ); |