Search code examples
phpmediawiki

Style MediaWiki internal link based on last revision date of a page


I have a large wiki with a lot of pages, many of which are stale. I want to apply custom CSS to each link based on the age of the page it links to.

I have been digging into the source code of MediaWiki, and for each link, I can get the DBKey starting from a LinkTarget. See the source code here.

I am looking for a process which is essentially:

$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 fetch this information from the API, I could add a JS snippet to recolor the links.

Could anyone point me at resources I could look at to do this?


Solution

  • You could use the HtmlPageLinkRendererEnd hook

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

    Just add something like this to your LocalSettings.php

        $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 the '20230704142055' with your desired or current date

    You probably need to add this to the top of your php file also

    use MediaWiki\MediaWikiServices;
    use Title;