MediaWiki  1.28.1
RCCacheEntryFactory.php
Go to the documentation of this file.
1 <?php
23 
25 
26  /* @var IContextSource */
27  private $context;
28 
29  /* @var string[] */
30  private $messages;
31 
35  private $linkRenderer;
36 
42  public function __construct(
44  ) {
45  $this->context = $context;
46  $this->messages = $messages;
47  $this->linkRenderer = $linkRenderer;
48  }
49 
56  public function newFromRecentChange( RecentChange $baseRC, $watched ) {
57  $user = $this->context->getUser();
58  $counter = $baseRC->counter;
59 
60  $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
61 
62  // Should patrol-related stuff be shown?
63  $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
64 
65  $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
66  $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
67 
68  $cacheEntry->link = $this->buildCLink( $cacheEntry );
69  $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
70 
71  // Make "cur" and "diff" links. Do not use link(), it is too slow if
72  // called too many times (50% of CPU time on RecentChanges!).
73  $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
74 
75  $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks, $counter );
76  $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
77  $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
78 
79  // Make user links
80  $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
81 
82  if ( !ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
83  $cacheEntry->usertalklink = Linker::userToolLinks(
84  $cacheEntry->mAttribs['rc_user'],
85  $cacheEntry->mAttribs['rc_user_text']
86  );
87  }
88 
89  return $cacheEntry;
90  }
91 
98  private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
99  return ChangesList::userCan( $cacheEntry, Revision::DELETED_TEXT, $user );
100  }
101 
107  private function buildCLink( RecentChange $cacheEntry ) {
108  $type = $cacheEntry->mAttribs['rc_type'];
109 
110  // New unpatrolled pages
111  if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
112  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
113  // Log entries
114  } elseif ( $type == RC_LOG ) {
115  $logType = $cacheEntry->mAttribs['rc_log_type'];
116 
117  if ( $logType ) {
118  $clink = $this->getLogLink( $logType );
119  } else {
120  wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
121  $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
122  }
123  // Log entries (old format) and special pages
124  } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
125  wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
126  $clink = '';
127  // Edits
128  } else {
129  $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
130  }
131 
132  return $clink;
133  }
134 
135  private function getLogLink( $logType ) {
136  $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
137  $logpage = new LogPage( $logType );
138  $logname = $logpage->getName()->text();
139 
140  $logLink = $this->context->msg( 'parentheses' )
141  ->rawParams(
142  $this->linkRenderer->makeKnownLink( $logtitle, $logname )
143  )->escaped();
144 
145  return $logLink;
146  }
147 
153  private function buildTimestamp( RecentChange $cacheEntry ) {
154  return $this->context->getLanguage()->userTime(
155  $cacheEntry->mAttribs['rc_timestamp'],
156  $this->context->getUser()
157  );
158  }
159 
165  private function buildCurQueryParams( RecentChange $recentChange ) {
166  return [
167  'curid' => $recentChange->mAttribs['rc_cur_id'],
168  'diff' => 0,
169  'oldid' => $recentChange->mAttribs['rc_this_oldid']
170  ];
171  }
172 
180  private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
181  $queryParams = $this->buildCurQueryParams( $cacheEntry );
182  $curMessage = $this->getMessage( 'cur' );
183  $logTypes = [ RC_LOG ];
184 
185  if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
186  $curLink = $curMessage;
187  } else {
188  $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
189  $curLink = "<a href=\"$curUrl\">$curMessage</a>";
190  }
191 
192  return $curLink;
193  }
194 
200  private function buildDiffQueryParams( RecentChange $recentChange ) {
201  return [
202  'curid' => $recentChange->mAttribs['rc_cur_id'],
203  'diff' => $recentChange->mAttribs['rc_this_oldid'],
204  'oldid' => $recentChange->mAttribs['rc_last_oldid']
205  ];
206  }
207 
215  private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
216  $queryParams = $this->buildDiffQueryParams( $cacheEntry );
217  $diffMessage = $this->getMessage( 'diff' );
218  $logTypes = [ RC_NEW, RC_LOG ];
219 
220  if ( !$showDiffLinks ) {
221  $diffLink = $diffMessage;
222  } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
223  $diffLink = $diffMessage;
224  } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
225  $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
226  $pageTitle = Title::newFromID( $rcCurId );
227  if ( $pageTitle === null ) {
228  wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
229  return $diffMessage;
230  }
231  $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
232  $diffLink = "<a href=\"$diffUrl\">$diffMessage</a>";
233  } else {
234  $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
235  $diffLink = "<a href=\"$diffUrl\">$diffMessage</a>";
236  }
237 
238  return $diffLink;
239  }
240 
247  private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
248  $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
249  $lastMessage = $this->getMessage( 'last' );
250  $type = $cacheEntry->mAttribs['rc_type'];
251  $logTypes = [ RC_LOG ];
252 
253  // Make "last" link
254  if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
255  $lastLink = $lastMessage;
256  } else {
257  $lastLink = $this->linkRenderer->makeKnownLink(
258  $cacheEntry->getTitle(),
259  new HtmlArmor( $lastMessage ),
260  [],
261  $this->buildDiffQueryParams( $cacheEntry )
262  );
263  }
264 
265  return $lastLink;
266  }
267 
273  private function getUserLink( RecentChange $cacheEntry ) {
274  if ( ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
275  $userLink = ' <span class="history-deleted">' .
276  $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
277  } else {
278  $userLink = Linker::userLink(
279  $cacheEntry->mAttribs['rc_user'],
280  $cacheEntry->mAttribs['rc_user_text']
281  );
282  }
283 
284  return $userLink;
285  }
286 
292  private function getMessage( $key ) {
293  return $this->messages[$key];
294  }
295 
296 }
static newFromID($id, $flags=0)
Create a new Title from an article ID.
Definition: Title.php:402
const RC_CATEGORIZE
Definition: Defines.php:140
Utility class for creating new RC entries.
Interface for objects which can provide a MediaWiki context on request.
static userCan($rc, $field, User $user=null)
Determine if the current user is allowed to view a particular field of this revision, if it's marked as deleted.
buildLastLink(RecentChange $cacheEntry, $showDiffLinks)
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static getTitleFor($name, $subpage=false, $fragment= '')
Get a localised Title object for a specified special page name If you don't need a full Title object...
Definition: SpecialPage.php:82
static isDeleted($rc, $field)
Determine if said field of a revision is hidden.
newFromRecentChange(RecentChange $baseRC, $watched)
buildDiffQueryParams(RecentChange $recentChange)
const NS_SPECIAL
Definition: Defines.php:45
buildCurLink(RecentChange $cacheEntry, $showDiffLinks, $counter)
static newFromParent($rc)
getAttribute($name)
Get an attribute value.
Class to simplify the use of log pages.
Definition: LogPage.php:32
passed in as a query string parameter to the various URLs constructed here(i.e.$prevlink) $ldel you ll need to handle error messages
Definition: hooks.txt:1230
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
Class that generates HTML links for pages.
buildDiffLink(RecentChange $cacheEntry, $showDiffLinks, $counter)
__construct(IContextSource $context, $messages, LinkRenderer $linkRenderer)
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
const DELETED_TEXT
Definition: Revision.php:85
static userLink($userId, $userName, $altUserName=false)
Make user link (or user contributions for unregistered users)
Definition: Linker.php:984
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
const DELETED_USER
Definition: Revision.php:87
buildCLink(RecentChange $cacheEntry)
buildTimestamp(RecentChange $cacheEntry)
showDiffLinks(RecentChange $cacheEntry, User $user)
static userToolLinks($userId, $userText, $redContribsWhenNoEdits=false, $flags=0, $edits=null)
Generate standard user tool links (talk, contributions, block link, etc.)
Definition: Linker.php:1017
getUserLink(RecentChange $cacheEntry)
const RC_NEW
Definition: Defines.php:137
static isUnpatrolled($rc, User $user)
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2491
const RC_LOG
Definition: Defines.php:138
buildCurQueryParams(RecentChange $recentChange)