Avoid validating gadget definition content multiple times on save

The validation appears to be occurring 4 times:
1. JsonContentHandler->preSaveTransform --> GadgetDefinitionContent->isValid
2. GadgetHooks::onEditFilterMergedContent --> GadgetDefinitionContent->validate
3. ContentHandler->validateSave --> GadgetDefinitionContent->isValid
4. RevisionStore->checkContent ---> GadgetDefinitionContent->isValid

Caching the validation result reduces it down to 2.

(Validation in GadgetHooks::onEditFilterMergedContent is to provide a more useful error message than what ContentHandler gives – which is just "Invalid content data".)

Change-Id: I026751b7e9b111b4f0bb9ab5fa0e9737a0385b07
This commit is contained in:
Siddharth VP 2022-01-14 22:23:19 +05:30
parent 43a6221fc9
commit bded600e36

View file

@ -22,6 +22,12 @@
class GadgetDefinitionContent extends JsonContent { class GadgetDefinitionContent extends JsonContent {
/** @var Status|null Cached validation result */
private $validation;
/**
* @param string $text
*/
public function __construct( $text ) { public function __construct( $text ) {
parent::__construct( $text, 'GadgetDefinition' ); parent::__construct( $text, 'GadgetDefinition' );
} }
@ -47,12 +53,16 @@ class GadgetDefinitionContent extends JsonContent {
* @return Status * @return Status
*/ */
public function validate() { public function validate() {
if ( !parent::isValid() ) { // Cache the validation result to avoid re-computations
return $this->getData(); if ( !$this->validation ) {
if ( !parent::isValid() ) {
$this->validation = $this->getData();
} else {
$validator = new GadgetDefinitionValidator();
$this->validation = $validator->validate( $this->getAssocArray() );
}
} }
return $this->validation;
$validator = new GadgetDefinitionValidator();
return $validator->validate( $this->getAssocArray() );
} }
/** /**