phpmediawikimediawiki-extensions

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


I'm trying to create a MediaWiki extension that would compare content of a page before and after saving an edit. I figured out MultiContentSave hook would be the proper one to use. Inside I can get edited page content using following code:

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;
    } 
}

How can I access content of the page before the edit?


Solution

  • Try something like this :

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