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