Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
TextboxBuilder
100.00% covered (success)
100.00%
40 / 40
100.00% covered (success)
100.00%
4 / 4
13
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%
20 / 20
100.00% covered (success)
100.00%
1 / 1
4
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\MediaWikiServices;
24use MediaWiki\Page\PageIdentity;
25use MediaWiki\Parser\Sanitizer;
26use MediaWiki\Title\Title;
27use MediaWiki\User\UserIdentity;
28
29/**
30 * Helps EditPage build textboxes
31 *
32 * @newable
33 * @since 1.31
34 * @author Kunal Mehta <legoktm@debian.org>
35 */
36class TextboxBuilder {
37
38    /**
39     * @param string $wikitext
40     * @return string
41     */
42    public function addNewLineAtEnd( $wikitext ) {
43        if ( strval( $wikitext ) !== '' ) {
44            // Ensure there's a newline at the end, otherwise adding lines
45            // is awkward.
46            // But don't add a newline if the text is empty, or Firefox in XHTML
47            // mode will show an extra newline. A bit annoying.
48            return $wikitext . "\n";
49        }
50        return $wikitext;
51    }
52
53    /**
54     * @param string[] $classes
55     * @param mixed[] $attribs
56     * @return mixed[]
57     */
58    public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
59        if ( $classes === [] ) {
60            return $attribs;
61        }
62
63        return Sanitizer::mergeAttributes(
64            $attribs,
65            [ 'class' => implode( ' ', $classes ) ]
66        );
67    }
68
69    /**
70     * @param PageIdentity $page
71     * @return string[]
72     */
73    public function getTextboxProtectionCSSClasses( PageIdentity $page ) {
74        $classes = []; // Textarea CSS
75        $services = MediaWikiServices::getInstance();
76        if ( $services->getRestrictionStore()->isProtected( $page, 'edit' ) &&
77            $services->getPermissionManager()
78                ->getNamespaceRestrictionLevels( $page->getNamespace() ) !== [ '' ]
79        ) {
80            # Is the title semi-protected?
81            if ( $services->getRestrictionStore()->isSemiProtected( $page ) ) {
82                $classes[] = 'mw-textarea-sprotected';
83            } else {
84                # Then it must be protected based on static groups (regular)
85                $classes[] = 'mw-textarea-protected';
86            }
87            # Is the title cascade-protected?
88            if ( $services->getRestrictionStore()->isCascadeProtected( $page ) ) {
89                $classes[] = 'mw-textarea-cprotected';
90            }
91        }
92
93        return $classes;
94    }
95
96    /**
97     * @param string $name
98     * @param mixed[] $customAttribs
99     * @param UserIdentity $user
100     * @param PageIdentity $page
101     * @return mixed[]
102     */
103    public function buildTextboxAttribs(
104        $name, array $customAttribs, UserIdentity $user, PageIdentity $page
105    ) {
106        $attribs = $customAttribs + [
107            'accesskey' => ',',
108            'id' => $name,
109            'cols' => 80,
110            'rows' => 25,
111            // Avoid PHP notices when appending preferences
112            // (appending allows customAttribs['style'] to still work).
113            'style' => ''
114        ];
115
116        // The following classes can be used here:
117        // * mw-editfont-monospace
118        // * mw-editfont-sans-serif
119        // * mw-editfont-serif
120        $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
121        $class = 'mw-editfont-' . $userOptionsLookup->getOption( $user, 'editfont' );
122
123        if ( isset( $attribs['class'] ) ) {
124            if ( is_string( $attribs['class'] ) ) {
125                $attribs['class'] .= ' ' . $class;
126            } elseif ( is_array( $attribs['class'] ) ) {
127                $attribs['class'][] = $class;
128            }
129        } else {
130            $attribs['class'] = $class;
131        }
132
133        $title = Title::newFromPageIdentity( $page );
134        $pageLang = $title->getPageLanguage();
135        $attribs['lang'] = $pageLang->getHtmlCode();
136        $attribs['dir'] = $pageLang->getDir();
137
138        return $attribs;
139    }
140
141}