http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88527

Revision: 88527
Author:   krinkle
Date:     2011-05-21 18:33:48 +0000 (Sat, 21 May 2011)
Log Message:
-----------
Fixing mediawiki.action.watch.ajax
* Adding error capturing in case there is an error (either in the API or with 
the request itself). Right now if it fails it just keeps reading "Watching..." 
and no error is displayed.
This lack became visible when r88522 changed ApiWatch to require a token and 
POST.
* Added message 'watcherrortext'.
* Moved messages-array in Resources.php form legacy.ajax to action.watch.ajax 
(looks like this was forgotten in r78147)
* Switched it to make a POST request in preparation of making it work with the 
changed API backend as of r88522.

Modified Paths:
--------------
    trunk/phase3/languages/messages/MessagesEn.php
    trunk/phase3/languages/messages/MessagesQqq.php
    trunk/phase3/maintenance/language/messages.inc
    trunk/phase3/resources/Resources.php
    trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js

Modified: trunk/phase3/languages/messages/MessagesEn.php
===================================================================
--- trunk/phase3/languages/messages/MessagesEn.php      2011-05-21 18:32:18 UTC 
(rev 88526)
+++ trunk/phase3/languages/messages/MessagesEn.php      2011-05-21 18:33:48 UTC 
(rev 88527)
@@ -2718,6 +2718,7 @@
 Future changes to this page and its associated talk page will be listed there, 
and the page will appear '''bolded''' in the [[Special:RecentChanges|list of 
recent changes]] to make it easier to pick out.",
 'removedwatch'         => 'Removed from watchlist',
 'removedwatchtext'     => 'The page "[[:$1]]" has been removed from 
[[Special:Watchlist|your watchlist]].',
+'watcherrortext'       => 'An error occurred while changing your watchlist 
settings for "$1".',
 'watch'                => 'Watch',
 'watchthispage'        => 'Watch this page',
 'unwatch'              => 'Unwatch',

Modified: trunk/phase3/languages/messages/MessagesQqq.php
===================================================================
--- trunk/phase3/languages/messages/MessagesQqq.php     2011-05-21 18:32:18 UTC 
(rev 88526)
+++ trunk/phase3/languages/messages/MessagesQqq.php     2011-05-21 18:33:48 UTC 
(rev 88527)
@@ -2360,6 +2360,7 @@
 'addedwatchtext'       => 'Explanation shown when clicking on the 
{{msg|watch}} tab. See also {{msg|addedwatch}}.',
 'removedwatch'         => 'Page title displayed when clicking on 
{{msg|unwatch}} tab (only when not using the AJAX feauture which allows 
watching a page without reloading the page or such). See also 
{{msg|removedwatchtext}}.',
 'removedwatchtext'     => "After a page has been removed from a user's 
watchlist by clicking the {{msg|unwatch}} tab at the top of an article, this 
message appears just below the title of the article. $1 is the title of the 
article. See also {{msg|removedwatch}} and {{msg|addedwatchtext}}.",
+'watcherrortext'       => "When a user clicked the watch/unwatch tab and the 
action did not succeed, this message is displayed. See also 
{{msg|addedwatchtext}}. and {{msg|addedwatchtext}}. This message is used raw 
and should not contain wikitext.",
 'watch'                => 'Name of the Watch tab. Should be in the imperative 
mood.',
 'watchthispage'        => '{{Identical|Watch this page}}',
 'unwatch'              => 'Label of "Unwatch" tab.',

Modified: trunk/phase3/maintenance/language/messages.inc
===================================================================
--- trunk/phase3/maintenance/language/messages.inc      2011-05-21 18:32:18 UTC 
(rev 88526)
+++ trunk/phase3/maintenance/language/messages.inc      2011-05-21 18:33:48 UTC 
(rev 88527)
@@ -1820,7 +1820,8 @@
        ),
        'watching' => array(
                'watching',
-               'unwatching',
+               'watching',
+               'watcherrortext',
        ),
        'enotif' => array(
                'enotif_mailer',

Modified: trunk/phase3/resources/Resources.php
===================================================================
--- trunk/phase3/resources/Resources.php        2011-05-21 18:32:18 UTC (rev 
88526)
+++ trunk/phase3/resources/Resources.php        2011-05-21 18:33:48 UTC (rev 
88527)
@@ -482,7 +482,15 @@
        ),
        'mediawiki.action.watch.ajax' => array(
                'scripts' => 
'resources/mediawiki.action/mediawiki.action.watch.ajax.js',
-               'dependencies' => 'mediawiki.util',
+               'messages' => array(
+                       'watch',
+                       'unwatch',
+                       'watching',
+                       'unwatching',
+                       'tooltip-ca-watch',
+                       'tooltip-ca-unwatch',
+                       'watcherrortext',
+               ),
        ),
 
        /* Special pages */
@@ -580,14 +588,6 @@
                'scripts' => 'common/ajax.js',
                'remoteBasePath' => $GLOBALS['wgStylePath'],
                'localBasePath' => "{$GLOBALS['IP']}/skins",
-               'messages' => array(
-                       'watch',
-                       'unwatch',
-                       'watching',
-                       'unwatching',
-                       'tooltip-ca-watch',
-                       'tooltip-ca-unwatch',
-               ),
                'dependencies' => 'mediawiki.legacy.wikibits',
        ),
        'mediawiki.legacy.commonPrint' => array(

Modified: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js
===================================================================
--- trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js      
2011-05-21 18:32:18 UTC (rev 88526)
+++ trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js      
2011-05-21 18:33:48 UTC (rev 88527)
@@ -23,9 +23,41 @@
        }
 };
 
+var errorHandler = function( $link ) {
+
+       // Reset link text to whatever it was before we switching it to the 
'(un)watch'+ing message.
+       setLinkText( $link, $link.data( 'action' ) );
+
+       // Format error message
+       var cleanTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
+       var link = mw.html.element(
+               'a', {
+                       'href': mw.util.wikiGetlink( mw.config.get( 
'wgPageName' ) ),
+                       'title': cleanTitle
+               }, cleanTitle
+       );
+       var msg = mw.msg( 'watcherrortext', link );
+
+       // Report to user about the error
+       mw.util.jsMessage( msg, 'watch' );
+};
+
+/**
+ * Process the result of the API watch action.
+ *
+ * @param response Data object from API request.
+ * @param $link jQuery object of the watch link.
+ * @return Boolean true on success, false otherwise.
+ */
 var processResult = function( response, $link ) {
-       watchResponse = response.watch;
 
+       if ( ( 'error' in response ) || !response.watch ) {
+               errorHandler( $link );
+               return false;
+       }
+
+       var watchResponse = response.watch;
+
        // To ensure we set the same status for all watch links with the
        // same target we trigger a custom event on *all* watch links.
        if ( watchResponse.watched !== undefined ) {
@@ -35,7 +67,7 @@
        } else {
                // Either we got an error code or it just plain broke.
                window.location.href = $link[0].href;
-               return;
+               return false;
        }
 
        mw.util.jsMessage( watchResponse.message, 'watch' );
@@ -47,6 +79,7 @@
        } else {
                $( '#wpWatchthis' ).removeAttr( 'checked' );
        }
+       return true;
 };
 
 $( document ).ready( function() {
@@ -88,15 +121,23 @@
                        // API return contains a localized data.watch.message 
string.
                        'uselang': mw.config.get( 'wgUserLanguage' )
                };
+
                if ( $link.data( 'action' ) == 'unwatch' ) {
                        reqData.unwatch = '';
                }
-               $.getJSON( mw.util.wikiScript( 'api' ),
-                       reqData,
-                       function( data, textStatus, xhr ) {
+
+               $.ajax({
+                       url: mw.util.wikiScript( 'api' ),
+                       dataType: 'json',
+                       type: 'POST',
+                       data: reqData,
+                       success: function( data, textStatus, xhr ) {
                                processResult( data, $link );
+                       },
+                       error: function(){
+                               processResult( {}, $link );
                        }
-               );
+               });
 
                return false;
        });


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

Reply via email to