r78150 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r78149‎ | r78150 | r78151 >
Date:22:45, 9 December 2010
Author:krinkle
Status:resolved (Comments)
Tags:
Comment:
Fixing bugs and modernising mediawiki.action.watch.ajax.js a little bit

* add() takes the selector directly. Passing a jQuery object either doesn't work or is an undocumented feature.
* using the mw.util functions instead of a less than perfect regex.
* closest() instead of parents() is faster and should be used whenever possible
* The this in processResult() didn't refer to the jQuery object of the anchor tag. It was probably never noticed since it's only used in the else{}-fallback (hard to mis). Fixed now by passing the link in the function
* The this in .bind('mw-ajaxwatch') didn't refer to this either, fixed also by passing into the function
* Replace() function in .bind('mw-ajaxwatch') didn't work, the slashes causes nothing to be matched at all. Before this the href stayed the same and didn't change accordingly.
Modified paths:
  • /trunk/phase3/resources/Resources.php (modified) (history)
  • /trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js (modified) (history)

Diff [purge]

Index: trunk/phase3/resources/mediawiki.action/mediawiki.action.watch.ajax.js
@@ -26,15 +26,16 @@
2727 }
2828 };
2929
30 -wgAjaxWatch.processResult = function( response ) {
 30+wgAjaxWatch.processResult = function( response, $link ) {
3131 response = response.watch;
32 - var $link = $( this );
 32+
 33+ console.log( $link.get() );
3334 // To ensure we set the same status for all watch links with the
3435 // same target we trigger a custom event on *all* watch links.
3536 if( response.watched !== undefined ) {
36 - wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch'] );
 37+ wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'watch', $link] );
3738 } else if ( response.unwatched !== undefined ) {
38 - wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch'] );
 39+ wgAjaxWatch.$links.trigger( 'mw-ajaxwatch', [response.title, 'unwatch', $link] );
3940 } else {
4041 // Either we got an error code or it just plain broke.
4142 window.location.href = $link.attr( 'href' );
@@ -56,18 +57,19 @@
5758 var $links = $( '.mw-watchlink a, a.mw-watchlink' );
5859 // BC with older skins
5960 $links = $links
60 - .add( $( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1' ) )
61 - .add( $( 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' ) );
 61+ .add( '#ca-watch a, #ca-unwatch a, a#mw-unwatch-link1, ' +
 62+ 'a#mw-unwatch-link2, a#mw-watch-link2, a#mw-watch-link1' );
6263 // allowing people to add inline animated links is a little scary
6364 $links = $links.filter( ':not( #bodyContent *, #content * )' );
6465
6566 $links.each( function() {
6667 var $link = $( this );
 68+ var link = this;
6769 $link
68 - .data( 'icon', $link.parents( 'li' ).hasClass( 'icon' ) )
69 - .data( 'action', $link.attr( 'href' ).match( /[\?&]action=unwatch/i ) ? 'unwatch' : 'watch' );
70 - var title = $link.attr( 'href' ).match( /[\?&]title=(.*?)&/i )[1];
71 - $link.data( 'target', decodeURIComponent( title ).replace( /_/g, ' ' ) );
 70+ .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) )
 71+ .data( 'action', mw.util.getParamValue( 'action', link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
 72+ var title = mw.util.getParamValue( 'title', link.href );
 73+ $link.data( 'target', title.replace( /_/g, ' ' ) );
7274 });
7375
7476 $links.click( function( event ) {
@@ -82,13 +84,13 @@
8385 }
8486
8587 wgAjaxWatch.setLinkText( $link, $link.data( 'action' ) + 'ing' );
86 - $.get( wgScriptPath
 88+ $.getJSON( wgScriptPath
8789 + '/api' + wgScriptExtension + '?action=watch&format=json&title='
8890 + encodeURIComponent( $link.data( 'target' ) )
8991 + ( $link.data( 'action' ) == 'unwatch' ? '&unwatch' : '' ),
90 - {},
91 - wgAjaxWatch.processResult,
92 - 'json'
 92+ function( data, textStatus, xhr ) {
 93+ wgAjaxWatch.processResult( data, $link );
 94+ }
9395 );
9496
9597 return false;
@@ -96,8 +98,7 @@
9799
98100 // When a request returns, a custom event 'mw-ajaxwatch' is triggered
99101 // on *all* watch links, so they can be updated if necessary
100 - $links.bind( 'mw-ajaxwatch', function( event, target, action ) {
101 - var $link = $( this );
 102+ $links.bind( 'mw-ajaxwatch', function( event, target, action, $link ) {
102103 var foo = $link.data( 'target' );
103104 if( $link.data( 'target' ) == target ) {
104105 var otheraction = action == 'watch'
@@ -106,13 +107,15 @@
107108
108109 $link.data( 'action', otheraction );
109110 wgAjaxWatch.setLinkText( $link, otheraction );
110 - $link.attr( 'href', $link.attr( 'href' ).replace( '/&action=' + action + '/', '&action=' + otheraction ) );
 111+ $link.attr( 'href', $link.attr( 'href' ).replace( '&action=' + action , '&action=' + otheraction ) );
111112 if( $link.parents( 'li' ).attr( 'id' ) == 'ca-' + action ) {
112113 $link.parents( 'li' ).attr( 'id', 'ca-' + otheraction );
113114 // update the link text with the new message
114115 $link.text( mediaWiki.msg( otheraction ) );
115116 }
116117 }
 118+
 119+ console.log( $link.get() );
117120 return false;
118121 });
119122
Index: trunk/phase3/resources/Resources.php
@@ -343,7 +343,7 @@
344344 ),
345345 'mediawiki.action.watch.ajax' => array(
346346 'scripts' => 'resources/mediawiki.action/mediawiki.action.watch.ajax.js',
347 - 'dependencies' => 'mediawiki.legacy.wikibits',
 347+ 'dependencies' => 'mediawiki.util',
348348 ),
349349 'mediawiki.special.preferences' => array(
350350 'scripts' => 'resources/mediawiki.special/mediawiki.special.preferences.js',

Follow-up revisions

RevisionCommit summaryAuthorDate
r78151Follow-up r78150, removed accidental left-over console.logs callskrinkle22:47, 9 December 2010
r107354[mediawiki.action.watch.ajax.js] Rewrite using mw.Api and fixes bug 27146...krinkle01:21, 27 December 2011

Comments

#Comment by Krinkle (talk | contribs)   20:45, 11 December 2010
* Replace() function in .bind('mw-ajaxwatch') didn't work, the slashes causes nothing to be matched at all. Before this the href stayed the same and didn't change accordingly.

-			$link.attr( 'href', $link.attr( 'href' ).replace( '/&action=' + action + '/', '&action=' + otheraction ) );
+			$link.attr( 'href', $link.attr( 'href' ).replace( '&action=' + action , '&action=' + otheraction ) );

This commit fixed bug 26305 (bugzilla):

#Comment by Platonides (talk | contribs)   21:27, 11 December 2010

Seems you left some debug code there:

+	console.log( $link.get() );
+		console.log( $link.get() );
#Comment by Krinkle (talk | contribs)   23:12, 11 December 2010

I noticed that 2 minute after I commited, I fixed that in the r78151 Follow-up

#Comment by Brion VIBBER (talk | contribs)   23:27, 3 February 2011

Marking this FIXME for bugzilla:27146; looks like the previous code might have been similarly borked though.

#Comment by Krinkle (talk | contribs)   22:17, 15 March 2011

Previous version used the same approach (finding 'action=[watch|unwatch]' and replace()-ing with the opposite).

That bug was fixed in r82498 by propely contructing a new url.

Setting status to NEW.

Status & tagging log