MediaWiki  master
TextboxBuilder.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\EditPage;
22 
28 
37 
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 
58  public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
59  if ( $classes === [] ) {
60  return $attribs;
61  }
62 
64  $attribs,
65  [ 'class' => implode( ' ', $classes ) ]
66  );
67  }
68 
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 
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 }
Helps EditPage build textboxes.
buildTextboxAttribs( $name, array $customAttribs, UserIdentity $user, PageIdentity $page)
mergeClassesIntoAttributes(array $classes, array $attribs)
getTextboxProtectionCSSClasses(PageIdentity $page)
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:46
static mergeAttributes( $a, $b)
Merge two sets of HTML attributes.
Definition: Sanitizer.php:680
Represents a title within MediaWiki.
Definition: Title.php:76
static newFromPageIdentity(PageIdentity $pageIdentity)
Return a Title for a given PageIdentity.
Definition: Title.php:329
Interface for objects (potentially) representing an editable wiki page.
getNamespace()
Returns the page's namespace number.
Interface for objects representing user identity.