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 
110  public function getUser() {
111  return User::newFromIdentity( $this->user );
112  }
113 
117  public function getUserIdentity() {
118  return $this->user;
119  }
120 
124  public function getLinkTarget() {
125  return $this->linkTarget;
126  }
127 
133  public function getNotificationTimestamp() {
135  }
136 
145  public function getExpiry( ?int $style = TS_MW ) {
146  return $this->expiry instanceof ConvertibleTimestamp
147  ? $this->expiry->getTimestamp( $style )
148  : $this->expiry;
149  }
150 
158  public function isExpired(): bool {
159  $expiry = $this->getExpiry();
160  if ( $expiry === null ) {
161  return false;
162  }
163  return $expiry < ConvertibleTimestamp::now();
164  }
165 
173  public function getExpiryInDays(): ?int {
174  return self::calculateExpiryInDays( $this->getExpiry() );
175  }
176 
187  public static function calculateExpiryInDays( ?string $expiry ): ?int {
188  if ( $expiry === null ) {
189  return null;
190  }
191 
192  $unixTimeExpiry = MWTimestamp::convert( TS_UNIX, $expiry );
193  $diffInSeconds = $unixTimeExpiry - wfTimestamp();
194  $diffInDays = $diffInSeconds / self::SECONDS_IN_A_DAY;
195 
196  if ( $diffInDays < 1 ) {
197  return 0;
198  }
199 
200  return (int)ceil( $diffInDays );
201  }
202 
213  public function getExpiryInDaysText( MessageLocalizer $msgLocalizer, $isDropdownOption = false ): string {
214  $expiryInDays = $this->getExpiryInDays();
215  if ( $expiryInDays === null ) {
216  return '';
217  }
218 
219  if ( $expiryInDays < 1 ) {
220  if ( $isDropdownOption ) {
221  return $msgLocalizer->msg( 'watchlist-expiry-hours-left' )->text();
222  }
223  return $msgLocalizer->msg( 'watchlist-expiring-hours-full-text' )->text();
224  }
225 
226  if ( $isDropdownOption ) {
227  return $msgLocalizer->msg( 'watchlist-expiry-days-left', [ $expiryInDays ] )->text();
228  }
229 
230  return $msgLocalizer->msg( 'watchlist-expiring-days-full-text', [ $expiryInDays ] )->text();
231  }
232 }
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:73
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1815
User\newFromIdentity
static newFromIdentity(UserIdentity $identity)
Returns a User object corresponding to the given UserIdentity.
Definition: User.php:616
WatchedItem\newFromRecentChange
static newFromRecentChange(RecentChange $recentChange, UserIdentity $user)
Definition: WatchedItem.php:97
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
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:145
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
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:187
WatchedItem\getUserIdentity
getUserIdentity()
Definition: WatchedItem.php:117
WatchedItem\getUser
getUser()
Definition: WatchedItem.php:110
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:213
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:124
WatchedItem\isExpired
isExpired()
Has the watched item expired?
Definition: WatchedItem.php:158
WatchedItem\getNotificationTimestamp
getNotificationTimestamp()
Get the notification timestamp of this entry.
Definition: WatchedItem.php:133
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
RecentChange\getTitle
& getTitle()
Definition: RecentChange.php:297
WatchedItem\$linkTarget
LinkTarget $linkTarget
Definition: WatchedItem.php:39
WatchedItem\getExpiryInDays
getExpiryInDays()
Get days remaining until a watched item expires.
Definition: WatchedItem.php:173