| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Context\IContextSource; |
| 22 |
use MediaWiki\Linker\Linker; |
| 23 |
use MediaWiki\Linker\LinkRenderer; |
| 24 |
use MediaWiki\Revision\RevisionRecord; |
| 25 |
use MediaWiki\SpecialPage\SpecialPage; |
| 26 |
use MediaWiki\Title\Title; |
| 27 |
use MediaWiki\User\ExternalUserNames; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
class RCCacheEntryFactory { |
| 35 |
|
| 36 |
|
| 37 |
private $context; |
| 38 |
|
| 39 |
|
| 40 |
private $messages; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
private $linkRenderer; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
private MapCacheLRU $userLinkCache; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
private MapCacheLRU $toolLinkCache; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public function __construct( |
| 63 |
IContextSource $context, $messages, LinkRenderer $linkRenderer |
| 64 |
) { |
| 65 |
$this->context = $context; |
| 66 |
$this->messages = $messages; |
| 67 |
$this->linkRenderer = $linkRenderer; |
| 68 |
$this->userLinkCache = new MapCacheLRU( 50 ); |
| 69 |
$this->toolLinkCache = new MapCacheLRU( 50 ); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public function newFromRecentChange( RecentChange $baseRC, $watched ) { |
| 79 |
$user = $this->context->getUser(); |
| 80 |
|
| 81 |
$cacheEntry = RCCacheEntry::newFromParent( $baseRC ); |
| 82 |
|
| 83 |
|
| 84 |
$cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user ); |
| 85 |
|
| 86 |
$cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched; |
| 87 |
$cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers; |
| 88 |
$cacheEntry->watchlistExpiry = $baseRC->watchlistExpiry; |
| 89 |
|
| 90 |
$cacheEntry->link = $this->buildCLink( $cacheEntry ); |
| 91 |
$cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry ); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
$showDiffLinks = ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user ); |
| 96 |
|
| 97 |
$cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks ); |
| 98 |
$cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks ); |
| 99 |
$cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks ); |
| 100 |
|
| 101 |
|
| 102 |
$cacheEntry->userlink = $this->getUserLink( $cacheEntry ); |
| 103 |
|
| 104 |
if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) { |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
$cacheEntry->usertalklink = $this->toolLinkCache->getWithSetCallback( |
| 112 |
$this->toolLinkCache->makeKey( |
| 113 |
$cacheEntry->mAttribs['rc_user_text'], |
| 114 |
$this->context->getUser()->getName(), |
| 115 |
$this->context->getLanguage()->getCode() |
| 116 |
), |
| 117 |
static fn () => Linker::userToolLinks( |
| 118 |
$cacheEntry->mAttribs['rc_user'], |
| 119 |
$cacheEntry->mAttribs['rc_user_text'], |
| 120 |
|
| 121 |
false, |
| 122 |
|
| 123 |
0, |
| 124 |
|
| 125 |
null, |
| 126 |
|
| 127 |
false |
| 128 |
) |
| 129 |
); |
| 130 |
} |
| 131 |
|
| 132 |
return $cacheEntry; |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private function buildCLink( RCCacheEntry $cacheEntry ) { |
| 141 |
$type = $cacheEntry->mAttribs['rc_type']; |
| 142 |
|
| 143 |
|
| 144 |
if ( $type == RC_LOG ) { |
| 145 |
$logType = $cacheEntry->mAttribs['rc_log_type']; |
| 146 |
|
| 147 |
if ( $logType ) { |
| 148 |
$clink = $this->getLogLink( $logType ); |
| 149 |
} else { |
| 150 |
wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' ); |
| 151 |
$clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() ); |
| 152 |
} |
| 153 |
|
| 154 |
} elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) { |
| 155 |
wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' ); |
| 156 |
$clink = ''; |
| 157 |
|
| 158 |
} else { |
| 159 |
$clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() ); |
| 160 |
} |
| 161 |
|
| 162 |
return $clink; |
| 163 |
} |
| 164 |
|
| 165 |
private function getLogLink( $logType ) { |
| 166 |
$logtitle = SpecialPage::getTitleFor( 'Log', $logType ); |
| 167 |
$logpage = new LogPage( $logType ); |
| 168 |
$logname = $logpage->getName()->text(); |
| 169 |
|
| 170 |
$logLink = $this->context->msg( 'parentheses' ) |
| 171 |
->rawParams( |
| 172 |
$this->linkRenderer->makeKnownLink( $logtitle, $logname ) |
| 173 |
)->escaped(); |
| 174 |
|
| 175 |
return $logLink; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
private function buildTimestamp( RecentChange $cacheEntry ) { |
| 184 |
return $this->context->getLanguage()->userTime( |
| 185 |
$cacheEntry->mAttribs['rc_timestamp'], |
| 186 |
$this->context->getUser() |
| 187 |
); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function buildCurQueryParams( RecentChange $recentChange ) { |
| 196 |
return [ |
| 197 |
'curid' => $recentChange->mAttribs['rc_cur_id'], |
| 198 |
'diff' => 0, |
| 199 |
'oldid' => $recentChange->mAttribs['rc_this_oldid'] |
| 200 |
]; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) { |
| 210 |
$curMessage = $this->getMessage( 'cur' ); |
| 211 |
$logTypes = [ RC_LOG ]; |
| 212 |
if ( $cacheEntry->mAttribs['rc_this_oldid'] == $cacheEntry->getAttribute( 'page_latest' ) ) { |
| 213 |
$showDiffLinks = false; |
| 214 |
} |
| 215 |
|
| 216 |
if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) { |
| 217 |
$curLink = $curMessage; |
| 218 |
} else { |
| 219 |
$queryParams = $this->buildCurQueryParams( $cacheEntry ); |
| 220 |
$curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) ); |
| 221 |
$curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>"; |
| 222 |
} |
| 223 |
|
| 224 |
return $curLink; |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
private function buildDiffQueryParams( RecentChange $recentChange ) { |
| 233 |
return [ |
| 234 |
'curid' => $recentChange->mAttribs['rc_cur_id'], |
| 235 |
'diff' => $recentChange->mAttribs['rc_this_oldid'], |
| 236 |
'oldid' => $recentChange->mAttribs['rc_last_oldid'] |
| 237 |
]; |
| 238 |
} |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) { |
| 247 |
$queryParams = $this->buildDiffQueryParams( $cacheEntry ); |
| 248 |
$diffMessage = $this->getMessage( 'diff' ); |
| 249 |
$logTypes = [ RC_NEW, RC_LOG ]; |
| 250 |
|
| 251 |
if ( !$showDiffLinks ) { |
| 252 |
$diffLink = $diffMessage; |
| 253 |
} elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) { |
| 254 |
$diffLink = $diffMessage; |
| 255 |
} elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) { |
| 256 |
$rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' ); |
| 257 |
$pageTitle = Title::newFromID( $rcCurId ); |
| 258 |
if ( $pageTitle === null ) { |
| 259 |
wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId ); |
| 260 |
return $diffMessage; |
| 261 |
} |
| 262 |
$diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) ); |
| 263 |
$diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>"; |
| 264 |
} else { |
| 265 |
$diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) ); |
| 266 |
$diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>"; |
| 267 |
} |
| 268 |
|
| 269 |
return $diffLink; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) { |
| 281 |
$lastOldid = $cacheEntry->mAttribs['rc_last_oldid']; |
| 282 |
$lastMessage = $this->getMessage( 'last' ); |
| 283 |
$type = $cacheEntry->mAttribs['rc_type']; |
| 284 |
$logTypes = [ RC_LOG ]; |
| 285 |
|
| 286 |
|
| 287 |
if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) { |
| 288 |
$lastLink = $lastMessage; |
| 289 |
} else { |
| 290 |
$lastLink = $this->linkRenderer->makeKnownLink( |
| 291 |
$cacheEntry->getTitle(), |
| 292 |
new HtmlArmor( $lastMessage ), |
| 293 |
[ 'class' => 'mw-changeslist-diff' ], |
| 294 |
$this->buildDiffQueryParams( $cacheEntry ) |
| 295 |
); |
| 296 |
} |
| 297 |
|
| 298 |
return $lastLink; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
private function getUserLink( RecentChange $cacheEntry ) { |
| 307 |
if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) { |
| 308 |
$deletedClass = 'history-deleted'; |
| 309 |
if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_RESTRICTED ) ) { |
| 310 |
$deletedClass .= ' mw-history-suppressed'; |
| 311 |
} |
| 312 |
$userLink = ' <span class="' . $deletedClass . '">' . |
| 313 |
$this->context->msg( 'rev-deleted-user' )->escaped() . '</span>'; |
| 314 |
} else { |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
$userLink = $this->userLinkCache->getWithSetCallback( |
| 321 |
$this->userLinkCache->makeKey( |
| 322 |
$cacheEntry->mAttribs['rc_user_text'], |
| 323 |
$this->context->getUser()->getName(), |
| 324 |
$this->context->getLanguage()->getCode() |
| 325 |
), |
| 326 |
static fn () => Linker::userLink( |
| 327 |
$cacheEntry->mAttribs['rc_user'], |
| 328 |
$cacheEntry->mAttribs['rc_user_text'], |
| 329 |
ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] ) |
| 330 |
) |
| 331 |
); |
| 332 |
} |
| 333 |
|
| 334 |
return $userLink; |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private function getMessage( $key ) { |
| 343 |
return $this->messages[$key]; |
| 344 |
} |
| 345 |
|
| 346 |
} |