Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TextboxBuilder
100.00% covered (success)
100.00%
34 / 34
100.00% covered (success)
100.00%
4 / 4
10
100.00% covered (success)
100.00%
1 / 1
 addNewLineAtEnd
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 mergeClassesIntoAttributes
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 getTextboxProtectionCSSClasses
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
5
 buildTextboxAttribs
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\EditPage;
22
23use MediaWiki\Html\Html;
24use MediaWiki\MediaWikiServices;
25use MediaWiki\Page\PageIdentity;
26use MediaWiki\Parser\Sanitizer;
27use MediaWiki\Title\Title;
28use MediaWiki\User\UserIdentity;
29
30/**
31 * Helps EditPage build textboxes
32 *
33 * @newable
34 * @since 1.31
35 * @author Kunal Mehta <legoktm@debian.org>
36 */
37class TextboxBuilder {
38
39    /**
40     * @param string $wikitext
41     * @return string
42     */
43    public function addNewLineAtEnd( $wikitext ) {
44        if ( strval( $wikitext ) !== '' ) {
45            // Ensure there's a newline at the end, otherwise adding lines
46            // is awkward.
47            // But don't add a newline if the text is empty, or Firefox in XHTML
48            // mode will show an extra newline. A bit annoying.
49            return $wikitext . "\n";
50        }
51        return $wikitext;
52    }
53
54    /**
55     * @param string[] $classes
56     * @param mixed[] $attribs
57     * @return mixed[]
58     * @deprecated since 1.44, use Html::addClass() instead
59     */
60    public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
61        if ( $classes === [] ) {
62            return $attribs;
63        }
64
65        return Sanitizer::mergeAttributes(
66            $attribs,
67            [ 'class' => implode( ' ', $classes ) ]
68        );
69    }
70
71    /**
72     * @param PageIdentity $page
73     * @return string[]
74     */
75    public function getTextboxProtectionCSSClasses( PageIdentity $page ) {
76        $classes = []; // Textarea CSS
77        $services = MediaWikiServices::getInstance();
78        if ( $services->getRestrictionStore()->isProtected( $page, 'edit' ) &&
79            $services->getPermissionManager()
80                ->getNamespaceRestrictionLevels( $page->getNamespace() ) !== [ '' ]
81        ) {
82            # Is the title semi-protected?
83            if ( $services->getRestrictionStore()->isSemiProtected( $page ) ) {
84                $classes[] = 'mw-textarea-sprotected';
85            } else {
86                # Then it must be protected based on static groups (regular)
87                $classes[] = 'mw-textarea-protected';
88            }
89            # Is the title cascade-protected?
90            if ( $services->getRestrictionStore()->isCascadeProtected( $page ) ) {
91                $classes[] = 'mw-textarea-cprotected';
92            }
93        }
94
95        return $classes;
96    }
97
98    /**
99     * @param string $name
100     * @param mixed[] $customAttribs
101     * @param UserIdentity $user
102     * @param PageIdentity $page
103     * @return mixed[]
104     */
105    public function buildTextboxAttribs(
106        $name, array $customAttribs, UserIdentity $user, PageIdentity $page
107    ) {
108        $attribs = $customAttribs + [
109            'accesskey' => ',',
110            'id' => $name,
111            'cols' => 80,
112            'rows' => 25,
113        ];
114
115        // The following classes can be used here:
116        // * mw-editfont-monospace
117        // * mw-editfont-sans-serif
118        // * mw-editfont-serif
119        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
120        $class = 'mw-editfont-' . $userOptionsLookup->getOption( $user, 'editfont' );
121        Html::addClass( $attribs['class'], $class );
122
123        $title = Title::newFromPageIdentity( $page );
124        $pageLang = $title->getPageLanguage();
125        $attribs['lang'] = $pageLang->getHtmlCode();
126        $attribs['dir'] = $pageLang->getDir();
127
128        return $attribs;
129    }
130
131}