Add the following to LocalSettings.php
:
$wgHooks['SkinAfterContent'][]='backlinks_func';
function backlinks_func( &$data, Skin $skin ) {
$title = $skin->getTitle()->getPrefixedText();
$wikitext = "{{Special:WhatLinksHere/{$title}}}";
$parserOptions = ParserOptions::newFromUser( $skin->getUser() );
$parsedText = MediaWiki\MediaWikiServices::getInstance()->getParser()->parse( $wikitext, $skin->getTitle(), $parserOptions )->getText();
$out = '<div id="backlinks" class="backlinks"><h2>Backlinks</h2>'. $parsedText .'</div>';
$data = $out . $data;
return true;
}
Add a div class to [[:Mediawiki:Common.css]]
:
.backlinks {
background-color: #f9f9f9;
border: 1px solid #ccc;
border-radius: 5px;
padding: 20px;
margin: 10px 0;
width: 90%;
box-sizing: border-box;
}
.backlinks h2 {
color: #333;
font-size: 24px;
font-weight: bold;
margin-bottom: 10px;
}
.backlinks ul {
list-style: none;
padding: 0;
margin: 0;
}
.backlinks ul li {
padding: 5px 0;
}
.backlinks ul li a {
text-decoration: none;
color: #007bff;
}
.backlinks ul li a:hover {
color: #0056b3;
text-decoration: underline;
}
The following could also work by directly querying the database, however, it has some issures surrounding subpages:
$wgHooks['SkinAfterContent'][]='backlinks_func';
function backlinks_func( &$data, Skin $skin ) {
$dbr = wfGetDB( DB_REPLICA );
$res = $dbr->select(
'pagelinks',
'pl_title',
array('pl_namespace' => 0, 'pl_from' => $skin->getTitle()->getArticleID())
);
$out = '<div id="backlinks" class="backlinks"><h2>Backlinks</h2><ul>';
foreach( $res as $row ) {
$title = Title::newFromText($row->pl_title);
if ($title) {
$url = $title->getFullURL();
$out .= "<li><a href='" . htmlspecialchars($url) . "'>" . htmlspecialchars($title->getFullText()) . "</a></li>";
}
}
$out .= '</ul></div>';
$data = $out . $data;
return true;
}
Thanks so much! This is exactly what I needed for my wiki... so much easier than installing an extension!