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

Revision: 84491
Author:   hashar
Date:     2011-03-21 21:50:20 +0000 (Mon, 21 Mar 2011)
Log Message:
-----------
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
    branches/hashar/prettyURL/resources/mediawiki.util/mediawiki.util.js

Modified: 
branches/hashar/prettyURL/resources/mediawiki.action/mediawiki.action.watch.ajax.js
===================================================================
--- 
branches/hashar/prettyURL/resources/mediawiki.action/mediawiki.action.watch.ajax.js
 2011-03-21 21:36:21 UTC (rev 84490)
+++ 
branches/hashar/prettyURL/resources/mediawiki.action/mediawiki.action.watch.ajax.js
 2011-03-21 21:50:20 UTC (rev 84491)
@@ -65,8 +65,8 @@
                var link = this;
                $link
                        .data( 'icon', $link.closest( 'li' ).hasClass( 'icon' ) 
)
-                       .data( 'action', mw.util.getParamValue( 'action', 
link.href ) == 'unwatch' ? 'unwatch' : 'watch' );
-               var title = mw.util.getParamValue( 'title', link.href );
+                       .data( 'action', mw.util.getActionFrom( link.href ) == 
'unwatch' ? 'unwatch' : 'watch' );
+               var title = mw.util.getTitleFrom( link.href );
                $link.data( 'target', title.replace( /_/g, ' ' ) );
        });
 

Modified: branches/hashar/prettyURL/resources/mediawiki.util/mediawiki.util.js
===================================================================
--- branches/hashar/prettyURL/resources/mediawiki.util/mediawiki.util.js        
2011-03-21 21:36:21 UTC (rev 84490)
+++ branches/hashar/prettyURL/resources/mediawiki.util/mediawiki.util.js        
2011-03-21 21:50:20 UTC (rev 84491)
@@ -209,6 +209,12 @@
                /**
                 * Grab the URL parameter value for the given parameter.
                 * Returns null if not found.
+                * Beware! When action paths are enabled (wgActionPaths) using 
this function
+                * to retrieve the 'action' or 'title' parameter will probably 
fail since
+                * those parameters are hidden in the path.
+                * To safely query for:
+                *   'action' use getActionFrom( url )
+                *   'title'  use getTitleFrom( url )
                 *
                 * @param param The parameter name
                 * @param url URL to search through (optional)
@@ -224,6 +230,63 @@
                        return null;
                },
 
+               /** 
+                * Try to find the wiki action for a given URL with actions 
paths support
+                *
+                * @param url URL to search for a wiki action
+                */
+               'getActionFrom' : function( url ) {
+                       // attempt to get the action from the parameter 
[&?]action=
+                       var action = mw.util.getParamValue( 'action', url );
+                       if( action !== null ) {
+                               return action;
+                       }
+
+                       // now from the action paths
+                       var actionPaths = mw.config.get( 'wgActionPaths' );
+                       if( actionPaths.length == 0 ) {
+                               actionPaths['view'] = mw.config.get( 
'wgArticlePath' );
+                       }
+                       var action = '';
+                       for ( action in actionPaths ) {
+                               var actionRe = new RegExp( 
actionPaths[action].replace( '$1', '.*?' ) );
+                               if( url.match( actionRe ) ) {
+                                       return action;
+                               }
+                       }
+
+                       return null;
+               },
+
+               /**
+                * Try to find the wiki title for a given URL with actions 
paths support
+                *
+                * @param url URL to search for a title
+                */
+               'getTitleFrom' : function( url ) {
+                       // attempt to get the title from the parameter 
[&?]title=
+                       var title = mw.util.getParamValue( 'title', url );
+                       if( title !== null ) {
+                               return title;
+                       }
+
+                       // now from the action paths    
+                       var actionPaths = mw.config.get( 'wgActionPaths' );
+                       if( actionPaths.length == 0 ) {
+                               actionPaths['view'] = mw.config.get( 
'wgArticlePath' );
+                       }
+                       var action = '';
+                       for ( action in actionPaths ) {
+                               var actionRe = new RegExp( '.*' + 
actionPaths[action].replace( '$1', '([^&?#]+)' ) );
+                               var title = url.match( actionRe );
+                               if( title !== null ) {
+                                       return title[1];
+                               }
+                       }
+
+                       return null;
+               },
+
                // Access key prefix.
                // Will be re-defined based on browser/operating system 
detection in
                // mw.util.init().
@@ -577,4 +640,4 @@
 
        mw.util.init();
 
-} )( jQuery, mediaWiki );
\ No newline at end of file
+} )( jQuery, mediaWiki );


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

Reply via email to