Use MultiContentSave hook

This commit is contained in:
paladox
2021-03-20 21:54:50 +00:00
committed by GitHub
parent a95a413302
commit 1b097dddf3

View File

@ -21,14 +21,49 @@
* *
* @author Daniel Kraus <bovender@bovender.de> * @author Daniel Kraus <bovender@bovender.de>
*/ */
namespace LinkTitles; namespace LinkTitles;
use CommentStoreComment;
use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\SlotRecord;
use Status;
use WikiPage;
use User;
/** /**
* Provides event handlers and entry points for the extension. * Provides event handlers and entry points for the extension.
*/ */
class Extension { class Extension {
const URL = 'https://github.com/bovender/LinkTitles'; const URL = 'https://github.com/bovender/LinkTitles';
/**
* Event handler for the MultiContentSave hook.
*
* This handler is used if the parseOnEdit configuration option is set.
*/
public static function onMultiContentSave( RenderedRevision $renderedRevision, User $user, CommentStoreComment $summary, $flags, Status $hookStatus ) {
$config = new Config();
if ( !$config->parseOnEdit ) return true;
$title = $renderedRevision->getRevision()->getPageAsLinkTarget();
$slots = $renderedRevision->getRevision()->getSlots();
$content = $renderedRevision->getRevision()->getSlots()->getContent( SlotRecord::MAIN );
$articleID = $renderedRevision->getRevision()->getPageId();
$wikiPage = WikiPage::newFromID( $articleID );
$source = Source::createFromPageandContent( $wikiPage, $content, $config );
$linker = new Linker( $config );
$result = $linker->linkContent( $source );
if ( $result ) {
$source->setText( $result );
$text = $source->getText($result);
$slots = $renderedRevision->getRevision()->getSlots();
$slots->setContent( 'main', \ContentHandler::makeContent( $text, $title ) );
}
return true;
}
/** /**
* Event handler for the PageContentSave hook. * Event handler for the PageContentSave hook.
* *