RCCacheEntryFactory.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. class RCCacheEntryFactory {
  24. /* @var IContextSource */
  25. private $context;
  26. /* @var string[] */
  27. private $messages;
  28. /**
  29. * @var LinkRenderer
  30. */
  31. private $linkRenderer;
  32. /**
  33. * @param IContextSource $context
  34. * @param string[] $messages
  35. * @param LinkRenderer $linkRenderer
  36. */
  37. public function __construct(
  38. IContextSource $context, $messages, LinkRenderer $linkRenderer
  39. ) {
  40. $this->context = $context;
  41. $this->messages = $messages;
  42. $this->linkRenderer = $linkRenderer;
  43. }
  44. /**
  45. * @param RecentChange $baseRC
  46. * @param bool $watched
  47. *
  48. * @return RCCacheEntry
  49. */
  50. public function newFromRecentChange( RecentChange $baseRC, $watched ) {
  51. $user = $this->context->getUser();
  52. $counter = $baseRC->counter;
  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, $counter );
  64. $cacheEntry->curlink = $this->buildCurLink( $cacheEntry, $showDiffLinks, $counter );
  65. $cacheEntry->lastlink = $this->buildLastLink( $cacheEntry, $showDiffLinks );
  66. // Make user links
  67. $cacheEntry->userlink = $this->getUserLink( $cacheEntry );
  68. if ( !ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
  69. $cacheEntry->usertalklink = Linker::userToolLinks(
  70. $cacheEntry->mAttribs['rc_user'],
  71. $cacheEntry->mAttribs['rc_user_text']
  72. );
  73. }
  74. return $cacheEntry;
  75. }
  76. /**
  77. * @param RecentChange $cacheEntry
  78. * @param User $user
  79. *
  80. * @return bool
  81. */
  82. private function showDiffLinks( RecentChange $cacheEntry, User $user ) {
  83. return ChangesList::userCan( $cacheEntry, Revision::DELETED_TEXT, $user );
  84. }
  85. /**
  86. * @param RecentChange $cacheEntry
  87. *
  88. * @return string
  89. */
  90. private function buildCLink( RecentChange $cacheEntry ) {
  91. $type = $cacheEntry->mAttribs['rc_type'];
  92. // New unpatrolled pages
  93. if ( $cacheEntry->unpatrolled && $type == RC_NEW ) {
  94. $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
  95. // Log entries
  96. } elseif ( $type == RC_LOG ) {
  97. $logType = $cacheEntry->mAttribs['rc_log_type'];
  98. if ( $logType ) {
  99. $clink = $this->getLogLink( $logType );
  100. } else {
  101. wfDebugLog( 'recentchanges', 'Unexpected log entry with no log type in recent changes' );
  102. $clink = $this->linkRenderer->makeLink( $cacheEntry->getTitle() );
  103. }
  104. // Log entries (old format) and special pages
  105. } elseif ( $cacheEntry->mAttribs['rc_namespace'] == NS_SPECIAL ) {
  106. wfDebugLog( 'recentchanges', 'Unexpected special page in recentchanges' );
  107. $clink = '';
  108. // Edits
  109. } else {
  110. $clink = $this->linkRenderer->makeKnownLink( $cacheEntry->getTitle() );
  111. }
  112. return $clink;
  113. }
  114. private function getLogLink( $logType ) {
  115. $logtitle = SpecialPage::getTitleFor( 'Log', $logType );
  116. $logpage = new LogPage( $logType );
  117. $logname = $logpage->getName()->text();
  118. $logLink = $this->context->msg( 'parentheses' )
  119. ->rawParams(
  120. $this->linkRenderer->makeKnownLink( $logtitle, $logname )
  121. )->escaped();
  122. return $logLink;
  123. }
  124. /**
  125. * @param RecentChange $cacheEntry
  126. *
  127. * @return string
  128. */
  129. private function buildTimestamp( RecentChange $cacheEntry ) {
  130. return $this->context->getLanguage()->userTime(
  131. $cacheEntry->mAttribs['rc_timestamp'],
  132. $this->context->getUser()
  133. );
  134. }
  135. /**
  136. * @param RecentChange $recentChange
  137. *
  138. * @return array
  139. */
  140. private function buildCurQueryParams( RecentChange $recentChange ) {
  141. return [
  142. 'curid' => $recentChange->mAttribs['rc_cur_id'],
  143. 'diff' => 0,
  144. 'oldid' => $recentChange->mAttribs['rc_this_oldid']
  145. ];
  146. }
  147. /**
  148. * @param RecentChange $cacheEntry
  149. * @param bool $showDiffLinks
  150. * @param int $counter
  151. *
  152. * @return string
  153. */
  154. private function buildCurLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
  155. $queryParams = $this->buildCurQueryParams( $cacheEntry );
  156. $curMessage = $this->getMessage( 'cur' );
  157. $logTypes = [ RC_LOG ];
  158. if ( !$showDiffLinks || in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
  159. $curLink = $curMessage;
  160. } else {
  161. $curUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
  162. $curLink = "<a class=\"mw-changeslist-diff-cur\" href=\"$curUrl\">$curMessage</a>";
  163. }
  164. return $curLink;
  165. }
  166. /**
  167. * @param RecentChange $recentChange
  168. *
  169. * @return array
  170. */
  171. private function buildDiffQueryParams( RecentChange $recentChange ) {
  172. return [
  173. 'curid' => $recentChange->mAttribs['rc_cur_id'],
  174. 'diff' => $recentChange->mAttribs['rc_this_oldid'],
  175. 'oldid' => $recentChange->mAttribs['rc_last_oldid']
  176. ];
  177. }
  178. /**
  179. * @param RecentChange $cacheEntry
  180. * @param bool $showDiffLinks
  181. * @param int $counter
  182. *
  183. * @return string
  184. */
  185. private function buildDiffLink( RecentChange $cacheEntry, $showDiffLinks, $counter ) {
  186. $queryParams = $this->buildDiffQueryParams( $cacheEntry );
  187. $diffMessage = $this->getMessage( 'diff' );
  188. $logTypes = [ RC_NEW, RC_LOG ];
  189. if ( !$showDiffLinks ) {
  190. $diffLink = $diffMessage;
  191. } elseif ( in_array( $cacheEntry->mAttribs['rc_type'], $logTypes ) ) {
  192. $diffLink = $diffMessage;
  193. } elseif ( $cacheEntry->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
  194. $rcCurId = $cacheEntry->getAttribute( 'rc_cur_id' );
  195. $pageTitle = Title::newFromID( $rcCurId );
  196. if ( $pageTitle === null ) {
  197. wfDebugLog( 'RCCacheEntryFactory', 'Could not get Title for rc_cur_id: ' . $rcCurId );
  198. return $diffMessage;
  199. }
  200. $diffUrl = htmlspecialchars( $pageTitle->getLinkURL( $queryParams ) );
  201. $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
  202. } else {
  203. $diffUrl = htmlspecialchars( $cacheEntry->getTitle()->getLinkURL( $queryParams ) );
  204. $diffLink = "<a class=\"mw-changeslist-diff\" href=\"$diffUrl\">$diffMessage</a>";
  205. }
  206. return $diffLink;
  207. }
  208. /**
  209. * Builds the link to the previous version
  210. *
  211. * @param RecentChange $cacheEntry
  212. * @param bool $showDiffLinks
  213. *
  214. * @return string
  215. */
  216. private function buildLastLink( RecentChange $cacheEntry, $showDiffLinks ) {
  217. $lastOldid = $cacheEntry->mAttribs['rc_last_oldid'];
  218. $lastMessage = $this->getMessage( 'last' );
  219. $type = $cacheEntry->mAttribs['rc_type'];
  220. $logTypes = [ RC_LOG ];
  221. // Make "last" link
  222. if ( !$showDiffLinks || !$lastOldid || in_array( $type, $logTypes ) ) {
  223. $lastLink = $lastMessage;
  224. } else {
  225. $lastLink = $this->linkRenderer->makeKnownLink(
  226. $cacheEntry->getTitle(),
  227. new HtmlArmor( $lastMessage ),
  228. [ 'class' => 'mw-changeslist-diff' ],
  229. $this->buildDiffQueryParams( $cacheEntry )
  230. );
  231. }
  232. return $lastLink;
  233. }
  234. /**
  235. * @param RecentChange $cacheEntry
  236. *
  237. * @return string
  238. */
  239. private function getUserLink( RecentChange $cacheEntry ) {
  240. if ( ChangesList::isDeleted( $cacheEntry, Revision::DELETED_USER ) ) {
  241. $userLink = ' <span class="history-deleted">' .
  242. $this->context->msg( 'rev-deleted-user' )->escaped() . '</span>';
  243. } else {
  244. $userLink = Linker::userLink(
  245. $cacheEntry->mAttribs['rc_user'],
  246. $cacheEntry->mAttribs['rc_user_text'],
  247. ExternalUserNames::getLocal( $cacheEntry->mAttribs['rc_user_text'] )
  248. );
  249. }
  250. return $userLink;
  251. }
  252. /**
  253. * @param string $key
  254. *
  255. * @return string
  256. */
  257. private function getMessage( $key ) {
  258. return $this->messages[$key];
  259. }
  260. }