DeletedContribsPager.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. <?php
  2. /**
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  16. * http://www.gnu.org/copyleft/gpl.html
  17. *
  18. * @file
  19. * @ingroup Pager
  20. */
  21. /**
  22. * @ingroup Pager
  23. */
  24. use MediaWiki\Linker\LinkRenderer;
  25. use MediaWiki\MediaWikiServices;
  26. use MediaWiki\Revision\RevisionRecord;
  27. use Wikimedia\Rdbms\IDatabase;
  28. use Wikimedia\Rdbms\IResultWrapper;
  29. use Wikimedia\Rdbms\FakeResultWrapper;
  30. class DeletedContribsPager extends IndexPager {
  31. /**
  32. * @var bool Default direction for pager
  33. */
  34. public $mDefaultDirection = IndexPager::DIR_DESCENDING;
  35. /**
  36. * @var string[] Local cache for escaped messages
  37. */
  38. public $messages;
  39. /**
  40. * @var string User name, or a string describing an IP address range
  41. */
  42. public $target;
  43. /**
  44. * @var string|int A single namespace number, or an empty string for all namespaces
  45. */
  46. public $namespace = '';
  47. /**
  48. * @var IDatabase
  49. */
  50. public $mDb;
  51. /**
  52. * @var string Navigation bar with paging links.
  53. */
  54. protected $mNavigationBar;
  55. public function __construct( IContextSource $context, $target, $namespace = false,
  56. LinkRenderer $linkRenderer
  57. ) {
  58. parent::__construct( $context, $linkRenderer );
  59. $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
  60. foreach ( $msgs as $msg ) {
  61. $this->messages[$msg] = $this->msg( $msg )->text();
  62. }
  63. $this->target = $target;
  64. $this->namespace = $namespace;
  65. $this->mDb = wfGetDB( DB_REPLICA, 'contributions' );
  66. }
  67. function getDefaultQuery() {
  68. $query = parent::getDefaultQuery();
  69. $query['target'] = $this->target;
  70. return $query;
  71. }
  72. function getQueryInfo() {
  73. $userCond = [
  74. // ->getJoin() below takes care of any joins needed
  75. ActorMigration::newMigration()->getWhere(
  76. wfGetDB( DB_REPLICA ), 'ar_user', User::newFromName( $this->target, false ), false
  77. )['conds']
  78. ];
  79. $conds = array_merge( $userCond, $this->getNamespaceCond() );
  80. $user = $this->getUser();
  81. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  82. // Paranoia: avoid brute force searches (T19792)
  83. if ( !$permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
  84. $conds[] = $this->mDb->bitAnd( 'ar_deleted', RevisionRecord::DELETED_USER ) . ' = 0';
  85. } elseif ( !$permissionManager->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' ) ) {
  86. $conds[] = $this->mDb->bitAnd( 'ar_deleted', RevisionRecord::SUPPRESSED_USER ) .
  87. ' != ' . RevisionRecord::SUPPRESSED_USER;
  88. }
  89. $commentQuery = CommentStore::getStore()->getJoin( 'ar_comment' );
  90. $actorQuery = ActorMigration::newMigration()->getJoin( 'ar_user' );
  91. return [
  92. 'tables' => [ 'archive' ] + $commentQuery['tables'] + $actorQuery['tables'],
  93. 'fields' => [
  94. 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
  95. 'ar_minor_edit', 'ar_deleted'
  96. ] + $commentQuery['fields'] + $actorQuery['fields'],
  97. 'conds' => $conds,
  98. 'options' => [],
  99. 'join_conds' => $commentQuery['joins'] + $actorQuery['joins'],
  100. ];
  101. }
  102. /**
  103. * This method basically executes the exact same code as the parent class, though with
  104. * a hook added, to allow extensions to add additional queries.
  105. *
  106. * @param string $offset Index offset, inclusive
  107. * @param int $limit Exact query limit
  108. * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
  109. * @return IResultWrapper
  110. */
  111. function reallyDoQuery( $offset, $limit, $order ) {
  112. $data = [ parent::reallyDoQuery( $offset, $limit, $order ) ];
  113. // This hook will allow extensions to add in additional queries, nearly
  114. // identical to ContribsPager::reallyDoQuery.
  115. Hooks::run(
  116. 'DeletedContribsPager::reallyDoQuery',
  117. [ &$data, $this, $offset, $limit, $order ]
  118. );
  119. $result = [];
  120. // loop all results and collect them in an array
  121. foreach ( $data as $query ) {
  122. foreach ( $query as $i => $row ) {
  123. // use index column as key, allowing us to easily sort in PHP
  124. $result[$row->{$this->getIndexField()} . "-$i"] = $row;
  125. }
  126. }
  127. // sort results
  128. if ( $order === self::QUERY_ASCENDING ) {
  129. ksort( $result );
  130. } else {
  131. krsort( $result );
  132. }
  133. // enforce limit
  134. $result = array_slice( $result, 0, $limit );
  135. // get rid of array keys
  136. $result = array_values( $result );
  137. return new FakeResultWrapper( $result );
  138. }
  139. function getIndexField() {
  140. return 'ar_timestamp';
  141. }
  142. /**
  143. * @return string
  144. */
  145. public function getTarget() {
  146. return $this->target;
  147. }
  148. /**
  149. * @return int|string
  150. */
  151. public function getNamespace() {
  152. return $this->namespace;
  153. }
  154. protected function getStartBody() {
  155. return "<ul>\n";
  156. }
  157. protected function getEndBody() {
  158. return "</ul>\n";
  159. }
  160. function getNavigationBar() {
  161. if ( isset( $this->mNavigationBar ) ) {
  162. return $this->mNavigationBar;
  163. }
  164. $linkTexts = [
  165. 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
  166. 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
  167. 'first' => $this->msg( 'histlast' )->escaped(),
  168. 'last' => $this->msg( 'histfirst' )->escaped()
  169. ];
  170. $pagingLinks = $this->getPagingLinks( $linkTexts );
  171. $limitLinks = $this->getLimitLinks();
  172. $lang = $this->getLanguage();
  173. $limits = $lang->pipeList( $limitLinks );
  174. $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
  175. $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
  176. $prevNext = $this->msg( 'viewprevnext' )
  177. ->rawParams(
  178. $pagingLinks['prev'],
  179. $pagingLinks['next'],
  180. $limits
  181. )->escaped();
  182. $separator = $this->msg( 'word-separator' )->escaped();
  183. $this->mNavigationBar = $firstLast . $separator . $prevNext;
  184. return $this->mNavigationBar;
  185. }
  186. function getNamespaceCond() {
  187. if ( $this->namespace !== '' ) {
  188. return [ 'ar_namespace' => (int)$this->namespace ];
  189. } else {
  190. return [];
  191. }
  192. }
  193. /**
  194. * Generates each row in the contributions list.
  195. *
  196. * @todo This would probably look a lot nicer in a table.
  197. * @param stdClass $row
  198. * @return string
  199. */
  200. function formatRow( $row ) {
  201. $ret = '';
  202. $classes = [];
  203. $attribs = [];
  204. /*
  205. * There may be more than just revision rows. To make sure that we'll only be processing
  206. * revisions here, let's _try_ to build a revision out of our row (without displaying
  207. * notices though) and then trying to grab data from the built object. If we succeed,
  208. * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
  209. * to extensions to subscribe to the hook to parse the row.
  210. */
  211. Wikimedia\suppressWarnings();
  212. try {
  213. $rev = Revision::newFromArchiveRow( $row );
  214. $validRevision = (bool)$rev->getId();
  215. } catch ( Exception $e ) {
  216. $validRevision = false;
  217. }
  218. Wikimedia\restoreWarnings();
  219. if ( $validRevision ) {
  220. $attribs['data-mw-revid'] = $rev->getId();
  221. $ret = $this->formatRevisionRow( $row );
  222. }
  223. // Let extensions add data
  224. Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
  225. $attribs = array_filter( $attribs,
  226. [ Sanitizer::class, 'isReservedDataAttribute' ],
  227. ARRAY_FILTER_USE_KEY
  228. );
  229. if ( $classes === [] && $attribs === [] && $ret === '' ) {
  230. wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
  231. $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
  232. } else {
  233. $attribs['class'] = $classes;
  234. $ret = Html::rawElement( 'li', $attribs, $ret ) . "\n";
  235. }
  236. return $ret;
  237. }
  238. /**
  239. * Generates each row in the contributions list for archive entries.
  240. *
  241. * Contributions which are marked "top" are currently on top of the history.
  242. * For these contributions, a [rollback] link is shown for users with sysop
  243. * privileges. The rollback link restores the most recent version that was not
  244. * written by the target user.
  245. *
  246. * @todo This would probably look a lot nicer in a table.
  247. * @param stdClass $row
  248. * @return string
  249. */
  250. function formatRevisionRow( $row ) {
  251. $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
  252. $linkRenderer = $this->getLinkRenderer();
  253. $rev = new Revision( [
  254. 'title' => $page,
  255. 'id' => $row->ar_rev_id,
  256. 'comment' => CommentStore::getStore()->getComment( 'ar_comment', $row )->text,
  257. 'user' => $row->ar_user,
  258. 'user_text' => $row->ar_user_text,
  259. 'actor' => $row->ar_actor ?? null,
  260. 'timestamp' => $row->ar_timestamp,
  261. 'minor_edit' => $row->ar_minor_edit,
  262. 'deleted' => $row->ar_deleted,
  263. ] );
  264. $undelete = SpecialPage::getTitleFor( 'Undelete' );
  265. $logs = SpecialPage::getTitleFor( 'Log' );
  266. $dellog = $linkRenderer->makeKnownLink(
  267. $logs,
  268. $this->messages['deletionlog'],
  269. [],
  270. [
  271. 'type' => 'delete',
  272. 'page' => $page->getPrefixedText()
  273. ]
  274. );
  275. $reviewlink = $linkRenderer->makeKnownLink(
  276. SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
  277. $this->messages['undeleteviewlink']
  278. );
  279. $user = $this->getUser();
  280. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  281. if ( $permissionManager->userHasRight( $user, 'deletedtext' ) ) {
  282. $last = $linkRenderer->makeKnownLink(
  283. $undelete,
  284. $this->messages['diff'],
  285. [],
  286. [
  287. 'target' => $page->getPrefixedText(),
  288. 'timestamp' => $rev->getTimestamp(),
  289. 'diff' => 'prev'
  290. ]
  291. );
  292. } else {
  293. $last = htmlspecialchars( $this->messages['diff'] );
  294. }
  295. $comment = Linker::revComment( $rev );
  296. $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
  297. if ( !$permissionManager->userHasRight( $user, 'undelete' ) ||
  298. !$rev->userCan( RevisionRecord::DELETED_TEXT, $user )
  299. ) {
  300. $link = htmlspecialchars( $date ); // unusable link
  301. } else {
  302. $link = $linkRenderer->makeKnownLink(
  303. $undelete,
  304. $date,
  305. [ 'class' => 'mw-changeslist-date' ],
  306. [
  307. 'target' => $page->getPrefixedText(),
  308. 'timestamp' => $rev->getTimestamp()
  309. ]
  310. );
  311. }
  312. // Style deleted items
  313. if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
  314. $link = '<span class="history-deleted">' . $link . '</span>';
  315. }
  316. $pagelink = $linkRenderer->makeLink(
  317. $page,
  318. null,
  319. [ 'class' => 'mw-changeslist-title' ]
  320. );
  321. if ( $rev->isMinor() ) {
  322. $mflag = ChangesList::flag( 'minor' );
  323. } else {
  324. $mflag = '';
  325. }
  326. // Revision delete link
  327. $del = Linker::getRevDeleteLink( $user, $rev, $page );
  328. if ( $del ) {
  329. $del .= ' ';
  330. }
  331. $tools = Html::rawElement(
  332. 'span',
  333. [ 'class' => 'mw-deletedcontribs-tools' ],
  334. $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
  335. [ $last, $dellog, $reviewlink ] ) )->escaped()
  336. );
  337. $separator = '<span class="mw-changeslist-separator">. .</span>';
  338. $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
  339. # Denote if username is redacted for this edit
  340. if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
  341. $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
  342. }
  343. return $ret;
  344. }
  345. /**
  346. * Get the Database object in use
  347. *
  348. * @return IDatabase
  349. */
  350. public function getDatabase() {
  351. return $this->mDb;
  352. }
  353. }