Krinkle has uploaded a new change for review. https://gerrit.wikimedia.org/r/176853
Change subject: content: Add validation for JavaScriptContent ...................................................................... content: Add validation for JavaScriptContent Bug: T76204 Change-Id: I44fcf2c7181b140964ae05c4b5676abf507494b6 --- M includes/content/JavaScriptContent.php M languages/i18n/en.json M languages/i18n/qqq.json 3 files changed, 49 insertions(+), 2 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/53/176853/1 diff --git a/includes/content/JavaScriptContent.php b/includes/content/JavaScriptContent.php index c0194c2..472af20 100644 --- a/includes/content/JavaScriptContent.php +++ b/includes/content/JavaScriptContent.php @@ -31,6 +31,8 @@ * @ingroup Content */ class JavaScriptContent extends TextContent { + /** @var JSParser */ + private static $jsParser; /** * @param string $text JavaScript code. @@ -41,6 +43,48 @@ } /** + * @since 1.25 + * @return JSParser + */ + protected static function getParser() { + if ( !self::$jsParser ) { + self::$jsParser = new JSParser(); + } + return self::$jsParser; + } + + /** + * @since 1.25 + * @return Status + */ + protected function validate( $fileName = '[inline]' ) { + $parser = self::getParser(); + try { + $parser->parse( $this->getNativeData(), $fileName, /* lineNr */ 1 ); + } catch ( Exception $e ) { + $err = $e->getMessage(); + return Status::newFatal( 'javascript-error-syntax', $err ); + } + return Status::newGood(); + } + + /** + * @since 1.25 + * @return bool Whether content is valid javascript. + */ + public function isValid() { + return $this->validate()->isOK(); + } + + /** + * @since 1.25 + * @return Status + */ + public function prepareSave( WikiPage $page, $flags, $baseRevId, User $user ) { + return $this->validate( $page->getTitle()->getSubpageText() ); + } + + /** * Returns a Content object with pre-save transformations applied using * Parser::preSaveTransform(). * diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 75bdb11..38116cc 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -3587,5 +3587,6 @@ "json-error-utf8": "Malformed UTF-8 characters, possibly incorrectly encoded", "json-error-recursion": "One or more recursive references in the value to be encoded", "json-error-inf-or-nan": "One or more NAN or INF values in the value to be encoded", - "json-error-unsupported-type": "A value of a type that cannot be encoded was given" + "json-error-unsupported-type": "A value of a type that cannot be encoded was given", + "javascript-error-syntax": "$1" } diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 0a59925..1c1ec6c 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -3751,5 +3751,7 @@ "json-error-utf8": "User error message when there are malformed UTF-8 characters, possibly incorrectly encoded.\nSee http://php.net/manual/en/function.json-last-error.php\n{{Related|Json-error}}", "json-error-recursion": "PHP JSON encoding/decoding error. See http://php.net/manual/en/function.json-last-error.php\n{{Related|Json-error}}", "json-error-inf-or-nan": "PHP JSON encoding/decoding error. See http://php.net/manual/en/function.json-last-error.php\n{{Related|Json-error}}", - "json-error-unsupported-type": "PHP JSON encoding/decoding error. See http://php.net/manual/en/function.json-last-error.php\n{{Related|Json-error}}" + "json-error-unsupported-type": "PHP JSON encoding/decoding error. See http://php.net/manual/en/function.json-last-error.php\n{{Related|Json-error}}", + "javascript-error-syntax": "Warning indicating that submitted JavaScript content contains a syntax error.\n * $1 - The syntax error from the Parser in English (including line number)." + } -- To view, visit https://gerrit.wikimedia.org/r/176853 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I44fcf2c7181b140964ae05c4b5676abf507494b6 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Krinkle <krinklem...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits