2012-09-24 20:51:53 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-12-20 19:44:47 +00:00
|
|
|
* Content for JavaScript 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
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Content for JavaScript pages.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Content
|
|
|
|
|
*/
|
2012-09-24 20:51:53 +00:00
|
|
|
class JavaScriptContent extends TextContent {
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2014-09-23 22:41:03 +00:00
|
|
|
/**
|
|
|
|
|
* @var bool|Title|null
|
|
|
|
|
*/
|
|
|
|
|
private $redirectTarget = false;
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $text JavaScript code.
|
2014-09-13 22:31:18 +00:00
|
|
|
* @param string $modelId the content model name
|
2014-03-03 17:08:05 +00:00
|
|
|
*/
|
2014-09-13 22:31:18 +00:00
|
|
|
public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) {
|
|
|
|
|
parent::__construct( $text, $modelId );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a Content object with pre-save transformations applied using
|
|
|
|
|
* Parser::preSaveTransform().
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @param User $user
|
|
|
|
|
* @param ParserOptions $popts
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
|
|
|
|
* @return JavaScriptContent
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
|
|
|
|
public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) {
|
|
|
|
|
global $wgParser;
|
2013-05-15 01:12:35 +00:00
|
|
|
// @todo Make pre-save transformation optional for script pages
|
2018-10-04 07:06:00 +00:00
|
|
|
// See T34858
|
2012-09-24 20:51:53 +00:00
|
|
|
|
|
|
|
|
$text = $this->getNativeData();
|
|
|
|
|
$pst = $wgParser->preSaveTransform( $text, $title, $user, $popts );
|
|
|
|
|
|
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 JavaScript wrapped in a <pre> tag.
|
|
|
|
|
*/
|
2013-03-17 15:13:22 +00:00
|
|
|
protected function getHtml() {
|
2012-09-24 20:51:53 +00:00
|
|
|
$html = "";
|
|
|
|
|
$html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
|
2014-05-05 15:59:54 +00:00
|
|
|
$html .= htmlspecialchars( $this->getNativeData() );
|
2012-09-24 20:51:53 +00:00
|
|
|
$html .= "\n</pre>\n";
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2014-09-23 22:41:03 +00:00
|
|
|
/**
|
|
|
|
|
* If this page is a redirect, return the content
|
|
|
|
|
* if it should redirect to $target instead
|
|
|
|
|
*
|
|
|
|
|
* @param Title $target
|
|
|
|
|
* @return JavaScriptContent
|
|
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
$text = $this->getNativeData();
|
|
|
|
|
if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) {
|
|
|
|
|
// Extract the title from the url
|
|
|
|
|
preg_match( '/title=(.*?)\\\\u0026action=raw/', $text, $matches );
|
|
|
|
|
if ( isset( $matches[1] ) ) {
|
2018-10-30 02:30:59 +00:00
|
|
|
$title = Title::newFromText( urldecode( $matches[1] ) );
|
2014-09-23 22:41:03 +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-12-20 19:44:47 +00:00
|
|
|
}
|