Jdlrobson has uploaded a new change for review.

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


Change subject: Bug 44126: Avoid API call to check watch status on pages
......................................................................

Bug 44126: Avoid API call to check watch status on pages

Instead pass as a config variable that can be looked up before making
an ajax request

Change-Id: If90d3b55060e52e423d309a67c9b567e4183bcd8
---
M MobileFrontend.php
M includes/MobileFrontend.hooks.php
M javascripts/modules/mf-watchstar.js
3 files changed, 44 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/04/51604/1

diff --git a/MobileFrontend.php b/MobileFrontend.php
index b5a3880..4a2299b 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -83,6 +83,7 @@
 $wgHooks['APIGetDescription'][] = 'ApiParseExtender::onAPIGetDescription';
 $wgHooks['OpenSearchXml'][] = 'ApiQueryExtracts::onOpenSearchXml';
 
+$wgHooks['MakeGlobalVariablesScript'][] = 
'MobileFrontendHooks::onMakeGlobalVariablesScript';
 $wgHooks['RequestContextCreateSkin'][] = 
'MobileFrontendHooks::onRequestContextCreateSkin';
 $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 
'MobileFrontendHooks::onSkinTemplateOutputPageBeforeExec';
 $wgHooks['BeforePageRedirect'][] = 'MobileFrontendHooks::onBeforePageRedirect';
diff --git a/includes/MobileFrontend.hooks.php 
b/includes/MobileFrontend.hooks.php
index 07cbca0..fe7080a 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -10,6 +10,24 @@
 
 class MobileFrontendHooks {
 
+
+       /**
+        * MakeGlobalVariablesScript hook handler
+        * @see 
http://www.mediawiki.org/wiki/Manual:Hooks/MakeGlobalVariablesScript
+        * Adds various mobile specific config variables
+        *
+        * @param array &$vars
+        * @param OutputPage $out
+        * @return boolean
+        */
+       public static function onMakeGlobalVariablesScript( &$vars, $out ) {
+               $title = $out->getTitle();
+               $vars[ 'wgWatchedPageCache' ] = array(
+                       $title->getText() => $out->getUser()->isWatched( $title 
),
+               );
+               return true;
+       }
+
        /**
         * RequestContextCreateSkin hook handler
         * @see 
https://www.mediawiki.org/wiki/Manual:Hooks/RequestContextCreateSkin
diff --git a/javascripts/modules/mf-watchstar.js 
b/javascripts/modules/mf-watchstar.js
index 8e690fc..cc62d9d 100644
--- a/javascripts/modules/mf-watchstar.js
+++ b/javascripts/modules/mf-watchstar.js
@@ -93,7 +93,13 @@
 
        }
 
-       function checkWatchStatus( titles, callback ) {
+       /**
+        * Checks whether a list of article titles are being watched by the 
current user via ajax request to server
+        *
+        * @param {Array} titles: A list of titles to check the watchlist 
status of
+        * @param {Function} callback: A callback that is passed a json of 
mappings from title to booleans describing whether page is watched
+        */
+       function asyncCheckWatchStatus( titles, callback ) {
                $.ajax( {
                        url:  M.getApiUrl(), dataType: 'json',
                        data: {
@@ -116,6 +122,24 @@
                } );
        }
 
+       /**
+        * Checks whether a list of article titles are being watched by the 
current user
+        * Checks a local cache before making a query to server
+        *
+        * @param {Array} titles: A list of titles to check the watchlist 
status of
+        * @param {Function} callback: A callback that is passed a json of 
mappings from title to booleans describing whether page is watched
+        */
+       function checkWatchStatus( titles, callback ) {
+               var cache = mw.config.get( 'wgWatchedPageCache' ) || {};
+               // check local cache in case where only one title is passed
+               // FIXME: allow this to work for more than one title
+               if ( titles.length === 1 && typeof cache[ titles[ 0 ] ] !== 
'undefined' ) {
+                       callback( cache );
+               } else {
+                       asyncCheckWatchStatus( titles, callback );
+               }
+       }
+
        function initWatchListIcon( container, title ) {
 
                api.getToken( 'watch', function( data ) {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If90d3b55060e52e423d309a67c9b567e4183bcd8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to