Jump to content

Topic on Extension talk:BreadCrumbs/Archive 2

Updated to MW 1.25+ to use wfLoadExtension

1
194.98.70.138 (talkcontribs)

I have updated my local copy of the extension code to use the newer extension loading mechanism in MW 1.25+. This has involved creating an extension.json file and changing the hooks file to be a class. This code is probably useful to others, but I do not have or want repository access so it is reproduced below. Sorry for the long message.

Ben

BreadCrumbsFunctions.php

Obsolete. Can be deleted.

BreadCrumbs.php

Obsolete. Can be deleted.

new LocalSettings.php contents:

wfLoadExtension( 'BreadCrumbs' );

$wgBreadCrumbsDelimiter = ' > ';

$wgBreadCrumbsCount = 5;

$wgBreadCrumbsShowAnons = true;

new file - extension.json

{

"name": "BreadCrumbs",

"author": "Manuel Schneider",

"url": "http://www.mediawiki.org/wiki/Extension:BreadCrumbs",

"descriptionmsg": "breadcrumbs-desc",

"type": "parserhook",

"ExtensionMessagesFiles": {

"Breadcrumbs": "BreadCrumbs.i18n.php"

},

"ResourceModules": {

"ext.breadCrumbs": {

"styles": "BreadCrumbs.css"

}

},

"ResourceFileModulePaths": {

"localBasePath": "",

"remoteExtPath": "BreadCrumbs"

},

"Hooks": {

"ArticleViewHeader": [

"BreadCrumbsHooks::fnBreadCrumbsShowHook"

],

"OutputPageParserOutput": [

"BreadCrumbsHooks::fnBreadCrumbsOutputHook"

]

},

"AutoloadClasses": {

"BreadCrumbsHooks": "BreadCrumbs.hooks.php"

}

}

new file - BreadCrumbs.hooks.php

class BreadCrumbsHooks {

public static function fnBreadCrumbsShowHook( &$article ) {

global $wgOut, $wgUser;

global $wgBreadCrumbsDelimiter, $wgBreadCrumbsCount, $wgBreadCrumbsShowAnons;

if ( !$wgBreadCrumbsShowAnons && $wgUser->isAnon() )

return true;

// deserialize data from session into array:

$m_BreadCrumbs = array();

if ( isset( $_SESSION['BreadCrumbs'] ) ) $m_BreadCrumbs = $_SESSION['BreadCrumbs'];

// cache index of last element:

$m_count = count( $m_BreadCrumbs ) - 1;

// Title object for the page we're viewing

$title = $article->getTitle();

// check for doubles:

if ( $m_count < 1 || $m_BreadCrumbs[ $m_count ] != $title->getPrefixedText() ) {

if ( $m_count >= 1 ) {

# reduce the array set, remove older elements:

$m_BreadCrumbs = array_slice( $m_BreadCrumbs, ( 1 - $wgBreadCrumbsCount ) );

}

// add new page:

array_push( $m_BreadCrumbs, $title->getPrefixedText() );

}

// serialize data from array to session:

$_SESSION['BreadCrumbs'] = $m_BreadCrumbs;

// update cache:

$m_count = count( $m_BreadCrumbs ) - 1;

// build the breadcrumbs trail:

$m_trail = '<div id="BreadCrumbsTrail">';

for ( $i = 0; $i <= $m_count; $i++ ) {

$title = Title::newFromText( $m_BreadCrumbs[$i] );

$m_trail .= Linker::link( $title, $m_BreadCrumbs[$i] );

if ( $i < $m_count ) $m_trail .= $wgBreadCrumbsDelimiter;

}

$m_trail .= '</div>';

$wgOut->addHTML( $m_trail );

// invalidate internal MediaWiki cache:

$title->invalidateCache();

$wgUser->invalidateCache();

// Return true to let the rest work:

return true;

}

// Entry point for the hook for printing the CSS:

public static function fnBreadCrumbsOutputHook( &$outputPage, $parserOutput ) {

global $wgBreadCrumbsShowAnons;

if ( $wgBreadCrumbsShowAnons || $outputPage->getUser()->isLoggedIn() ) {

$outputPage->addModules( 'ext.breadCrumbs' );

}

// Be nice:

return true;

}

}

Reply to "Updated to MW 1.25+ to use wfLoadExtension"