根据页面的最后修订日期,样式化MediaWiki内部链接

huangapple go评论71阅读模式
英文:

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

问题

$dbKey = $target->getDBkey();
$page = find_page_by_title($dbKey);
$last_revision = get_last_revision($page);
// 根据 $last_revision 的日期进行附加处理
英文:

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?

答案1

得分: 1

你可以使用HtmlPageLinkRendererEnd挂钩

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

只需将类似以下内容添加到您的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";
        }        
    
    };

只需将'20230704142055'更改为您所需的或当前日期

您可能还需要将此添加到您的php文件顶部

use MediaWiki\MediaWikiServices;
use Title;
英文:

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;

huangapple
  • 本文由 发表于 2023年7月17日 23:53:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定