Debian Codegraph @

includes/editpage/TextboxBuilder.php

raw symbols SCIP
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
21 namespace MediaWiki\EditPage;
22
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Page\PageIdentity;
25 use MediaWiki\Parser\Sanitizer;
26 use MediaWiki\Title\Title;
27 use MediaWiki\User\UserIdentity;
28
29 /**
30 * Helps EditPage build textboxes
31 *
32 * @newable
33 * @since 1.31
34 * @author Kunal Mehta <[email protected]>
35 */
36 class 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 }