Extension:BackLinksFunctions
From MediaWiki.org
|
Release status: experimental |
|
|---|---|
| Implementation | Parser function |
| Description | Counts backward links to a specified page. |
| Author(s) | Juraj Simlovic |
| Last Version | 1.3 (2006-06-29) |
| MediaWiki | 1.6.7 |
| License | No license specified |
| Download | see below |
|
check usage (experimental) |
|
When there is a need to:
- Display how many pages link to a specific page
- Decide whether one page links to another
BackLinksFunctions may come to mind. For example, you can easily and automatically highlight all the pages that link to a specific category.
Note: The extension is still under development.
Contents |
[edit] Functions
[edit] 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
[edit] #linking
{{#linking: page }}
Returns a number of pages linking to the given page.
[edit] #including
{{#including: template }}
Returns a number of pages that include the given template.
[edit] #refering
{{#refering: page }}
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.
[edit] 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.
[edit] #iflinksto
{{#iflinksto: 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.
[edit] #ifincludes
{{#ifincludes: page | template | true | false }}
If the given page includes the given template, returns the given true parameter. Otherwise returns the given false parameter.
[edit] #ifbelongsto
{{#ifbelongsto: page | category | true | false }}
If the given page belongs to the given category, returns the given true parameter. Otherwise returns the given false parameter.
[edit] #ifrefersto
{{#ifrefersto: 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 {{#iflinksto}}, {{#ifrefersto}} and {{#ifbelongsto}} functions.
[edit] Issues and Bugs
[edit] 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: bugzilla:5683.
- You can fix the bug yourself: Enforcing DisableCache.
[edit] 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" );
[edit] Related extensions
- DynamicPageList has a similar focus
[edit] 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. {{#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}}. {{#iflinksto:from|to|true|false}} If page 'from' links to page 'to', returns 'true'. Otherwise returns 'false'. {{#ifincludes:from|to|true|false}} If page 'from' includes page 'to', returns 'true'. Otherwise returns 'false'. {{#ifbelongsto:page|cat|true|false}} If the 'page' belongs to the 'cat', returns 'true'. Otherwise returns 'false'. {{#ifrefersto:from|to|true|false}} This is practically a union of the {{#iflinksto}}, {{#ifrefersto}} and {{#ifbelongsto}}. Author: jsimlo [http://meta.wikimedia.org/wiki/User:jsimlo] Version: 1.3, Jun 29, 2006. */ $wgExtensionFunctions[] = 'wfBackLinksFunctions'; $wgExtensionCredits['parserhook'][] = array ( 'name' => 'BackLinksFunctions', 'url' => 'http://www.mediawiki.org/wiki/Extension: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; } } ?>