TextboxBuilder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. namespace MediaWiki\EditPage;
  25. use MWNamespace;
  26. use Sanitizer;
  27. use Title;
  28. use User;
  29. /**
  30. * Helps EditPage build textboxes
  31. *
  32. * @since 1.31
  33. */
  34. class TextboxBuilder {
  35. /**
  36. * @param string $wikitext
  37. * @return string
  38. */
  39. public function addNewLineAtEnd( $wikitext ) {
  40. if ( strval( $wikitext ) !== '' ) {
  41. // Ensure there's a newline at the end, otherwise adding lines
  42. // is awkward.
  43. // But don't add a newline if the text is empty, or Firefox in XHTML
  44. // mode will show an extra newline. A bit annoying.
  45. $wikitext .= "\n";
  46. return $wikitext;
  47. }
  48. return $wikitext;
  49. }
  50. /**
  51. * @param string[] $classes
  52. * @param mixed[] $attribs
  53. * @return mixed[]
  54. */
  55. public function mergeClassesIntoAttributes( array $classes, array $attribs ) {
  56. if ( !count( $classes ) ) {
  57. return $attribs;
  58. }
  59. return Sanitizer::mergeAttributes(
  60. $attribs,
  61. [ 'class' => implode( ' ', $classes ) ]
  62. );
  63. }
  64. /**
  65. * @param Title $title
  66. * @return string[]
  67. */
  68. public function getTextboxProtectionCSSClasses( Title $title ) {
  69. $classes = []; // Textarea CSS
  70. if ( $title->isProtected( 'edit' ) &&
  71. MWNamespace::getRestrictionLevels( $title->getNamespace() ) !== [ '' ]
  72. ) {
  73. # Is the title semi-protected?
  74. if ( $title->isSemiProtected() ) {
  75. $classes[] = 'mw-textarea-sprotected';
  76. } else {
  77. # Then it must be protected based on static groups (regular)
  78. $classes[] = 'mw-textarea-protected';
  79. }
  80. # Is the title cascade-protected?
  81. if ( $title->isCascadeProtected() ) {
  82. $classes[] = 'mw-textarea-cprotected';
  83. }
  84. }
  85. return $classes;
  86. }
  87. /**
  88. * @param string $name
  89. * @param mixed[] $customAttribs
  90. * @param User $user
  91. * @param Title $title
  92. * @return mixed[]
  93. */
  94. public function buildTextboxAttribs( $name, array $customAttribs, User $user, Title $title ) {
  95. $attribs = $customAttribs + [
  96. 'accesskey' => ',',
  97. 'id' => $name,
  98. 'cols' => 80,
  99. 'rows' => 25,
  100. // Avoid PHP notices when appending preferences
  101. // (appending allows customAttribs['style'] to still work).
  102. 'style' => ''
  103. ];
  104. // The following classes can be used here:
  105. // * mw-editfont-monospace
  106. // * mw-editfont-sans-serif
  107. // * mw-editfont-serif
  108. $class = 'mw-editfont-' . $user->getOption( 'editfont' );
  109. if ( isset( $attribs['class'] ) ) {
  110. if ( is_string( $attribs['class'] ) ) {
  111. $attribs['class'] .= ' ' . $class;
  112. } elseif ( is_array( $attribs['class'] ) ) {
  113. $attribs['class'][] = $class;
  114. }
  115. } else {
  116. $attribs['class'] = $class;
  117. }
  118. $pageLang = $title->getPageLanguage();
  119. $attribs['lang'] = $pageLang->getHtmlCode();
  120. $attribs['dir'] = $pageLang->getDir();
  121. return $attribs;
  122. }
  123. }