r69014 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69013‎ | r69014 | r69015 >
Date:20:39, 4 July 2010
Author:laner
Status:deferred
Tags:
Comment:
* added different site name support
* added support for preferences
** Users can enable/disable bread crumbs
** Users can enable/disable prepending sitename on titles
** Users can set the number of bread crumbs to display
* delete cookies on log out
* removed some global preferences (replaced with default user options)
* fixed localization issue with namespaces
* upped version to 0.4
Modified paths:
  • /trunk/extensions/JSBreadCrumbs/JSBreadCrumbs.hooks.php (modified) (history)
  • /trunk/extensions/JSBreadCrumbs/JSBreadCrumbs.i18n.php (modified) (history)
  • /trunk/extensions/JSBreadCrumbs/JSBreadCrumbs.php (modified) (history)
  • /trunk/extensions/JSBreadCrumbs/js/BreadCrumbs.js (modified) (history)

Diff [purge]

Index: trunk/extensions/JSBreadCrumbs/JSBreadCrumbs.i18n.php
@@ -15,6 +15,11 @@
1616 'jsbreadcrumbs-desc' => 'Adds a trail of breadcrumbs below the tab bar',
1717 'jsbreadcrumbs-separator' => '»',
1818 'jsbreadcrumbs-leading-description' => 'Navigation trail',
 19+ 'prefs-jsbreadcrumbs' => 'Bread Crumbs',
 20+ 'prefs-jsbreadcrumbs-showsite' => 'Include site name in bread crumbs (useful when used across wikis)',
 21+ 'prefs-jsbreadcrumbs-showcrumbs' => 'Display bread crumbs',
 22+ 'prefs-jsbreadcrumbs-numberofcrumbs' => 'Number of bread crumbs',
 23+ 'prefs-jsbreadcrumbs-numberofcrumbs-max' => 'Maximum 20 bread crumbs',
1924 );
2025
2126 /** Afrikaans (Afrikaans)
Index: trunk/extensions/JSBreadCrumbs/JSBreadCrumbs.php
@@ -16,17 +16,19 @@
1717 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1818 # http://www.gnu.org/copyleft/gpl.html
1919
20 -$wgJSBreadCrumbsMaxCrumbs = 5;
2120 //BreadCrumbsSeparator default set via localization
2221 $wgJSBreadCrumbsSeparator = '';
2322 $wgJSBreadCrumbsCookiePath = '/';
 23+$wgDefaultUserOptions['jsbreadcrumbs-showcrumbs'] = false;
 24+$wgDefaultUserOptions['jsbreadcrumbs-showsite'] = false;
 25+$wgDefaultUserOptions['jsbreadcrumbs-numberofcrumbs'] = 5;
2426
2527 // Sets Credits
2628 $wgExtensionCredits['other'][] = array(
2729 'path' => __FILE__,
2830 'name' => 'JSBreadCrumbs',
2931 'author' => 'Ryan Lane',
30 - 'version' => '0.3',
 32+ 'version' => '0.4',
3133 'url' => 'http://www.mediawiki.org/wiki/Extension:JSBreadCrumbs',
3234 'descriptionmsg' => 'jsbreadcrumbs-desc',
3335 );
@@ -42,3 +44,4 @@
4345 // Registers Hooks
4446 $wgHooks['BeforePageDisplay'][] = 'JSBreadCrumbsHooks::addResources';
4547 $wgHooks['MakeGlobalVariablesScript'][] = 'JSBreadCrumbsHooks::addJSVars';
 48+$wgHooks['GetPreferences'][] = 'JSBreadCrumbsHooks::addPreferences';
\ No newline at end of file
Index: trunk/extensions/JSBreadCrumbs/js/BreadCrumbs.js
@@ -13,20 +13,38 @@
1414 if ( typeof wgJSBreadCrumbsLeadingDescription == "undefined" ) {
1515 wgJSBreadCrumbsLeadingDescription = "Navigation trail";
1616 }
 17+ if ( typeof wgJSBreadCrumbsShowSiteName == "undefined" ) {
 18+ wgJSBreadCrumbsShowSiteName = false;
 19+ }
1720
 21+ if ( wgCanonicalSpecialPageName == "Userlogout" ) {
 22+ $j.cookie( 'mwext-bc-title', '', { path: wgJSBreadCrumbsCookiePath } );
 23+ $j.cookie( 'mwext-bc-url', '', { path: wgJSBreadCrumbsCookiePath } );
 24+ $j.cookie( 'mwext-bc-site', '', { path: wgJSBreadCrumbsCookiePath } );
 25+ }
1826 // Get the breadcrumbs from the cookies
1927 var titleState = ( $j.cookie( 'mwext-bc-title' ) || "" ).split( '|' );
2028 var urlState = ( $j.cookie( 'mwext-bc-url' ) || "" ).split( '|' );
 29+ var siteState = ( $j.cookie( 'mwext-bc-site' ) || "" ).split( '|' );
2130
2231 // Strip the first title/url if it is empty
2332 if ( titleState[0].length == 0 ) {
2433 titleState.splice( 0, 1 );
2534 urlState.splice( 0, 1 );
 35+ siteState.splice( 0, 1 );
2636 }
2737
 38+ // Get the full title
2839 var title = wgTitle;
29 - if ( wgCanonicalNamespace.length ) {
30 - title = wgCanonicalNamespace + ":" + wgTitle;
 40+ if ( wgNamespaceNumber != 0 ) {
 41+ // If this is the project namespace, or we aren't showing the site name
 42+ // then add the namespace to the title. This avoids showing the sitename
 43+ // twice when adding pages in the project namespace.
 44+ // TODO: This is an awkward way to do this, and bug prone when the user
 45+ // logs in with a project page in the history. Find a better way.
 46+ if ( wgNamespaceNumber != 4 || !wgJSBreadCrumbsShowSiteName ) {
 47+ title = wgFormattedNamespaces[wgNamespaceNumber] + ":" + wgTitle;
 48+ }
3149 }
3250
3351 // Remove duplicates
@@ -35,16 +53,19 @@
3654 if ( matchTitleIndex != -1 && ( matchUrlIndex == matchTitleIndex ) ) {
3755 titleState.splice( matchTitleIndex, 1 );
3856 urlState.splice( matchTitleIndex, 1 );
 57+ siteState.splice( matchTitleIndex, 1 );
3958 }
4059
4160 // Add the current page
4261 titleState.push( title );
4362 urlState.push( location.pathname + location.search );
 63+ siteState.push( wgSiteName );
4464
4565 // Ensure we only display the maximum breadcrumbs set
4666 if ( titleState.length > wgJSBreadCrumbsMaxCrumbs ) {
4767 titleState = titleState.slice( titleState.length - wgJSBreadCrumbsMaxCrumbs );
4868 urlState = urlState.slice( urlState.length - wgJSBreadCrumbsMaxCrumbs );
 69+ siteState = siteState.slice( siteState.length - wgJSBreadCrumbsMaxCrumbs );
4970 }
5071
5172 // Insert the span we are going to populate
@@ -57,15 +78,20 @@
5879
5980 // Add the bread crumbs
6081 for ( var i = 0; i < titleState.length; i++ ) {
61 - urltoappend = '<a href="' + urlState[i] + '">' + titleState[i] + '</a> ';
 82+ if ( wgJSBreadCrumbsShowSiteName == true ) {
 83+ urltoappend = '<a href="' + urlState[i] + '">' + siteState[i] + ':' + titleState[i] + '</a> ';
 84+ } else {
 85+ urltoappend = '<a href="' + urlState[i] + '">' + titleState[i] + '</a> ';
 86+ }
6287 if ( i < titleState.length - 1 ) {
63 - // Only add the separator if this isn't the last title
64 - urltoappend = urltoappend + wgJSBreadCrumbsSeparator + ' ';
65 - }
 88+ // Only add the separator if this isn't the last title
 89+ urltoappend = urltoappend + wgJSBreadCrumbsSeparator + ' ';
 90+ }
6691 mwextbc.append( urltoappend );
6792 }
6893
6994 // Save the bread crumb states to the cookies
7095 $j.cookie( 'mwext-bc-title', titleState.join( '|' ), { path: wgJSBreadCrumbsCookiePath } );
7196 $j.cookie( 'mwext-bc-url', urlState.join( '|' ), { path: wgJSBreadCrumbsCookiePath } );
72 -} );
 97+ $j.cookie( 'mwext-bc-site', siteState.join( '|' ), { path: wgJSBreadCrumbsCookiePath } );
 98+});
\ No newline at end of file
Index: trunk/extensions/JSBreadCrumbs/JSBreadCrumbs.hooks.php
@@ -1,14 +1,18 @@
22 <?php
33 class JSBreadCrumbsHooks {
44
 5+ private static $variables = array();
 6+
57 /**
68 * BeforePageDisplay hook
79 */
810 public static function addResources( $out ) {
911 global $wgExtensionAssetsPath;
1012
11 - $out->addScriptFile( "$wgExtensionAssetsPath/JSBreadCrumbs/js/BreadCrumbs.js", 5 );
12 - $out->addExtensionStyle( "$wgExtensionAssetsPath/JSBreadCrumbs/css/BreadCrumbs.css?1" );
 13+ if ( self::enableBreadCrumbs() ) {
 14+ $out->addScriptFile( "$wgExtensionAssetsPath/JSBreadCrumbs/js/BreadCrumbs.js", 6 );
 15+ $out->addExtensionStyle( "$wgExtensionAssetsPath/JSBreadCrumbs/css/BreadCrumbs.css?1" );
 16+ }
1317
1418 return true;
1519 }
@@ -17,8 +21,13 @@
1822 * MakeGlobalVariablesScript hook
1923 */
2024 public static function addJSVars( $vars ) {
21 - global $wgJSBreadCrumbsMaxCrumbs, $wgJSBreadCrumbsSeparator, $wgJSBreadCrumbsCookiePath;
 25+ global $wgJSBreadCrumbsSeparator, $wgJSBreadCrumbsCookiePath;
 26+ global $wgUser;
2227
 28+ if ( !self::enableBreadCrumbs() ) {
 29+ return true;
 30+ }
 31+
2332 wfLoadExtensionMessages( 'JSBreadCrumbs' );
2433
2534 // Allow localized separator to be overriden
@@ -30,12 +39,54 @@
3140
3241 $variables = array();
3342
34 - $variables['wgJSBreadCrumbsMaxCrumbs'] = $wgJSBreadCrumbsMaxCrumbs;
 43+ $variables['wgJSBreadCrumbsMaxCrumbs'] = $wgUser->getOption( "jsbreadcrumbs-numberofcrumbs" );
3544 $variables['wgJSBreadCrumbsSeparator'] = $separator;
3645 $variables['wgJSBreadCrumbsCookiePath'] = $wgJSBreadCrumbsCookiePath;
3746 $variables['wgJSBreadCrumbsLeadingDescription'] = wfMsg( "jsbreadcrumbs-leading-description" );
 47+ $variables['wgJSBreadCrumbsShowSiteName'] = $wgUser->getOption( "jsbreadcrumbs-showsite" );
3848
3949 $vars = array_merge( $vars, $variables );
 50+
4051 return true;
4152 }
 53+
 54+ /**
 55+ * GetPreferences hook
 56+ *
 57+ * Add module-releated items to the preferences
 58+ */
 59+ public static function addPreferences( $user, $defaultPreferences ) {
 60+ $defaultPreferences['jsbreadcrumbs-showcrumbs'] = array(
 61+ 'type' => 'toggle',
 62+ 'label-message' => 'prefs-jsbreadcrumbs-showcrumbs',
 63+ 'section' => 'rendering/jsbreadcrumbs',
 64+ );
 65+
 66+ $defaultPreferences['jsbreadcrumbs-showsite'] = array(
 67+ 'type' => 'toggle',
 68+ 'label-message' => 'prefs-jsbreadcrumbs-showsite',
 69+ 'section' => 'rendering/jsbreadcrumbs',
 70+ );
 71+
 72+ $defaultPreferences['jsbreadcrumbs-numberofcrumbs'] = array(
 73+ 'type' => 'int',
 74+ 'min' => 1,
 75+ 'max' => 20,
 76+ 'section' => 'rendering/jsbreadcrumbs',
 77+ 'help' => wfMsgHtml( 'prefs-jsbreadcrumbs-numberofcrumbs-max' ),
 78+ 'label-message' => 'prefs-jsbreadcrumbs-numberofcrumbs',
 79+ );
 80+
 81+ return true;
 82+ }
 83+
 84+ static function enableBreadCrumbs() {
 85+ global $wgUser;
 86+
 87+ // Ensure we only enable bread crumbs if we are using vector and
 88+ // the user has them enabled
 89+ if ( $wgUser->getSkin() instanceof SkinVector && $wgUser->getOption( "jsbreadcrumbs-showcrumbs" ) ) {
 90+ return true;
 91+ }
 92+ }
4293 }

Status & tagging log