Cenarium has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/271952

Change subject: Cleanup references parameters before saving them to the database
......................................................................

Cleanup references parameters before saving them to the database

Before saving references data, this processes the 'text' parameter
by trimming it and removing strip markers. This also provides a
more meaningful 'count', and removes the 'number' param.

In addition, this merges the content of the LinksUpdateConstructed
hook callback into the LinksUpdate hook callback since it's fine to
defer saving to the cache.

Change-Id: Iad6008a31aaf659af3c560858df278335bc57c8f
---
M CiteHooks.php
M extension.json
2 files changed, 37 insertions(+), 25 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cite 
refs/changes/52/271952/1

diff --git a/CiteHooks.php b/CiteHooks.php
index 9db0621..c73cd5a 100644
--- a/CiteHooks.php
+++ b/CiteHooks.php
@@ -67,27 +67,6 @@
        }
 
        /**
-        * Callback for LinksUpdateConstructed hook
-        * If $wgCiteCacheRawReferencesOnParse is set to true, caches the raw 
references
-        * in array form
-        *
-        * @param LinksUpdate $linksUpdate
-        */
-       public static function onLinksUpdateConstructed( LinksUpdate 
&$linksUpdate ) {
-               global $wgCiteStoreReferencesData, 
$wgCiteCacheRawReferencesOnParse;
-               if ( !$wgCiteStoreReferencesData || 
!$wgCiteCacheRawReferencesOnParse ) {
-                       return;
-               }
-               $refs = $linksUpdate->getParserOutput()->getExtensionData( 
Cite::EXT_DATA_KEY );
-               if ( $refs !== null ) {
-                       $cache = ObjectCache::getMainWANInstance();
-                       $articleID = $linksUpdate->getTitle()->getArticleID();
-                       $key = $cache->makeKey( Cite::EXT_DATA_KEY, $articleID 
);
-                       $cache->set( $key, $refs, Cite::CACHE_DURATION_ONPARSE 
);
-               }
-       }
-
-       /**
         * Callback for LinksUpdate hook
         * Post-output processing of references property, for proper db storage
         * Deferred to avoid performance overhead when outputting the page
@@ -95,12 +74,19 @@
         * @param LinksUpdate $linksUpdate
         */
        public static function onLinksUpdate( LinksUpdate &$linksUpdate ) {
-               global $wgCiteStoreReferencesData;
+               global $wgCiteStoreReferencesData, 
$wgCiteCacheRawReferencesOnParse;
                if ( !$wgCiteStoreReferencesData ) {
                        return;
                }
                $refs = $linksUpdate->getParserOutput()->getExtensionData( 
Cite::EXT_DATA_KEY );
                if ( $refs !== null ) {
+                       $refs['refs'] = self::processReferencesData( 
$refs['refs'] );
+                       if ( $wgCiteCacheRawReferencesOnParse ) {
+                               $cache = ObjectCache::getMainWANInstance();
+                               $articleID = 
$linksUpdate->getTitle()->getArticleID();
+                               $key = $cache->makeKey( Cite::EXT_DATA_KEY, 
$articleID );
+                               $cache->set( $key, $refs, 
Cite::CACHE_DURATION_ONPARSE );
+                       }
                        // JSON encode
                        $ppValue = FormatJson::encode( $refs, false, 
FormatJson::ALL_OK );
                        // GZIP encode references data at maximum compression
@@ -115,6 +101,35 @@
                }
        }
 
+       private static function processReferencesData( $refs ) {
+               foreach ( $refs as $index => $listing ) {
+                       foreach ( $listing as $group => $refsInGroup ) {
+                               foreach ( $refsInGroup as $key => $ref ) {
+                                       // remove spaces or newlines added at 
the beginning or end of references
+                                       $ref['text'] = trim( $ref['text'] );
+                                       // <ref> tags added within {{#tag:ref}} 
calls are not retrieved
+                                       // the parser replaces them by a strip 
marker and the original wikitext
+                                       // cannot be recovered at this point, 
so remove these strip markers
+                                       $stripState = new StripState();
+                                       $ref['text'] = 
$stripState->killMarkers( $ref['text'] );
+
+                                       // return more meaningful count
+                                       if ( $ref['count'] < 0 ) {
+                                               $ref['count'] = 1;
+                                       } else {
+                                               $ref['count']++;
+                                       }
+
+                                       // remove 'number' param, of no use 
after parse
+                                       unset( $ref['number'] );
+
+                                       $refs[$index][$group][$key] = $ref;
+                               }
+                       }
+               }
+               return $refs;
+       }
+
        /**
         * Callback for LinksUpdateComplete hook
         * If $wgCiteCacheRawReferencesOnParse is set to false, purges the cache
diff --git a/extension.json b/extension.json
index 24e3fe3..8080d9e 100644
--- a/extension.json
+++ b/extension.json
@@ -29,9 +29,6 @@
                        "CiteHooks::onResourceLoaderTestModules"
                ],
                "LinksUpdateConstructed": [
-                       "CiteHooks::onLinksUpdateConstructed"
-               ],
-               "LinksUpdate": [
                        "CiteHooks::onLinksUpdate"
                ],
                "LinksUpdateComplete": [

-- 
To view, visit https://gerrit.wikimedia.org/r/271952
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iad6008a31aaf659af3c560858df278335bc57c8f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cite
Gerrit-Branch: master
Gerrit-Owner: Cenarium <cenarium.sy...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to