Cenarium has uploaded a new change for review.

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

Change subject: Retrieve stored references from parser cache
......................................................................

Retrieve stored references from parser cache

Since it might not be possible to use page_props to store
references data, this retrieves them from the parser cache.

If using FlaggedRevs, the API retrieves references for
the stable revision rather than the latest revision, if
distinct.

Bug: T125329
Change-Id: Ibc63dac28abe02195e46e14976617a8ade46ff82
---
M ApiQueryReferences.php
M CiteHooks.php
M Cite_body.php
M README.md
M extension.json
5 files changed, 60 insertions(+), 68 deletions(-)


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

diff --git a/ApiQueryReferences.php b/ApiQueryReferences.php
index 2069a56..a91f9ee 100644
--- a/ApiQueryReferences.php
+++ b/ApiQueryReferences.php
@@ -36,6 +36,7 @@
                } else {
                        $startId = false;
                }
+               $isFlaggedRevsEnabled = class_exists( 'FlaggableWikiPage' );
 
                foreach ( $titles as $pageId => $title ) {
                        // Skip until you have the correct starting point
@@ -44,7 +45,19 @@
                        } else {
                                $startId = false;
                        }
-                       $storedRefs = Cite::getStoredReferences( $title );
+                       if ( $isFlaggedRevsEnabled ) {
+                               // if using FlaggedRevs, get references for the 
stable revision
+                               // if it is distinct from the latest revision
+                               $page = FlaggableWikiPage::getTitleInstance( 
$title );
+                               if ( !$page->isStableShownByDefault() || 
!$page->revsArePending() ) {
+                                       $storedRefs = 
Cite::getStoredReferences( $title );
+                               } else {
+                                       $storedRefs = 
Cite::getStableReferences( $page );
+                               }
+                       } else {
+                               $storedRefs = Cite::getStoredReferences( $title 
);
+                       }
+
                        $allReferences = array();
                        // some pages may not have references stored
                        if ( $storedRefs !== false ) {
diff --git a/CiteHooks.php b/CiteHooks.php
index d3cabdd..128fcbb 100644
--- a/CiteHooks.php
+++ b/CiteHooks.php
@@ -68,26 +68,19 @@
 
        /**
         * Callback for LinksUpdate hook
-        * Post-output processing of references property, for proper db storage
+        * Post-output processing of references property to store in db
         * Deferred to avoid performance overhead when outputting the page
         *
         * @param LinksUpdate $linksUpdate
         */
        public static function onLinksUpdate( LinksUpdate &$linksUpdate ) {
-               global $wgCiteStoreReferencesData, 
$wgCiteCacheRawReferencesOnParse;
-               if ( !$wgCiteStoreReferencesData ) {
+               global $wgCiteStoreReferencesData, 
$wgCiteStoreReferencesDataInDB;
+               if ( !$wgCiteStoreReferencesData || 
!wgCiteStoreReferencesDataInDB ) {
                        return;
                }
                $refData = $linksUpdate->getParserOutput()->getExtensionData( 
Cite::EXT_DATA_KEY );
-               if ( $refData === null ) {
+               if ( $refData === false ) {
                        return;
-               }
-               if ( $wgCiteCacheRawReferencesOnParse ) {
-                       // caching
-                       $cache = ObjectCache::getMainWANInstance();
-                       $articleID = $linksUpdate->getTitle()->getArticleID();
-                       $key = $cache->makeKey( Cite::EXT_DATA_KEY, $articleID 
);
-                       $cache->set( $key, $refData, 
Cite::CACHE_DURATION_ONPARSE );
                }
                // JSON encode
                $ppValue = FormatJson::encode( $refData, false, 
FormatJson::ALL_OK );
@@ -100,36 +93,5 @@
                        $linksUpdate->mProperties[$key] = $ppValue;
                }
                $linksUpdate->getParserOutput()->setExtensionData( 
Cite::EXT_DATA_KEY, null );
-       }
-
-       /**
-        * Callback for LinksUpdateComplete hook
-        * If $wgCiteCacheRawReferencesOnParse is set to false, purges the cache
-        * when references are modified
-        *
-        * @param LinksUpdate $linksUpdate
-        */
-       public static function onLinksUpdateComplete( LinksUpdate &$linksUpdate 
) {
-               global $wgCiteStoreReferencesData, 
$wgCiteCacheRawReferencesOnParse;
-               if ( !$wgCiteStoreReferencesData || 
$wgCiteCacheRawReferencesOnParse ) {
-                       return;
-               }
-               // if we can, avoid clearing the cache when references were not 
changed
-               if ( method_exists( $linksUpdate, 'getAddedProperties' )
-                       && method_exists( $linksUpdate, 'getRemovedProperties' )
-               ) {
-                       $addedProps = $linksUpdate->getAddedProperties();
-                       $removedProps = $linksUpdate->getRemovedProperties();
-                       if ( !isset( $addedProps['references-1'] )
-                               && !isset( $removedProps['references-1'] )
-                       ) {
-                               return;
-                       }
-               }
-               $cache = ObjectCache::getMainWANInstance();
-               $articleID = $linksUpdate->getTitle()->getArticleID();
-               $key = $cache->makeKey( Cite::EXT_DATA_KEY, $articleID );
-               // delete with reduced hold off period (LinksUpdate uses a 
master connection)
-               $cache->delete( $key, WANObjectCache::MAX_COMMIT_DELAY );
        }
 }
diff --git a/Cite_body.php b/Cite_body.php
index cbb2e95..ce10aa6 100644
--- a/Cite_body.php
+++ b/Cite_body.php
@@ -1305,32 +1305,53 @@
        }
 
        /**
-        * Fetch references stored for the given title in page_props
-        * For performance, results are cached
+        * Fetch references stored for the given title from parser cache
+        * If not in parser cache and db storage is enabled, try page_props
         *
         * @param Title $title
         * @return array|false
         */
        public static function getStoredReferences( Title $title ) {
+               global $wgCiteStoreReferencesData, 
$wgCiteStoreReferencesDataInDB;
+               if ( !$wgCiteStoreReferencesData ) {
+                       return false;
+               }
+               $page = WikiPage::factory( $title );
+               $parserOptions = $page->makeParserOptions( 
RequestContext::getMain() );
+               $parserCache = ParserCache::singleton();
+
+               // get cached parser output, even if outdated
+               $parserOutput = $parserCache->get( $page, $parserOptions, true 
);
+               if ( $parserOutput !== false ) {
+                       return $parserOutput->getExtensionData( 
self::EXT_DATA_KEY );
+               } elseif ( $wgCiteStoreReferencesDataInDB ) {
+                       // no parser cache, try db if enabled
+                       $dbr = wfGetDB( DB_SLAVE );
+                       return self::recursiveFetchRefsFromDB( $title, $dbr );
+               }
+               return false;
+       }
+
+       /**
+        * Fetch references stored in the FlaggedRevs stable parser cache
+        *
+        * @param FlaggableWikiPage $page
+        * @return array|false
+        */
+       public static function getStableReferences( FlaggableWikiPage $page ) {
                global $wgCiteStoreReferencesData;
                if ( !$wgCiteStoreReferencesData ) {
                        return false;
                }
-               $cache = ObjectCache::getMainWANInstance();
-               $key = $cache->makeKey( self::EXT_DATA_KEY, 
$title->getArticleID() );
-               return $cache->getWithSetCallback(
-                       $key,
-                       self::CACHE_DURATION_ONFETCH,
-                       function ( $oldValue, &$ttl, array &$setOpts ) use ( 
$title ) {
-                               $dbr = wfGetDB( DB_SLAVE );
-                               $setOpts += Database::getCacheSetOptions( $dbr 
);
-                               return self::recursiveFetchRefsFromDB( $title, 
$dbr );
-                       },
-                       array(
-                               'checkKeys' => array( $key ),
-                               'lockTSE' => 30,
-                       )
-               );
+               $parserOptions = $page->makeParserOptions( 
RequestContext::getMain() );
+               $parserCache = FRParserCacheStable::singleton();
+
+               // get cached parser output, even if outdated
+               $parserOutput = $parserCache->get( $page, $parserOptions, true 
);
+               if ( $parserOutput !== false ) {
+                       return $parserOutput->getExtensionData( 
self::EXT_DATA_KEY );
+               }
+               return false;
        }
 
        /**
diff --git a/README.md b/README.md
index 47a6345..e7d3384 100644
--- a/README.md
+++ b/README.md
@@ -7,8 +7,7 @@
 
 Configuration
 -------------
-* `$wgCiteStoreReferencesData`: If set to true, references are saved in the 
database so that
+* `$wgCiteStoreReferencesData`: If set to true, references are saved in the 
parser cache so that
 other extensions can retrieve them independently of the main article content.
-* `$wgCiteCacheReferencesDataOnParse`: (`$wgCiteStoreReferencesData` required) 
By default,
-references are cached only on database access. If set to true, references are 
also cached
-whenever pages are parsed.
+* `$wgCiteStoreReferencesDataInDB`: (`$wgCiteStoreReferencesData` required) If 
set to true, in
+addition to storing references in the parser cache, they are stored in the 
page_props table.
diff --git a/extension.json b/extension.json
index b010e1c..4923ff8 100644
--- a/extension.json
+++ b/extension.json
@@ -35,9 +35,6 @@
                ],
                "LinksUpdate": [
                        "CiteHooks::onLinksUpdate"
-               ],
-               "LinksUpdateComplete": [
-                       "CiteHooks::onLinksUpdateComplete"
                ]
        },
        "ResourceModules": {
@@ -170,7 +167,7 @@
                "AllowCiteGroups": true,
                "CiteCacheReferences": false,
                "CiteStoreReferencesData": false,
-               "CiteCacheReferencesDataOnParse": false
+               "CiteStoreReferencesDataInDB": false
        },
        "AutoloadClasses": {
                "ApiQueryReferences": "ApiQueryReferences.php",

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ibc63dac28abe02195e46e14976617a8ade46ff82
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