mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TemplateData
synced 2024-11-23 23:43:54 +00:00
Merge "Replace deprecated onPageContentSave hook"
This commit is contained in:
commit
4fe8f81dac
|
@ -29,7 +29,7 @@
|
|||
"Hooks": {
|
||||
"EditPage::showEditForm:fields": "TemplateDataHooks::onEditPageShowEditFormFields",
|
||||
"ParserFirstCallInit": "TemplateDataHooks::onParserFirstCallInit",
|
||||
"PageContentSave": "TemplateDataHooks::onPageContentSave",
|
||||
"MultiContentSave": "TemplateDataHooks::onMultiContentSave",
|
||||
"ResourceLoaderRegisterModules": "TemplateDataHooks::onResourceLoaderRegisterModules",
|
||||
"EditPage::showEditForm:initial": "TemplateDataHooks::onEditPage",
|
||||
"ParserFetchTemplateData": "TemplateDataHooks::onParserFetchTemplateData",
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
<?php
|
||||
|
||||
use MediaWiki\MediaWikiServices;
|
||||
use MediaWiki\Revision\RenderedRevision;
|
||||
use MediaWiki\Revision\RevisionRecord;
|
||||
use MediaWiki\Revision\SlotRecord;
|
||||
use MediaWiki\User\UserIdentity;
|
||||
|
||||
/**
|
||||
* Hooks for TemplateData extension
|
||||
|
@ -56,66 +60,56 @@ class TemplateDataHooks {
|
|||
}
|
||||
|
||||
/**
|
||||
* @param WikiPage &$page
|
||||
* @param User &$user
|
||||
* @param Content &$content
|
||||
* @param string &$summary
|
||||
* @param bool $minor
|
||||
* @param bool|null $watchthis
|
||||
* @param string $sectionanchor
|
||||
* @param int &$flags
|
||||
* @param Status &$status
|
||||
* @param RenderedRevision $renderedRevision
|
||||
* @param UserIdentity $user
|
||||
* @param CommentStoreComment $summary
|
||||
* @param int $flags
|
||||
* @param Status $hookStatus
|
||||
* @return bool
|
||||
*/
|
||||
public static function onPageContentSave( WikiPage &$page, &$user, &$content, &$summary, $minor,
|
||||
$watchthis, $sectionanchor, &$flags, &$status
|
||||
public static function onMultiContentSave(
|
||||
RenderedRevision $renderedRevision, UserIdentity $user, CommentStoreComment $summary, $flags, Status $hookStatus
|
||||
) {
|
||||
if ( $page->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
|
||||
$revisionRecord = $renderedRevision->getRevision();
|
||||
$contentModel = $revisionRecord
|
||||
->getContent( SlotRecord::MAIN )
|
||||
->getModel();
|
||||
|
||||
if ( $contentModel !== CONTENT_MODEL_WIKITEXT ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// The PageContentSave hook provides raw $text, but not $parser because at this stage
|
||||
// the page is not actually parsed yet. Which means we can't know whether self::render()
|
||||
// got a valid tag or not. Looking at $text directly is not a solution either as
|
||||
// it may not be in the current page (it can be transcluded).
|
||||
// Since there is no later hook that allows aborting the save and showing an error,
|
||||
// we will have to trigger the parser ourselves.
|
||||
// Fortunately this causes no overhead since the below (copied from WikiPage::doEditContent,
|
||||
// right after this hook is ran) has guards that lazy-init and return early if called again
|
||||
// later by the real WikiPage.
|
||||
|
||||
// Specify format the same way the API and EditPage do to avoid extra parsing
|
||||
$format = $content->getContentHandler()->getDefaultFormat();
|
||||
$editInfo = $page->prepareContentForEdit( $content, null, $user, $format );
|
||||
$parserOutput = $editInfo->getOutput();
|
||||
|
||||
// Revision hasn't been parsed yet, so parse to know if self::render got a
|
||||
// valid tag (via inclusion and transclusion) and abort save if it didn't
|
||||
$parserOutput = $renderedRevision->getRevisionParserOutput();
|
||||
$templateDataStatus = self::getStatusFromParserOutput( $parserOutput );
|
||||
if ( $templateDataStatus instanceof Status && !$templateDataStatus->isOK() ) {
|
||||
// Abort edit, show error message from TemplateDataBlob::getStatus
|
||||
$status->merge( $templateDataStatus );
|
||||
$hookStatus->merge( $templateDataStatus );
|
||||
return false;
|
||||
}
|
||||
|
||||
// TODO: Remove when not needed any more, see T267926
|
||||
self::logChangeEvent( $page, $parserOutput->getProperty( 'templatedata' ), $user );
|
||||
self::logChangeEvent( $revisionRecord, $parserOutput->getProperty( 'templatedata' ), $user );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param WikiPage $page
|
||||
* @param RevisionRecord $revisionRecord
|
||||
* @param string|false $newPageProperty
|
||||
* @param User $user
|
||||
* @param UserIdentity $user
|
||||
*/
|
||||
private static function logChangeEvent( WikiPage $page, $newPageProperty, User $user ) {
|
||||
private static function logChangeEvent( RevisionRecord $revisionRecord, $newPageProperty, UserIdentity $user ) {
|
||||
if ( !ExtensionRegistry::getInstance()->isLoaded( 'EventLogging' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
$services = MediaWikiServices::getInstance();
|
||||
$title = $page->getTitle();
|
||||
$page = $revisionRecord->getPage();
|
||||
$props = $services->getPageProps()->getProperties( $page, 'templatedata' );
|
||||
|
||||
$pageId = $page->getId();
|
||||
$props = $services->getPageProps()->getProperties( $title, 'templatedata' );
|
||||
// The JSON strings here are guaranteed to be normalized (and possibly compressed) the same
|
||||
// way. No need to normalize them again for this comparison.
|
||||
if ( $newPageProperty === ( $props[$pageId] ?? false ) ) {
|
||||
|
@ -123,8 +117,7 @@ class TemplateDataHooks {
|
|||
}
|
||||
|
||||
$generatorUsed = RequestContext::getMain()->getRequest()->getBool( 'TemplateDataGeneratorUsed' );
|
||||
$revision = $page->getRevisionRecord();
|
||||
|
||||
$userEditCount = MediaWikiServices::getInstance()->getUserEditTracker()->getUserEditCount( $user );
|
||||
// Note: We know that irrelevant changes (e.g. whitespace changes) aren't logged here
|
||||
EventLogging::logEvent(
|
||||
'TemplateDataEditor',
|
||||
|
@ -134,10 +127,10 @@ class TemplateDataHooks {
|
|||
// very likely (but not guaranteed) the generator was used to make the changes
|
||||
'action' => $generatorUsed ? 'save-tag-edit-generator-used' : 'save-tag-edit-no-generator',
|
||||
'page_id' => $pageId,
|
||||
'page_namespace' => $title->getNamespace(),
|
||||
'page_title' => $title->getText(),
|
||||
'rev_id' => $revision ? $revision->getId() : 0,
|
||||
'user_edit_count' => $user->getEditCount() ?? 0,
|
||||
'page_namespace' => $page->getNamespace(),
|
||||
'page_title' => $page->getDBkey(),
|
||||
'rev_id' => $revisionRecord->getId() ?? 0,
|
||||
'user_edit_count' => $userEditCount ?? 0,
|
||||
'user_id' => $user->getId(),
|
||||
]
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue