在 MediaWiki 扩展中编辑前后访问页面内容

Accessing content of a page before and after edit in a MediaWiki extension

我正在尝试创建一个 MediaWiki 扩展,用于比较保存编辑前后的页面内容。我发现 MultiContentSave 钩子是合适的。在里面,我可以使用以下代码获取编辑后的页面内容:

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();

        // ...

        return true;
    } 
}

如何访问编辑前的页面内容?

尝试这样的事情:

$revision = $renderedRevision->getRevision();
$title = $revision->getPageAsLinkTarget(); // this will return a LinkTarget Object, not the actual title.


/*
Get parent revision ID (the original previous page revision).
If there is no parent revision, this returns 0. If the parent revision is undefined or unknown, this returns null. So make sure to check it.
*/
$parent_id = $revision->getParentId();

/*
Load a page revision from a given revision ID number.
Returns null if no such revision can be found else a revision record. So make sure to check!
*/
$previous_revision = RevisionStore::getRevisionById( $parent_id );

//Grab the content from there.
$old_content      = $previous_revision->getContent( Revision::RAW );
$old_content_text = ContentHandler::getContentText( $old_content );