Revert "Revert "ApiEditPage: Show existing watchlist expiry if status

is not being changed.""

This reverts commit 149e99f072.

It's not necessary to change the constructor now, the module is already
using service locator to fetch RevisionLookup and ContentHandlerFactory.

The WatchedItemStore can also be gotten from there, voiding the need for
altering the constructor now. As Daniel said in T259960#6380471 dependency
injection for API modules is good but not urgent.

Bug: T261030
Bug: T264200
Change-Id: I16aa942cc800cd66a2cd538680a02b10cb0b1bfe
This commit is contained in:
Ammar Abdulhamid 2020-09-30 18:43:39 +01:00 committed by MusikAnimal
parent e3d4f1db9b
commit 30b947ad5f
2 changed files with 39 additions and 6 deletions

View file

@ -366,7 +366,6 @@ class ApiEditPage extends ApiBase {
}
$watch = $this->getWatchlistValue( $params['watchlist'], $titleObj, $user );
$watchlistExpiry = $params['watchlistexpiry'] ?? null;
// Deprecated parameters
if ( $params['watch'] ) {
@ -376,9 +375,10 @@ class ApiEditPage extends ApiBase {
}
if ( $watch ) {
$requestArray['wpWatchthis'] = '';
$requestArray['wpWatchthis'] = true;
$watchlistExpiry = $this->getExpiryFromParams( $params );
if ( $this->watchlistExpiryEnabled && $watchlistExpiry ) {
if ( $watchlistExpiry ) {
$requestArray['wpWatchlistExpiry'] = $watchlistExpiry;
}
}
@ -496,10 +496,17 @@ class ApiEditPage extends ApiBase {
}
if ( $watch ) {
$r['watched'] = $status->isOK();
$r['watched'] = true;
if ( $this->watchlistExpiryEnabled ) {
$r['watchlistexpiry'] = ApiResult::formatExpiry( $watchlistExpiry );
$watchedItemStore = MediaWikiServices::getInstance()->getWatchedItemStore();
$watchlistExpiry = $this->getWatchlistExpiry(
$watchedItemStore,
$titleObj,
$user
);
if ( $watchlistExpiry ) {
$r['watchlistexpiry'] = $watchlistExpiry;
}
}
break;

View file

@ -140,4 +140,30 @@ trait ApiWatchlistTrait {
return $watchlistExpiry;
}
/**
* Get existing expiry from the database.
*
* @param WatchedItemStoreInterface $store
* @param Title $title
* @param User $user The user to get the expiry for.
* @return string|null
*/
protected function getWatchlistExpiry(
WatchedItemStoreInterface $store,
Title $title,
User $user
): ?string {
$watchedItem = $store->getWatchedItem( $user, $title );
if ( $watchedItem ) {
$expiry = $watchedItem->getExpiry();
if ( $expiry !== null ) {
return ApiResult::formatExpiry( $expiry );
}
}
return null;
}
}