2012-09-24 20:51:53 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-12-20 19:44:47 +00:00
|
|
|
* Content object for CSS pages.
|
|
|
|
|
*
|
2012-10-16 18:04:32 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-10-05 13:03:24 +00:00
|
|
|
* @since 1.21
|
2012-10-16 18:04:32 +00:00
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @ingroup Content
|
|
|
|
|
*
|
|
|
|
|
* @author Daniel Kinzler
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
2012-12-20 19:44:47 +00:00
|
|
|
|
2019-04-11 13:36:15 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
2012-12-20 19:44:47 +00:00
|
|
|
/**
|
|
|
|
|
* Content object for CSS pages.
|
|
|
|
|
*
|
2020-06-26 12:56:03 +00:00
|
|
|
* @newable
|
2012-12-20 19:44:47 +00:00
|
|
|
* @ingroup Content
|
|
|
|
|
*/
|
2012-09-24 20:51:53 +00:00
|
|
|
class CssContent extends TextContent {
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2015-07-19 16:16:16 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool|Title|null
|
|
|
|
|
*/
|
|
|
|
|
private $redirectTarget = false;
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param string $text CSS code.
|
2014-09-13 22:31:18 +00:00
|
|
|
* @param string $modelId the content content model
|
2014-03-03 17:08:05 +00:00
|
|
|
*/
|
2014-09-13 22:31:18 +00:00
|
|
|
public function __construct( $text, $modelId = CONTENT_MODEL_CSS ) {
|
|
|
|
|
parent::__construct( $text, $modelId );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a Content object with pre-save transformations applied using
|
|
|
|
|
* Parser::preSaveTransform().
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param Title $title
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param ParserOptions $popts
|
|
|
|
|
*
|
|
|
|
|
* @return CssContent
|
|
|
|
|
*
|
|
|
|
|
* @see TextContent::preSaveTransform
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
|
|
|
|
public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
|
2013-05-15 01:12:35 +00:00
|
|
|
// @todo Make pre-save transformation optional for script pages
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2018-11-08 15:19:23 +00:00
|
|
|
$text = $this->getText();
|
2019-04-11 13:36:15 +00:00
|
|
|
$pst = MediaWikiServices::getInstance()->getParser()
|
|
|
|
|
->preSaveTransform( $text, $title, $user, $popts );
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2014-08-17 06:04:08 +00:00
|
|
|
return new static( $pst );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
|
|
|
|
* @return string CSS wrapped in a <pre> tag.
|
|
|
|
|
*/
|
2013-03-17 15:13:22 +00:00
|
|
|
protected function getHtml() {
|
2019-11-18 19:31:04 +00:00
|
|
|
return Html::element( 'pre',
|
|
|
|
|
[ 'class' => 'mw-code mw-css', 'dir' => 'ltr' ],
|
|
|
|
|
"\n" . $this->getText() . "\n"
|
|
|
|
|
) . "\n";
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2015-07-19 16:16:16 +00:00
|
|
|
/**
|
|
|
|
|
* @param Title $target
|
|
|
|
|
* @return CssContent
|
|
|
|
|
*/
|
|
|
|
|
public function updateRedirect( Title $target ) {
|
|
|
|
|
if ( !$this->isRedirect() ) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->getContentHandler()->makeRedirectContent( $target );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Title|null
|
|
|
|
|
*/
|
|
|
|
|
public function getRedirectTarget() {
|
|
|
|
|
if ( $this->redirectTarget !== false ) {
|
|
|
|
|
return $this->redirectTarget;
|
|
|
|
|
}
|
|
|
|
|
$this->redirectTarget = null;
|
2018-11-08 15:19:23 +00:00
|
|
|
$text = $this->getText();
|
2015-07-19 16:16:16 +00:00
|
|
|
if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
|
|
|
|
|
// Extract the title from the url
|
2019-01-15 16:54:44 +00:00
|
|
|
if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) {
|
2018-10-30 02:30:59 +00:00
|
|
|
$title = Title::newFromText( urldecode( $matches[1] ) );
|
2015-07-19 16:16:16 +00:00
|
|
|
if ( $title ) {
|
|
|
|
|
// Have a title, check that the current content equals what
|
|
|
|
|
// the redirect content should be
|
|
|
|
|
if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) {
|
|
|
|
|
$this->redirectTarget = $title;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->redirectTarget;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|