3 * Content object implementation for representing flat text.
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.
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.
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
25 * @author Daniel Kinzler
28 namespace MediaWiki\Content
;
30 use InvalidArgumentException
;
31 use MediaWiki\Language\Language
;
32 use MediaWiki\MainConfigNames
;
33 use MediaWiki\MediaWikiServices
;
34 use MWUnknownContentModelException
;
35 use Wikimedia\Diff\Diff
;
38 * Content object implementation for representing flat text.
40 * TextContent instances are immutable
46 class TextContent
extends AbstractContent
{
56 * @param string $model_id
58 public function __construct( $text, $model_id = CONTENT_MODEL_TEXT
) {
59 parent
::__construct( $model_id );
61 if ( $text === null ||
$text === false ) {
62 wfWarn( "TextContent constructed with \$text = " . var_export( $text, true ) . "! "
63 . "This may indicate an error in the caller's scope.", 2 );
68 if ( !is_string( $text ) ) {
69 throw new InvalidArgumentException( "TextContent expects a string in the constructor." );
76 * @note Mutable subclasses MUST override this to return a copy!
78 * @return Content $this
80 public function copy() {
81 return $this; # NOTE: this is ok since TextContent are immutable.
87 * @param int $maxlength
91 public function getTextForSummary( $maxlength = 250 ) {
92 $text = $this->getText();
94 $truncatedtext = MediaWikiServices
::getInstance()->getContentLanguage()->
95 truncateForDatabase( preg_replace( "/[\n\r]/", ' ', $text ), max( 0, $maxlength ) );
97 return $truncatedtext;
101 * Returns the text's size in bytes.
103 * @stable to override
107 public function getSize() {
108 $text = $this->getText();
110 return strlen( $text );
114 * Returns true if this content is not a redirect, and $wgArticleCountMethod
117 * @stable to override
119 * @param bool|null $hasLinks If it is known whether this content contains links,
120 * provide this information here, to avoid redundant parsing to find out.
124 public function isCountable( $hasLinks = null ) {
125 $articleCountMethod = MediaWikiServices
::getInstance()->getMainConfig()->get(
126 MainConfigNames
::ArticleCountMethod
);
128 if ( $this->isRedirect() ) {
132 if ( $articleCountMethod === 'any' ) {
140 * Returns the text represented by this Content object, as a string.
142 * @deprecated since 1.33 use getText() instead.
144 * @return string The raw text. Subclasses may guarantee a specific syntax here.
146 public function getNativeData() {
147 return $this->getText();
151 * Returns the text represented by this Content object, as a string.
154 * @note This method should not be overwritten by subclasses. If a subclass find itself in
155 * need to override this method, it should probably not be based on TextContent, but
156 * should rather extend AbstractContent instead.
158 * @return string The raw text.
160 public function getText() {
165 * Returns the text represented by this Content object, as a string.
167 * @stable to override
169 * @return string The raw text.
171 public function getTextForSearchIndex() {
172 return $this->getText();
176 * Returns attempts to convert this content object to wikitext,
177 * and then returns the text string. The conversion may be lossy.
179 * @stable to override
181 * @note this allows any text-based content to be transcluded as if it was wikitext.
183 * @return string|false The raw text, or false if the conversion failed.
185 public function getWikitextForTransclusion() {
186 /** @var WikitextContent $wikitext */
187 $wikitext = $this->convert( CONTENT_MODEL_WIKITEXT
, 'lossy' );
188 '@phan-var WikitextContent $wikitext';
191 return $wikitext->getText();
198 * Do a "\r\n" -> "\n" and "\r" -> "\n" transformation
199 * as well as trim trailing whitespace
201 * This was formerly part of Parser::preSaveTransform, but
202 * for non-wikitext content models they probably still want
203 * to normalize line endings without all of the other PST
207 * @param string $text
210 public static function normalizeLineEndings( $text ) {
211 return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) );
215 * Diff this content object with another content object.
217 * @stable to override
220 * @param Content $that The other content object to compare this content object to.
221 * @param Language|null $lang The language object to use for text segmentation.
222 * If not given, the content language is used.
224 * @return Diff A diff representing the changes that would have to be
225 * made to this content object to make it equal to $that.
227 public function diff( Content
$that, ?Language
$lang = null ) {
228 $this->checkModelID( $that->getModel() );
229 /** @var self $that */
230 '@phan-var self $that';
231 // @todo could implement this in DifferenceEngine and just delegate here?
234 $lang = MediaWikiServices
::getInstance()->getContentLanguage();
237 $otext = $this->getText();
238 $ntext = $that->getText();
240 # Note: Use native PHP diff, external engines don't give us abstract output
241 $ota = explode( "\n", $lang->segmentForDiff( $otext ) );
242 $nta = explode( "\n", $lang->segmentForDiff( $ntext ) );
244 $diff = new Diff( $ota, $nta );
250 * This implementation provides lossless conversion between content models based
253 * @stable to override
255 * @param string $toModel The desired content model, use the CONTENT_MODEL_XXX flags.
256 * @param string $lossy Flag, set to "lossy" to allow lossy conversion. If lossy conversion is not
257 * allowed, full round-trip conversion is expected to work without losing information.
259 * @return Content|false A content object with the content model $toModel, or false if that
260 * conversion is not supported.
261 * @throws MWUnknownContentModelException
263 * @see Content::convert()
265 public function convert( $toModel, $lossy = '' ) {
266 $converted = parent
::convert( $toModel, $lossy );
268 if ( $converted !== false ) {
272 $toHandler = $this->getContentHandlerFactory()->getContentHandler( $toModel );
274 if ( $toHandler instanceof TextContentHandler
) {
275 // NOTE: ignore content serialization format - it's just text anyway.
276 $text = $this->getText();
277 $converted = $toHandler->unserializeContent( $text );
284 /** @deprecated class alias since 1.43 */
285 class_alias( TextContent
::class, 'TextContent' );