MediaWiki  master
TextboxBuilder.php
Go to the documentation of this file.
1 <?php
25 namespace MediaWiki\EditPage;
26 
30 use Sanitizer;
31 use Title;
32 
39 
44  public function addNewLineAtEnd( $wikitext ) {
45  if ( strval( $wikitext ) !== '' ) {
46  // Ensure there's a newline at the end, otherwise adding lines
47  // is awkward.
48  // But don't add a newline if the text is empty, or Firefox in XHTML
49  // mode will show an extra newline. A bit annoying.
50  $wikitext .= "\n";
51  return $wikitext;
52  }
53  return $wikitext;
54  }
55 
61  public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
62  if ( $classes === [] ) {
63  return $attribs;
64  }
65 
67  $attribs,
68  [ 'class' => implode( ' ', $classes ) ]
69  );
70  }
71 
76  public function getTextboxProtectionCSSClasses( PageIdentity $page ) {
77  $classes = []; // Textarea CSS
78  $services = MediaWikiServices::getInstance();
79  if ( $services->getRestrictionStore()->isProtected( $page, 'edit' ) &&
80  $services->getPermissionManager()
81  ->getNamespaceRestrictionLevels( $page->getNamespace() ) !== [ '' ]
82  ) {
83  # Is the title semi-protected?
84  if ( $services->getRestrictionStore()->isSemiProtected( $page ) ) {
85  $classes[] = 'mw-textarea-sprotected';
86  } else {
87  # Then it must be protected based on static groups (regular)
88  $classes[] = 'mw-textarea-protected';
89  }
90  # Is the title cascade-protected?
91  if ( $services->getRestrictionStore()->isCascadeProtected( $page ) ) {
92  $classes[] = 'mw-textarea-cprotected';
93  }
94  }
95 
96  return $classes;
97  }
98 
106  public function buildTextboxAttribs(
107  $name, array $customAttribs, UserIdentity $user, PageIdentity $page
108  ) {
109  $attribs = $customAttribs + [
110  'accesskey' => ',',
111  'id' => $name,
112  'cols' => 80,
113  'rows' => 25,
114  // Avoid PHP notices when appending preferences
115  // (appending allows customAttribs['style'] to still work).
116  'style' => ''
117  ];
118 
119  // The following classes can be used here:
120  // * mw-editfont-monospace
121  // * mw-editfont-sans-serif
122  // * mw-editfont-serif
123  $userOptionsLookup = MediaWikiServices::getInstance()->getUserOptionsLookup();
124  $class = 'mw-editfont-' . $userOptionsLookup->getOption( $user, 'editfont' );
125 
126  if ( isset( $attribs['class'] ) ) {
127  if ( is_string( $attribs['class'] ) ) {
128  $attribs['class'] .= ' ' . $class;
129  } elseif ( is_array( $attribs['class'] ) ) {
130  $attribs['class'][] = $class;
131  }
132  } else {
133  $attribs['class'] = $class;
134  }
135 
137  $pageLang = $title->getPageLanguage();
138  $attribs['lang'] = $pageLang->getHtmlCode();
139  $attribs['dir'] = $pageLang->getDir();
140 
141  return $attribs;
142  }
143 
144 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MediaWiki\EditPage\TextboxBuilder\mergeClassesIntoAttributes
mergeClassesIntoAttributes(array $classes, array $attribs)
Definition: TextboxBuilder.php:61
MediaWiki\EditPage\TextboxBuilder\buildTextboxAttribs
buildTextboxAttribs( $name, array $customAttribs, UserIdentity $user, PageIdentity $page)
Definition: TextboxBuilder.php:106
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
MediaWiki\EditPage\TextboxBuilder
Helps EditPage build textboxes.
Definition: TextboxBuilder.php:38
Sanitizer\mergeAttributes
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:542
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
Title\castFromPageIdentity
static castFromPageIdentity(?PageIdentity $pageIdentity)
Return a Title for a given PageIdentity.
Definition: Title.php:331
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
Definition: MediaWikiServices.php:261
Page\PageReference\getNamespace
getNamespace()
Returns the page's namespace number.
$title
$title
Definition: testCompression.php:38
MediaWiki\EditPage\TextboxBuilder\addNewLineAtEnd
addNewLineAtEnd( $wikitext)
Definition: TextboxBuilder.php:44
$userOptionsLookup
UserOptionsLookup $userOptionsLookup
Definition: ApiWatchlistTrait.php:33
Title
Represents a title within MediaWiki.
Definition: Title.php:47
Sanitizer
HTML sanitizer for MediaWiki.
Definition: Sanitizer.php:35
MediaWiki\EditPage\TextboxBuilder\getTextboxProtectionCSSClasses
getTextboxProtectionCSSClasses(PageIdentity $page)
Definition: TextboxBuilder.php:76
MediaWiki\EditPage