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
210
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
110
 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        $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' ]
58                );
59                if ( $rollbackLink ) {
60                    $this->preventClickjacking = true;
61                    $tools['mw-rollback'] = $rollbackLink;
62                }
63            }
64        }
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,
77                    [
78                        'action' => 'edit',
79                        'undoafter' => $previousRevRecord->getId(),
80                        'undo' => $revRecord->getId()
81                    ]
82                );
83                $tools['mw-undo'] = "<span class=\"mw-history-undo\">{$undolink}</span>";
84            }
85        }
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()
94        );
95        $this->tools = $tools;
96    }
97
98    public function shouldPreventClickjacking() {
99        return $this->preventClickjacking;
100    }
101
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 );
109            }
110            $s2 .= Html::closeElement( 'span' );
111        }
112        return $s2;
113    }
114}
115
116/**
117 * Retain the old class name for backwards compatibility.
118 * @deprecated since 1.41
119 */
120class_alias( PagerTools::class, 'PagerTools' );