Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PagerTools
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
240
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 1
132
 shouldPreventClickjacking
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 toHTML
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Pager;
4
5use MediaWiki\Context\IContextSource;
6use MediaWiki\HookContainer\HookRunner;
7use MediaWiki\Html\Html;
8use MediaWiki\Linker\Linker;
9use MediaWiki\Linker\LinkRenderer;
10use MediaWiki\Page\PageIdentity;
11use MediaWiki\Revision\RevisionRecord;
12
13/**
14 * Generate a set of tools for a revision.
15 * @since 1.40
16 */
17class PagerTools {
18    /** @var bool */
19    private $preventClickjacking = false;
20    /** @var array */
21    private $tools = [];
22
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
37     */
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        if ( ( $showRollbackLink || $previousRevRecord )
51            // probablyCan loads page restriction data, call only when needed
52            && $authority->probablyCan( 'edit', $title )
53        ) {
54            if ( $showRollbackLink && $authority->probablyCan( 'rollback', $title ) ) {
55                // Get a rollback link without the brackets
56                $rollbackLink = Linker::generateRollback(
57                    $revRecord,
58                    $context,
59                    [ 'noBrackets' ]
60                );
61                if ( $rollbackLink ) {
62                    $this->preventClickjacking = true;
63                    $tools['mw-rollback'] = $rollbackLink;
64                }
65            }
66            if ( $previousRevRecord
67                && !$revRecord->isDeleted( RevisionRecord::DELETED_TEXT )
68                && !$previousRevRecord->isDeleted( RevisionRecord::DELETED_TEXT )
69            ) {
70                # Create undo tooltip for the first (=latest) line only
71                $undoTooltip = $showRollbackLink
72                    ? [ 'title' => $context->msg( 'tooltip-undo' )->text() ]
73                    : [];
74                $undolink = $linkRenderer->makeKnownLink(
75                    $title,
76                    $context->msg( 'editundo' )->text(),
77                    $undoTooltip,
78                    [
79                        'action' => 'edit',
80                        'undoafter' => $previousRevRecord->getId(),
81                        'undo' => $revRecord->getId()
82                    ]
83                );
84                $tools['mw-undo'] = "<span class=\"mw-history-undo\">{$undolink}</span>";
85            }
86        }
87        // Allow extension to add their own links here
88        // FIXME previously this was only called on history; restore that and deprecate in favor
89        //   of a more generic hook (See T326180)
90        $hookRunner->onHistoryTools(
91            $revRecord,
92            $tools,
93            $previousRevRecord,
94            $authority->getUser()
95        );
96        $this->tools = $tools;
97    }
98
99    public function shouldPreventClickjacking(): bool {
100        return $this->preventClickjacking;
101    }
102
103    public function toHTML(): string {
104        $tools = $this->tools;
105        $s2 = '';
106        if ( $tools ) {
107            $s2 .= ' ' . Html::openElement( 'span', [ 'class' => [ 'mw-changeslist-links', 'mw-pager-tools' ] ] );
108            foreach ( $tools as $tool ) {
109                $s2 .= Html::rawElement( 'span', [], $tool );
110            }
111            $s2 .= Html::closeElement( 'span' );
112        }
113        return $s2;
114    }
115}
116
117/**
118 * Retain the old class name for backwards compatibility.
119 * @deprecated since 1.41
120 */
121class_alias( PagerTools::class, 'PagerTools' );