Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / editpage / TextboxBuilder.php
1 <?php
2 /**
3 * Helps EditPage build textboxes
4 *
5 * (C) Copyright 2017 Kunal Mehta <legoktm@member.fsf.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
21 *
22 * @file
23 */
24
25 namespace MediaWiki\EditPage;
26
27 use MWNamespace;
28 use Sanitizer;
29 use Title;
30 use User;
31
32 /**
33 * Helps EditPage build textboxes
34 *
35 * @since 1.31
36 */
37 class 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 $wikitext .= "\n";
50 return $wikitext;
51 }
52 return $wikitext;
53 }
54
55 /**
56 * @param string[] $classes
57 * @param mixed[] $attribs
58 * @return mixed[]
59 */
60 public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
61 if ( !count( $classes ) ) {
62 return $attribs;
63 }
64
65 return Sanitizer::mergeAttributes(
66 $attribs,
67 [ 'class' => implode( ' ', $classes ) ]
68 );
69 }
70
71 /**
72 * @param Title $title
73 * @return string[]
74 */
75 public function getTextboxProtectionCSSClasses( Title $title ) {
76 $classes = []; // Textarea CSS
77 if ( $title->isProtected( 'edit' ) &&
78 MWNamespace::getRestrictionLevels( $title->getNamespace() ) !== [ '' ]
79 ) {
80 # Is the title semi-protected?
81 if ( $title->isSemiProtected() ) {
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 ( $title->isCascadeProtected() ) {
89 $classes[] = 'mw-textarea-cprotected';
90 }
91 }
92
93 return $classes;
94 }
95
96 /**
97 * @param string $name
98 * @param mixed[] $customAttribs
99 * @param User $user
100 * @param Title $title
101 * @return mixed[]
102 */
103 public function buildTextboxAttribs( $name, array $customAttribs, User $user, Title $title ) {
104 $attribs = $customAttribs + [
105 'accesskey' => ',',
106 'id' => $name,
107 'cols' => 80,
108 'rows' => 25,
109 // Avoid PHP notices when appending preferences
110 // (appending allows customAttribs['style'] to still work).
111 'style' => ''
112 ];
113
114 // The following classes can be used here:
115 // * mw-editfont-monospace
116 // * mw-editfont-sans-serif
117 // * mw-editfont-serif
118 $class = 'mw-editfont-' . $user->getOption( 'editfont' );
119
120 if ( isset( $attribs['class'] ) ) {
121 if ( is_string( $attribs['class'] ) ) {
122 $attribs['class'] .= ' ' . $class;
123 } elseif ( is_array( $attribs['class'] ) ) {
124 $attribs['class'][] = $class;
125 }
126 } else {
127 $attribs['class'] = $class;
128 }
129
130 $pageLang = $title->getPageLanguage();
131 $attribs['lang'] = $pageLang->getHtmlCode();
132 $attribs['dir'] = $pageLang->getDir();
133
134 return $attribs;
135 }
136
137 }