EnhancedChangesList.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. use MediaWiki\Revision\RevisionRecord;
  3. /**
  4. * Generates a list of changes using an Enhanced system (uses javascript).
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. * http://www.gnu.org/copyleft/gpl.html
  20. *
  21. * @file
  22. */
  23. class EnhancedChangesList extends ChangesList {
  24. /**
  25. * @var RCCacheEntryFactory
  26. */
  27. protected $cacheEntryFactory;
  28. /**
  29. * @var array Array of array of RCCacheEntry
  30. */
  31. protected $rc_cache;
  32. /**
  33. * @var TemplateParser
  34. */
  35. protected $templateParser;
  36. /**
  37. * @param IContextSource|Skin $obj
  38. * @param array $filterGroups Array of ChangesListFilterGroup objects (currently optional)
  39. * @throws MWException
  40. */
  41. public function __construct( $obj, array $filterGroups = [] ) {
  42. if ( $obj instanceof Skin ) {
  43. // @todo: deprecate constructing with Skin
  44. $context = $obj->getContext();
  45. } else {
  46. if ( !$obj instanceof IContextSource ) {
  47. throw new MWException( 'EnhancedChangesList must be constructed with a '
  48. . 'context source or skin.' );
  49. }
  50. $context = $obj;
  51. }
  52. parent::__construct( $context, $filterGroups );
  53. // message is set by the parent ChangesList class
  54. $this->cacheEntryFactory = new RCCacheEntryFactory(
  55. $context,
  56. $this->message,
  57. $this->linkRenderer
  58. );
  59. $this->templateParser = new TemplateParser();
  60. }
  61. /**
  62. * Add the JavaScript file for enhanced changeslist
  63. * @return string
  64. */
  65. public function beginRecentChangesList() {
  66. $this->rc_cache = [];
  67. $this->rcMoveIndex = 0;
  68. $this->rcCacheIndex = 0;
  69. $this->lastdate = '';
  70. $this->rclistOpen = false;
  71. $this->getOutput()->addModuleStyles( [
  72. 'mediawiki.icon',
  73. 'mediawiki.interface.helpers.styles',
  74. 'mediawiki.special.changeslist',
  75. 'mediawiki.special.changeslist.enhanced',
  76. ] );
  77. $this->getOutput()->addModules( [
  78. 'jquery.makeCollapsible',
  79. ] );
  80. return '<div class="mw-changeslist">';
  81. }
  82. /**
  83. * Format a line for enhanced recentchange (aka with javascript and block of lines).
  84. *
  85. * @param RecentChange &$rc
  86. * @param bool $watched
  87. * @param int|null $linenumber (default null)
  88. *
  89. * @return string
  90. */
  91. public function recentChangesLine( &$rc, $watched = false, $linenumber = null ) {
  92. $date = $this->getLanguage()->userDate(
  93. $rc->mAttribs['rc_timestamp'],
  94. $this->getUser()
  95. );
  96. if ( $this->lastdate === '' ) {
  97. $this->lastdate = $date;
  98. }
  99. $ret = '';
  100. # If it's a new day, flush the cache and update $this->lastdate
  101. if ( $date !== $this->lastdate ) {
  102. # Process current cache (uses $this->lastdate to generate a heading)
  103. $ret = $this->recentChangesBlock();
  104. $this->rc_cache = [];
  105. $this->lastdate = $date;
  106. }
  107. $cacheEntry = $this->cacheEntryFactory->newFromRecentChange( $rc, $watched );
  108. $this->addCacheEntry( $cacheEntry );
  109. return $ret;
  110. }
  111. /**
  112. * Put accumulated information into the cache, for later display.
  113. * Page moves go on their own line.
  114. *
  115. * @param RCCacheEntry $cacheEntry
  116. */
  117. protected function addCacheEntry( RCCacheEntry $cacheEntry ) {
  118. $cacheGroupingKey = $this->makeCacheGroupingKey( $cacheEntry );
  119. if ( !isset( $this->rc_cache[$cacheGroupingKey] ) ) {
  120. $this->rc_cache[$cacheGroupingKey] = [];
  121. }
  122. array_push( $this->rc_cache[$cacheGroupingKey], $cacheEntry );
  123. }
  124. /**
  125. * @todo use rc_source to group, if set; fallback to rc_type
  126. *
  127. * @param RCCacheEntry $cacheEntry
  128. *
  129. * @return string
  130. */
  131. protected function makeCacheGroupingKey( RCCacheEntry $cacheEntry ) {
  132. $title = $cacheEntry->getTitle();
  133. $cacheGroupingKey = $title->getPrefixedDBkey();
  134. $type = $cacheEntry->mAttribs['rc_type'];
  135. if ( $type == RC_LOG ) {
  136. // Group by log type
  137. $cacheGroupingKey = SpecialPage::getTitleFor(
  138. 'Log',
  139. $cacheEntry->mAttribs['rc_log_type']
  140. )->getPrefixedDBkey();
  141. }
  142. return $cacheGroupingKey;
  143. }
  144. /**
  145. * Enhanced RC group
  146. * @param RCCacheEntry[] $block
  147. * @return string
  148. * @throws DomainException
  149. */
  150. protected function recentChangesBlockGroup( $block ) {
  151. $recentChangesFlags = $this->getConfig()->get( 'RecentChangesFlags' );
  152. # Add the namespace and title of the block as part of the class
  153. $tableClasses = [ 'mw-collapsible', 'mw-collapsed', 'mw-enhanced-rc', 'mw-changeslist-line' ];
  154. if ( $block[0]->mAttribs['rc_log_type'] ) {
  155. # Log entry
  156. $tableClasses[] = 'mw-changeslist-log';
  157. $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-log-'
  158. . $block[0]->mAttribs['rc_log_type'] );
  159. } else {
  160. $tableClasses[] = 'mw-changeslist-edit';
  161. $tableClasses[] = Sanitizer::escapeClass( 'mw-changeslist-ns'
  162. . $block[0]->mAttribs['rc_namespace'] . '-' . $block[0]->mAttribs['rc_title'] );
  163. }
  164. if ( $block[0]->watched ) {
  165. $tableClasses[] = 'mw-changeslist-line-watched';
  166. } else {
  167. $tableClasses[] = 'mw-changeslist-line-not-watched';
  168. }
  169. # Collate list of users
  170. $userlinks = [];
  171. # Other properties
  172. $curId = 0;
  173. # Some catalyst variables...
  174. $namehidden = true;
  175. $allLogs = true;
  176. $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
  177. # Default values for RC flags
  178. $collectedRcFlags = [];
  179. foreach ( $recentChangesFlags as $key => $value ) {
  180. $flagGrouping = ( $recentChangesFlags[$key]['grouping'] ?? 'any' );
  181. switch ( $flagGrouping ) {
  182. case 'all':
  183. $collectedRcFlags[$key] = true;
  184. break;
  185. case 'any':
  186. $collectedRcFlags[$key] = false;
  187. break;
  188. default:
  189. throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
  190. }
  191. }
  192. foreach ( $block as $rcObj ) {
  193. // If all log actions to this page were hidden, then don't
  194. // give the name of the affected page for this block!
  195. if ( !static::isDeleted( $rcObj, LogPage::DELETED_ACTION ) ) {
  196. $namehidden = false;
  197. }
  198. $u = $rcObj->userlink;
  199. if ( !isset( $userlinks[$u] ) ) {
  200. $userlinks[$u] = 0;
  201. }
  202. if ( $rcObj->mAttribs['rc_type'] != RC_LOG ) {
  203. $allLogs = false;
  204. }
  205. # Get the latest entry with a page_id and oldid
  206. # since logs may not have these.
  207. if ( !$curId && $rcObj->mAttribs['rc_cur_id'] ) {
  208. $curId = $rcObj->mAttribs['rc_cur_id'];
  209. }
  210. $userlinks[$u]++;
  211. }
  212. # Sort the list and convert to text
  213. krsort( $userlinks );
  214. asort( $userlinks );
  215. $users = [];
  216. foreach ( $userlinks as $userlink => $count ) {
  217. $text = $userlink;
  218. $text .= $this->getLanguage()->getDirMark();
  219. if ( $count > 1 ) {
  220. $formattedCount = $this->msg( 'ntimes' )->numParams( $count )->escaped();
  221. $text .= ' ' . $this->msg( 'parentheses' )->rawParams( $formattedCount )->escaped();
  222. }
  223. array_push( $users, $text );
  224. }
  225. # Article link
  226. $articleLink = '';
  227. $revDeletedMsg = false;
  228. if ( $namehidden ) {
  229. $revDeletedMsg = $this->msg( 'rev-deleted-event' )->escaped();
  230. } elseif ( $allLogs ) {
  231. $articleLink = $this->maybeWatchedLink( $block[0]->link, $block[0]->watched );
  232. } else {
  233. $articleLink = $this->getArticleLink(
  234. $block[0], $block[0]->unpatrolled, $block[0]->watched );
  235. }
  236. $queryParams = [ 'curid' => $curId ];
  237. # Sub-entries
  238. $lines = [];
  239. $filterClasses = [];
  240. foreach ( $block as $i => $rcObj ) {
  241. $line = $this->getLineData( $block, $rcObj, $queryParams );
  242. if ( !$line ) {
  243. // completely ignore this RC entry if we don't want to render it
  244. unset( $block[$i] );
  245. continue;
  246. }
  247. // Roll up flags
  248. foreach ( $line['recentChangesFlagsRaw'] as $key => $value ) {
  249. $flagGrouping = ( $recentChangesFlags[$key]['grouping'] ?? 'any' );
  250. switch ( $flagGrouping ) {
  251. case 'all':
  252. if ( !$value ) {
  253. $collectedRcFlags[$key] = false;
  254. }
  255. break;
  256. case 'any':
  257. if ( $value ) {
  258. $collectedRcFlags[$key] = true;
  259. }
  260. break;
  261. default:
  262. throw new DomainException( "Unknown grouping type \"{$flagGrouping}\"" );
  263. }
  264. }
  265. // Roll up filter-based CSS classes
  266. $filterClasses = array_merge( $filterClasses, $this->getHTMLClassesForFilters( $rcObj ) );
  267. // Add classes for change tags separately, getHTMLClassesForFilters() doesn't add them
  268. $this->getTags( $rcObj, $filterClasses );
  269. $filterClasses = array_unique( $filterClasses );
  270. $lines[] = $line;
  271. }
  272. // Further down are some assumptions that $block is a 0-indexed array
  273. // with (count-1) as last key. Let's make sure it is.
  274. $block = array_values( $block );
  275. $filterClasses = array_values( $filterClasses );
  276. if ( empty( $block ) || !$lines ) {
  277. // if we can't show anything, don't display this block altogether
  278. return '';
  279. }
  280. $logText = $this->getLogText( $block, $queryParams, $allLogs,
  281. $collectedRcFlags['newpage'], $namehidden
  282. );
  283. # Character difference (does not apply if only log items)
  284. $charDifference = false;
  285. if ( $RCShowChangedSize && !$allLogs ) {
  286. $last = 0;
  287. $first = count( $block ) - 1;
  288. # Some events (like logs and category changes) have an "empty" size, so we need to skip those...
  289. while ( $last < $first && $block[$last]->mAttribs['rc_new_len'] === null ) {
  290. $last++;
  291. }
  292. while ( $last < $first && $block[$first]->mAttribs['rc_old_len'] === null ) {
  293. $first--;
  294. }
  295. # Get net change
  296. $charDifference = $this->formatCharacterDifference( $block[$first], $block[$last] ) ?: false;
  297. }
  298. $numberofWatchingusers = $this->numberofWatchingusers( $block[0]->numberofWatchingusers );
  299. $usersList = $this->msg( 'brackets' )->rawParams(
  300. implode( $this->message['semicolon-separator'], $users )
  301. )->escaped();
  302. $prefix = '';
  303. if ( is_callable( $this->changeLinePrefixer ) ) {
  304. $prefix = call_user_func( $this->changeLinePrefixer, $block[0], $this, true );
  305. }
  306. $templateParams = [
  307. 'articleLink' => $articleLink,
  308. 'charDifference' => $charDifference,
  309. 'collectedRcFlags' => $this->recentChangesFlags( $collectedRcFlags ),
  310. 'filterClasses' => $filterClasses,
  311. 'languageDirMark' => $this->getLanguage()->getDirMark(),
  312. 'lines' => $lines,
  313. 'logText' => $logText,
  314. 'numberofWatchingusers' => $numberofWatchingusers,
  315. 'prefix' => $prefix,
  316. 'rev-deleted-event' => $revDeletedMsg,
  317. 'tableClasses' => $tableClasses,
  318. 'timestamp' => $block[0]->timestamp,
  319. 'fullTimestamp' => $block[0]->getAttribute( 'rc_timestamp' ),
  320. 'users' => $usersList,
  321. ];
  322. $this->rcCacheIndex++;
  323. return $this->templateParser->processTemplate(
  324. 'EnhancedChangesListGroup',
  325. $templateParams
  326. );
  327. }
  328. /**
  329. * @param RCCacheEntry[] $block
  330. * @param RCCacheEntry $rcObj
  331. * @param array $queryParams
  332. * @return array
  333. * @throws Exception
  334. * @throws FatalError
  335. * @throws MWException
  336. */
  337. protected function getLineData( array $block, RCCacheEntry $rcObj, array $queryParams = [] ) {
  338. $RCShowChangedSize = $this->getConfig()->get( 'RCShowChangedSize' );
  339. $type = $rcObj->mAttribs['rc_type'];
  340. $data = [];
  341. $lineParams = [ 'targetTitle' => $rcObj->getTitle() ];
  342. $classes = [ 'mw-enhanced-rc' ];
  343. if ( $rcObj->watched ) {
  344. $classes[] = 'mw-enhanced-watched';
  345. }
  346. $classes = array_merge( $classes, $this->getHTMLClasses( $rcObj, $rcObj->watched ) );
  347. $separator = ' <span class="mw-changeslist-separator"></span> ';
  348. $data['recentChangesFlags'] = [
  349. 'newpage' => $type == RC_NEW,
  350. 'minor' => $rcObj->mAttribs['rc_minor'],
  351. 'unpatrolled' => $rcObj->unpatrolled,
  352. 'bot' => $rcObj->mAttribs['rc_bot'],
  353. ];
  354. $params = $queryParams;
  355. if ( $rcObj->mAttribs['rc_this_oldid'] != 0 ) {
  356. $params['oldid'] = $rcObj->mAttribs['rc_this_oldid'];
  357. }
  358. # Log timestamp
  359. if ( $type == RC_LOG ) {
  360. $link = htmlspecialchars( $rcObj->timestamp );
  361. # Revision link
  362. } elseif ( !ChangesList::userCan( $rcObj, RevisionRecord::DELETED_TEXT, $this->getUser() ) ) {
  363. $link = Html::element( 'span', [ 'class' => 'history-deleted' ], $rcObj->timestamp );
  364. } else {
  365. $link = $this->linkRenderer->makeKnownLink(
  366. $rcObj->getTitle(),
  367. $rcObj->timestamp,
  368. [],
  369. $params
  370. );
  371. if ( static::isDeleted( $rcObj, RevisionRecord::DELETED_TEXT ) ) {
  372. $link = '<span class="history-deleted">' . $link . '</span> ';
  373. }
  374. }
  375. $data['timestampLink'] = $link;
  376. $currentAndLastLinks = '';
  377. if ( !$type == RC_LOG || $type == RC_NEW ) {
  378. $currentAndLastLinks .= ' ' . $this->msg( 'parentheses' )->rawParams(
  379. $rcObj->curlink .
  380. $this->message['pipe-separator'] .
  381. $rcObj->lastlink
  382. )->escaped();
  383. }
  384. $data['currentAndLastLinks'] = $currentAndLastLinks;
  385. $data['separatorAfterCurrentAndLastLinks'] = $separator;
  386. # Character diff
  387. if ( $RCShowChangedSize ) {
  388. $cd = $this->formatCharacterDifference( $rcObj );
  389. if ( $cd !== '' ) {
  390. $data['characterDiff'] = $cd;
  391. $data['separatorAfterCharacterDiff'] = $separator;
  392. }
  393. }
  394. if ( $rcObj->mAttribs['rc_type'] == RC_LOG ) {
  395. $data['logEntry'] = $this->insertLogEntry( $rcObj );
  396. } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
  397. $data['comment'] = $this->insertComment( $rcObj );
  398. } else {
  399. # User links
  400. $data['userLink'] = $rcObj->userlink;
  401. $data['userTalkLink'] = $rcObj->usertalklink;
  402. $data['comment'] = $this->insertComment( $rcObj );
  403. }
  404. # Rollback
  405. $data['rollback'] = $this->getRollback( $rcObj );
  406. # Tags
  407. $data['tags'] = $this->getTags( $rcObj, $classes );
  408. $attribs = $this->getDataAttributes( $rcObj );
  409. // give the hook a chance to modify the data
  410. $success = Hooks::run( 'EnhancedChangesListModifyLineData',
  411. [ $this, &$data, $block, $rcObj, &$classes, &$attribs ] );
  412. if ( !$success ) {
  413. // skip entry if hook aborted it
  414. return [];
  415. }
  416. $attribs = array_filter( $attribs,
  417. [ Sanitizer::class, 'isReservedDataAttribute' ],
  418. ARRAY_FILTER_USE_KEY
  419. );
  420. $lineParams['recentChangesFlagsRaw'] = [];
  421. if ( isset( $data['recentChangesFlags'] ) ) {
  422. $lineParams['recentChangesFlags'] = $this->recentChangesFlags( $data['recentChangesFlags'] );
  423. # FIXME: This is used by logic, don't return it in the template params.
  424. $lineParams['recentChangesFlagsRaw'] = $data['recentChangesFlags'];
  425. unset( $data['recentChangesFlags'] );
  426. }
  427. if ( isset( $data['timestampLink'] ) ) {
  428. $lineParams['timestampLink'] = $data['timestampLink'];
  429. unset( $data['timestampLink'] );
  430. }
  431. $lineParams['classes'] = array_values( $classes );
  432. $lineParams['attribs'] = Html::expandAttributes( $attribs );
  433. // everything else: makes it easier for extensions to add or remove data
  434. $lineParams['data'] = array_values( $data );
  435. return $lineParams;
  436. }
  437. /**
  438. * Generates amount of changes (linking to diff ) & link to history.
  439. *
  440. * @param RCCacheEntry[] $block
  441. * @param array $queryParams
  442. * @param bool $allLogs
  443. * @param bool $isnew
  444. * @param bool $namehidden
  445. * @return string
  446. */
  447. protected function getLogText( $block, $queryParams, $allLogs, $isnew, $namehidden ) {
  448. if ( empty( $block ) ) {
  449. return '';
  450. }
  451. # Changes message
  452. static $nchanges = [];
  453. static $sinceLastVisitMsg = [];
  454. $n = count( $block );
  455. if ( !isset( $nchanges[$n] ) ) {
  456. $nchanges[$n] = $this->msg( 'nchanges' )->numParams( $n )->escaped();
  457. }
  458. $sinceLast = 0;
  459. $unvisitedOldid = null;
  460. /** @var RCCacheEntry $rcObj */
  461. foreach ( $block as $rcObj ) {
  462. // Same logic as below inside main foreach
  463. if ( $rcObj->watched ) {
  464. $sinceLast++;
  465. $unvisitedOldid = $rcObj->mAttribs['rc_last_oldid'];
  466. }
  467. }
  468. if ( !isset( $sinceLastVisitMsg[$sinceLast] ) ) {
  469. $sinceLastVisitMsg[$sinceLast] =
  470. $this->msg( 'enhancedrc-since-last-visit' )->numParams( $sinceLast )->escaped();
  471. }
  472. $currentRevision = 0;
  473. foreach ( $block as $rcObj ) {
  474. if ( !$currentRevision ) {
  475. $currentRevision = $rcObj->mAttribs['rc_this_oldid'];
  476. }
  477. }
  478. # Total change link
  479. $links = [];
  480. /** @var RecentChange $block0 */
  481. $block0 = $block[0];
  482. $last = $block[count( $block ) - 1];
  483. if ( !$allLogs ) {
  484. if (
  485. $isnew ||
  486. $rcObj->mAttribs['rc_type'] == RC_CATEGORIZE ||
  487. !ChangesList::userCan( $rcObj, RevisionRecord::DELETED_TEXT, $this->getUser() )
  488. ) {
  489. $links['total-changes'] = Html::rawElement( 'span', [], $nchanges[$n] );
  490. } else {
  491. $links['total-changes'] = Html::rawElement( 'span', [],
  492. $this->linkRenderer->makeKnownLink(
  493. $block0->getTitle(),
  494. new HtmlArmor( $nchanges[$n] ),
  495. [ 'class' => 'mw-changeslist-groupdiff' ],
  496. $queryParams + [
  497. 'diff' => $currentRevision,
  498. 'oldid' => $last->mAttribs['rc_last_oldid'],
  499. ]
  500. )
  501. );
  502. }
  503. if (
  504. $rcObj->mAttribs['rc_type'] != RC_CATEGORIZE &&
  505. $sinceLast > 0 &&
  506. $sinceLast < $n
  507. ) {
  508. $links['total-changes-since-last'] = Html::rawElement( 'span', [],
  509. $this->linkRenderer->makeKnownLink(
  510. $block0->getTitle(),
  511. new HtmlArmor( $sinceLastVisitMsg[$sinceLast] ),
  512. [ 'class' => 'mw-changeslist-groupdiff' ],
  513. $queryParams + [
  514. 'diff' => $currentRevision,
  515. 'oldid' => $unvisitedOldid,
  516. ]
  517. )
  518. );
  519. }
  520. }
  521. # History
  522. if ( $allLogs || $rcObj->mAttribs['rc_type'] == RC_CATEGORIZE ) {
  523. // don't show history link for logs
  524. } elseif ( $namehidden || !$block0->getTitle()->exists() ) {
  525. $links['history'] = Html::rawElement( 'span', [], $this->message['enhancedrc-history'] );
  526. } else {
  527. $params = $queryParams;
  528. $params['action'] = 'history';
  529. $links['history'] = Html::rawElement( 'span', [],
  530. $this->linkRenderer->makeKnownLink(
  531. $block0->getTitle(),
  532. new HtmlArmor( $this->message['enhancedrc-history'] ),
  533. [ 'class' => 'mw-changeslist-history' ],
  534. $params
  535. )
  536. );
  537. }
  538. # Allow others to alter, remove or add to these links
  539. Hooks::run( 'EnhancedChangesList::getLogText',
  540. [ $this, &$links, $block ] );
  541. if ( !$links ) {
  542. return '';
  543. }
  544. $logtext = Html::rawElement( 'span', [ 'class' => 'mw-changeslist-links' ],
  545. implode( ' ', $links ) );
  546. return ' ' . $logtext;
  547. }
  548. /**
  549. * Enhanced RC ungrouped line.
  550. *
  551. * @param RecentChange|RCCacheEntry $rcObj
  552. * @return string A HTML formatted line (generated using $r)
  553. */
  554. protected function recentChangesBlockLine( $rcObj ) {
  555. $data = [];
  556. $query = [ 'curid' => $rcObj->mAttribs['rc_cur_id'] ];
  557. $type = $rcObj->mAttribs['rc_type'];
  558. $logType = $rcObj->mAttribs['rc_log_type'];
  559. $classes = $this->getHTMLClasses( $rcObj, $rcObj->watched );
  560. $classes[] = 'mw-enhanced-rc';
  561. if ( $logType ) {
  562. # Log entry
  563. $classes[] = 'mw-changeslist-log';
  564. $classes[] = Sanitizer::escapeClass( 'mw-changeslist-log-' . $logType );
  565. } else {
  566. $classes[] = 'mw-changeslist-edit';
  567. $classes[] = Sanitizer::escapeClass( 'mw-changeslist-ns' .
  568. $rcObj->mAttribs['rc_namespace'] . '-' . $rcObj->mAttribs['rc_title'] );
  569. }
  570. # Flag and Timestamp
  571. $data['recentChangesFlags'] = [
  572. 'newpage' => $type == RC_NEW,
  573. 'minor' => $rcObj->mAttribs['rc_minor'],
  574. 'unpatrolled' => $rcObj->unpatrolled,
  575. 'bot' => $rcObj->mAttribs['rc_bot'],
  576. ];
  577. // timestamp is not really a link here, but is called timestampLink
  578. // for consistency with EnhancedChangesListModifyLineData
  579. $data['timestampLink'] = htmlspecialchars( $rcObj->timestamp );
  580. # Article or log link
  581. if ( $logType ) {
  582. $logPage = new LogPage( $logType );
  583. $logTitle = SpecialPage::getTitleFor( 'Log', $logType );
  584. $logName = $logPage->getName()->text();
  585. $data['logLink'] = Html::rawElement( 'span', [ 'class' => 'mw-changeslist-links' ],
  586. $this->linkRenderer->makeKnownLink( $logTitle, $logName )
  587. );
  588. } else {
  589. $data['articleLink'] = $this->getArticleLink( $rcObj, $rcObj->unpatrolled, $rcObj->watched );
  590. }
  591. # Diff and hist links
  592. if ( $type != RC_LOG && $type != RC_CATEGORIZE ) {
  593. $query['action'] = 'history';
  594. $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query, false );
  595. }
  596. $data['separatorAfterLinks'] = ' <span class="mw-changeslist-separator"></span> ';
  597. # Character diff
  598. if ( $this->getConfig()->get( 'RCShowChangedSize' ) ) {
  599. $cd = $this->formatCharacterDifference( $rcObj );
  600. if ( $cd !== '' ) {
  601. $data['characterDiff'] = $cd;
  602. $data['separatorAftercharacterDiff'] = ' <span class="mw-changeslist-separator"></span> ';
  603. }
  604. }
  605. if ( $type == RC_LOG ) {
  606. $data['logEntry'] = $this->insertLogEntry( $rcObj );
  607. } elseif ( $this->isCategorizationWithoutRevision( $rcObj ) ) {
  608. $data['comment'] = $this->insertComment( $rcObj );
  609. } else {
  610. $data['userLink'] = $rcObj->userlink;
  611. $data['userTalkLink'] = $rcObj->usertalklink;
  612. $data['comment'] = $this->insertComment( $rcObj );
  613. if ( $type == RC_CATEGORIZE ) {
  614. $data['historyLink'] = $this->getDiffHistLinks( $rcObj, $query, false );
  615. }
  616. $data['rollback'] = $this->getRollback( $rcObj );
  617. }
  618. # Tags
  619. $data['tags'] = $this->getTags( $rcObj, $classes );
  620. # Show how many people are watching this if enabled
  621. $data['watchingUsers'] = $this->numberofWatchingusers( $rcObj->numberofWatchingusers );
  622. $data['attribs'] = array_merge( $this->getDataAttributes( $rcObj ), [ 'class' => $classes ] );
  623. // give the hook a chance to modify the data
  624. $success = Hooks::run( 'EnhancedChangesListModifyBlockLineData',
  625. [ $this, &$data, $rcObj ] );
  626. if ( !$success ) {
  627. // skip entry if hook aborted it
  628. return '';
  629. }
  630. $attribs = $data['attribs'];
  631. unset( $data['attribs'] );
  632. $attribs = array_filter( $attribs, function ( $key ) {
  633. return $key === 'class' || Sanitizer::isReservedDataAttribute( $key );
  634. }, ARRAY_FILTER_USE_KEY );
  635. $prefix = '';
  636. if ( is_callable( $this->changeLinePrefixer ) ) {
  637. $prefix = call_user_func( $this->changeLinePrefixer, $rcObj, $this, false );
  638. }
  639. $line = Html::openElement( 'table', $attribs ) . Html::openElement( 'tr' );
  640. // Highlight block
  641. $line .= Html::rawElement( 'td', [],
  642. $this->getHighlightsContainerDiv()
  643. );
  644. $line .= Html::rawElement( 'td', [], '<span class="mw-enhancedchanges-arrow-space"></span>' );
  645. $line .= Html::rawElement( 'td', [ 'class' => 'mw-changeslist-line-prefix' ], $prefix );
  646. $line .= '<td class="mw-enhanced-rc" colspan="2">';
  647. if ( isset( $data['recentChangesFlags'] ) ) {
  648. $line .= $this->recentChangesFlags( $data['recentChangesFlags'] );
  649. unset( $data['recentChangesFlags'] );
  650. }
  651. if ( isset( $data['timestampLink'] ) ) {
  652. $line .= "\u{00A0}" . $data['timestampLink'];
  653. unset( $data['timestampLink'] );
  654. }
  655. $line .= "\u{00A0}</td>";
  656. $line .= Html::openElement( 'td', [
  657. 'class' => 'mw-changeslist-line-inner',
  658. // Used for reliable determination of the affiliated page
  659. 'data-target-page' => $rcObj->getTitle(),
  660. ] );
  661. // everything else: makes it easier for extensions to add or remove data
  662. foreach ( $data as $key => $dataItem ) {
  663. $line .= Html::rawElement( 'span', [
  664. 'class' => 'mw-changeslist-line-inner-' . $key,
  665. ], $dataItem );
  666. }
  667. $line .= "</td></tr></table>\n";
  668. return $line;
  669. }
  670. /**
  671. * Returns value to be used in 'historyLink' element of $data param in
  672. * EnhancedChangesListModifyBlockLineData hook.
  673. *
  674. * @since 1.27
  675. *
  676. * @param RCCacheEntry $rc
  677. * @param array $query array of key/value pairs to append as a query string
  678. * @param bool $useParentheses (optional) Wrap comments in parentheses where needed
  679. * @return string HTML
  680. */
  681. public function getDiffHistLinks( RCCacheEntry $rc, array $query, $useParentheses = true ) {
  682. $pageTitle = $rc->getTitle();
  683. if ( $rc->getAttribute( 'rc_type' ) == RC_CATEGORIZE ) {
  684. // For categorizations we must swap the category title with the page title!
  685. $pageTitle = Title::newFromID( $rc->getAttribute( 'rc_cur_id' ) );
  686. if ( !$pageTitle ) {
  687. // The page has been deleted, but the RC entry
  688. // deletion job has not run yet. Just skip.
  689. return '';
  690. }
  691. }
  692. $histLink = $this->linkRenderer->makeKnownLink(
  693. $pageTitle,
  694. new HtmlArmor( $this->message['hist'] ),
  695. [ 'class' => 'mw-changeslist-history' ],
  696. $query
  697. );
  698. if ( $useParentheses ) {
  699. $retVal = $this->msg( 'parentheses' )
  700. ->rawParams( $rc->difflink . $this->message['pipe-separator']
  701. . $histLink )->escaped();
  702. } else {
  703. $retVal = Html::rawElement( 'span', [ 'class' => 'mw-changeslist-links' ],
  704. Html::rawElement( 'span', [], $rc->difflink ) .
  705. Html::rawElement( 'span', [], $histLink )
  706. );
  707. }
  708. return ' ' . $retVal;
  709. }
  710. /**
  711. * If enhanced RC is in use, this function takes the previously cached
  712. * RC lines, arranges them, and outputs the HTML
  713. *
  714. * @return string
  715. */
  716. protected function recentChangesBlock() {
  717. if ( count( $this->rc_cache ) == 0 ) {
  718. return '';
  719. }
  720. $blockOut = '';
  721. foreach ( $this->rc_cache as $block ) {
  722. if ( count( $block ) < 2 ) {
  723. $blockOut .= $this->recentChangesBlockLine( array_shift( $block ) );
  724. } else {
  725. $blockOut .= $this->recentChangesBlockGroup( $block );
  726. }
  727. }
  728. if ( $blockOut === '' ) {
  729. return '';
  730. }
  731. // $this->lastdate is kept up to date by recentChangesLine()
  732. return Xml::element( 'h4', null, $this->lastdate ) . "\n<div>" . $blockOut . '</div>';
  733. }
  734. /**
  735. * Returns text for the end of RC
  736. * If enhanced RC is in use, returns pretty much all the text
  737. * @return string
  738. */
  739. public function endRecentChangesList() {
  740. return $this->recentChangesBlock() . '</div>';
  741. }
  742. }