MediaWiki  master
WatchedItem.php
Go to the documentation of this file.
1 <?php
26 use Wikimedia\Timestamp\ConvertibleTimestamp;
27 
36 class WatchedItem {
40  private $target;
41 
45  private $user;
46 
51 
56  private $expiry;
57 
65  private const SECONDS_IN_A_DAY = 86400;
66 
73  public function __construct(
75  $target,
77  ?string $expiry = null
78  ) {
79  $this->user = $user;
80  $this->target = $target;
81  $this->notificationTimestamp = $notificationTimestamp;
82 
83  // Expiry will be saved in ConvertibleTimestamp
84  $this->expiry = ExpiryDef::normalizeExpiry( $expiry );
85 
86  // If the normalization returned 'infinity' then set it as null since they are synonymous
87  if ( $this->expiry === 'infinity' ) {
88  $this->expiry = null;
89  }
90  }
91 
98  public static function newFromRecentChange( RecentChange $recentChange, UserIdentity $user ) {
99  return new self(
100  $user,
101  $recentChange->getTitle(),
102  $recentChange->notificationtimestamp,
103  $recentChange->watchlistExpiry
104  );
105  }
106 
110  public function getUserIdentity() {
111  return $this->user;
112  }
113 
118  public function getLinkTarget() {
119  if ( !$this->target instanceof LinkTarget ) {
120  return TitleValue::newFromPage( $this->target );
121  }
122  return $this->getTarget();
123  }
124 
129  public function getTarget() {
130  return $this->target;
131  }
132 
138  public function getNotificationTimestamp() {
140  }
141 
150  public function getExpiry( ?int $style = TS_MW ) {
151  return $this->expiry instanceof ConvertibleTimestamp
152  ? $this->expiry->getTimestamp( $style )
153  : $this->expiry;
154  }
155 
163  public function isExpired(): bool {
164  $expiry = $this->getExpiry();
165  if ( $expiry === null ) {
166  return false;
167  }
168  return $expiry < ConvertibleTimestamp::now();
169  }
170 
178  public function getExpiryInDays(): ?int {
179  return self::calculateExpiryInDays( $this->getExpiry() );
180  }
181 
192  public static function calculateExpiryInDays( ?string $expiry ): ?int {
193  if ( $expiry === null ) {
194  return null;
195  }
196 
197  $unixTimeExpiry = MWTimestamp::convert( TS_UNIX, $expiry );
198  $diffInSeconds = $unixTimeExpiry - wfTimestamp();
199  $diffInDays = $diffInSeconds / self::SECONDS_IN_A_DAY;
200 
201  if ( $diffInDays < 1 ) {
202  return 0;
203  }
204 
205  return (int)ceil( $diffInDays );
206  }
207 
218  public function getExpiryInDaysText( MessageLocalizer $msgLocalizer, $isDropdownOption = false ): string {
219  $expiryInDays = $this->getExpiryInDays();
220  if ( $expiryInDays === null ) {
221  return '';
222  }
223 
224  if ( $expiryInDays < 1 ) {
225  if ( $isDropdownOption ) {
226  return $msgLocalizer->msg( 'watchlist-expiry-hours-left' )->text();
227  }
228  return $msgLocalizer->msg( 'watchlist-expiring-hours-full-text' )->text();
229  }
230 
231  if ( $isDropdownOption ) {
232  return $msgLocalizer->msg( 'watchlist-expiry-days-left', [ $expiryInDays ] )->text();
233  }
234 
235  return $msgLocalizer->msg( 'watchlist-expiring-days-full-text', [ $expiryInDays ] )->text();
236  }
237 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
WatchedItem\$notificationTimestamp
null string $notificationTimestamp
the value of the wl_notificationtimestamp field
Definition: WatchedItem.php:50
RecentChange
Utility class for creating new RC entries.
Definition: RecentChange.php:80
WatchedItem\__construct
__construct(UserIdentity $user, $target, $notificationTimestamp, ?string $expiry=null)
Definition: WatchedItem.php:73
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
WatchedItem\$target
LinkTarget PageIdentity $target
deprecated LinkTarget since 1.36
Definition: WatchedItem.php:40
WatchedItem\newFromRecentChange
static newFromRecentChange(RecentChange $recentChange, UserIdentity $user)
Definition: WatchedItem.php:98
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
RecentChange\getTitle
getTitle()
Definition: RecentChange.php:329
WatchedItem\$user
UserIdentity $user
Definition: WatchedItem.php:45
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:150
Wikimedia\ParamValidator\TypeDef\ExpiryDef
Type definition for expiry timestamps.
Definition: ExpiryDef.php:17
TitleValue\newFromPage
static newFromPage(PageReference $page)
Constructs a TitleValue from a local PageReference.
Definition: TitleValue.php:119
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:56
WatchedItem\calculateExpiryInDays
static calculateExpiryInDays(?string $expiry)
Get the number of days remaining until the given expiry time.
Definition: WatchedItem.php:192
WatchedItem\getUserIdentity
getUserIdentity()
Definition: WatchedItem.php:110
WatchedItem
Representation of a pair of user and title for watchlist entries.
Definition: WatchedItem.php:36
WatchedItem\getTarget
getTarget()
Definition: WatchedItem.php:129
WatchedItem\getExpiryInDaysText
getExpiryInDaysText(MessageLocalizer $msgLocalizer, $isDropdownOption=false)
Get days remaining until a watched item expires as a text.
Definition: WatchedItem.php:218
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:65
WatchedItem\getLinkTarget
getLinkTarget()
Definition: WatchedItem.php:118
WatchedItem\isExpired
isExpired()
Has the watched item expired?
Definition: WatchedItem.php:163
WatchedItem\getNotificationTimestamp
getNotificationTimestamp()
Get the notification timestamp of this entry.
Definition: WatchedItem.php:138
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
WatchedItem\getExpiryInDays
getExpiryInDays()
Get days remaining until a watched item expires.
Definition: WatchedItem.php:178