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
|
|
|
|
2018-07-30 14:18:09 +00:00
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
|
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-05-02 20:16:51 +00:00
|
|
|
private $redirectTargetAndText = null;
|
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
|
|
|
*
|
|
|
|
|
* @return Content|bool|null
|
|
|
|
|
*
|
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();
|
2019-04-11 13:36:15 +00:00
|
|
|
$sect = MediaWikiServices::getInstance()->getParser()
|
|
|
|
|
->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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-12-14 16:01:47 +00:00
|
|
|
* @param string|int|null|bool $sectionId
|
2014-03-03 17:08:05 +00:00
|
|
|
* @param Content $with
|
|
|
|
|
* @param string $sectionTitle
|
|
|
|
|
*
|
|
|
|
|
* @throws MWException
|
|
|
|
|
* @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 = '' ) {
|
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 ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
throw new MWException( "Incompatible content model for section: " .
|
|
|
|
|
"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
|
|
|
|
|
$subject = $sectionTitle ? wfMessage( 'newsectionheaderdefaultlevel' )
|
2017-12-07 02:54:03 +00:00
|
|
|
->plaintextParams( $sectionTitle )->inContentLanguage()->text() . "\n\n" : '';
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
if ( Hooks::runner()->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
|
2019-04-11 13:36:15 +00:00
|
|
|
$text = MediaWikiServices::getInstance()->getParser()
|
|
|
|
|
->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 ) {
|
|
|
|
|
$text = wfMessage( 'newsectionheaderdefaultlevel' )
|
2012-10-08 11:58:54 +00:00
|
|
|
->rawParams( $header )->inContentLanguage()->text();
|
2012-09-24 20:51:53 +00:00
|
|
|
$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-07-24 09:30:25 +00:00
|
|
|
* @note migrated here from Title::newFromRedirectInternal()
|
2012-09-24 20:51:53 +00:00
|
|
|
*
|
2014-01-04 22:07:33 +00:00
|
|
|
* @since 1.23
|
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
|
|
|
*/
|
2021-08-24 12:17:12 +00:00
|
|
|
public function getRedirectTargetAndText() {
|
2012-09-24 20:51:53 +00:00
|
|
|
global $wgMaxRedirects;
|
2014-05-02 20:16:51 +00:00
|
|
|
|
|
|
|
|
if ( $this->redirectTargetAndText !== null ) {
|
|
|
|
|
return $this->redirectTargetAndText;
|
|
|
|
|
}
|
|
|
|
|
|
2012-09-24 20:51:53 +00:00
|
|
|
if ( $wgMaxRedirects < 1 ) {
|
|
|
|
|
// redirects are disabled, so quit early
|
2018-11-08 15:19:23 +00:00
|
|
|
$this->redirectTargetAndText = [ null, $this->getText() ];
|
2014-05-02 20:16:51 +00:00
|
|
|
return $this->redirectTargetAndText;
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
2014-05-02 20:16:51 +00:00
|
|
|
|
2018-07-30 14:18:09 +00:00
|
|
|
$redir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
|
2018-11-08 15:19:23 +00:00
|
|
|
$text = ltrim( $this->getText() );
|
2012-09-24 20:51:53 +00:00
|
|
|
if ( $redir->matchStartAndRemove( $text ) ) {
|
|
|
|
|
// Extract the first link and see if it's usable
|
|
|
|
|
// Ensure that it really does come directly after #REDIRECT
|
|
|
|
|
// Some older redirects included a colon, so don't freak about that!
|
2016-02-17 09:09:32 +00:00
|
|
|
$m = [];
|
2014-01-04 22:07:33 +00:00
|
|
|
if ( preg_match( '!^\s*:?\s*\[{2}(.*?)(?:\|.*?)?\]{2}\s*!', $text, $m ) ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
// Strip preceding colon used to "escape" categories, etc.
|
|
|
|
|
// and URL-decode links
|
|
|
|
|
if ( strpos( $m[1], '%' ) !== false ) {
|
|
|
|
|
// Match behavior of inline link parsing here;
|
|
|
|
|
$m[1] = rawurldecode( ltrim( $m[1], ':' ) );
|
|
|
|
|
}
|
|
|
|
|
$title = Title::newFromText( $m[1] );
|
|
|
|
|
// If the title is a redirect to bad special pages or is invalid, return null
|
|
|
|
|
if ( !$title instanceof Title || !$title->isValidRedirectTarget() ) {
|
2018-11-08 15:19:23 +00:00
|
|
|
$this->redirectTargetAndText = [ null, $this->getText() ];
|
2014-05-02 20:16:51 +00:00
|
|
|
return $this->redirectTargetAndText;
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
2013-11-19 21:26:16 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->redirectTargetAndText = [ $title, substr( $text, strlen( $m[0] ) ) ];
|
2014-05-02 20:16:51 +00:00
|
|
|
return $this->redirectTargetAndText;
|
2012-09-24 20:51:53 +00:00
|
|
|
}
|
|
|
|
|
}
|
2013-11-19 21:26:16 +00:00
|
|
|
|
2018-11-08 15:19:23 +00:00
|
|
|
$this->redirectTargetAndText = [ null, $this->getText() ];
|
2014-05-02 20:16:51 +00:00
|
|
|
return $this->redirectTargetAndText;
|
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() {
|
|
|
|
|
list( $title, ) = $this->getRedirectTargetAndText();
|
2014-02-05 11:02:29 +00:00
|
|
|
|
2014-01-04 22:07:33 +00:00
|
|
|
return $title;
|
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 ) {
|
|
|
|
|
global $wgArticleCountMethod;
|
|
|
|
|
|
2013-03-17 15:13:22 +00:00
|
|
|
if ( $this->isRedirect() ) {
|
2012-09-24 20:51:53 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-28 01:45:27 +00:00
|
|
|
if ( $wgArticleCountMethod === 'link' ) {
|
|
|
|
|
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();
|
|
|
|
|
$po = $contentRenderer->getParserOutput( $this, $title, null, null, false );
|
2018-02-28 01:45:27 +00:00
|
|
|
$links = $po->getLinks();
|
|
|
|
|
$hasLinks = !empty( $links );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
* @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
|
|
|
|
|
* @internal for use by WikitextContentHandler
|
|
|
|
|
* @param string[] $flags
|
|
|
|
|
*/
|
|
|
|
|
public function setPreSaveTransformFlags( array $flags ) {
|
|
|
|
|
$this->preSaveTransformFlags = $flags;
|
|
|
|
|
}
|
2021-08-24 12:17:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Records flags set by preSaveTransform
|
|
|
|
|
* @internal for use by WikitextContentHandler
|
|
|
|
|
* @return string[]
|
|
|
|
|
*/
|
|
|
|
|
public function getPreSaveTransformFlags() {
|
|
|
|
|
return $this->preSaveTransformFlags;
|
|
|
|
|
}
|
2012-10-15 00:49:28 +00:00
|
|
|
}
|