| 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 | |
| 28 | namespace MediaWiki\Content; |
| 29 | |
| 30 | use MediaWiki\Title\Title; |
| 31 | |
| 32 | /** |
| 33 | * Content for JavaScript pages. |
| 34 | * |
| 35 | * @newable |
| 36 | * @ingroup Content |
| 37 | */ |
| 38 | class JavaScriptContent extends TextContent { |
| 39 | |
| 40 | /** |
| 41 | * @var Title|null|false |
| 42 | */ |
| 43 | private $redirectTarget = false; |
| 44 | |
| 45 | /** |
| 46 | * @stable to call |
| 47 | * @param string $text JavaScript code. |
| 48 | * @param string $modelId the content model name |
| 49 | */ |
| 50 | public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) { |
| 51 | parent::__construct( $text, $modelId ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * If this page is a redirect, return the content |
| 56 | * if it should redirect to $target instead |
| 57 | * |
| 58 | * @param Title $target |
| 59 | * @return JavaScriptContent |
| 60 | */ |
| 61 | public function updateRedirect( Title $target ) { |
| 62 | if ( !$this->isRedirect() ) { |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | // @phan-suppress-next-line PhanTypeMismatchReturnSuperType False positive |
| 67 | return $this->getContentHandler()->makeRedirectContent( $target ); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @return Title|null |
| 72 | */ |
| 73 | public function getRedirectTarget() { |
| 74 | if ( $this->redirectTarget !== false ) { |
| 75 | return $this->redirectTarget; |
| 76 | } |
| 77 | $this->redirectTarget = null; |
| 78 | $text = $this->getText(); |
| 79 | if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) { |
| 80 | // Compatiblity with pages created by MW 1.41 and earlier: |
| 81 | // Older redirects use an over-escaped \u0026 instead of a literal ampersand (T107289) |
| 82 | $text = str_replace( '\u0026', '&', $text ); |
| 83 | // Extract the title from the url |
| 84 | if ( preg_match( '/title=(.*?)&action=raw/', $text, $matches ) ) { |
| 85 | $title = Title::newFromText( urldecode( $matches[1] ) ); |
| 86 | if ( $title ) { |
| 87 | // Have a title, check that the current content equals what |
| 88 | // the redirect content should be |
| 89 | $expected = $this->getContentHandler()->makeRedirectContent( $title ); |
| 90 | '@phan-var JavaScriptContent $expected'; |
| 91 | if ( $expected->getText() === $text ) { |
| 92 | $this->redirectTarget = $title; |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return $this->redirectTarget; |
| 99 | } |
| 100 | |
| 101 | } |
| 102 | /** @deprecated class alias since 1.43 */ |
| 103 | class_alias( JavaScriptContent::class, 'JavaScriptContent' ); |