You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MediaWiki扩展开发:MultiContentSave钩子下如何获取页面编辑前内容

实现方案

你可以通过MediaWiki核心的RevisionStore服务,获取当前页面已持久化存储的最新修订版本,该版本的内容就是本次编辑前的原始内容。修改后的回调逻辑参考如下:

use MediaWiki\MediaWikiServices;

class MyExtensionHooks {
    public static function onMultiContentSave(
        RenderedRevision $renderedRevision,
        UserIdentity $user,
        CommentStoreComment $summary,
        $flags,
        Status $hookStatus
    ) {
        $revision = $renderedRevision->getRevision();
        $title = $revision->getPageAsLinkTarget();
        $new_content = $revision->getContent(SlotRecord::MAIN, RevisionRecord::RAW)->getNativeData();

        // 新增:获取编辑前的旧版本内容
        $old_content = null;
        $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
        // 获取当前页面已存储的最新修订版本(新版本尚未写入,因此该版本就是编辑前的版本)
        $oldRevision = $revisionStore->getRevisionByPageId(
            $revision->getPageId(),
            0,
            \MediaWiki\Revision\RevisionRecord::RAW
        );
        if ($oldRevision) {
            $oldContentObj = $oldRevision->getContent(SlotRecord::MAIN, RevisionRecord::RAW);
            if ($oldContentObj) {
                $old_content = $oldContentObj->getNativeData();
            }
        }

        // 这里可以执行新旧内容比对逻辑
        // ...

        return true;
    } 
}

注意事项

  • 如果是首次创建的新页面,不存在历史版本,$oldRevision会返回null,需要提前做判空处理避免抛出错误
  • 如果需要比对非主槽(SlotRecord::MAIN)的自定义槽位内容,替换为对应槽位的标识即可
  • 对内容对象调用方法前建议先做判空,兼容特殊内容模型的异常场景

内容的提问来源于stack exchange,提问作者Presto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 13:45:04