| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
namespace MediaWiki\Watchlist; |
| 23 |
|
| 24 |
use MediaWiki\Linker\LinkTarget; |
| 25 |
use MediaWiki\Page\PageIdentity; |
| 26 |
use MediaWiki\Title\TitleValue; |
| 27 |
use MediaWiki\User\UserIdentity; |
| 28 |
use MediaWiki\Utils\MWTimestamp; |
| 29 |
use MessageLocalizer; |
| 30 |
use RecentChange; |
| 31 |
use Wikimedia\ParamValidator\TypeDef\ExpiryDef; |
| 32 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class WatchedItem { |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private $target; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private $user; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private $notificationTimestamp; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private $expiry; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private const SECONDS_IN_A_DAY = 86400; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function __construct( |
| 80 |
UserIdentity $user, |
| 81 |
$target, |
| 82 |
$notificationTimestamp, |
| 83 |
?string $expiry = null |
| 84 |
) { |
| 85 |
$this->user = $user; |
| 86 |
$this->target = $target; |
| 87 |
$this->notificationTimestamp = $notificationTimestamp; |
| 88 |
|
| 89 |
|
| 90 |
$this->expiry = ExpiryDef::normalizeExpiry( $expiry ); |
| 91 |
|
| 92 |
|
| 93 |
if ( $this->expiry === 'infinity' ) { |
| 94 |
$this->expiry = null; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public static function newFromRecentChange( RecentChange $recentChange, UserIdentity $user ) { |
| 105 |
return new self( |
| 106 |
$user, |
| 107 |
$recentChange->getTitle(), |
| 108 |
$recentChange->notificationtimestamp, |
| 109 |
$recentChange->watchlistExpiry |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getUserIdentity() { |
| 117 |
return $this->user; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
public function getLinkTarget() { |
| 125 |
if ( !$this->target instanceof LinkTarget ) { |
| 126 |
return TitleValue::newFromPage( $this->target ); |
| 127 |
} |
| 128 |
return $this->getTarget(); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public function getTarget() { |
| 136 |
return $this->target; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public function getNotificationTimestamp() { |
| 145 |
return $this->notificationTimestamp; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function getExpiry( ?int $style = TS_MW ) { |
| 157 |
return $this->expiry instanceof ConvertibleTimestamp |
| 158 |
? $this->expiry->getTimestamp( $style ) |
| 159 |
: $this->expiry; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public function isExpired(): bool { |
| 170 |
$expiry = $this->getExpiry(); |
| 171 |
if ( $expiry === null ) { |
| 172 |
return false; |
| 173 |
} |
| 174 |
return $expiry < ConvertibleTimestamp::now(); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public function getExpiryInDays(): ?int { |
| 185 |
return self::calculateExpiryInDays( $this->getExpiry() ); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public static function calculateExpiryInDays( ?string $expiry ): ?int { |
| 199 |
if ( $expiry === null ) { |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
$unixTimeExpiry = (int)MWTimestamp::convert( TS_UNIX, $expiry ); |
| 204 |
$diffInSeconds = $unixTimeExpiry - (int)wfTimestamp( TS_UNIX ); |
| 205 |
$diffInDays = $diffInSeconds / self::SECONDS_IN_A_DAY; |
| 206 |
|
| 207 |
if ( $diffInDays < 1 ) { |
| 208 |
return 0; |
| 209 |
} |
| 210 |
|
| 211 |
return (int)ceil( $diffInDays ); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function getExpiryInDaysText( MessageLocalizer $msgLocalizer, $isDropdownOption = false ): string { |
| 225 |
$expiryInDays = $this->getExpiryInDays(); |
| 226 |
if ( $expiryInDays === null ) { |
| 227 |
return ''; |
| 228 |
} |
| 229 |
|
| 230 |
if ( $expiryInDays < 1 ) { |
| 231 |
if ( $isDropdownOption ) { |
| 232 |
return $msgLocalizer->msg( 'watchlist-expiry-hours-left' )->text(); |
| 233 |
} |
| 234 |
return $msgLocalizer->msg( 'watchlist-expiring-hours-full-text' )->text(); |
| 235 |
} |
| 236 |
|
| 237 |
if ( $isDropdownOption ) { |
| 238 |
return $msgLocalizer->msg( 'watchlist-expiry-days-left', [ $expiryInDays ] )->text(); |
| 239 |
} |
| 240 |
|
| 241 |
return $msgLocalizer->msg( 'watchlist-expiring-days-full-text', [ $expiryInDays ] )->text(); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
class_alias( WatchedItem::class, 'WatchedItem' ); |