CssContent.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Content object for CSS 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. /**
  28. * Content object for CSS pages.
  29. *
  30. * @ingroup Content
  31. */
  32. class CssContent extends TextContent {
  33. /**
  34. * @var bool|Title|null
  35. */
  36. private $redirectTarget = false;
  37. /**
  38. * @param string $text CSS code.
  39. * @param string $modelId the content content model
  40. */
  41. public function __construct( $text, $modelId = CONTENT_MODEL_CSS ) {
  42. parent::__construct( $text, $modelId );
  43. }
  44. /**
  45. * Returns a Content object with pre-save transformations applied using
  46. * Parser::preSaveTransform().
  47. *
  48. * @param Title $title
  49. * @param User $user
  50. * @param ParserOptions $popts
  51. *
  52. * @return CssContent
  53. *
  54. * @see TextContent::preSaveTransform
  55. */
  56. public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
  57. global $wgParser;
  58. // @todo Make pre-save transformation optional for script pages
  59. $text = $this->getNativeData();
  60. $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
  61. return new static( $pst );
  62. }
  63. /**
  64. * @return string CSS wrapped in a <pre> tag.
  65. */
  66. protected function getHtml() {
  67. $html = "";
  68. $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
  69. $html .= htmlspecialchars( $this->getNativeData() );
  70. $html .= "\n</pre>\n";
  71. return $html;
  72. }
  73. /**
  74. * @param Title $target
  75. * @return CssContent
  76. */
  77. public function updateRedirect( Title $target ) {
  78. if ( !$this->isRedirect() ) {
  79. return $this;
  80. }
  81. return $this->getContentHandler()->makeRedirectContent( $target );
  82. }
  83. /**
  84. * @return Title|null
  85. */
  86. public function getRedirectTarget() {
  87. if ( $this->redirectTarget !== false ) {
  88. return $this->redirectTarget;
  89. }
  90. $this->redirectTarget = null;
  91. $text = $this->getNativeData();
  92. if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
  93. // Extract the title from the url
  94. preg_match( '/title=(.*?)&action=raw/', $text, $matches );
  95. if ( isset( $matches[1] ) ) {
  96. $title = Title::newFromText( $matches[1] );
  97. if ( $title ) {
  98. // Have a title, check that the current content equals what
  99. // the redirect content should be
  100. if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
  101. $this->redirectTarget = $title;
  102. }
  103. }
  104. }
  105. }
  106. return $this->redirectTarget;
  107. }
  108. }