ChangesList.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. <?php
  2. /**
  3. * Base class for all changes lists.
  4. *
  5. * The class is used for formatting recent changes, related changes and watchlist.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. */
  24. use MediaWiki\Linker\LinkRenderer;
  25. use MediaWiki\MediaWikiServices;
  26. use MediaWiki\Revision\RevisionRecord;
  27. use Wikimedia\Rdbms\IResultWrapper;
  28. class ChangesList extends ContextSource {
  29. const CSS_CLASS_PREFIX = 'mw-changeslist-';
  30. /**
  31. * @var Skin
  32. */
  33. public $skin;
  34. protected $watchlist = false;
  35. protected $lastdate;
  36. protected $message;
  37. protected $rc_cache;
  38. protected $rcCacheIndex;
  39. protected $rclistOpen;
  40. protected $rcMoveIndex;
  41. /** @var callable */
  42. protected $changeLinePrefixer;
  43. /** @var MapCacheLRU */
  44. protected $watchMsgCache;
  45. /**
  46. * @var LinkRenderer
  47. */
  48. protected $linkRenderer;
  49. /**
  50. * @var array
  51. */
  52. protected $filterGroups;
  53. /**
  54. * @param Skin|IContextSource $obj
  55. * @param array $filterGroups Array of ChangesListFilterGroup objects (currently optional)
  56. */
  57. public function __construct( $obj, array $filterGroups = [] ) {
  58. if ( $obj instanceof IContextSource ) {
  59. $this->setContext( $obj );
  60. $this->skin = $obj->getSkin();
  61. } else {
  62. $this->setContext( $obj->getContext() );
  63. $this->skin = $obj;
  64. }
  65. $this->preCacheMessages();
  66. $this->watchMsgCache = new MapCacheLRU( 50 );
  67. $this->linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
  68. $this->filterGroups = $filterGroups;
  69. }
  70. /**
  71. * Fetch an appropriate changes list class for the specified context
  72. * Some users might want to use an enhanced list format, for instance
  73. *
  74. * @param IContextSource $context
  75. * @param array $groups Array of ChangesListFilterGroup objects (currently optional)
  76. * @return ChangesList
  77. */
  78. public static function newFromContext( IContextSource $context, array $groups = [] ) {
  79. $user = $context->getUser();
  80. $sk = $context->getSkin();
  81. $list = null;
  82. if ( Hooks::run( 'FetchChangesList', [ $user, &$sk, &$list, $groups ] ) ) {
  83. $new = $context->getRequest()->getBool( 'enhanced', $user->getOption( 'usenewrc' ) );
  84. return $new ?
  85. new EnhancedChangesList( $context, $groups ) :
  86. new OldChangesList( $context, $groups );
  87. } else {
  88. return $list;
  89. }
  90. }
  91. /**
  92. * Format a line
  93. *
  94. * @since 1.27
  95. *
  96. * @param RecentChange &$rc Passed by reference
  97. * @param bool $watched (default false)
  98. * @param int|null $linenumber (default null)
  99. *
  100. * @return string|bool
  101. */
  102. public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
  103. throw new RuntimeException( 'recentChangesLine should be implemented' );
  104. }
  105. /**
  106. * Get the container for highlights that are used in the new StructuredFilters
  107. * system
  108. *
  109. * @return string HTML structure of the highlight container div
  110. */
  111. protected function getHighlightsContainerDiv() {
  112. $highlightColorDivs = '';
  113. foreach ( [ 'none', 'c1', 'c2', 'c3', 'c4', 'c5' ] as $color ) {
  114. $highlightColorDivs .= Html::rawElement(
  115. 'div',
  116. [
  117. 'class' => 'mw-rcfilters-ui-highlights-color-' . $color,
  118. 'data-color' => $color
  119. ]
  120. );
  121. }
  122. return Html::rawElement(
  123. 'div',
  124. [ 'class' => 'mw-rcfilters-ui-highlights' ],
  125. $highlightColorDivs
  126. );
  127. }
  128. /**
  129. * Sets the list to use a "<li class='watchlist-(namespace)-(page)'>" tag
  130. * @param bool $value
  131. */
  132. public function setWatchlistDivs( $value = true ) {
  133. $this->watchlist = $value;
  134. }
  135. /**
  136. * @return bool True when setWatchlistDivs has been called
  137. * @since 1.23
  138. */
  139. public function isWatchlist() {
  140. return (bool)$this->watchlist;
  141. }
  142. /**
  143. * As we use the same small set of messages in various methods and that
  144. * they are called often, we call them once and save them in $this->message
  145. */
  146. private function preCacheMessages() {
  147. if ( !isset( $this->message ) ) {
  148. $this->message = [];
  149. foreach ( [
  150. 'cur', 'diff', 'hist', 'enhancedrc-history', 'last', 'blocklink', 'history',
  151. 'semicolon-separator', 'pipe-separator' ] as $msg
  152. ) {
  153. $this->message[$msg] = $this->msg( $msg )->escaped();
  154. }
  155. }
  156. }
  157. /**
  158. * Returns the appropriate flags for new page, minor change and patrolling
  159. * @param array $flags Associative array of 'flag' => Bool
  160. * @param string $nothing To use for empty space
  161. * @return string
  162. */
  163. public function recentChangesFlags( $flags, $nothing = "\u{00A0}" ) {
  164. $f = '';
  165. foreach ( array_keys( $this->getConfig()->get( 'RecentChangesFlags' ) ) as $flag ) {
  166. $f .= isset( $flags[$flag] ) && $flags[$flag]
  167. ? self::flag( $flag, $this->getContext() )
  168. : $nothing;
  169. }
  170. return $f;
  171. }
  172. /**
  173. * Get an array of default HTML class attributes for the change.
  174. *
  175. * @param RecentChange|RCCacheEntry $rc
  176. * @param string|bool $watched Optionally timestamp for adding watched class
  177. *
  178. * @return string[] List of CSS class names
  179. */
  180. protected function getHTMLClasses( $rc, $watched ) {
  181. $classes = [ self::CSS_CLASS_PREFIX . 'line' ];
  182. $logType = $rc->mAttribs['rc_log_type'];
  183. if ( $logType ) {
  184. $classes[] = self::CSS_CLASS_PREFIX . 'log';
  185. $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'log-' . $logType );
  186. } else {
  187. $classes[] = self::CSS_CLASS_PREFIX . 'edit';
  188. $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns' .
  189. $rc->mAttribs['rc_namespace'] . '-' . $rc->mAttribs['rc_title'] );
  190. }
  191. // Indicate watched status on the line to allow for more
  192. // comprehensive styling.
  193. $classes[] = $watched && $rc->mAttribs['rc_timestamp'] >= $watched
  194. ? self::CSS_CLASS_PREFIX . 'line-watched'
  195. : self::CSS_CLASS_PREFIX . 'line-not-watched';
  196. $classes = array_merge( $classes, $this->getHTMLClassesForFilters( $rc ) );
  197. return $classes;
  198. }
  199. /**
  200. * Get an array of CSS classes attributed to filters for this row. Used for highlighting
  201. * in the front-end.
  202. *
  203. * @param RecentChange $rc
  204. * @return array Array of CSS classes
  205. */
  206. protected function getHTMLClassesForFilters( $rc ) {
  207. $classes = [];
  208. $classes[] = Sanitizer::escapeClass( self::CSS_CLASS_PREFIX . 'ns-' .
  209. $rc->mAttribs['rc_namespace'] );
  210. $nsInfo = MediaWikiServices::getInstance()->getNamespaceInfo();
  211. $classes[] = Sanitizer::escapeClass(
  212. self::CSS_CLASS_PREFIX .
  213. 'ns-' .
  214. ( $nsInfo->isTalk( $rc->mAttribs['rc_namespace'] ) ? 'talk' : 'subject' )
  215. );
  216. if ( $this->filterGroups !== null ) {
  217. foreach ( $this->filterGroups as $filterGroup ) {
  218. foreach ( $filterGroup->getFilters() as $filter ) {
  219. $filter->applyCssClassIfNeeded( $this, $rc, $classes );
  220. }
  221. }
  222. }
  223. return $classes;
  224. }
  225. /**
  226. * Make an "<abbr>" element for a given change flag. The flag indicating a new page, minor edit,
  227. * bot edit, or unpatrolled edit. In English it typically contains "N", "m", "b", or "!".
  228. *
  229. * @param string $flag One key of $wgRecentChangesFlags
  230. * @param IContextSource|null $context
  231. * @return string HTML
  232. */
  233. public static function flag( $flag, IContextSource $context = null ) {
  234. static $map = [ 'minoredit' => 'minor', 'botedit' => 'bot' ];
  235. static $flagInfos = null;
  236. if ( is_null( $flagInfos ) ) {
  237. global $wgRecentChangesFlags;
  238. $flagInfos = [];
  239. foreach ( $wgRecentChangesFlags as $key => $value ) {
  240. $flagInfos[$key]['letter'] = $value['letter'];
  241. $flagInfos[$key]['title'] = $value['title'];
  242. // Allow customized class name, fall back to flag name
  243. $flagInfos[$key]['class'] = $value['class'] ?? $key;
  244. }
  245. }
  246. $context = $context ?: RequestContext::getMain();
  247. // Inconsistent naming, kepted for b/c
  248. if ( isset( $map[$flag] ) ) {
  249. $flag = $map[$flag];
  250. }
  251. $info = $flagInfos[$flag];
  252. return Html::element( 'abbr', [
  253. 'class' => $info['class'],
  254. 'title' => wfMessage( $info['title'] )->setContext( $context )->text(),
  255. ], wfMessage( $info['letter'] )->setContext( $context )->text() );
  256. }
  257. /**
  258. * Returns text for the start of the tabular part of RC
  259. * @return string
  260. */
  261. public function beginRecentChangesList() {
  262. $this->rc_cache = [];
  263. $this->rcMoveIndex = 0;
  264. $this->rcCacheIndex = 0;
  265. $this->lastdate = '';
  266. $this->rclistOpen = false;
  267. $this->getOutput()->addModuleStyles( [
  268. 'mediawiki.interface.helpers.styles',
  269. 'mediawiki.special.changeslist'
  270. ] );
  271. return '<div class="mw-changeslist">';
  272. }
  273. /**
  274. * @param IResultWrapper|array $rows
  275. */
  276. public function initChangesListRows( $rows ) {
  277. Hooks::run( 'ChangesListInitRows', [ $this, $rows ] );
  278. }
  279. /**
  280. * Show formatted char difference
  281. *
  282. * Needs the css module 'mediawiki.special.changeslist' to style output
  283. *
  284. * @param int $old Number of bytes
  285. * @param int $new Number of bytes
  286. * @param IContextSource|null $context
  287. * @return string
  288. */
  289. public static function showCharacterDifference( $old, $new, IContextSource $context = null ) {
  290. if ( !$context ) {
  291. $context = RequestContext::getMain();
  292. }
  293. $new = (int)$new;
  294. $old = (int)$old;
  295. $szdiff = $new - $old;
  296. $lang = $context->getLanguage();
  297. $config = $context->getConfig();
  298. $code = $lang->getCode();
  299. static $fastCharDiff = [];
  300. if ( !isset( $fastCharDiff[$code] ) ) {
  301. $fastCharDiff[$code] = $config->get( 'MiserMode' )
  302. || $context->msg( 'rc-change-size' )->plain() === '$1';
  303. }
  304. $formattedSize = $lang->formatNum( $szdiff );
  305. if ( !$fastCharDiff[$code] ) {
  306. $formattedSize = $context->msg( 'rc-change-size', $formattedSize )->text();
  307. }
  308. if ( abs( $szdiff ) > abs( $config->get( 'RCChangedSizeThreshold' ) ) ) {
  309. $tag = 'strong';
  310. } else {
  311. $tag = 'span';
  312. }
  313. if ( $szdiff === 0 ) {
  314. $formattedSizeClass = 'mw-plusminus-null';
  315. } elseif ( $szdiff > 0 ) {
  316. $formattedSize = '+' . $formattedSize;
  317. $formattedSizeClass = 'mw-plusminus-pos';
  318. } else {
  319. $formattedSizeClass = 'mw-plusminus-neg';
  320. }
  321. $formattedSizeClass .= ' mw-diff-bytes';
  322. $formattedTotalSize = $context->msg( 'rc-change-size-new' )->numParams( $new )->text();
  323. return Html::element( $tag,
  324. [ 'dir' => 'ltr', 'class' => $formattedSizeClass, 'title' => $formattedTotalSize ],
  325. $formattedSize ) . $lang->getDirMark();
  326. }
  327. /**
  328. * Format the character difference of one or several changes.
  329. *
  330. * @param RecentChange $old
  331. * @param RecentChange|null $new Last change to use, if not provided, $old will be used
  332. * @return string HTML fragment
  333. */
  334. public function formatCharacterDifference( RecentChange $old, RecentChange $new = null ) {
  335. $oldlen = $old->mAttribs['rc_old_len'];
  336. if ( $new ) {
  337. $newlen = $new->mAttribs['rc_new_len'];
  338. } else {
  339. $newlen = $old->mAttribs['rc_new_len'];
  340. }
  341. if ( $oldlen === null || $newlen === null ) {
  342. return '';
  343. }
  344. return self::showCharacterDifference( $oldlen, $newlen, $this->getContext() );
  345. }
  346. /**
  347. * Returns text for the end of RC
  348. * @return string
  349. */
  350. public function endRecentChangesList() {
  351. $out = $this->rclistOpen ? "</ul>\n" : '';
  352. $out .= '</div>';
  353. return $out;
  354. }
  355. /**
  356. * Render the date and time of a revision in the current user language
  357. * based on whether the user is able to view this information or not.
  358. * @param Revision $rev
  359. * @param User $user
  360. * @param Language $lang
  361. * @param Title|null $title (optional) where Title does not match
  362. * the Title associated with the Revision
  363. * @internal For usage by Pager classes only (e.g. HistoryPager and ContribsPager).
  364. * @return string HTML
  365. */
  366. public static function revDateLink( Revision $rev, User $user, Language $lang, $title = null ) {
  367. $ts = $rev->getTimestamp();
  368. $date = $lang->userTimeAndDate( $ts, $user );
  369. if ( $rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
  370. $link = MediaWikiServices::getInstance()->getLinkRenderer()->makeKnownLink(
  371. $title ?? $rev->getTitle(),
  372. $date,
  373. [ 'class' => 'mw-changeslist-date' ],
  374. [ 'oldid' => $rev->getId() ]
  375. );
  376. } else {
  377. $link = htmlspecialchars( $date );
  378. }
  379. if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
  380. $link = "<span class=\"history-deleted mw-changeslist-date\">$link</span>";
  381. }
  382. return $link;
  383. }
  384. /**
  385. * @param string &$s HTML to update
  386. * @param mixed $rc_timestamp
  387. */
  388. public function insertDateHeader( &$s, $rc_timestamp ) {
  389. # Make date header if necessary
  390. $date = $this->getLanguage()->userDate( $rc_timestamp, $this->getUser() );
  391. if ( $date != $this->lastdate ) {
  392. if ( $this->lastdate != '' ) {
  393. $s .= "</ul>\n";
  394. }
  395. $s .= Xml::element( 'h4', null, $date ) . "\n<ul class=\"special\">";
  396. $this->lastdate = $date;
  397. $this->rclistOpen = true;
  398. }
  399. }
  400. /**
  401. * @param string &$s HTML to update
  402. * @param Title $title
  403. * @param string $logtype
  404. * @param bool $useParentheses (optional) Wrap log entry in parentheses where needed
  405. */
  406. public function insertLog( &$s, $title, $logtype, $useParentheses = true ) {
  407. $page = new LogPage( $logtype );
  408. $logname = $page->getName()->setContext( $this->getContext() )->text();
  409. $link = $this->linkRenderer->makeKnownLink( $title, $logname, [
  410. 'class' => $useParentheses ? '' : 'mw-changeslist-links'
  411. ] );
  412. if ( $useParentheses ) {
  413. $s .= $this->msg( 'parentheses' )->rawParams(
  414. $link
  415. )->escaped();
  416. } else {
  417. $s .= $link;
  418. }
  419. }
  420. /**
  421. * @param string &$s HTML to update
  422. * @param RecentChange &$rc
  423. * @param bool|null $unpatrolled Unused variable, since 1.27.
  424. */
  425. public function insertDiffHist( &$s, &$rc, $unpatrolled = null ) {
  426. # Diff link
  427. if (
  428. $rc->mAttribs['rc_type'] == RC_NEW ||
  429. $rc->mAttribs['rc_type'] == RC_LOG ||
  430. $rc->mAttribs['rc_type'] == RC_CATEGORIZE
  431. ) {
  432. $diffLink = $this->message['diff'];
  433. } elseif ( !self::userCan( $rc, RevisionRecord::DELETED_TEXT, $this->getUser() ) ) {
  434. $diffLink = $this->message['diff'];
  435. } else {
  436. $query = [
  437. 'curid' => $rc->mAttribs['rc_cur_id'],
  438. 'diff' => $rc->mAttribs['rc_this_oldid'],
  439. 'oldid' => $rc->mAttribs['rc_last_oldid']
  440. ];
  441. $diffLink = $this->linkRenderer->makeKnownLink(
  442. $rc->getTitle(),
  443. new HtmlArmor( $this->message['diff'] ),
  444. [ 'class' => 'mw-changeslist-diff' ],
  445. $query
  446. );
  447. }
  448. if ( $rc->mAttribs['rc_type'] == RC_CATEGORIZE ) {
  449. $histLink = $this->message['hist'];
  450. } else {
  451. $histLink = $this->linkRenderer->makeKnownLink(
  452. $rc->getTitle(),
  453. new HtmlArmor( $this->message['hist'] ),
  454. [ 'class' => 'mw-changeslist-history' ],
  455. [
  456. 'curid' => $rc->mAttribs['rc_cur_id'],
  457. 'action' => 'history'
  458. ]
  459. );
  460. }
  461. $s .= Html::rawElement( 'div', [ 'class' => 'mw-changeslist-links' ],
  462. Html::rawElement( 'span', [], $diffLink ) .
  463. Html::rawElement( 'span', [], $histLink )
  464. ) .
  465. ' <span class="mw-changeslist-separator"></span> ';
  466. }
  467. /**
  468. * @param RecentChange &$rc
  469. * @param bool $unpatrolled
  470. * @param bool $watched
  471. * @return string HTML
  472. * @since 1.26
  473. */
  474. public function getArticleLink( &$rc, $unpatrolled, $watched ) {
  475. $params = [];
  476. if ( $rc->getTitle()->isRedirect() ) {
  477. $params = [ 'redirect' => 'no' ];
  478. }
  479. $articlelink = $this->linkRenderer->makeLink(
  480. $rc->getTitle(),
  481. null,
  482. [ 'class' => 'mw-changeslist-title' ],
  483. $params
  484. );
  485. if ( $this->isDeleted( $rc, RevisionRecord::DELETED_TEXT ) ) {
  486. $articlelink = '<span class="history-deleted">' . $articlelink . '</span>';
  487. }
  488. # To allow for boldening pages watched by this user
  489. $articlelink = "<span class=\"mw-title\">{$articlelink}</span>";
  490. # RTL/LTR marker
  491. $articlelink .= $this->getLanguage()->getDirMark();
  492. # TODO: Deprecate the $s argument, it seems happily unused.
  493. $s = '';
  494. # Avoid PHP 7.1 warning from passing $this by reference
  495. $changesList = $this;
  496. Hooks::run( 'ChangesListInsertArticleLink',
  497. [ &$changesList, &$articlelink, &$s, &$rc, $unpatrolled, $watched ] );
  498. return "{$s} {$articlelink}";
  499. }
  500. /**
  501. * Get the timestamp from $rc formatted with current user's settings
  502. * and a separator
  503. *
  504. * @param RecentChange $rc
  505. * @deprecated use revDateLink instead.
  506. * @return string HTML fragment
  507. */
  508. public function getTimestamp( $rc ) {
  509. // @todo FIXME: Hard coded ". .". Is there a message for this? Should there be?
  510. return $this->message['semicolon-separator'] . '<span class="mw-changeslist-date">' .
  511. htmlspecialchars( $this->getLanguage()->userTime(
  512. $rc->mAttribs['rc_timestamp'],
  513. $this->getUser()
  514. ) ) . '</span> <span class="mw-changeslist-separator"></span> ';
  515. }
  516. /**
  517. * Insert time timestamp string from $rc into $s
  518. *
  519. * @param string &$s HTML to update
  520. * @param RecentChange $rc
  521. */
  522. public function insertTimestamp( &$s, $rc ) {
  523. $s .= $this->getTimestamp( $rc );
  524. }
  525. /**
  526. * Insert links to user page, user talk page and eventually a blocking link
  527. *
  528. * @param string &$s HTML to update
  529. * @param RecentChange &$rc
  530. */
  531. public function insertUserRelatedLinks( &$s, &$rc ) {
  532. if ( $this->isDeleted( $rc, RevisionRecord::DELETED_USER ) ) {
  533. $s .= ' <span class="history-deleted">' .
  534. $this->msg( 'rev-deleted-user' )->escaped() . '</span>';
  535. } else {
  536. $s .= $this->getLanguage()->getDirMark() . Linker::userLink( $rc->mAttribs['rc_user'],
  537. $rc->mAttribs['rc_user_text'] );
  538. $s .= Linker::userToolLinks(
  539. $rc->mAttribs['rc_user'], $rc->mAttribs['rc_user_text'],
  540. false, 0, null,
  541. // The text content of tools is not wrapped with parenthesises or "piped".
  542. // This will be handled in CSS (T205581).
  543. false
  544. );
  545. }
  546. }
  547. /**
  548. * Insert a formatted action
  549. *
  550. * @param RecentChange $rc
  551. * @return string
  552. */
  553. public function insertLogEntry( $rc ) {
  554. $formatter = LogFormatter::newFromRow( $rc->mAttribs );
  555. $formatter->setContext( $this->getContext() );
  556. $formatter->setShowUserToolLinks( true );
  557. $mark = $this->getLanguage()->getDirMark();
  558. return Html::openElement( 'span', [ 'class' => 'mw-changeslist-log-entry' ] )
  559. . $formatter->getActionText() . " $mark" . $formatter->getComment()
  560. . Html::closeElement( 'span' );
  561. }
  562. /**
  563. * Insert a formatted comment
  564. * @param RecentChange $rc
  565. * @return string
  566. */
  567. public function insertComment( $rc ) {
  568. if ( $this->isDeleted( $rc, RevisionRecord::DELETED_COMMENT ) ) {
  569. return ' <span class="history-deleted comment">' .
  570. $this->msg( 'rev-deleted-comment' )->escaped() . '</span>';
  571. } else {
  572. return Linker::commentBlock( $rc->mAttribs['rc_comment'], $rc->getTitle(),
  573. // Whether section links should refer to local page (using default false)
  574. false,
  575. // wikid to generate links for (using default null) */
  576. null,
  577. // whether parentheses should be rendered as part of the message
  578. false );
  579. }
  580. }
  581. /**
  582. * Returns the string which indicates the number of watching users
  583. * @param int $count Number of user watching a page
  584. * @return string
  585. */
  586. protected function numberofWatchingusers( $count ) {
  587. if ( $count <= 0 ) {
  588. return '';
  589. }
  590. return $this->watchMsgCache->getWithSetCallback(
  591. "watching-users-msg:$count",
  592. function () use ( $count ) {
  593. return $this->msg( 'number-of-watching-users-for-recent-changes' )
  594. ->numParams( $count )->escaped();
  595. }
  596. );
  597. }
  598. /**
  599. * Determine if said field of a revision is hidden
  600. * @param RCCacheEntry|RecentChange $rc
  601. * @param int $field One of DELETED_* bitfield constants
  602. * @return bool
  603. */
  604. public static function isDeleted( $rc, $field ) {
  605. return ( $rc->mAttribs['rc_deleted'] & $field ) == $field;
  606. }
  607. /**
  608. * Determine if the current user is allowed to view a particular
  609. * field of this revision, if it's marked as deleted.
  610. * @param RCCacheEntry|RecentChange $rc
  611. * @param int $field
  612. * @param User|null $user User object to check against. If null, the global RequestContext's
  613. * User is assumed instead.
  614. * @return bool
  615. */
  616. public static function userCan( $rc, $field, User $user = null ) {
  617. if ( $user === null ) {
  618. $user = RequestContext::getMain()->getUser();
  619. }
  620. if ( $rc->mAttribs['rc_type'] == RC_LOG ) {
  621. return LogEventsList::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
  622. }
  623. return RevisionRecord::userCanBitfield( $rc->mAttribs['rc_deleted'], $field, $user );
  624. }
  625. /**
  626. * @param string $link
  627. * @param bool $watched
  628. * @return string
  629. */
  630. protected function maybeWatchedLink( $link, $watched = false ) {
  631. if ( $watched ) {
  632. return '<strong class="mw-watched">' . $link . '</strong>';
  633. } else {
  634. return '<span class="mw-rc-unwatched">' . $link . '</span>';
  635. }
  636. }
  637. /**
  638. * Insert a rollback link
  639. *
  640. * @param string &$s
  641. * @param RecentChange &$rc
  642. */
  643. public function insertRollback( &$s, &$rc ) {
  644. if ( $rc->mAttribs['rc_type'] == RC_EDIT
  645. && $rc->mAttribs['rc_this_oldid']
  646. && $rc->mAttribs['rc_cur_id']
  647. && $rc->getAttribute( 'page_latest' ) == $rc->mAttribs['rc_this_oldid']
  648. ) {
  649. $title = $rc->getTitle();
  650. /** Check for rollback permissions, disallow special pages, and only
  651. * show a link on the top-most revision
  652. */
  653. if ( MediaWikiServices::getInstance()->getPermissionManager()
  654. ->quickUserCan( 'rollback', $this->getUser(), $title )
  655. ) {
  656. $rev = new Revision( [
  657. 'title' => $title,
  658. 'id' => $rc->mAttribs['rc_this_oldid'],
  659. 'user' => $rc->mAttribs['rc_user'],
  660. 'user_text' => $rc->mAttribs['rc_user_text'],
  661. 'actor' => $rc->mAttribs['rc_actor'] ?? null,
  662. 'deleted' => $rc->mAttribs['rc_deleted']
  663. ] );
  664. $s .= ' ' . Linker::generateRollback( $rev, $this->getContext(),
  665. [ 'noBrackets' ] );
  666. }
  667. }
  668. }
  669. /**
  670. * @param RecentChange $rc
  671. * @return string
  672. * @since 1.26
  673. */
  674. public function getRollback( RecentChange $rc ) {
  675. $s = '';
  676. $this->insertRollback( $s, $rc );
  677. return $s;
  678. }
  679. /**
  680. * @param string &$s
  681. * @param RecentChange &$rc
  682. * @param array &$classes
  683. */
  684. public function insertTags( &$s, &$rc, &$classes ) {
  685. if ( empty( $rc->mAttribs['ts_tags'] ) ) {
  686. return;
  687. }
  688. list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
  689. $rc->mAttribs['ts_tags'],
  690. 'changeslist',
  691. $this->getContext()
  692. );
  693. $classes = array_merge( $classes, $newClasses );
  694. $s .= ' ' . $tagSummary;
  695. }
  696. /**
  697. * @param RecentChange $rc
  698. * @param array &$classes
  699. * @return string
  700. * @since 1.26
  701. */
  702. public function getTags( RecentChange $rc, array &$classes ) {
  703. $s = '';
  704. $this->insertTags( $s, $rc, $classes );
  705. return $s;
  706. }
  707. public function insertExtra( &$s, &$rc, &$classes ) {
  708. // Empty, used for subclasses to add anything special.
  709. }
  710. protected function showAsUnpatrolled( RecentChange $rc ) {
  711. return self::isUnpatrolled( $rc, $this->getUser() );
  712. }
  713. /**
  714. * @param object|RecentChange $rc Database row from recentchanges or a RecentChange object
  715. * @param User $user
  716. * @return bool
  717. */
  718. public static function isUnpatrolled( $rc, User $user ) {
  719. if ( $rc instanceof RecentChange ) {
  720. $isPatrolled = $rc->mAttribs['rc_patrolled'];
  721. $rcType = $rc->mAttribs['rc_type'];
  722. $rcLogType = $rc->mAttribs['rc_log_type'];
  723. } else {
  724. $isPatrolled = $rc->rc_patrolled;
  725. $rcType = $rc->rc_type;
  726. $rcLogType = $rc->rc_log_type;
  727. }
  728. if ( !$isPatrolled ) {
  729. if ( $user->useRCPatrol() ) {
  730. return true;
  731. }
  732. if ( $user->useNPPatrol() && $rcType == RC_NEW ) {
  733. return true;
  734. }
  735. if ( $user->useFilePatrol() && $rcLogType == 'upload' ) {
  736. return true;
  737. }
  738. }
  739. return false;
  740. }
  741. /**
  742. * Determines whether a revision is linked to this change; this may not be the case
  743. * when the categorization wasn't done by an edit but a conditional parser function
  744. *
  745. * @since 1.27
  746. *
  747. * @param RecentChange|RCCacheEntry $rcObj
  748. * @return bool
  749. */
  750. protected function isCategorizationWithoutRevision( $rcObj ) {
  751. return intval( $rcObj->getAttribute( 'rc_type' ) ) === RC_CATEGORIZE
  752. && intval( $rcObj->getAttribute( 'rc_this_oldid' ) ) === 0;
  753. }
  754. /**
  755. * Get recommended data attributes for a change line.
  756. * @param RecentChange $rc
  757. * @return string[] attribute name => value
  758. */
  759. protected function getDataAttributes( RecentChange $rc ) {
  760. $attrs = [];
  761. $type = $rc->getAttribute( 'rc_source' );
  762. switch ( $type ) {
  763. case RecentChange::SRC_EDIT:
  764. case RecentChange::SRC_NEW:
  765. $attrs['data-mw-revid'] = $rc->mAttribs['rc_this_oldid'];
  766. break;
  767. case RecentChange::SRC_LOG:
  768. $attrs['data-mw-logid'] = $rc->mAttribs['rc_logid'];
  769. $attrs['data-mw-logaction'] =
  770. $rc->mAttribs['rc_log_type'] . '/' . $rc->mAttribs['rc_log_action'];
  771. break;
  772. }
  773. $attrs[ 'data-mw-ts' ] = $rc->getAttribute( 'rc_timestamp' );
  774. return $attrs;
  775. }
  776. /**
  777. * Sets the callable that generates a change line prefix added to the beginning of each line.
  778. *
  779. * @param callable $prefixer Callable to run that generates the change line prefix.
  780. * Takes three parameters: a RecentChange object, a ChangesList object,
  781. * and whether the current entry is a grouped entry.
  782. */
  783. public function setChangeLinePrefixer( callable $prefixer ) {
  784. $this->changeLinePrefixer = $prefixer;
  785. }
  786. }