RCCacheEntryFactory.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. <?php
  2. /**
  3. * Creates a RCCacheEntry from a RecentChange to use in EnhancedChangesList
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. use MediaWiki\Linker\LinkRenderer;
  23. use MediaWiki\Revision\RevisionRecord;
  24. class RCCacheEntryFactory {
  25. /* @var IContextSource */
  26. private $context;
  27. /* @var string[] */
  28. private $messages;
  29. /**
  30. * @var LinkRenderer
  31. */
  32. private $linkRenderer;
  33. /**
  34. * @param IContextSource $context
  35. * @param string[] $messages
  36. * @param LinkRenderer $linkRenderer
  37. */
  38. public function __construct(
  39. IContextSource $context, $messages, LinkRenderer $linkRenderer
  40. ) {
  41. $this->context = $context;
  42. $this->messages = $messages;
  43. $this->linkRenderer = $linkRenderer;
  44. }
  45. /**
  46. * @param RecentChange $baseRC
  47. * @param bool $watched
  48. *
  49. * @return RCCacheEntry
  50. */
  51. public function newFromRecentChange( RecentChange $baseRC, $watched ) {
  52. $user = $this->context->getUser();
  53. $cacheEntry = RCCacheEntry::newFromParent( $baseRC );
  54. // Should patrol-related stuff be shown?
  55. $cacheEntry->unpatrolled = ChangesList::isUnpatrolled( $baseRC, $user );
  56. $cacheEntry->watched = $cacheEntry->mAttribs['rc_type'] == RC_LOG ? false : $watched;
  57. $cacheEntry->numberofWatchingusers = $baseRC->numberofWatchingusers;
  58. $cacheEntry->link = $this->buildCLink( $cacheEntry );
  59. $cacheEntry->timestamp = $this->buildTimestamp( $cacheEntry );
  60. // Make "cur" and "diff" links. Do not use link(), it is too slow if
  61. // called too many times (50% of CPU time on RecentChanges!).
  62. $showDiffLinks = $this->showDiffLinks( $cacheEntry, $user );
  63. $cacheEntry->difflink = $this->buildDiffLink( $cacheEntry, $showDiffLinks );
  64. $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks );
  65. $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
  66. // Make user links
  67. $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
  68. if ( !ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
  69. $cacheEntry->usertalklink = Linker::userToolLinks(
  70. $cacheEntry->mAttribs['rc_user'],
  71. $cacheEntry->mAttribs['rc_user_text'],
  72. // Should the contributions link be red if the user has no edits (using default)
  73. false,
  74. // Customisation flags (using default 0)
  75. 0,
  76. // User edit count (using default )
  77. null,
  78. // do not wrap the message in parentheses
  79. false
  80. );
  81. }
  82. return $cacheEntry;
  83. }
  84. /**
  85. * @param RecentChange $cacheEntry
  86. * @param User $user
  87. *
  88. * @return bool
  89. */
  90. private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
  91. return ChangesList::userCan( $cacheEntry, RevisionRecord::DELETED_TEXT, $user );
  92. }
  93. /**
  94. * @param RCCacheEntry $cacheEntry
  95. *
  96. * @return string
  97. */
  98. private function buildCLink( RCCacheEntry $cacheEntry ) {
  99. $type = $cacheEntry->mAttribs['rc_type'];
  100. // New unpatrolled pages
  101. if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
  102. $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
  103. // Log entries
  104. } elseif ( $type == RC_LOG ) {
  105. $logType = $cacheEntry->mAttribs['rc_log_type'];
  106. if ( $logType ) {
  107. $clink = $this->getLogLink( $logType );
  108. } else {
  109. wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
  110. $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
  111. }
  112. // Log entries (old format) and special pages
  113. } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
  114. wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
  115. $clink = '';
  116. // Edits
  117. } else {
  118. $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
  119. }
  120. return $clink;
  121. }
  122. private function getLogLink( $logType ) {
  123. $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
  124. $logpage = new LogPage( $logType );
  125. $logname = $logpage->getName()->text();
  126. $logLink = $this->context->msg( 'parentheses' )
  127. ->rawParams(
  128. $this->linkRenderer->makeKnownLink( $logtitle, $logname )
  129. )->escaped();
  130. return $logLink;
  131. }
  132. /**
  133. * @param RecentChange $cacheEntry
  134. *
  135. * @return string
  136. */
  137. private function buildTimestamp( RecentChange $cacheEntry ) {
  138. return $this->context->getLanguage()->userTime(
  139. $cacheEntry->mAttribs['rc_timestamp'],
  140. $this->context->getUser()
  141. );
  142. }
  143. /**
  144. * @param RecentChange $recentChange
  145. *
  146. * @return array
  147. */
  148. private function buildCurQueryParams( RecentChange $recentChange ) {
  149. return [
  150. 'curid' => $recentChange->mAttribs['rc_cur_id'],
  151. 'diff' => 0,
  152. 'oldid' => $recentChange->mAttribs['rc_this_oldid']
  153. ];
  154. }
  155. /**
  156. * @param RecentChange $cacheEntry
  157. * @param bool $showDiffLinks
  158. *
  159. * @return string
  160. */
  161. private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks ) {
  162. $queryParams = $this->buildCurQueryParams( $cacheEntry );
  163. $curMessage = $this->getMessage( 'cur' );
  164. $logTypes = [ RC_LOG ];
  165. if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
  166. $curLink = $curMessage;
  167. } else {
  168. $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
  169. $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
  170. }
  171. return $curLink;
  172. }
  173. /**
  174. * @param RecentChange $recentChange
  175. *
  176. * @return array
  177. */
  178. private function buildDiffQueryParams( RecentChange $recentChange ) {
  179. return [
  180. 'curid' => $recentChange->mAttribs['rc_cur_id'],
  181. 'diff' => $recentChange->mAttribs['rc_this_oldid'],
  182. 'oldid' => $recentChange->mAttribs['rc_last_oldid']
  183. ];
  184. }
  185. /**
  186. * @param RecentChange $cacheEntry
  187. * @param bool $showDiffLinks
  188. *
  189. * @return string
  190. */
  191. private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks ) {
  192. $queryParams = $this->buildDiffQueryParams( $cacheEntry );
  193. $diffMessage = $this->getMessage( 'diff' );
  194. $logTypes = [ RC_NEW, RC_LOG ];
  195. if ( !$showDiffLinks ) {
  196. $diffLink = $diffMessage;
  197. } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
  198. $diffLink = $diffMessage;
  199. } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
  200. $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
  201. $pageTitle = Title::newFromID( $rcCurId );
  202. if ( $pageTitle === null ) {
  203. wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
  204. return $diffMessage;
  205. }
  206. $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
  207. $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
  208. } else {
  209. $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
  210. $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
  211. }
  212. return $diffLink;
  213. }
  214. /**
  215. * Builds the link to the previous version
  216. *
  217. * @param RecentChange $cacheEntry
  218. * @param bool $showDiffLinks
  219. *
  220. * @return string
  221. */
  222. private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
  223. $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
  224. $lastMessage = $this->getMessage( 'last' );
  225. $type = $cacheEntry->mAttribs['rc_type'];
  226. $logTypes = [ RC_LOG ];
  227. // Make "last" link
  228. if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
  229. $lastLink = $lastMessage;
  230. } else {
  231. $lastLink = $this->linkRenderer->makeKnownLink(
  232. $cacheEntry->getTitle(),
  233. new HtmlArmor( $lastMessage ),
  234. [ 'class' => 'mw-changeslist-diff' ],
  235. $this->buildDiffQueryParams( $cacheEntry )
  236. );
  237. }
  238. return $lastLink;
  239. }
  240. /**
  241. * @param RecentChange $cacheEntry
  242. *
  243. * @return string
  244. */
  245. private function getUserLink( RecentChange $cacheEntry ) {
  246. if ( ChangesList::isDeleted( $cacheEntry, RevisionRecord::DELETED_USER ) ) {
  247. $userLink = ' <span class="history-deleted">' .
  248. $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
  249. } else {
  250. $userLink = Linker::userLink(
  251. $cacheEntry->mAttribs['rc_user'],
  252. $cacheEntry->mAttribs['rc_user_text'],
  253. ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
  254. );
  255. }
  256. return $userLink;
  257. }
  258. /**
  259. * @param string $key
  260. *
  261. * @return string
  262. */
  263. private function getMessage( $key ) {
  264. return $this->messages[$key];
  265. }
  266. }