JavaScriptContent.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /**
  3. * Content for JavaScript pages.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @since 1.21
  21. *
  22. * @file
  23. * @ingroup Content
  24. *
  25. * @author Daniel Kinzler
  26. */
  27. use MediaWiki\MediaWikiServices;
  28. /**
  29. * Content for JavaScript pages.
  30. *
  31. * @ingroup Content
  32. */
  33. class JavaScriptContent extends TextContent {
  34. /**
  35. * @var bool|Title|null
  36. */
  37. private $redirectTarget = false;
  38. /**
  39. * @param string $text JavaScript code.
  40. * @param string $modelId the content model name
  41. */
  42. public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
  43. parent::__construct( $text, $modelId );
  44. }
  45. /**
  46. * Returns a Content object with pre-save transformations applied using
  47. * Parser::preSaveTransform().
  48. *
  49. * @param Title $title
  50. * @param User $user
  51. * @param ParserOptions $popts
  52. *
  53. * @return JavaScriptContent
  54. */
  55. public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
  56. // @todo Make pre-save transformation optional for script pages
  57. // See T34858
  58. $text = $this->getText();
  59. $pst = MediaWikiServices::getInstance()->getParser()
  60. ->preSaveTransform( $text, $title, $user, $popts );
  61. return new static( $pst );
  62. }
  63. /**
  64. * @return string JavaScript wrapped in a <pre> tag.
  65. */
  66. protected function getHtml() {
  67. $html = "";
  68. $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
  69. $html .= htmlspecialchars( $this->getText() );
  70. $html .= "\n</pre>\n";
  71. return $html;
  72. }
  73. /**
  74. * If this page is a redirect, return the content
  75. * if it should redirect to $target instead
  76. *
  77. * @param Title $target
  78. * @return JavaScriptContent
  79. */
  80. public function updateRedirect( Title $target ) {
  81. if ( !$this->isRedirect() ) {
  82. return $this;
  83. }
  84. return $this->getContentHandler()->makeRedirectContent( $target );
  85. }
  86. /**
  87. * @return Title|null
  88. */
  89. public function getRedirectTarget() {
  90. if ( $this->redirectTarget !== false ) {
  91. return $this->redirectTarget;
  92. }
  93. $this->redirectTarget = null;
  94. $text = $this->getText();
  95. if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
  96. // Extract the title from the url
  97. preg_match( '/title=(.*?)\\\\u0026action=raw/', $text, $matches );
  98. if ( isset( $matches[1] ) ) {
  99. $title = Title::newFromText( urldecode( $matches[1] ) );
  100. if ( $title ) {
  101. // Have a title, check that the current content equals what
  102. // the redirect content should be
  103. if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
  104. $this->redirectTarget = $title;
  105. }
  106. }
  107. }
  108. }
  109. return $this->redirectTarget;
  110. }
  111. }