Extension:BackLinksFunctions

When there is a need to


 * display, how many pages link to a specific page,
 * or decide, whether one page links to another,

BackLinksFunctions may come to mind. For example, you can easilly and automatically highlight all the pages that link to a specific category.

Note: The extension is still under development.

= Functions =

Links counters
These functions count number of backward links. They return either
 * the number of direct links,
 * or the number of including pages,
 * or both.

#linking
The #linking function returns a number of pages linking to the given page.

#including
The #including function returns a number of pages that include the given template.

#refering
The #refering function returns a number of pages linking to or including the given page. This is practically a union of the and  functions.

Note: The #refering function does not count category links. Adding page to a category does not count as a backward link. This is, however not very good, is it? It should be fixed somehow in the future. If and when a missing #belonging function will be available.. -- jsimlo 22:30, 25 June 2006 (UTC)

Conditions about linkage
These functions works like conditions. They decide if one page links to or includes another and return one phrase or another.

They resembles if-then-else constructs similar to #ifeq parser function.

#iflinksto
If the given page from links to the given page to, returns the given true parameter. Otherwise returns the given false parameter.

#ifincludes
If the given page includes the given template, returns the given true parameter. Otherwise returns the given false parameter.

#ifbelongsto
If the given page belongs to the given category, returns the given true parameter. Otherwise returns the given false parameter.

#ifrefersto
If the given page source links to or includes a page destination, returns the given true parameter. Otherwise returns the given false parameter. If the the destination is a category to which the source page belongs to, true is returned as well.

This is practically a union of the, and  functions.

= Issues and Bugs =

Caching
In MediaWiki version 1.6 and early versions of 1.7, even pages that use these functions are being cached, as all other pages. The provided information is, therefore, not always up to date.


 * The bug causing this is described here: 5683.
 * You can fix the bug yourself: Enforcing DisableCache.

= Installation =

Copy and paste the source code below into a file named BackLinksFunctions.php in a new directory called BackLinksFunctions in your extensions directory.

Then put the following at the end of your LocalSettings.php:

require_once( "$IP/extensions/BackLinksFunctions/BackLinksFunctions.php" );

= Code =

This code has been tested on MediaWiki 1.6.7 and above.

History:


 * 25 Jul 2006 -- v1.0 -- First release.
 * 26 Jul 2006 -- v1.2 -- Added more functions.
 * 29 Jul 2006 -- v1.3 -- Added cache disabling.

<?php /*

Defines a subset of parser functions that operate on back links.

Returns the number of pages linking to the given page.

Returns the number of pages including the given page.

Returns the number of pages linking to the given page or including the page. This is practically a union of                     the  and.

If page 'from' links to page 'to', returns 'true'. Otherwise returns 'false'.

If page 'from' includes page 'to', returns 'true'. Otherwise returns 'false'.

If the 'page' belongs to the 'cat', returns 'true'. Otherwise returns 'false'.

This is practically a union of the , and.

Author: jsimlo Version: 1.3, Jun 29, 2006.



$wgExtensionFunctions[] = 'wfBackLinksFunctions';

$wgExtensionCredits['parserhook'][] = array ( 'name'   => 'BackLinksFunctions',  'url'    => 'http://meta.wikimedia.org/wiki/BackLinksFunctions',  'author' => 'Juraj Simlovic' );

$wgHooks['LanguageGetMagic'][] = 'wfBackLinksFunctionsGetMagic';

function wfBackLinksFunctions {   global $wgParser, $wgExtBackLinksFunctions;

$wgExtBackLinksFunctions = new ExtBackLinksFunctions ;

$wgParser->setFunctionHook ( '#linking',   array( &$wgExtBackLinksFunctions, 'linking'   )); $wgParser->setFunctionHook ( '#including', array( &$wgExtBackLinksFunctions, 'including' )); $wgParser->setFunctionHook ( '#refering',  array( &$wgExtBackLinksFunctions, 'refering'  ));

$wgParser->setFunctionHook ( '#iflinksto',   array( &$wgExtBackLinksFunctions, 'iflinksto'   )); $wgParser->setFunctionHook ( '#ifincludes',  array( &$wgExtBackLinksFunctions, 'ifincludes'  )); $wgParser->setFunctionHook ( '#ifbelongsto', array( &$wgExtBackLinksFunctions, 'ifbelongsto' )); $wgParser->setFunctionHook ( '#ifrefersto',  array( &$wgExtBackLinksFunctions, 'ifrefersto'  )); }

function wfBackLinksFunctionsGetMagic( &$magicWords, $langCode ) { switch ( $langCode ) { default: $magicWords['#linking']    = array( 0, '#linking' ); $magicWords['#including']  = array( 0, '#including' ); $magicWords['#refering']   = array( 0, '#refering' ); $magicWords['#iflinksto']  = array( 0, '#iflinksto' ); $magicWords['#ifincludes'] = array( 0, '#ifincludes' ); $magicWords['#ifbelongsto'] = array( 0, '#ifbelongsto' ); $magicWords['#ifrefersto'] = array( 0, '#ifrefersto' ); }   return true; }

class ExtBackLinksFunctions {	function countLinks ($links1, $links2 = 0) {		$backlinks = array ;

if (is_array ($links1)) foreach ($links1 as $val) $backlinks[(string)$val->mNamespace.(string)$val->mUrlform] = "1";

if (is_array ($links2)) foreach ($links2 as $val) $backlinks[(string)$val->mNamespace.(string)$val->mUrlform] = "1"; return (string) count ($backlinks); }

function linking ( &$parser, $page = '') {		$parser->disableCache;

$title = Title::newFromText ($page);

if (!is_object ($title)) return "?"; return $this->countLinks ($title->getLinksTo ); }	function including ( &$parser, $page = '') {		$parser->disableCache;

$title = Title::newFromText ($page);

if (!is_object ($title)) return "?"; return $this->countLinks ($title->getTemplateLinksTo ); }

function refering ( &$parser, $page = '') {		$parser->disableCache;

$title = Title::newFromText ($page);

if (!is_object ($title)) return "?"; $links1 = $title->getLinksTo; $links2 = $title->getTemplateLinksTo;

return $this->countLinks ($title->getLinksTo, $title->getTemplateLinksTo ); }

function searchLinks ($links, $title) {		if ($links) foreach ($links as $val): if (				  $val->mDbkeyform == $title->mDbkeyform &&				   $val->mNamespace == $title->mNamespace && 				   $val->mInterwiki == $title->mInterwiki				): return true; endif; endforeach; return false; }

function searchCategories ($categs, $wanted) {		if ($categs) foreach ($categs as $key => $val): if ($key == $wanted): return true; endif; endforeach; return false;

}	function iflinksto ( &$parser, $pagefrom = , $pageto = , $outtrue = , $outfalse = ) {		$parser->disableCache;

$titlefrom = Title::newFromText ($pagefrom); $titleto = Title::newFromText ($pageto); if (!is_object ($titleto) || !is_object ($titlefrom)) return "?"; if ($this->searchLinks ($titleto->getLinksTo, $titlefrom)) return $outtrue; else return $outfalse; }	function ifincludes ( &$parser, $pagefrom = , $pageto = , $outtrue = , $outfalse = , $time = '') {		$parser->disableCache;

$titlefrom = Title::newFromText ($pagefrom); $titleto = Title::newFromText ($pageto); if (!is_object ($titleto) || !is_object ($titlefrom)) return "?"; if ($this->searchLinks ($titleto->getTemplateLinksTo, $titlefrom)) return $outtrue; else return $outfalse; }

function ifbelongsto ( &$parser, $pagefrom = , $pageto = , $outtrue = , $outfalse = ) {		$parser->disableCache;

global $wgContLang;

$titlefrom = Title::newFromText ($pagefrom); $titleto = Title::newFromText ($pageto); if (!is_object ($titleto) || !is_object ($titlefrom)) return "?";

$wanted = $wgContLang->getNSText ($titleto->mNamespace).':'.$titleto->mDbkeyform; $categs = $titlefrom->getParentCategories ;

if ($this->searchCategories ($categs, $wanted)) return $outtrue; else return $outfalse; }

function ifrefersto ( &$parser, $pagefrom = , $pageto = , $outtrue = , $outfalse = ) {		$parser->disableCache;

global $wgContLang;

$titlefrom = Title::newFromText ($pagefrom); $titleto = Title::newFromText ($pageto); if (!is_object ($titleto) || !is_object ($titlefrom)) return "?"; if (		   $this->searchLinks ($titleto->getLinksTo, $titlefrom) ||		    $this->searchLinks ($titleto->getTemplateLinksTo , $titlefrom)		): return $outtrue; endif;

$wanted = $wgContLang->getNSText ($titleto->mNamespace).':'.$titleto->mDbkeyform; $categs = $titlefrom->getParentCategories ;

if ($this->searchCategories ($categs, $wanted)) return $outtrue; else return $outfalse; }

}

?>