search

Home  >  Q&A  >  body text

Style MediaWiki internal links based on the page's last revision date.

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粉184747536P粉184747536482 days ago443

reply all(1)I'll reply

  • P粉448130258

    P粉4481302582023-07-21 09:03:48

    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;

    reply
    0
  • Cancelreply