Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
UIUtils
100.00% covered (success)
100.00%
48 / 48
100.00% covered (success)
100.00%
3 / 3
6
100.00% covered (success)
100.00%
1 / 1
 createErrorChip
100.00% covered (success)
100.00%
25 / 25
100.00% covered (success)
100.00%
1 / 1
2
 createCodexProgressIndicator
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 checkRevisionViewable
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2/**
3 * WikiLambda UI rendering utilities
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda;
12
13use MediaWiki\Html\Html;
14use MediaWiki\Output\OutputPage;
15use MediaWiki\Permissions\Authority;
16use MediaWiki\Revision\RevisionRecord;
17use MediaWiki\Title\Title;
18
19class UIUtils {
20    /**
21     * Generate an inline Codex error chip (cdx-info-chip--error).
22     *
23     * Inline styles are required because Codex does not support inline chip rendering natively.
24     *
25     * @param string $errorKey i18n message key for the visible error text
26     * @param string $dataErrorKey Value for the data-error-key attribute (defaults to $errorKey)
27     * @return string HTML string
28     */
29    public static function createErrorChip( string $errorKey, string $dataErrorKey = '' ): string {
30        return Html::rawElement(
31            'span',
32            [
33                'class' => 'cdx-info-chip cdx-info-chip--error',
34                'style' => 'position:relative;line-height:var(--line-height-medium,1.375rem);'
35                    . 'padding-left:calc(var(--font-size-medium,1rem) + calc(var(--font-size-medium,1rem) - 6px));',
36                'data-error-key' => $dataErrorKey !== '' ? $dataErrorKey : $errorKey,
37            ],
38            Html::element(
39                'span',
40                [
41                    'class' => 'cdx-info-chip__icon',
42                    'style' => 'position:absolute;left:calc((var(--font-size-medium,1rem) - 2px) * .5);',
43                    'aria-hidden' => 'true',
44                ]
45            ) .
46            Html::element(
47                'span',
48                [
49                    'class' => 'cdx-info-chip__text',
50                    'style' => 'font-size:var(--font-size-medium,1rem);',
51                ],
52                wfMessage( $errorKey )->text()
53            )
54        );
55    }
56
57    /**
58     * Generate a Codex progress indicator HTML element.
59     *
60     * @param string $ariaLabel The ARIA label for the progress indicator
61     * @return string The HTML of the progress indicator element
62     */
63    public static function createCodexProgressIndicator( string $ariaLabel ): string {
64        return Html::rawElement(
65            'div',
66            [ 'class' => 'cdx-progress-indicator' ],
67            Html::rawElement(
68                'div',
69                [ 'class' => 'cdx-progress-indicator__indicator' ],
70                Html::element(
71                    'progress',
72                    [
73                        'class' => 'cdx-progress-indicator__indicator__progress',
74                        'aria-label' => $ariaLabel
75                    ]
76                )
77            )
78        );
79    }
80
81    /**
82     * Guard a user-facing view of a (possibly deleted/suppressed) revision.
83     *
84     * Modelled on Article::fetchRevisionRecord(): if the performer may not see the
85     * revision's deleted text, add the appropriate core warning box to the output
86     * and return false so the caller can stop before leaking content.
87     *
88     * Used by both the Abstract-mode and repo-mode view/edit surfaces, which is why
89     * this lives here rather than on either content handler.
90     *
91     * @param RevisionRecord $revision
92     * @param Authority $performer
93     * @param Title $title Page title, for the rev-deleted-text-permission message
94     * @param OutputPage $output
95     * @return bool True if the revision may be shown to the performer
96     */
97    public static function checkRevisionViewable(
98        RevisionRecord $revision,
99        Authority $performer,
100        Title $title,
101        OutputPage $output
102    ): bool {
103        if ( $revision->userCan( RevisionRecord::DELETED_TEXT, $performer ) ) {
104            return true;
105        }
106
107        $msg = $revision->isDeleted( RevisionRecord::DELETED_RESTRICTED )
108            ? $output->msg( 'rev-suppressed-text' )
109            : $output->msg( 'rev-deleted-text-permission', $title->getPrefixedDBkey() );
110        // Core pairs its copy of this box with the Codex message-box styles; without
111        // them the box renders unstyled on the early-return paths, which emit nothing else.
112        $output->addModuleStyles( [ 'mediawiki.codex.messagebox.styles' ] );
113        $output->addHTML( Html::errorBox( $msg->parse() ) );
114
115        return false;
116    }
117
118}