Cenarium has uploaded a new change for review.
https://gerrit.wikimedia.org/r/279893
Change subject: Don't serialize useless StripStates and LinkHolders
......................................................................
Don't serialize useless StripStates and LinkHolders
This adds methods to StipState and LinksHolder to check if they
are worth being serialized and thus stored. Also, methods are
added to these two classes to return data about these objects
that can be served publicly.
Change-Id: Iba9bcf11d7d7ff0f0af542214efe3535d6815080
---
M includes/parser/LinkHolderArray.php
M includes/parser/Parser.php
M includes/parser/StripState.php
3 files changed, 69 insertions(+), 5 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/93/279893/1
diff --git a/includes/parser/LinkHolderArray.php
b/includes/parser/LinkHolderArray.php
index 04b5614..7ec51ca 100644
--- a/includes/parser/LinkHolderArray.php
+++ b/includes/parser/LinkHolderArray.php
@@ -95,6 +95,28 @@
}
/**
+ * Returns whether the links holder does not have any useful data
+ * (and is thus useless to serialize)
+ *
+ * @return bool
+ */
+ public function isTrivial() {
+ return !count( $this->internals ) && !count( $this->interwikis
);
+ }
+
+ /**
+ * Returns all data about this object that can be made available
publicly through the API
+ *
+ * @return array
+ */
+ public function getPublicData() {
+ return [
+ 'internals' => $this->internals,
+ 'interwikis' => $this->interwikis
+ ];
+ }
+
+ /**
* Merge another LinkHolderArray into this one
* @param LinkHolderArray $other
*/
diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php
index 5ee0c5a..d4adc93 100644
--- a/includes/parser/Parser.php
+++ b/includes/parser/Parser.php
@@ -6316,6 +6316,14 @@
'stripState' => $this->mStripState->getSubState( $text
),
'linkHolders' => $this->mLinkHolders->getSubArray(
$text )
];
+ // no need to store strip state if it has no markers
+ if ( $data['stripState']->isTrivial() ) {
+ $data['stripState'] = null;
+ }
+ // no need to store link holders if it has no links
+ if ( $data['linkHolders']->isTrivial() ) {
+ $data['linkHolders'] = null;
+ }
return $data;
}
@@ -6331,20 +6339,27 @@
* check whether it is still valid, by calling isValidHalfParsedText().
*
* @param array $data Serialized data
+ * @param bool $checkVersion
* @throws MWException
* @return string
*/
- public function unserializeHalfParsedText( $data ) {
- if ( !isset( $data['version'] ) || $data['version'] !=
self::HALF_PARSED_VERSION ) {
+ public function unserializeHalfParsedText( $data, $checkVersion = true
) {
+ if ( $checkVersion &&
+ ( !isset( $data['version'] ) || $data['version'] !=
self::HALF_PARSED_VERSION )
+ ) {
throw new MWException( __METHOD__ . ': invalid version'
);
}
# First, extract the strip state.
$texts = [ $data['text'] ];
- $texts = $this->mStripState->merge( $data['stripState'], $texts
);
+ if ( $data['stripState'] !== null ) {
+ $texts = $this->mStripState->merge(
$data['stripState'], $texts );
+ }
# Now renumber links
- $texts = $this->mLinkHolders->mergeForeign(
$data['linkHolders'], $texts );
+ if ( $data['linkHolders'] !== null ) {
+ $texts = $this->mLinkHolders->mergeForeign(
$data['linkHolders'], $texts );
+ }
# Should be good to go.
return $texts[0];
diff --git a/includes/parser/StripState.php b/includes/parser/StripState.php
index c168aa6..22137d6 100644
--- a/includes/parser/StripState.php
+++ b/includes/parser/StripState.php
@@ -26,7 +26,6 @@
* @ingroup Parser
*/
class StripState {
- protected $prefix;
protected $data;
protected $regex;
@@ -54,6 +53,34 @@
$this->circularRefGuard = [];
}
+ function __sleep() {
+ return [ 'data' ];
+ }
+
+ function __wakeup() {
+ $this->regex = '/' . Parser::MARKER_PREFIX . "([^\x7f]+)" .
Parser::MARKER_SUFFIX . '/';
+ $this->circularRefGuard = [];
+ }
+
+ /**
+ * Returns whether the strip state does not have any useful data
+ * (and is thus useless to serialize)
+ *
+ * @return bool
+ */
+ public function isTrivial() {
+ return !count( $this->data['nowiki'] ) && !count(
$this->data['general'] );
+ }
+
+ /**
+ * Returns all data about this object that can be made available
publicly through the API
+ *
+ * @return array
+ */
+ public function getPublicData() {
+ return [ 'markers' => $this->data ];
+ }
+
/**
* Add a nowiki strip item
* @param string $marker
--
To view, visit https://gerrit.wikimedia.org/r/279893
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Iba9bcf11d7d7ff0f0af542214efe3535d6815080
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Cenarium <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits