User:Czheng/List Backlinks
From Meta
Contents |
[edit] Description
- This extension is a modification of Fxparlant's Blog extension. When called, it outputs a list of pages within a defined category that link to the current page.
- It will only work with MediaWiki 1.5. If you're using an earlier version, the link above is probably what you're looking for. I had to change Fxparlant's version in order to get it working on my 1.5 install, because the DB scheme has changed. In the process, I used some code from the DynamicPageList extension.
BE WARNED: I am a complete newbie to MediaWiki, to PHP and to MySQL. I therefore cannot vouch for the effectiveness or safety of this code beyond saying that it works for me.
Note: If you can suggest any improvements to this code, they would be greatly appreciated.
[edit] Usage
Like Fxparlant's original extension, you will call this from somewhere in your code using the following:
<blink>cat=YourCategory</blink>
This will display a list of pages in YourCategory that link to the current page. Like Fxparlant's code, you can also specify a page that is not the current page:
<blink>page=Frantz_Fanon;cat=Sources</blink>
This will return a list of articles in the "Sources" category that link to Frantz Fanon (presumably as their author).
[edit] Example
In the example below, I inserted the following code into a page called Frantz_Fanon:
== Works == <blink>cat=Sources</blink> == Citations == <blink>cat=Citations</blink>
Here is the result.
This of course, assumes that I've created either sources or citations that link to Fanon's author page.
[edit] Code
<?php
$wgExtensionFunctions[] = "wfBlinkExtension";
function WfBlinkExtension() {
global $wgParser, $wgMessageCache;
$wgMessageCache->addMessages( array(
'blink_noresults' => 'None listed',
)
);
$wgParser->setHook( "blink", "renderExample" );
} function renderExample( $input ) {
global $wgTitle;
$pageTitle = $wgTitle->getPrefixedDbKey();
//Get parameters
$params = split(';', trim($input));
$arguments = array();
foreach ($params as $param) {
$parts = split( '=', $param );
if (isset($parts[0]) && isset($parts[1])) {
$name = trim($parts[0]);
$arguments[$name] = trim($parts[1]);
}
}
if (isset($arguments['page'])){
$pageTitle = $arguments['page'];
}
if (isset($arguments['cat'])){
$cat = $arguments['cat'];
}
$output .= wfBlink($pageTitle,$cat);
return $output;
}
function wfBlink($target, $cat){
global $wgUseCategoryMagic, $wgUser,$wgParser,$parserOptions,$wgTitle, $wgOut,$wgLinkCache;
$dbr =& wfGetDB( DB_SLAVE );
$pageTitle = $wgTitle->getText();
$nt = Title::newFromURL( $target );
$limit = 500;
$fname = "wfBlink";
$sStartList = '<ul>';
$sEndList = '</ul>';
$sStartItem = '<li>';
$sEndItem = '</li>';
$sql = "SELECT page_id, page_namespace, page_title, page_is_redirect FROM pagelinks, page, categorylinks WHERE ((pl_title = CAST('$target' AS BINARY)) AND (pl_from = page_id) AND (page_id = cl_from) AND (cl_to = CAST('$cat' AS BINARY ))) LIMIT $limit";
$res = $dbr->query( $sql, DB_READ, $fname );
$sk =& $wgUser->getSkin();
if ($dbr->numRows( $res ) == 0)
{
if (false == $bSuppressErrors)
return htmlspecialchars( wfMsg( 'blink_noresults' ) );
else
return '';
}
$output .= $sStartList . "\n";
//process results of query, outputing equivalent of <li>[[Article]]</li> for each result,
//or something similar if the list uses other startlist/endlist
while ($row = $dbr->fetchObject( $res ) ) {
$title = Title::makeTitle( $row->page_namespace, $row->page_title);
$output .= $sStartItem . $sk->makeKnownLinkObj($title) . $sEndItem . "\n";
}
//end unordered list
$output .= $sEndList . "\n";
return $output;
}
?>

