MediaWiki master
TextboxBuilder.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\EditPage;
8
18
26
27 private readonly PermissionManager $permissionManager;
28 private readonly RestrictionStore $restrictionStore;
29 private readonly UserOptionsLookup $userOptionsLookup;
30
36 public function __construct(
37 ?PermissionManager $permissionManager = null,
38 ?RestrictionStore $restrictionStore = null,
39 ?UserOptionsLookup $userOptionsLookup = null,
40 ) {
41 $this->permissionManager = $permissionManager ?? MediaWikiServices::getInstance()->getPermissionManager();
42 $this->restrictionStore = $restrictionStore ?? MediaWikiServices::getInstance()->getRestrictionStore();
43 $this->userOptionsLookup = $userOptionsLookup ?? MediaWikiServices::getInstance()->getUserOptionsLookup();
44 }
45
50 public function addNewLineAtEnd( $wikitext ) {
51 if ( strval( $wikitext ) !== '' ) {
52 // Ensure there's a newline at the end, otherwise adding lines
53 // is awkward.
54 // But don't add a newline if the text is empty, or Firefox in XHTML
55 // mode will show an extra newline. A bit annoying.
56 return $wikitext . "\n";
57 }
58 return $wikitext;
59 }
60
67 public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
68 if ( $classes === [] ) {
69 return $attribs;
70 }
71
72 return Sanitizer::mergeAttributes(
73 $attribs,
74 [ 'class' => implode( ' ', $classes ) ]
75 );
76 }
77
83 $classes = []; // Textarea CSS
84 if ( $this->restrictionStore->isProtected( $page, 'edit' ) &&
85 $this->permissionManager->getNamespaceRestrictionLevels( $page->getNamespace() ) !== [ '' ]
86 ) {
87 # Is the title semi-protected?
88 if ( $this->restrictionStore->isSemiProtected( $page ) ) {
89 $classes[] = 'mw-textarea-sprotected';
90 } else {
91 # Then it must be protected based on static groups (regular)
92 $classes[] = 'mw-textarea-protected';
93 }
94 # Is the title cascade-protected?
95 if ( $this->restrictionStore->isCascadeProtected( $page ) ) {
96 $classes[] = 'mw-textarea-cprotected';
97 }
98 }
99
100 return $classes;
101 }
102
110 public function buildTextboxAttribs(
111 $name, array $customAttribs, UserIdentity $user, PageIdentity $page
112 ) {
113 $attribs = $customAttribs + [
114 'accesskey' => ',',
115 'id' => $name,
116 'cols' => 80,
117 'rows' => 25,
118 ];
119
120 // The following classes can be used here:
121 // * mw-editfont-monospace
122 // * mw-editfont-sans-serif
123 // * mw-editfont-serif
124 $class = 'mw-editfont-' . $this->userOptionsLookup->getOption( $user, 'editfont' );
125 Html::addClass( $attribs['class'], $class );
126
127 $title = Title::newFromPageIdentity( $page );
128 $pageLang = $title->getPageLanguage();
129 $attribs['lang'] = $pageLang->getHtmlCode();
130 $attribs['dir'] = $pageLang->getDir();
131
132 return $attribs;
133 }
134
135}
Helps EditPage build textboxes.
buildTextboxAttribs( $name, array $customAttribs, UserIdentity $user, PageIdentity $page)
mergeClassesIntoAttributes(array $classes, array $attribs)
getTextboxProtectionCSSClasses(PageIdentity $page)
__construct(?PermissionManager $permissionManager=null, ?RestrictionStore $restrictionStore=null, ?UserOptionsLookup $userOptionsLookup=null,)
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:34
A service class for checking permissions To obtain an instance, use MediaWikiServices::getInstance()-...
Represents a title within MediaWiki.
Definition Title.php:69
Provides access to user options.
Interface for objects (potentially) representing an editable wiki page.
getNamespace()
Returns the page's namespace number.
Interface for objects representing user identity.