Extension:BreadCrumbsBar

From MediaWiki.org
Jump to: navigation, search
Language: English  • italiano
MediaWiki extensions manual
Crystal Clear action run.png
BreadCrumbsBar

Release status: beta

Implementation User interface
Description Creates a bread crumbs navigation bar
Author(s) Alessandra Bilardi (Bilarditalk)
Latest version 0.2 (2015-05-12)
MediaWiki 1.14+
Database changes No
License GPL
Download See the code section
Example CRIBI Genomics
Parameters
  • $wgBreadCrumbsBarDelimiter
  • $wgBreadCrumbsBarRoot
  • $wgBreadCrumbsBarHome
  • $wgBreadCrumbsBarTitle
  • $wgBreadCrumbsBarLink
  • $wgBreadCrumbsBarLabel
  • $wgBreadCrumbsBarNamespace

Translate the BreadCrumbsBar extension if it is available at translatewiki.net

Check usage and version matrix; code metrics

The BreadCrumbsBar extension provides a real navigation bar like the extensions listed in the last paragraph of this page. These extensions use global $wgUser;.. so that if you set $wgGroupPermissions['*']['read'] = false; or others, then anonymous user has not got navigation bar even if there is $wgWhitelistRead.

Moreover, more wiki site have got a complex categories tree and each pages have got more categories. In this case, how does extension decide navigation bar? I resolved this question to thinking that there are some categories more important in comparison to others. These categories could link an category root added into $wgBreadCrumbsBarRoot. You can also see the navigation bar in the category pages or others namespace pages (see $wgBreadCrumbsBarNamespace).

I have got public pages and private pages (two categories 'root') and I create redirect pages about all public categories pages.. so that I need to decide if I want navigation bar into special page (see $wgBreadCrumbsBarTitle), if I want namespace into link and/or label about each category (see $wgBreadCrumbsBarLink and $wgBreadCrumbsBarLabel) and what it is my first link (see $wgBreadCrumbsBarHome).

Settings[edit | edit source]

In LocalSettings.php add the following code:

// add BreadCrumbsBar
// delimiter between links
$wgBreadCrumbsBarDelimiter = ' > ';
// categories root names
$wgBreadCrumbsBarRoot = 'Site_Map;Archive';
// HTML code about first link omnipresent
$wgBreadCrumbsBarHome = '<a href="Main_Page">Home</a>';
// true: all pages have got bar; false: only pages created from users have got bar
$wgBreadCrumbsBarTitle = false;
// true: all links have got namespace category; false: all links have not got namespace category 
$wgBreadCrumbsBarLink = true;
// true: all links labels have got namespace category; false: all links labels have not got namespace category 
$wgBreadCrumbsBarLabel = true;
// empty: none of the namespaces have not got bar; with namespace with comma delimiter: those namespaces have got bar
$wgBreadCrumbsBarNamespace = "Category;User";

require_once("extensions/BreadCrumbsBar/BreadCrumbsBar.php");


In Monobook.php change lines:

<div id='content'>
  <a name='top' id='top'></a>

to:

<div id='content'>
  <?php $BreadCrumbsBar=new BreadCrumbsBar();
  $Title=Title::newFromId($this->data['articleid']);
  echo $BreadCrumbsBar->displayBar($Title); ?>
  <a name='top' id='top'></a>

If you use ltrMenuPlus skin, then change lines:

<div id='content'>
  <a name='top' id='top'></a>

to:

<?php $BreadCrumbsBar=new BreadCrumbsBar();
$Title=Title::newFromId($this->data['articleid']);
echo $BreadCrumbsBar->displayBar($Title); ?>
<div id='content'>
  <a name='top' id='top'></a>

Code[edit | edit source]

BreadCrumbsBar.php
<?php
/*
 * BreadCrumbsBar is an extension for providing an 
 * bread crumbs navigation bar.
 */

//Not a valid entry point, skip unless MEDIAWIKI is defined
if (!defined('MEDIAWIKI')) {
//This text is a problem for 1.24 and lower
//        echo <<<EOT
//To install my extension, put the following line in LocalSettings.php:
//require_once( "$IP/extensions/BreadCrumbsBar/BreadCrumbsBar.php" );
//EOT;
        exit( 1 );
}

/**
 * The Credits Extension
 */
$wgExtensionCredits['parserhook'][] = array(
	'name' => 'Bread Crumbs Bar',
	'version' => '0.2',
	'author' => '[http://www.mediawiki.org/User:Bilardi Alessandra Bilardi]',
	'url' => 'http://www.mediawiki.org/wiki/Extension:BreadCrumbsBar',
	'description' => "Creates a bread crumb navigation bar."
);

/**
 * Definition of the class
 */    
class BreadCrumbsBar {

	/**
	 * constructor
	 */
	function __construct() {
		global $wgScript;
		global $wgBreadCrumbsBarDelimiter, $wgBreadCrumbsBarRoot, $wgBreadCrumbsBarHome, $wgBreadCrumbsBarTitle, $wgBreadCrumbsBarLink, $wgBreadCrumbsBarLabel, $wgBreadCrumbsBarNamespace;

		/**
		 * This is the configuration file for this extension
		 */
		if (is_null($wgBreadCrumbsBarDelimiter)) $wgBreadCrumbsBarDelimiter = ' &gt; ';
		if (is_null($wgBreadCrumbsBarRoot)) $wgBreadCrumbsBarRoot = 'Site_Map';
		if (is_null($wgBreadCrumbsBarHome)) $wgBreadCrumbsBarHome = '<a href="'.$wgScript.'">Home</a>';
		if (is_null($wgBreadCrumbsBarTitle)) $wgBreadCrumbsBarTitle = false;
		if (is_null($wgBreadCrumbsBarLink)) $wgBreadCrumbsBarLink = true;
		if (is_null($wgBreadCrumbsBarLabel)) $wgBreadCrumbsBarLabel = true;
                if (is_null($wgBreadCrumbsBarNamespace)) $wgBreadCrumbsBarNamespace = 'Category;User';
 
	}	      

	/**
	 * get categories links and create hash
	 */
	private function createHash($dbr) {
		// Query the database.
		$res = $dbr->select(
			array('page', 'categorylinks'),
			array('page_title', 'page_namespace', 'cl_sortkey', 'cl_to'),
			'cl_from = page_id','',''
		);
		if ($res === false)
		return array_keys();
 
		// Convert the results list into an array.
		$hash = array_keys();
		while ($x = $dbr->fetchObject($res)) {
			if ($hash[$x->cl_sortkey]) $hash[$x->cl_sortkey] .= ','.$x->cl_to;
			else $hash[$x->cl_sortkey] = $x->cl_to;
			//Underconstruction: Make a Title for this item.
			//$title = Title::makeTitle($l->page_namespace, $l->page_title);
		}
		// Free the results.
		$dbr->freeResult($res);

		return $hash;
	}

	/**
	 * is there root? 
	 */
	private function isThereRoot($cat,&$hash) {
		global $wgBreadCrumbsBarRoot;
		$loop = true;

		while ($loop) {
			if (!$cat) return false;
			if (strstr($wgBreadCrumbsBarRoot,$cat)) return true;
			$cat = str_replace("_", " ", $cat);
			if (strstr($hash[$cat],",")) {
				$matches = explode(",",$hash[$cat]);
				foreach ($matches as $ct)
					if ($this->isThereRoot($ct,$hash)) 
						return true;
			} else $cat = $hash[$cat];
		}

		return true;
	}

	/**
	 * get category
	 */
	private function getCat($page,&$hash) {
                global $wgBreadCrumbsBarNamespace;
		$cat = '';
//		$page = str_replace("_", " ", $page); // 1.14 and higher
		$page = strtoupper(str_replace("_", " ", $page)); // 1.24 and lower
		if (!empty($wgBreadCrumbsBarNamespace)) {
		        $namespaces = preg_replace("/;/", ":|", $wgBreadCrumbsBarNamespace);
                        if (preg_match("/^" . $namespaces . ":/i", $page)) {
                                $page = preg_replace("/^" . $namespaces . ":/i", "", $page);
                        }
                }
		if (strstr($hash[$page],",")) {
			$matches = explode(",",$hash[$page]);
			foreach ($matches as $ct)
				if ($this->isThereRoot($ct,$hash)) {
					$cat = $ct;
					break;
				}
		} else {
			$cat = $hash[$page];
		}

		return $cat;
	}

	/**
	 * back root
	 */
	private function backRoot($page,&$hash) {
		global $wgBreadCrumbsBarRoot;

		$cts = array();

		if (strstr($wgBreadCrumbsBarRoot,$page) === false) {
			$loop = true;
			while ($loop) {
				$cat = $this->getCat($page,$hash);
				if ($cat && strstr($wgBreadCrumbsBarRoot,$cat) === false) {
					$cts[] = $cat;
					$page = $cat;
				} else $loop = false;
			}
		}

		return $cts;
	}

	/**
	 * create array of categories
	 */
	private function createArray($title) {
		global $wgBreadCrumbsBarDelimiter, $wgBreadCrumbsBarRoot;

		// Query the database.
		$dbr =& wfGetDB(DB_SLAVE);
		$hash = array_keys();
		$matches = array();
		$array = array();
		$backcat = '';
		$hash=$this->createHash($dbr);
		$page = str_replace("_", " ", $title);
		$array=$this->backRoot($page,$hash);

		return $array;
	}

	/**
	 * displayBar
	 */
	public function displayBar($Title) {
		global $wgBreadCrumbsBarDelimiter, $wgBreadCrumbsBarRoot, $wgBreadCrumbsBarHome, $wgBreadCrumbsBarTitle, $wgBreadCrumbsBarLink, $wgBreadCrumbsBarLabel;
		$links = array();
		$links = $this->createArray($Title);
		$bar= '';
		foreach ($links as $l) {
			$l = str_replace("_", " ", $l);
			if ($wgBreadCrumbsBarLink) $nl = ':Category:'.$l; else $nl = $l;
			if ($wgBreadCrumbsBarLabel) $l = 'Category:'.$l;
			$link = '<a href="'.$nl.'">'.$l.'</a>';
			$bar = $bar ? $link.' '.$wgBreadCrumbsBarDelimiter.' '.$bar : $link; 
		}

		//if( get_class( $Title ) !="Title") return "";

		if ($Title != "Main Page" && ($wgBreadCrumbsBarTitle || get_class( $Title ) == "Title")) 
			$bar = $bar ? 
//				'<div id="BreadCrumbsBar">'.$wgBreadCrumbsBarHome.' '.$wgBreadCrumbsBarDelimiter.' '.$bar.' '.$wgBreadCrumbsBarDelimiter.' '.$Title.'</div>'
				'<div id="BreadCrumbsBar">'.$wgBreadCrumbsBarHome.' '.$wgBreadCrumbsBarDelimiter.' '.$bar.'</div>'
//				: '<div id="BreadCrumbsBar">'.$wgBreadCrumbsBarHome.' '.$wgBreadCrumbsBarDelimiter.' '.$Title.'</div>';
				: '<div id="BreadCrumbsBar">'.$wgBreadCrumbsBarHome.'</div>';

		return $bar;
	}

}

See also[edit | edit source]