Merge "Add attributes parameter to ShowSearchHitTitle"
[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 Title;
28 use User;
29
30 /**
31 * Helps EditPage build textboxes
32 *
33 * @since 1.31
34 */
35 class TextboxBuilder {
36
37 /**
38 * @param string $wikitext
39 * @return string
40 */
41 public function addNewLineAtEnd( $wikitext ) {
42 if ( strval( $wikitext ) !== '' ) {
43 // Ensure there's a newline at the end, otherwise adding lines
44 // is awkward.
45 // But don't add a newline if the text is empty, or Firefox in XHTML
46 // mode will show an extra newline. A bit annoying.
47 $wikitext .= "\n";
48 return $wikitext;
49 }
50 return $wikitext;
51 }
52
53 /**
54 * @param string $name
55 * @param mixed[] $customAttribs
56 * @param User $user
57 * @param Title $title
58 * @return mixed[]
59 */
60 public function buildTextboxAttribs( $name, array $customAttribs, User $user, Title $title ) {
61 $attribs = $customAttribs + [
62 'accesskey' => ',',
63 'id' => $name,
64 'cols' => 80,
65 'rows' => 25,
66 // Avoid PHP notices when appending preferences
67 // (appending allows customAttribs['style'] to still work).
68 'style' => ''
69 ];
70
71 // The following classes can be used here:
72 // * mw-editfont-monospace
73 // * mw-editfont-sans-serif
74 // * mw-editfont-serif
75 $class = 'mw-editfont-' . $user->getOption( 'editfont' );
76
77 if ( isset( $attribs['class'] ) ) {
78 if ( is_string( $attribs['class'] ) ) {
79 $attribs['class'] .= ' ' . $class;
80 } elseif ( is_array( $attribs['class'] ) ) {
81 $attribs['class'][] = $class;
82 }
83 } else {
84 $attribs['class'] = $class;
85 }
86
87 $pageLang = $title->getPageLanguage();
88 $attribs['lang'] = $pageLang->getHtmlCode();
89 $attribs['dir'] = $pageLang->getDir();
90
91 return $attribs;
92 }
93
94 }