英文:
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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论