Jdlrobson has submitted this change and it was merged.

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, 47 insertions(+), 1 deletion(-)

Approvals:
  JGonera: Looks good to me, but someone else must approve
  Jdlrobson: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/MobileFrontend.php b/MobileFrontend.php
index 455e4e6..62f2cd7 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..899a5df 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -10,6 +10,27 @@
 
 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();
+               $user = $out->getUser();
+               if ( !$user->isAnon() ) {
+                       $vars[ 'wgWatchedPageCache' ] = array(
+                               $title->getText() => $user->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 6eaa4ce..21d2360 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: merged
Gerrit-Change-Id: If90d3b55060e52e423d309a67c9b567e4183bcd8
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to