r84491 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r84490‎ | r84491 | r84492 >
Date:21:50, 21 March 2011
Author:hashar
Status:reverted (Comments)
Tags:
Comment:
Fix javascript watch/unwatch with pretty URL

mw.util.getParamValue is great but it should not be used to get the 'title'
or 'action' parameters:
- 'title' is embed in the PATH when using pretty URL
- 'action' is hidden when using action paths ($wgActionPaths)

Two new functions will give you the expected parameters:
- getActionFrom( url )
- getTitleFrom( url )

They firstly attempt to retrieve the action/title from the query string, on
failure, they will parse the URL through the action paths. When action paths
are not set a default 'view' action is set to wgArticlePath.

Should fix one of the r84386 issue:
E> mediawiki.action.watch.ajax.js:70
E> Uncaught TypeError: Cannot call method 'replace' of null
Modified paths:
  • /branches/hashar/prettyURL/resources/mediawiki.action/mediawiki.action.watch.ajax.js (modified) (history)
  • /branches/hashar/prettyURL/resources/mediawiki.util/mediawiki.util.js (modified) (history)

Diff [purge]

Index: branches/hashar/prettyURL/resources/mediawiki.action/mediawiki.action.watch.ajax.js
@@ -65,8 +65,8 @@
6666 var link = this;
6767 $link
6868 .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) )
69 - .data( 'action', mw.util.getParamValue( 'action', link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
70 - var title = mw.util.getParamValue( 'title', link.href );
 69+ .data( 'action', mw.util.getActionFrom( link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
 70+ var title = mw.util.getTitleFrom( link.href );
7171 $link.data( 'target', title.replace( /_/g, ' ' ) );
7272 });
7373
Index: branches/hashar/prettyURL/resources/mediawiki.util/mediawiki.util.js
@@ -209,6 +209,12 @@
210210 /**
211211 * Grab the URL parameter value for the given parameter.
212212 * Returns null if not found.
 213+ * Beware! When action paths are enabled (wgActionPaths) using this function
 214+ * to retrieve the 'action' or 'title' parameter will probably fail since
 215+ * those parameters are hidden in the path.
 216+ * To safely query for:
 217+ * 'action' use getActionFrom( url )
 218+ * 'title' use getTitleFrom( url )
213219 *
214220 * @param param The parameter name
215221 * @param url URL to search through (optional)
@@ -224,6 +230,63 @@
225231 return null;
226232 },
227233
 234+ /**
 235+ * Try to find the wiki action for a given URL with actions paths support
 236+ *
 237+ * @param url URL to search for a wiki action
 238+ */
 239+ 'getActionFrom' : function( url ) {
 240+ // attempt to get the action from the parameter [&?]action=
 241+ var action = mw.util.getParamValue( 'action', url );
 242+ if( action !== null ) {
 243+ return action;
 244+ }
 245+
 246+ // now from the action paths
 247+ var actionPaths = mw.config.get( 'wgActionPaths' );
 248+ if( actionPaths.length == 0 ) {
 249+ actionPaths['view'] = mw.config.get( 'wgArticlePath' );
 250+ }
 251+ var action = '';
 252+ for ( action in actionPaths ) {
 253+ var actionRe = new RegExp( actionPaths[action].replace( '$1', '.*?' ) );
 254+ if( url.match( actionRe ) ) {
 255+ return action;
 256+ }
 257+ }
 258+
 259+ return null;
 260+ },
 261+
 262+ /**
 263+ * Try to find the wiki title for a given URL with actions paths support
 264+ *
 265+ * @param url URL to search for a title
 266+ */
 267+ 'getTitleFrom' : function( url ) {
 268+ // attempt to get the title from the parameter [&?]title=
 269+ var title = mw.util.getParamValue( 'title', url );
 270+ if( title !== null ) {
 271+ return title;
 272+ }
 273+
 274+ // now from the action paths
 275+ var actionPaths = mw.config.get( 'wgActionPaths' );
 276+ if( actionPaths.length == 0 ) {
 277+ actionPaths['view'] = mw.config.get( 'wgArticlePath' );
 278+ }
 279+ var action = '';
 280+ for ( action in actionPaths ) {
 281+ var actionRe = new RegExp( '.*' + actionPaths[action].replace( '$1', '([^&?#]+)' ) );
 282+ var title = url.match( actionRe );
 283+ if( title !== null ) {
 284+ return title[1];
 285+ }
 286+ }
 287+
 288+ return null;
 289+ },
 290+
228291 // Access key prefix.
229292 // Will be re-defined based on browser/operating system detection in
230293 // mw.util.init().
@@ -577,4 +640,4 @@
578641
579642 mw.util.init();
580643
581 -} )( jQuery, mediaWiki );
\ No newline at end of file
 644+} )( jQuery, mediaWiki );

Follow-up revisions

RevisionCommit summaryAuthorDate
r87964merge in prettyURL patch...hashar11:41, 13 May 2011
r88667Revert r87964: destroyed standard segregation of non-view action links outsid...brion19:03, 23 May 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r84386Makes wikilinks nicer when possible...hashar14:57, 20 March 2011
r84428branch trunk@r84386 to play with the pretty URL featurehashar22:04, 20 March 2011

Comments

#Comment by Brion VIBBER (talk | contribs)   20:09, 23 May 2011

This seems to have been since reverted, re-applied in r87964, and re-reverted in r88667.

Status & tagging log