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