Style MediaWiki internal links based on the page's last revision date.
P粉184747536
P粉184747536 2023-07-20 10:47:09
0
1
439

I have a huge Wikipedia with many pages, many of which are out of date. I want to apply custom CSS styles to each link based on the age of the linked page.

I have been looking at the source code of MediaWiki and for each link I can get the DBKey starting from the LinkTarget. The source code can be viewed here.

I'm looking for a process that basically goes like this:

$dbKey = $target->getDBkey();
$page = find_page_by_title($dbKey);
$last_revision = get_last_revision($page);
// Additional processing based on the date of $last_revision

Alternatively, if there is a way to get this information from the API, I could add a JS snippet to recolor the link.

Can someone direct me to resources to accomplish this?

P粉184747536
P粉184747536

reply all(1)
P粉448130258

You can use HtmlPageLinkRendererEnd hook.

https://www.mediawiki.org/wiki/Manual:Hooks/HtmlPageLinkRendererEnd

Simply add the following to your LocalSettings.php file:

$wgHooks['HtmlPageLinkRendererEnd'][] = function($linkRenderer, $target, $isKnown, &$text, &$attribs, &$ret) {

        $title = Title::newFromLinkTarget($target);
        $id = $title->getLatestRevID();
        $revStore = MediaWikiServices::getInstance()->getRevisionStore();
        $date = $revStore->getTimestampFromId( $id );

        if ($date > '20230704142055') {
            $attribs['class'] = "old-page";
        }

        if ($date > '20230704142070') {
            $attribs['class'] = "newer-page";
        }        
    
    };

Just change '20230704142055' to your desired or current date.

You may also need to add this code to the top of your php file.

use MediaWiki\MediaWikiServices;
use Title;
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template