Jump to content

Extension:BackLinksFunctions: Difference between revisions

From mediawiki.org
Content deleted Content added
No edit summary
Jsimlo (talk | contribs)
m #refering: one more try :))
Line 39: Line 39:
The #refering function returns a number of pages linking to or including the given page. This is practically a union of the {{#linking}} and {{#including}} functions.
The #refering function returns a number of pages linking to or including the given page. This is practically a union of the {{#linking}} and {{#including}} 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.. -- [[User:195.113.23.156|195.113.23.156]] 22:30, 25 June 2006 (UTC)
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.. -- [[User:Jsimlo|jsimlo]] 22:30, 25 June 2006 (UTC)


== #linksto ==
== #linksto ==

Revision as of 22:30, 25 June 2006

MediaWiki extensions manual
BackLinksFunctions
Release status: unknown
Implementation parser extension (invalid type)
Description Counts backward links to a specified page.
Latest version 1.0 (2006-06-25)
MediaWiki 1.6.7
Licence No licence specified
Download No link

When there is a need to

  • display, how may 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

#linking

{{#linking: page }}

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

#including

{{#including: template }}

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

#refering

{{#refering: page }}

The #refering function returns a number of pages linking to or including the given page. This is practically a union of the {{#linking}} and {{#including}} 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)

#linksto

{{#linksto: from | to | true | false }}

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

#includes

{{#includes: page | template | true | false }}

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

#belongsto

{{#belongsto: page | category | true | false }}

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

#refersto

{{#refersto: source | destination | true | false }}

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 {{#linksto}}, {{#refersto}} and {{#belongsto}} functions.

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 2005 -- v1.1 -- Added more functions.
<?php
/*

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

 {{#linking:page}}    Returns the number of pages linking to the given page.

 {{#including:page}}  Returns the number of pages including the given page.

 {{#refering:page}}   Returns the number of pages linking to the given page
                      or including the page. This is practically a union of
                      the {{#linking}} and {{#including}}.


{{#linksto:from|to|true|false}}    If page 'from' links to page 'to', returns 'true'.
                                   Otherwise returns 'false'.

{{#includes:from|to|true|false}}   If page 'from' includes page 'to', returns 'true'.
                                   Otherwise returns 'false'.

{{#belongsto:page|cat|true|false}} If the 'page' belongs to the 'cat', returns 'true'.
                                   Otherwise returns 'false'.

{{#refersto:from|to|true|false}}   This is practically a union of the {{#linksto}},
                                   {{#refersto}} and {{#belongsto}}.


 Author: jsimlo [http://meta.wikimedia.org/wiki/User:jsimlo]
 Version: 1.1, Jun 26, 2006.

*/

$wgExtensionFunctions[] = 'wfBackLinksFunctions';

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

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 ( '#linksto',    array( &$wgExtBackLinksFunctions, 'linksto'   ));
    $wgParser->setFunctionHook ( '#includes',   array( &$wgExtBackLinksFunctions, 'includes'  ));
    $wgParser->setFunctionHook ( '#belongsto',  array( &$wgExtBackLinksFunctions, 'belongsto' ));
    $wgParser->setFunctionHook ( '#refersto',   array( &$wgExtBackLinksFunctions, 'refersto'  ));
}

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 = '')
	{
		$title = Title::newFromText ($page);

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

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

	function refering ( &$parser, $page = '')
	{
		$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 linksto ( &$parser, $pagefrom = '', $pageto = '', $outtrue = '', $outfalse = '')
	{
		$titlefrom = Title::newFromText ($pagefrom);
		$titleto = Title::newFromText ($pageto);
		
		if (!is_object ($titleto) || !is_object ($titlefrom))
			return "?";
		
		if ($this->searchLinks ($titleto->getLinksTo (), $titlefrom))
			return "[[$pagefrom]] links to [[$pageto]] -- $outtrue";
		else
			return "[[$pagefrom]] does not link to [[$pageto]] -- $outfalse";
	}
	
	function includes ( &$parser, $pagefrom = '', $pageto = '', $outtrue = '', $outfalse = '', $time = '')
	{
		$titlefrom = Title::newFromText ($pagefrom);
		$titleto = Title::newFromText ($pageto);
		
		if (!is_object ($titleto) || !is_object ($titlefrom))
			return "?";
		
		if ($this->searchLinks ($titleto->getTemplateLinksTo (), $titlefrom))
			return "[[$pagefrom]] includes [[$pageto]] -- $outtrue";
		else
			return "[[$pagefrom]] does not include [[$pageto]] -- $outfalse";
	}

	function belongsto ( &$parser, $pagefrom = '', $pageto = '', $outtrue = '', $outfalse = '')
	{
		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 "[[:$pagefrom]] belongs to [[:$pageto]] -- $outtrue";
		else
			return "[[:$pagefrom]] does not belong to [[:$pageto]] -- $outfalse";
	}

	function refersto ( &$parser, $pagefrom = '', $pageto = '', $outtrue = '', $outfalse = '')
	{
		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 "[[$pagefrom]] refers to [[$pageto]] -- $outtrue";
		endif;

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

		if ($this->searchCategories ($categs, $wanted))
			return "[[:$pagefrom]] refers to [[:$pageto]] -- $outtrue";
		else
			return "[[:$pagefrom]] does not refer to [[:$pageto]] -- $outfalse";
	}

}

?>