Merge "Remove requirement for ApiWatchlistTrait to be in ApiBase."

This commit is contained in:
jenkins-bot 2020-09-11 00:36:58 +00:00 committed by Gerrit Code Review
commit f78bf7963d
9 changed files with 26 additions and 30 deletions

View file

@ -52,7 +52,6 @@ use Wikimedia\Rdbms\IDatabase;
abstract class ApiBase extends ContextSource {
use ApiBlockInfoTrait;
use ApiWatchlistTrait;
/** @var HookContainer */
private $hookContainer;

View file

@ -103,7 +103,7 @@ class ApiDelete extends ApiBase {
}
$watchlistExpiry = $this->getExpiryFromParams( $params );
$this->setWatch( $watch, $titleObj, 'watchdeletion', $watchlistExpiry );
$this->setWatch( $watch, $titleObj, $user, 'watchdeletion', $watchlistExpiry );
$r = [
'title' => $titleObj->getPrefixedText(),

View file

@ -369,7 +369,7 @@ class ApiEditPage extends ApiBase {
$requestArray['wpSection'] = '';
}
$watch = $this->getWatchlistValue( $params['watchlist'], $titleObj );
$watch = $this->getWatchlistValue( $params['watchlist'], $titleObj, $user );
$watchlistExpiry = $params['watchlistexpiry'] ?? null;
// Deprecated parameters

View file

@ -169,8 +169,8 @@ class ApiMove extends ApiBase {
$watchlistExpiry = $this->getExpiryFromParams( $params );
// Watch pages
$this->setWatch( $watch, $fromTitle, 'watchmoves', $watchlistExpiry );
$this->setWatch( $watch, $toTitle, 'watchmoves', $watchlistExpiry );
$this->setWatch( $watch, $fromTitle, $user, 'watchmoves', $watchlistExpiry );
$this->setWatch( $watch, $toTitle, $user, 'watchmoves', $watchlistExpiry );
$result->addValue( null, $this->getModuleName(), $r );
}

View file

@ -113,7 +113,7 @@ class ApiProtect extends ApiBase {
$watch = $params['watch'] ? 'watch' : $params['watchlist'];
$watchlistExpiry = $this->getExpiryFromParams( $params );
$this->setWatch( $watch, $titleObj, 'watchdefault', $watchlistExpiry );
$this->setWatch( $watch, $titleObj, $user, 'watchdefault', $watchlistExpiry );
$status = $pageObj->doUpdateRestrictions(
$protections,

View file

@ -94,7 +94,7 @@ class ApiRollback extends ApiBase {
$watchlistExpiry = $this->getExpiryFromParams( $params );
// Watch pages
$this->setWatch( $watch, $titleObj, 'watchrollback', $watchlistExpiry );
$this->setWatch( $watch, $titleObj, $user, 'watchrollback', $watchlistExpiry );
$currentRevisionRecord = $details['current-revision-record'];
$targetRevisionRecord = $details['target-revision-record'];

View file

@ -93,7 +93,7 @@ class ApiUndelete extends ApiBase {
}
$watchlistExpiry = $this->getExpiryFromParams( $params );
$this->setWatch( $params['watchlist'], $titleObj, null, $watchlistExpiry );
$this->setWatch( $params['watchlist'], $titleObj, $user, null, $watchlistExpiry );
$info = [
'title' => $titleObj->getPrefixedText(),

View file

@ -819,20 +819,22 @@ class ApiUpload extends ApiBase {
/** @var LocalFile $file */
$file = $this->mUpload->getLocalFile();
$user = $this->getUser();
$title = $file->getTitle();
// For preferences mode, we want to watch if 'watchdefault' is set,
// for preferences mode, we want to watch if 'watchdefault' is set,
// or if the *file* doesn't exist, and either 'watchuploads' or
// 'watchcreations' is set. But getWatchlistValue()'s automatic
// handling checks if the *title* exists or not, so we need to check
// all three preferences manually.
$watch = $this->getWatchlistValue(
$this->mParams['watchlist'], $file->getTitle(), 'watchdefault'
$this->mParams['watchlist'], $title, $user, 'watchdefault'
);
if ( !$watch && $this->mParams['watchlist'] == 'preferences' && !$file->exists() ) {
$watch = (
$this->getWatchlistValue( 'preferences', $file->getTitle(), 'watchuploads' ) ||
$this->getWatchlistValue( 'preferences', $file->getTitle(), 'watchcreations' )
$this->getWatchlistValue( 'preferences', $title, $user, 'watchuploads' ) ||
$this->getWatchlistValue( 'preferences', $title, $user, 'watchcreations' )
);
}
$watchlistExpiry = $this->getExpiryFromParams( $this->mParams );

View file

@ -63,36 +63,39 @@ trait ApiWatchlistTrait {
/**
* Set a watch (or unwatch) based the based on a watchlist parameter.
* @param string $watch Valid values: 'watch', 'unwatch', 'preferences', 'nochange'
* @param Title $titleObj The article's title to change
* @param Title $title The article's title to change
* @param User $user The user to set watch/unwatch for
* @param string|null $userOption The user option to consider when $watch=preferences
* @param string|null $expiry Optional expiry timestamp in any format acceptable to wfTimestamp(),
* null will not create expiries, or leave them unchanged should they already exist.
*/
protected function setWatch(
string $watch,
Title $titleObj,
Title $title,
User $user,
?string $userOption = null,
?string $expiry = null
): void {
$value = $this->getWatchlistValue( $watch, $titleObj, $userOption );
WatchAction::doWatchOrUnwatch( $value, $titleObj, $this->getUser(), $expiry );
$value = $this->getWatchlistValue( $watch, $title, $user, $userOption );
WatchAction::doWatchOrUnwatch( $value, $title, $user, $expiry );
}
/**
* Return true if we're to watch the page, false if not.
* @param string $watchlist Valid values: 'watch', 'unwatch', 'preferences', 'nochange'
* @param Title $titleObj The page under consideration
* @param Title $title The page under consideration
* @param User $user The user get the the value for.
* @param string|null $userOption The user option to consider when $watchlist=preferences.
* If not set will use watchdefault always and watchcreations if $titleObj doesn't exist.
* If not set will use watchdefault always and watchcreations if $title doesn't exist.
* @return bool
*/
protected function getWatchlistValue(
string $watchlist,
Title $titleObj,
Title $title,
User $user,
?string $userOption = null
): bool {
$user = $this->getUser();
$userWatching = $user->isWatched( $titleObj, User::IGNORE_USER_RIGHTS );
$userWatching = $user->isWatched( $title, User::IGNORE_USER_RIGHTS );
switch ( $watchlist ) {
case 'watch':
@ -110,7 +113,7 @@ trait ApiWatchlistTrait {
if ( $userOption === null ) {
return $user->getBoolOption( 'watchdefault' ) ||
$user->getBoolOption( 'watchcreations' ) &&
!$titleObj->exists();
!$title->exists();
}
// Watch the article based on the user preference
@ -137,12 +140,4 @@ trait ApiWatchlistTrait {
return $watchlistExpiry;
}
/**
* Implemented by ApiBase. We do this because otherwise Phan would complain
* about calls to $this->getUser() in this trait, since it doesn't infer that
* classes using the trait are also extending ApiBase.
* @return User
*/
abstract protected function getUser();
}