Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / pagers / PagerTools.php
blob1cc1f236e83d56953122495bf0926615ea0aa6e8
1 <?php
3 namespace MediaWiki\Pager;
5 use MediaWiki\Context\IContextSource;
6 use MediaWiki\HookContainer\HookRunner;
7 use MediaWiki\Html\Html;
8 use MediaWiki\Linker\Linker;
9 use MediaWiki\Linker\LinkRenderer;
10 use MediaWiki\Page\PageIdentity;
11 use MediaWiki\Revision\RevisionRecord;
13 /**
14 * Generate a set of tools for a revision.
15 * @since 1.40
17 class PagerTools {
18 /** @var bool */
19 private $preventClickjacking = false;
20 /** @var array */
21 private $tools = [];
23 /**
24 * Generate a set of tools for a revision.
25 * Will perform permission checks where necessary.
26 * @param RevisionRecord $revRecord The revision to generate tools for.
27 * @param RevisionRecord|null $previousRevRecord The previous revision (if any). Optional.
28 * Used to produce undo links.
29 * @param bool $showRollbackLink Whether to show the rollback link. Only set to true if the
30 * revision is the latest revision of its page and it has a parent.
31 * FIXME why don't we do these checks ourselves?
32 * @param HookRunner $hookRunner
33 * @param PageIdentity $title The page to generate tools for. It is the caller's responsibility
34 * to ensure that the page is already in the link cache.
35 * @param IContextSource $context
36 * @param LinkRenderer $linkRenderer
38 public function __construct(
39 RevisionRecord $revRecord,
40 ?RevisionRecord $previousRevRecord,
41 bool $showRollbackLink,
42 HookRunner $hookRunner,
43 PageIdentity $title,
44 IContextSource $context,
45 LinkRenderer $linkRenderer
46 ) {
47 $tools = [];
48 $authority = $context->getAuthority();
49 # Rollback and undo links
50 $userCanEditTitle = $authority->probablyCan( 'edit', $title );
51 if ( $showRollbackLink && $userCanEditTitle ) {
52 if ( $authority->probablyCan( 'rollback', $title ) ) {
53 // Get a rollback link without the brackets
54 $rollbackLink = Linker::generateRollback(
55 $revRecord,
56 $context,
57 [ 'noBrackets' ]
59 if ( $rollbackLink ) {
60 $this->preventClickjacking = true;
61 $tools['mw-rollback'] = $rollbackLink;
65 if ( $userCanEditTitle && $previousRevRecord ) {
66 if ( !$revRecord->isDeleted( RevisionRecord::DELETED_TEXT )
67 && !$previousRevRecord->isDeleted( RevisionRecord::DELETED_TEXT )
68 ) {
69 # Create undo tooltip for the first (=latest) line only
70 $undoTooltip = $showRollbackLink
71 ? [ 'title' => $context->msg( 'tooltip-undo' )->text() ]
72 : [];
73 $undolink = $linkRenderer->makeKnownLink(
74 $title,
75 $context->msg( 'editundo' )->text(),
76 $undoTooltip,
78 'action' => 'edit',
79 'undoafter' => $previousRevRecord->getId(),
80 'undo' => $revRecord->getId()
83 $tools['mw-undo'] = "<span class=\"mw-history-undo\">{$undolink}</span>";
86 // Allow extension to add their own links here
87 // FIXME previously this was only called on history; restore that and deprecate in favor
88 // of a more generic hook (See T326180)
89 $hookRunner->onHistoryTools(
90 $revRecord,
91 $tools,
92 $previousRevRecord,
93 $authority->getUser()
95 $this->tools = $tools;
98 public function shouldPreventClickjacking() {
99 return $this->preventClickjacking;
102 public function toHTML() {
103 $tools = $this->tools;
104 $s2 = '';
105 if ( $tools ) {
106 $s2 .= ' ' . Html::openElement( 'span', [ 'class' => 'mw-changeslist-links mw-pager-tools' ] );
107 foreach ( $tools as $tool ) {
108 $s2 .= Html::rawElement( 'span', [], $tool );
110 $s2 .= Html::closeElement( 'span' );
112 return $s2;
117 * Retain the old class name for backwards compatibility.
118 * @deprecated since 1.41
120 class_alias( PagerTools::class, 'PagerTools' );