2012-09-24 20:51:53 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2012-12-20 19:44:47 +00:00
|
|
|
* Content object for wiki text 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
|
|
|
|
2024-08-06 13:40:20 +00:00
|
|
|
namespace MediaWiki\Content;
|
|
|
|
|
|
|
|
|
|
use Content;
|
|
|
|
|
use InvalidArgumentException;
|
2024-02-08 14:56:54 +00:00
|
|
|
use MediaWiki\Context\RequestContext;
|
2023-05-06 20:01:10 +00:00
|
|
|
use MediaWiki\HookContainer\HookRunner;
|
2022-04-26 15:48:03 +00:00
|
|
|
use MediaWiki\MainConfigNames;
|
2018-07-30 14:18:09 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
2022-12-09 12:28:41 +00:00
|
|
|
use MediaWiki\Parser\MagicWord;
|
2023-03-01 20:33:26 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2018-07-30 14:18:09 +00:00
|
|
|
|
2012-12-20 19:44:47 +00:00
|
|
|
/**
|
|
|
|
|
* Content object for wiki text 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 WikitextContent extends TextContent {
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2018-08-06 17:48:15 +00:00
|
|
|
/**
|
2021-07-21 01:03:59 +00:00
|
|
|
* @var string[] flags set by PST
|
2018-08-06 17:48:15 +00:00
|
|
|
*/
|
2021-07-21 01:03:59 +00:00
|
|
|
private $preSaveTransformFlags = [];
|
2018-08-06 17:48:15 +00:00
|
|
|
|
2020-06-26 12:56:03 +00:00
|
|
|
/**
|
2020-07-13 08:53:06 +00:00
|
|
|
* @stable to call
|
2020-06-26 12:56:03 +00:00
|
|
|
*
|
|
|
|
|
* @param string $text
|
|
|
|
|
*/
|
2012-09-24 20:51:53 +00:00
|
|
|
public function __construct( $text ) {
|
|
|
|
|
parent::__construct( $text, CONTENT_MODEL_WIKITEXT );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-12-14 16:01:47 +00:00
|
|
|
* @param string|int $sectionId
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
2022-07-31 00:02:18 +00:00
|
|
|
* @return Content|false|null
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
2012-09-24 20:51:53 +00:00
|
|
|
* @see Content::getSection()
|
|
|
|
|
*/
|
2014-06-12 14:05:18 +00:00
|
|
|
public function getSection( $sectionId ) {
|
2018-11-08 15:19:23 +00:00
|
|
|
$text = $this->getText();
|
2022-06-20 06:43:08 +00:00
|
|
|
$sect = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
|
2019-04-11 13:36:15 +00:00
|
|
|
->getSection( $text, $sectionId, false );
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2012-10-15 00:49:28 +00:00
|
|
|
if ( $sect === false ) {
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
2014-08-17 06:04:08 +00:00
|
|
|
return new static( $sect );
|
2012-10-15 00:49:28 +00:00
|
|
|
}
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-07-31 00:02:18 +00:00
|
|
|
* @param string|int|null|false $sectionId
|
2024-01-28 21:41:46 +00:00
|
|
|
* @param Content $with New section content, must have the same content model as $this.
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param string $sectionTitle
|
2024-08-06 13:40:20 +00:00
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return Content
|
|
|
|
|
*
|
2012-09-24 20:51:53 +00:00
|
|
|
* @see Content::replaceSection()
|
|
|
|
|
*/
|
2014-06-12 14:05:18 +00:00
|
|
|
public function replaceSection( $sectionId, Content $with, $sectionTitle = '' ) {
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-previous-line PhanParamSignatureMismatch False positive
|
2012-09-24 20:51:53 +00:00
|
|
|
$myModelId = $this->getModel();
|
|
|
|
|
$sectionModelId = $with->getModel();
|
|
|
|
|
|
2013-04-27 12:02:08 +00:00
|
|
|
if ( $sectionModelId != $myModelId ) {
|
2024-01-28 21:41:46 +00:00
|
|
|
throw new InvalidArgumentException( "Incompatible content model for section: " .
|
2012-09-24 20:51:53 +00:00
|
|
|
"document uses $myModelId but " .
|
|
|
|
|
"section uses $sectionModelId." );
|
|
|
|
|
}
|
2019-08-31 16:14:38 +00:00
|
|
|
/** @var self $with $oldtext */
|
|
|
|
|
'@phan-var self $with';
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2018-11-08 15:19:23 +00:00
|
|
|
$oldtext = $this->getText();
|
|
|
|
|
$text = $with->getText();
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2014-06-12 14:05:18 +00:00
|
|
|
if ( strval( $sectionId ) === '' ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
return $with; # XXX: copy first?
|
2013-11-19 21:26:16 +00:00
|
|
|
}
|
|
|
|
|
|
2014-06-12 14:05:18 +00:00
|
|
|
if ( $sectionId === 'new' ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
# Inserting a new section
|
2021-11-20 03:13:58 +00:00
|
|
|
$subject = strval( $sectionTitle ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
|
2017-12-07 02:54:03 +00:00
|
|
|
->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
|
2023-05-06 20:01:10 +00:00
|
|
|
$hookRunner = ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) );
|
|
|
|
|
if ( $hookRunner->onPlaceNewSection( $this, $oldtext, $subject, $text ) ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
$text = strlen( trim( $oldtext ) ) > 0
|
|
|
|
|
? "{$oldtext}\n\n{$subject}{$text}"
|
|
|
|
|
: "{$subject}{$text}";
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
# Replacing an existing section; roll out the big guns
|
2022-06-20 06:43:08 +00:00
|
|
|
$text = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
|
2019-04-11 13:36:15 +00:00
|
|
|
->replaceSection( $oldtext, $sectionId, $text );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2014-08-17 06:04:08 +00:00
|
|
|
$newContent = new static( $text );
|
2012-09-24 20:51:53 +00:00
|
|
|
|
|
|
|
|
return $newContent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a new WikitextContent object with the given section heading
|
|
|
|
|
* prepended.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param string $header
|
|
|
|
|
*
|
2012-09-24 20:51:53 +00:00
|
|
|
* @return Content
|
|
|
|
|
*/
|
|
|
|
|
public function addSectionHeader( $header ) {
|
2022-06-28 20:24:09 +00:00
|
|
|
$text = strval( $header ) !== '' ? wfMessage( 'newsectionheaderdefaultlevel' )
|
|
|
|
|
->plaintextParams( $header )->inContentLanguage()->text() . "\n\n" : '';
|
2018-11-08 15:19:23 +00:00
|
|
|
$text .= $this->getText();
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2014-08-17 06:04:08 +00:00
|
|
|
return new static( $text );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-01-04 22:07:33 +00:00
|
|
|
* Extract the redirect target and the remaining text on the page.
|
2012-09-24 20:51:53 +00:00
|
|
|
*
|
2014-01-04 22:07:33 +00:00
|
|
|
* @since 1.23
|
2023-09-18 07:39:10 +00:00
|
|
|
* @deprecated since 1.41, use WikitextContentHandler::getRedirectTargetAndText
|
2014-03-03 17:08:05 +00:00
|
|
|
*
|
|
|
|
|
* @return array List of two elements: Title|null and string.
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
2023-09-18 07:39:10 +00:00
|
|
|
public function getRedirectTargetAndText() {
|
|
|
|
|
wfDeprecated( __METHOD__, '1.41' );
|
2013-11-19 21:26:16 +00:00
|
|
|
|
2023-09-18 07:39:10 +00:00
|
|
|
$handler = $this->getContentHandler();
|
|
|
|
|
[ $target, $content ] = $handler->extractRedirectTargetAndText( $this );
|
2013-11-19 21:26:16 +00:00
|
|
|
|
2023-09-18 07:39:10 +00:00
|
|
|
return [ Title::castFromLinkTarget( $target ), $content->getText() ];
|
2014-01-04 22:07:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Implement redirect extraction for wikitext.
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return Title|null
|
2014-01-04 22:07:33 +00:00
|
|
|
*
|
|
|
|
|
* @see Content::getRedirectTarget
|
|
|
|
|
*/
|
|
|
|
|
public function getRedirectTarget() {
|
2023-09-18 07:39:10 +00:00
|
|
|
// TODO: The redirect target should be injected on construction.
|
|
|
|
|
// But that only works if the object is created by WikitextContentHandler.
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2023-09-18 07:39:10 +00:00
|
|
|
$handler = $this->getContentHandler();
|
|
|
|
|
[ $target, ] = $handler->extractRedirectTargetAndText( $this );
|
|
|
|
|
|
|
|
|
|
return Title::castFromLinkTarget( $target );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This implementation replaces the first link on the page with the given new target
|
|
|
|
|
* if this Content object is a redirect. Otherwise, this method returns $this.
|
|
|
|
|
*
|
2012-10-05 13:03:24 +00:00
|
|
|
* @since 1.21
|
2012-09-24 20:51:53 +00:00
|
|
|
*
|
|
|
|
|
* @param Title $target
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return Content
|
|
|
|
|
*
|
|
|
|
|
* @see Content::updateRedirect()
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
|
|
|
|
public function updateRedirect( Title $target ) {
|
|
|
|
|
if ( !$this->isRedirect() ) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# Fix the text
|
|
|
|
|
# Remember that redirect pages can have categories, templates, etc.,
|
|
|
|
|
# so the regex has to be fairly general
|
|
|
|
|
$newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
|
|
|
|
|
'[[' . $target->getFullText() . ']]',
|
2018-11-08 15:19:23 +00:00
|
|
|
$this->getText(), 1 );
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2014-08-17 06:04:08 +00:00
|
|
|
return new static( $newText );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if this content is not a redirect, and this content's text
|
|
|
|
|
* is countable according to the criteria defined by $wgArticleCountMethod.
|
|
|
|
|
*
|
2016-02-25 13:13:22 +00:00
|
|
|
* @param bool|null $hasLinks If it is known whether this content contains
|
2012-09-24 20:51:53 +00:00
|
|
|
* links, provide this information here, to avoid redundant parsing to
|
2012-10-22 10:29:52 +00:00
|
|
|
* find out (default: null).
|
2016-02-25 13:13:22 +00:00
|
|
|
* @param Title|null $title Optional title, defaults to the title from the current main request.
|
2012-09-24 20:51:53 +00:00
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return bool
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
|
|
|
|
public function isCountable( $hasLinks = null, Title $title = null ) {
|
2022-04-26 15:48:03 +00:00
|
|
|
$articleCountMethod = MediaWikiServices::getInstance()->getMainConfig()
|
|
|
|
|
->get( MainConfigNames::ArticleCountMethod );
|
2012-09-24 20:51:53 +00:00
|
|
|
|
2013-03-17 15:13:22 +00:00
|
|
|
if ( $this->isRedirect() ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-06 18:44:56 +00:00
|
|
|
if ( $articleCountMethod === 'link' ) {
|
2018-02-28 01:45:27 +00:00
|
|
|
if ( $hasLinks === null ) { # not known, find out
|
2020-10-20 22:53:48 +00:00
|
|
|
// @TODO: require an injected title
|
2018-02-28 01:45:27 +00:00
|
|
|
if ( !$title ) {
|
|
|
|
|
$context = RequestContext::getMain();
|
|
|
|
|
$title = $context->getTitle();
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
2021-10-14 14:01:58 +00:00
|
|
|
$contentRenderer = MediaWikiServices::getInstance()->getContentRenderer();
|
2021-10-25 19:15:52 +00:00
|
|
|
// @phan-suppress-next-line PhanTypeMismatchArgumentNullable getTitle does not return null here
|
2021-10-14 14:01:58 +00:00
|
|
|
$po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
|
2018-02-28 01:45:27 +00:00
|
|
|
$links = $po->getLinks();
|
2023-09-08 21:18:11 +00:00
|
|
|
$hasLinks = $links !== [];
|
2018-02-28 01:45:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $hasLinks;
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2018-02-28 01:45:27 +00:00
|
|
|
return true;
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
|
2014-03-03 17:08:05 +00:00
|
|
|
/**
|
|
|
|
|
* @param int $maxlength
|
2024-08-06 13:40:20 +00:00
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2012-09-24 20:51:53 +00:00
|
|
|
public function getTextForSummary( $maxlength = 250 ) {
|
|
|
|
|
$truncatedtext = parent::getTextForSummary( $maxlength );
|
|
|
|
|
|
|
|
|
|
# clean up unfinished links
|
|
|
|
|
# XXX: make this optional? wasn't there in autosummary, but required for
|
|
|
|
|
# deletion summary.
|
|
|
|
|
$truncatedtext = preg_replace( '/\[\[([^\]]*)\]?$/', '$1', $truncatedtext );
|
|
|
|
|
|
|
|
|
|
return $truncatedtext;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This implementation calls $word->match() on the this TextContent object's text.
|
|
|
|
|
*
|
|
|
|
|
* @param MagicWord $word
|
|
|
|
|
*
|
2014-03-03 17:08:05 +00:00
|
|
|
* @return bool
|
|
|
|
|
*
|
|
|
|
|
* @see Content::matchMagicWord()
|
2012-09-24 20:51:53 +00:00
|
|
|
*/
|
|
|
|
|
public function matchMagicWord( MagicWord $word ) {
|
2018-11-08 15:19:23 +00:00
|
|
|
return $word->match( $this->getText() );
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
2014-03-03 17:08:05 +00:00
|
|
|
|
2021-07-21 01:03:59 +00:00
|
|
|
/**
|
|
|
|
|
* Records flags set by preSaveTransform
|
2024-08-06 13:40:20 +00:00
|
|
|
*
|
2021-07-21 01:03:59 +00:00
|
|
|
* @internal for use by WikitextContentHandler
|
2024-08-06 13:40:20 +00:00
|
|
|
*
|
2021-07-21 01:03:59 +00:00
|
|
|
* @param string[] $flags
|
|
|
|
|
*/
|
|
|
|
|
public function setPreSaveTransformFlags( array $flags ) {
|
|
|
|
|
$this->preSaveTransformFlags = $flags;
|
|
|
|
|
}
|
2021-08-24 12:17:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Records flags set by preSaveTransform
|
2024-08-06 13:40:20 +00:00
|
|
|
*
|
2021-08-24 12:17:12 +00:00
|
|
|
* @internal for use by WikitextContentHandler
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function getPreSaveTransformFlags() {
|
|
|
|
|
return $this->preSaveTransformFlags;
|
|
|
|
|
}
|
2023-09-18 07:39:10 +00:00
|
|
|
|
|
|
|
|
public function getContentHandler(): WikitextContentHandler {
|
|
|
|
|
$handler = parent::getContentHandler();
|
|
|
|
|
'@phan-var WikitextContentHandler $handler';
|
2024-08-06 13:40:20 +00:00
|
|
|
|
2023-09-18 07:39:10 +00:00
|
|
|
return $handler;
|
|
|
|
|
}
|
2012-10-15 00:49:28 +00:00
|
|
|
}
|
2024-08-06 13:40:20 +00:00
|
|
|
|
|
|
|
|
/** @deprecated class alias since 1.43 */
|
|
|
|
|
class_alias( WikitextContent::class, 'WikitextContent' );
|