MediaWiki  master
WatchedItem.php
Go to the documentation of this file.
1 <?php
25 use Wikimedia\Timestamp\ConvertibleTimestamp;
26 
35 class WatchedItem {
39  private $linkTarget;
40 
44  private $user;
45 
50 
55  private $expiry;
56 
64  private const SECONDS_IN_A_DAY = 86400;
65 
72  public function __construct(
76  ?string $expiry = null
77  ) {
78  $this->user = $user;
79  $this->linkTarget = $linkTarget;
80  $this->notificationTimestamp = $notificationTimestamp;
81 
82  // Expiry will be saved in ConvertibleTimestamp
83  $this->expiry = ExpiryDef::normalizeExpiry( $expiry );
84 
85  // If the normalization returned 'infinity' then set it as null since they are synonymous
86  if ( $this->expiry === 'infinity' ) {
87  $this->expiry = null;
88  }
89  }
90 
97  public static function newFromRecentChange( RecentChange $recentChange, UserIdentity $user ) {
98  return new self(
99  $user,
100  $recentChange->getTitle(),
101  $recentChange->notificationtimestamp,
102  $recentChange->watchlistExpiry
103  );
104  }
105 
111  public function getUser() {
112  wfDeprecated( __METHOD__, '1.34' );
113  return User::newFromIdentity( $this->user );
114  }
115 
119  public function getUserIdentity() {
120  return $this->user;
121  }
122 
126  public function getLinkTarget() {
127  return $this->linkTarget;
128  }
129 
135  public function getNotificationTimestamp() {
137  }
138 
147  public function getExpiry( ?int $style = TS_MW ) {
148  return $this->expiry instanceof ConvertibleTimestamp
149  ? $this->expiry->getTimestamp( $style )
150  : $this->expiry;
151  }
152 
160  public function isExpired(): bool {
161  $expiry = $this->getExpiry();
162  if ( $expiry === null ) {
163  return false;
164  }
165  return $expiry < ConvertibleTimestamp::now();
166  }
167 
175  public function getExpiryInDays(): ?int {
176  return self::calculateExpiryInDays( $this->getExpiry() );
177  }
178 
189  public static function calculateExpiryInDays( ?string $expiry ): ?int {
190  if ( $expiry === null ) {
191  return null;
192  }
193 
194  $unixTimeExpiry = MWTimestamp::convert( TS_UNIX, $expiry );
195  $diffInSeconds = $unixTimeExpiry - wfTimestamp();
196  $diffInDays = $diffInSeconds / self::SECONDS_IN_A_DAY;
197 
198  if ( $diffInDays < 1 ) {
199  return 0;
200  }
201 
202  return (int)ceil( $diffInDays );
203  }
204 
215  public function getExpiryInDaysText( MessageLocalizer $msgLocalizer, $isDropdownOption = false ): string {
216  $expiryInDays = $this->getExpiryInDays();
217  if ( $expiryInDays === null ) {
218  return '';
219  }
220 
221  if ( $expiryInDays < 1 ) {
222  if ( $isDropdownOption ) {
223  return $msgLocalizer->msg( 'watchlist-expiry-hours-left' )->text();
224  }
225  return $msgLocalizer->msg( 'watchlist-expiring-hours-full-text' )->text();
226  }
227 
228  if ( $isDropdownOption ) {
229  return $msgLocalizer->msg( 'watchlist-expiry-days-left', [ $expiryInDays ] )->text();
230  }
231 
232  return $msgLocalizer->msg( 'watchlist-expiring-days-full-text', [ $expiryInDays ] )->text();
233  }
234 }
WatchedItem\$notificationTimestamp
null string $notificationTimestamp
the value of the wl_notificationtimestamp field
Definition: WatchedItem.php:49
RecentChange
Utility class for creating new RC entries.
Definition: RecentChange.php:76
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1832
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:663
WatchedItem\newFromRecentChange
static newFromRecentChange(RecentChange $recentChange, UserIdentity $user)
Definition: WatchedItem.php:97
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
WatchedItem\$user
UserIdentity $user
Definition: WatchedItem.php:44
MessageLocalizer
Interface for localizing messages in MediaWiki.
Definition: MessageLocalizer.php:29
WatchedItem\getExpiry
getExpiry(?int $style=TS_MW)
When the watched item will expire.
Definition: WatchedItem.php:147
Wikimedia\ParamValidator\TypeDef\ExpiryDef
Type definition for expiry timestamps.
Definition: ExpiryDef.php:17
WatchedItem\__construct
__construct(UserIdentity $user, LinkTarget $linkTarget, $notificationTimestamp, ?string $expiry=null)
Definition: WatchedItem.php:72
wfDeprecated
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Logs a warning that $function is deprecated.
Definition: GlobalFunctions.php:1034
MessageLocalizer\msg
msg( $key,... $params)
This is the method for getting translated interface messages.
WatchedItem\$expiry
ConvertibleTimestamp null $expiry
value that determines when a watched item will expire.
Definition: WatchedItem.php:55
WatchedItem\calculateExpiryInDays
static calculateExpiryInDays(?string $expiry)
Get the number of days remaining until the given expiry time.
Definition: WatchedItem.php:189
WatchedItem\getUserIdentity
getUserIdentity()
Definition: WatchedItem.php:119
WatchedItem\getUser
getUser()
Definition: WatchedItem.php:111
WatchedItem
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:35
WatchedItem\getExpiryInDaysText
getExpiryInDaysText(MessageLocalizer $msgLocalizer, $isDropdownOption=false)
Get days remaining until a watched item expires as a text.
Definition: WatchedItem.php:215
WatchedItem\SECONDS_IN_A_DAY
const SECONDS_IN_A_DAY
Used to calculate how many days are remaining until a watched item will expire.
Definition: WatchedItem.php:64
WatchedItem\getLinkTarget
getLinkTarget()
Definition: WatchedItem.php:126
WatchedItem\isExpired
isExpired()
Has the watched item expired?
Definition: WatchedItem.php:160
WatchedItem\getNotificationTimestamp
getNotificationTimestamp()
Get the notification timestamp of this entry.
Definition: WatchedItem.php:135
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
RecentChange\getTitle
& getTitle()
Definition: RecentChange.php:300
WatchedItem\$linkTarget
LinkTarget $linkTarget
Definition: WatchedItem.php:39
WatchedItem\getExpiryInDays
getExpiryInDays()
Get days remaining until a watched item expires.
Definition: WatchedItem.php:175