r88527 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88526‎ | r88527 | r88528 >
Date:18:33, 21 May 2011
Author:krinkle
Status:ok (Comments)
Tags:
Comment:
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 (modified) (history)
  • /trunk/phase3/languages/messages/MessagesQqq.php (modified) (history)
  • /trunk/phase3/maintenance/language/messages.inc (modified) (history)
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js (modified) (history)

Diff [purge]

Index: trunk/phase3/maintenance/language/messages.inc
@@ -1820,7 +1820,8 @@
18211821 ),
18221822 'watching' => array(
18231823 'watching',
1824 - 'unwatching',
 1824+ 'watching',
 1825+ 'watcherrortext',
18251826 ),
18261827 'enotif' => array(
18271828 'enotif_mailer',
Index: trunk/phase3/languages/messages/MessagesQqq.php
@@ -2360,6 +2360,7 @@
23612361 'addedwatchtext' => 'Explanation shown when clicking on the {{msg|watch}} tab. See also {{msg|addedwatch}}.',
23622362 '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}}.',
23632363 '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}}.",
 2364+'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.",
23642365 'watch' => 'Name of the Watch tab. Should be in the imperative mood.',
23652366 'watchthispage' => '{{Identical|Watch this page}}',
23662367 'unwatch' => 'Label of "Unwatch" tab.',
Index: trunk/phase3/languages/messages/MessagesEn.php
@@ -2718,6 +2718,7 @@
27192719 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.",
27202720 'removedwatch' => 'Removed from watchlist',
27212721 'removedwatchtext' => 'The page "[[:$1]]" has been removed from [[Special:Watchlist|your watchlist]].',
 2722+'watcherrortext' => 'An error occurred while changing your watchlist settings for "$1".',
27222723 'watch' => 'Watch',
27232724 'watchthispage' => 'Watch this page',
27242725 'unwatch' => 'Unwatch',
Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js
@@ -23,9 +23,41 @@
2424 }
2525 };
2626
 27+var errorHandler = function( $link ) {
 28+
 29+ // Reset link text to whatever it was before we switching it to the '(un)watch'+ing message.
 30+ setLinkText( $link, $link.data( 'action' ) );
 31+
 32+ // Format error message
 33+ var cleanTitle = mw.config.get( 'wgPageName' ).replace( /_/g, ' ' );
 34+ var link = mw.html.element(
 35+ 'a', {
 36+ 'href': mw.util.wikiGetlink( mw.config.get( 'wgPageName' ) ),
 37+ 'title': cleanTitle
 38+ }, cleanTitle
 39+ );
 40+ var msg = mw.msg( 'watcherrortext', link );
 41+
 42+ // Report to user about the error
 43+ mw.util.jsMessage( msg, 'watch' );
 44+};
 45+
 46+/**
 47+ * Process the result of the API watch action.
 48+ *
 49+ * @param response Data object from API request.
 50+ * @param $link jQuery object of the watch link.
 51+ * @return Boolean true on success, false otherwise.
 52+ */
2753 var processResult = function( response, $link ) {
28 - watchResponse = response.watch;
2954
 55+ if ( ( 'error' in response ) || !response.watch ) {
 56+ errorHandler( $link );
 57+ return false;
 58+ }
 59+
 60+ var watchResponse = response.watch;
 61+
3062 // To ensure we set the same status for all watch links with the
3163 // same target we trigger a custom event on *all* watch links.
3264 if ( watchResponse.watched !== undefined ) {
@@ -35,7 +67,7 @@
3668 } else {
3769 // Either we got an error code or it just plain broke.
3870 window.location.href = $link[0].href;
39 - return;
 71+ return false;
4072 }
4173
4274 mw.util.jsMessage( watchResponse.message, 'watch' );
@@ -47,6 +79,7 @@
4880 } else {
4981 $( '#wpWatchthis' ).removeAttr( 'checked' );
5082 }
 83+ return true;
5184 };
5285
5386 $( document ).ready( function() {
@@ -88,15 +121,23 @@
89122 // API return contains a localized data.watch.message string.
90123 'uselang': mw.config.get( 'wgUserLanguage' )
91124 };
 125+
92126 if ( $link.data( 'action' ) == 'unwatch' ) {
93127 reqData.unwatch = '';
94128 }
95 - $.getJSON( mw.util.wikiScript( 'api' ),
96 - reqData,
97 - function( data, textStatus, xhr ) {
 129+
 130+ $.ajax({
 131+ url: mw.util.wikiScript( 'api' ),
 132+ dataType: 'json',
 133+ type: 'POST',
 134+ data: reqData,
 135+ success: function( data, textStatus, xhr ) {
98136 processResult( data, $link );
 137+ },
 138+ error: function(){
 139+ processResult( {}, $link );
99140 }
100 - );
 141+ });
101142
102143 return false;
103144 });
Index: trunk/phase3/resources/Resources.php
@@ -482,7 +482,15 @@
483483 ),
484484 'mediawiki.action.watch.ajax' => array(
485485 'scripts' => 'resources/mediawiki.action/mediawiki.action.watch.ajax.js',
486 - 'dependencies' => 'mediawiki.util',
 486+ 'messages' => array(
 487+ 'watch',
 488+ 'unwatch',
 489+ 'watching',
 490+ 'unwatching',
 491+ 'tooltip-ca-watch',
 492+ 'tooltip-ca-unwatch',
 493+ 'watcherrortext',
 494+ ),
487495 ),
488496
489497 /* Special pages */
@@ -580,14 +588,6 @@
581589 'scripts' => 'common/ajax.js',
582590 'remoteBasePath' => $GLOBALS['wgStylePath'],
583591 'localBasePath' => "{$GLOBALS['IP']}/skins",
584 - 'messages' => array(
585 - 'watch',
586 - 'unwatch',
587 - 'watching',
588 - 'unwatching',
589 - 'tooltip-ca-watch',
590 - 'tooltip-ca-unwatch',
591 - ),
592592 'dependencies' => 'mediawiki.legacy.wikibits',
593593 ),
594594 'mediawiki.legacy.commonPrint' => array(

Follow-up revisions

RevisionCommit summaryAuthorDate
r88538Follow-up r88527: Fix array item namekrinkle19:46, 21 May 2011
r88554Passing token paremeter in mw.action.watch.ajax since this is required as of ...krinkle23:14, 21 May 2011
r107354[mediawiki.action.watch.ajax.js] Rewrite using mw.Api and fixes bug 27146...krinkle01:21, 27 December 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r78147start resourceloadify mw.legacy.ajaxwatchkrinkle21:59, 9 December 2010
r88522* (bug 29070) Add token to action=watchreedy16:38, 21 May 2011

Comments

#Comment by Siebrand (talk | contribs)   19:15, 21 May 2011

Something looks wrong in the changes to messages.inc:

- 'unwatching',
+ 'watching',
+ 'watcherrortext',

I only see that key "watcherrortext" was added and I do not see "(un)watching" messages.

#Comment by Krinkle (talk | contribs)   20:06, 21 May 2011
 		'watching',
-		'unwatching',
+		'watching',
+		'watcherrortext',

Not sure what happened there, wrong in any case.

There's two times "watching" now. Fixed in r88538.

Status & tagging log