X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fcontent%2FTextContent.php;h=225522e92d1dee40d447a21e455e9f22fa5f56b2;hb=a9007e8baf802f0f57d095e3bb4ad201c98c0cb3;hp=c479f202fbe035da9ed93ab37afa737d03f26d5e;hpb=49230edd04a1b8260b7823c10d4bee63e71bd10c;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/content/TextContent.php b/includes/content/TextContent.php index c479f202fb..45d00c5e42 100644 --- a/includes/content/TextContent.php +++ b/includes/content/TextContent.php @@ -37,6 +37,7 @@ class TextContent extends AbstractContent { /** * @param string $text * @param string $model_id + * @throws MWException */ public function __construct( $text, $model_id = CONTENT_MODEL_TEXT ) { parent::__construct( $model_id ); @@ -91,7 +92,7 @@ class TextContent extends AbstractContent { * Returns true if this content is not a redirect, and $wgArticleCountMethod * is "any". * - * @param bool $hasLinks If it is known whether this content contains links, + * @param bool|null $hasLinks If it is known whether this content contains links, * provide this information here, to avoid redundant parsing to find out. * * @return bool @@ -146,9 +147,28 @@ class TextContent extends AbstractContent { } } + /** + * Do a "\r\n" -> "\n" and "\r" -> "\n" transformation + * as well as trim trailing whitespace + * + * This was formerly part of Parser::preSaveTransform, but + * for non-wikitext content models they probably still want + * to normalize line endings without all of the other PST + * changes. + * + * @since 1.28 + * @param $text + * @return string + */ + public static function normalizeLineEndings( $text ) { + return str_replace( [ "\r\n", "\r" ], "\n", rtrim( $text ) ); + } + /** * Returns a Content object with pre-save transformations applied. - * This implementation just trims trailing whitespace. + * + * At a minimum, subclasses should make sure to call TextContent::normalizeLineEndings() + * either directly or part of Parser::preSaveTransform(). * * @param Title $title * @param User $user @@ -158,7 +178,7 @@ class TextContent extends AbstractContent { */ public function preSaveTransform( Title $title, User $user, ParserOptions $popts ) { $text = $this->getNativeData(); - $pst = rtrim( $text ); + $pst = self::normalizeLineEndings( $text ); return ( $text === $pst ) ? $this : new static( $pst, $this->getModel() ); } @@ -213,7 +233,7 @@ class TextContent extends AbstractContent { * @param int $revId Revision ID (for {{REVISIONID}}) * @param ParserOptions $options Parser options * @param bool $generateHtml Whether or not to generate HTML - * @param ParserOutput $output The output object to fill (reference). + * @param ParserOutput &$output The output object to fill (reference). */ protected function fillParserOutput( Title $title, $revId, ParserOptions $options, $generateHtml, ParserOutput &$output