ApiEditPage: Show existing watchlist expiry if status is not being changed.

Bug: T261030
Change-Id: I795db12aefeffb1cfbbe2ab00fbb19444df7d37b
This commit is contained in:
Ammar Abdulhamid 2020-08-23 01:28:34 +01:00
parent fcd843b5aa
commit 07e547f47c
3 changed files with 54 additions and 8 deletions

View file

@ -38,11 +38,20 @@ class ApiEditPage extends ApiBase {
use ApiWatchlistTrait;
public function __construct( ApiMain $mainModule, $moduleName, $modulePrefix = '' ) {
/** @var WatchedItemStoreInterface */
private $watchedItemStore;
public function __construct(
ApiMain $mainModule,
$moduleName,
WatchedItemStore $watchedItemStore,
$modulePrefix = ''
) {
parent::__construct( $mainModule, $moduleName, $modulePrefix );
$this->watchlistExpiryEnabled = $this->getConfig()->get( 'WatchlistExpiry' );
$this->watchlistMaxDuration = $this->getConfig()->get( 'WatchlistExpiryMaxDuration' );
$this->watchedItemStore = $watchedItemStore;
}
public function execute() {
@ -370,7 +379,6 @@ class ApiEditPage extends ApiBase {
}
$watch = $this->getWatchlistValue( $params['watchlist'], $titleObj, $user );
$watchlistExpiry = $params['watchlistexpiry'] ?? null;
// Deprecated parameters
if ( $params['watch'] ) {
@ -380,9 +388,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;
}
}
@ -500,10 +509,16 @@ class ApiEditPage extends ApiBase {
}
if ( $watch ) {
$r['watched'] = $status->isOK();
$r['watched'] = true;
if ( $this->watchlistExpiryEnabled ) {
$r['watchlistexpiry'] = ApiResult::formatExpiry( $watchlistExpiry );
$watchlistExpiry = $this->getWatchlistExpiry(
$this->watchedItemStore,
$titleObj,
$user
);
if ( $watchlistExpiry ) {
$r['watchlistexpiry'] = $watchlistExpiry;
}
}
break;

View file

@ -111,7 +111,12 @@ class ApiMain extends ApiBase {
]
],
'move' => ApiMove::class,
'edit' => ApiEditPage::class,
'edit' => [
'class' => ApiEditPage::class,
'services' => [
'WatchedItemStore'
],
],
'upload' => ApiUpload::class,
'filerevert' => ApiFileRevert::class,
'emailuser' => ApiEmailUser::class,

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;
}
}