Extension:RDFa Breadcrumbs

From MediaWiki.org
Jump to navigation Jump to search
MediaWiki extensions manual
OOjs UI icon advanced.svg
RDFa Breadcrumbs
Release status: unmaintained
Implementation User interface, Parser function
Description Allows users to include breadcrumbs on pages which will get RDFa markup
Author(s) Hendrik Brummermanntalk
Latest version 1.0 (2010-09-18)
Database changes No
License Creative Commons Attribution 3.0
Download See the code section
Translate the RDFa Breadcrumbs extension if it is available at translatewiki.net
Check usage and version matrix.

The RDFa Breadcrumbs extension allows users to include breadcrumbs on pages which will get RDFa markup. It does not depend on categories but allows the definition on the individual pages.

Usage[edit]

{{#breadcrumbs:
   [[Main Page]]
   | {{ll|Manual:Extensions|Extensions}}
   | [[Extension:RDFa Breadcrumbs|RDFa Breadcrumbs]]
   | ... }}

This will look like this:

Main Page > Extensions > RDFa Breadcrumbs

In addition it will include RDFa markup so that Google will recognize it as Breadcrumb.

Installation[edit]

  • Add the following code at the bottom of your LocalSettings.php:
    require_once "$IP/extensions/Rdfabreadcrumbs/Rdfabreadcrumbs.php";
    
  • Edit [[MediaWiki:common.css]] and add the following section at the end:
.breadcrumbs {
   clear: both;
   border:1px solid #AAAAAA;
   background-color:#F9F9F9;
   padding:5px;
}
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.

Code[edit]

Rdfabreadcrumbs.php
<?php

// License: CC BY 2010 Hendrik Brummermann <nhb_web@nexgo.de>

if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is an extension to MediaWiki and thus not a valid entry point.' );
}

$wgHooks['ParserFirstCallInit'][] = 'efRDFaBreadcrumbs_Setup';
$wgHooks['LanguageGetMagic'][]       = 'efRDFaBreadcrumbs_Magic';

$wgExtensionCredits['parserhook'][] = array(
	'path' => __FILE__,
	'name' => 'RDFa_Breadcrumbs',
	'version' => '1.0',
	'url' => 'https://www.mediawiki.org/wiki/Extension:RDFa_Breadcrumbs',
	'author' => 'Hendrik Brummermann',
	'description' => 'Allows for breadcrumbs with RDFa markup',
);

function efRDFaBreadcrumbs_Setup( &$parser ) {
	$parser->setFunctionHook('breadcrumbs', 'RDFaBreadcrumbs_Render' );
	return true;
}

function efRDFaBreadcrumbs_Magic( &$magicWords, $langCode ) {
	$magicWords['breadcrumbs'] = array( 0, 'breadcrumbs' );
	return true;
}

function RDFaBreadcrumbs_Render($parser) {
	$output = '<div class="breadcrumbs" xmlns:v="http://rdf.data-vocabulary.org/#">';
	for ($i = 1; $i < func_num_args(); $i++) {
		if ($i > 1) {
			$output .= ' &gt; ';
		}
		$output .= '<span typeof="v:Breadcrumb">';
		$item = trim(func_get_arg($i));
		$output .= '<a href="'.htmlspecialchars(getURLFromLink($item))
			.'" rel="v:url" property="v:title">';
		$output .= htmlspecialchars(getTextFromLink($item));
		$output .= '</a></span>';
	}
	$output = $output . '</div>';
	return array($output, 'noparse' => true, 'isHTML' => true);
}

function getURLFromLink($link) {
	global $wgArticlePath;
	if (strpos($link, '[[') === 0) {
		// internal link
		$delimiter = '|';
		$start = 2;
		$pos2 = strpos($link, $delimiter);
		if ($pos2 === FALSE) {
			$pos2 = strlen($link) - 2;
		}
		$url = substr($link, $start, $pos2 - $start);
		return trim(preg_replace('/\$1/', urlencode(preg_replace('/[ +]/', '_', trim($url))), $wgArticlePath));
	} else if (strpos($link, '[') === 0) {
		// external link
		$delimiter = ' ';
		$start = 1;
		$pos2 = strpos($link, $delimiter);
		if ($pos2 === FALSE) {
			$pos2 = strlen($link) - 1;
		}
		$url = substr($link, $start, $pos2 - $start);
		return trim($url);
	} else {
		return trim(preg_replace('/\$1/', urlencode(preg_replace('/[ +]/', '_', trim($link))), $wgArticlePath));
	}
}

function getTextFromLink($link) {
	if (strpos($link, '[[') === 0) {
		// internal link
		$delimiter = '|';
		$start = 1;
	} else if (strpos($link, '[') === 0) {
		// external link
		$delimiter = ' ';
		$start = 0;
	} else {
		return trim($link);
	}
	$pos1 = strpos($link, $delimiter);
	if ($pos1 === FALSE) {
		$pos1 = $start;
	}
	$pos2 = strpos($link, ']');
	return trim(substr($link, $pos1 + 1, $pos2 - $pos1 - 1));
}