Merge "Remove parameter 'options' from hook 'SkinEditSectionLinks'"
[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 MediaWiki\MediaWikiServices;
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 ( $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 MediaWikiServices::getInstance()->getNamespaceInfo()->
79 getRestrictionLevels( $title->getNamespace() ) !== [ '' ]
80 ) {
81 # Is the title semi-protected?
82 if ( $title->isSemiProtected() ) {
83 $classes[] = 'mw-textarea-sprotected';
84 } else {
85 # Then it must be protected based on static groups (regular)
86 $classes[] = 'mw-textarea-protected';
87 }
88 # Is the title cascade-protected?
89 if ( $title->isCascadeProtected() ) {
90 $classes[] = 'mw-textarea-cprotected';
91 }
92 }
93
94 return $classes;
95 }
96
97 /**
98 * @param string $name
99 * @param mixed[] $customAttribs
100 * @param User $user
101 * @param Title $title
102 * @return mixed[]
103 */
104 public function buildTextboxAttribs( $name, array $customAttribs, User $user, Title $title ) {
105 $attribs = $customAttribs + [
106 'accesskey' => ',',
107 'id' => $name,
108 'cols' => 80,
109 'rows' => 25,
110 // Avoid PHP notices when appending preferences
111 // (appending allows customAttribs['style'] to still work).
112 'style' => ''
113 ];
114
115 // The following classes can be used here:
116 // * mw-editfont-monospace
117 // * mw-editfont-sans-serif
118 // * mw-editfont-serif
119 $class = 'mw-editfont-' . $user->getOption( 'editfont' );
120
121 if ( isset( $attribs['class'] ) ) {
122 if ( is_string( $attribs['class'] ) ) {
123 $attribs['class'] .= ' ' . $class;
124 } elseif ( is_array( $attribs['class'] ) ) {
125 $attribs['class'][] = $class;
126 }
127 } else {
128 $attribs['class'] = $class;
129 }
130
131 $pageLang = $title->getPageLanguage();
132 $attribs['lang'] = $pageLang->getHtmlCode();
133 $attribs['dir'] = $pageLang->getDir();
134
135 return $attribs;
136 }
137
138 }