Convert onRevisionFromEditComplete handler to DomainEvents

Bug: T378937
Depends-On: I34c7aca80cfa96306e2c350a5f9bc723713b98c3
Change-Id: I020038da7c02d6c62071cdb3b572a527cee8fbd2
This commit is contained in:
Aaron Schulz 2024-11-14 12:11:31 -08:00 committed by Daniel Kinzler
parent 9cf7c4d30b
commit bfb4ef65b5
3 changed files with 56 additions and 43 deletions

View file

@ -52,10 +52,16 @@
"APIQuerySiteInfoGeneralInfo": "main", "APIQuerySiteInfoGeneralInfo": "main",
"InfoAction": "main", "InfoAction": "main",
"WikiPageDeletionUpdates": "main", "WikiPageDeletionUpdates": "main",
"RevisionFromEditComplete": "main",
"ParserLogLinterData": "main", "ParserLogLinterData": "main",
"RevisionDataUpdates": "main" "RevisionDataUpdates": "main"
}, },
"DomainEventSubscribers": [
{
"events": [ "PageUpdated" ],
"class": "MediaWiki\\Linter\\LintSubscriber",
"services": [ "Linter.TotalsLookup", "Linter.Database" ]
}
],
"APIListModules": { "APIListModules": {
"linterrors": { "linterrors": {
"class": "MediaWiki\\Linter\\ApiQueryLintErrors", "class": "MediaWiki\\Linter\\ApiQueryLintErrors",

View file

@ -35,16 +35,13 @@ use MediaWiki\Linker\LinkRenderer;
use MediaWiki\Logger\LoggerFactory; use MediaWiki\Logger\LoggerFactory;
use MediaWiki\Output\Hook\BeforePageDisplayHook; use MediaWiki\Output\Hook\BeforePageDisplayHook;
use MediaWiki\Output\OutputPage; use MediaWiki\Output\OutputPage;
use MediaWiki\Page\Hook\RevisionFromEditCompleteHook;
use MediaWiki\Page\Hook\WikiPageDeletionUpdatesHook; use MediaWiki\Page\Hook\WikiPageDeletionUpdatesHook;
use MediaWiki\Page\ParserOutputAccess; use MediaWiki\Page\ParserOutputAccess;
use MediaWiki\Page\WikiPageFactory; use MediaWiki\Page\WikiPageFactory;
use MediaWiki\Revision\RenderedRevision; use MediaWiki\Revision\RenderedRevision;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\SpecialPage\SpecialPage; use MediaWiki\SpecialPage\SpecialPage;
use MediaWiki\Storage\Hook\RevisionDataUpdatesHook; use MediaWiki\Storage\Hook\RevisionDataUpdatesHook;
use MediaWiki\Title\Title; use MediaWiki\Title\Title;
use MediaWiki\User\UserIdentity;
use Skin; use Skin;
use WikiPage; use WikiPage;
@ -53,7 +50,6 @@ class Hooks implements
BeforePageDisplayHook, BeforePageDisplayHook,
InfoActionHook, InfoActionHook,
ParserLogLinterDataHook, ParserLogLinterDataHook,
RevisionFromEditCompleteHook,
WikiPageDeletionUpdatesHook, WikiPageDeletionUpdatesHook,
RevisionDataUpdatesHook RevisionDataUpdatesHook
{ {
@ -66,6 +62,11 @@ class Hooks implements
private Database $database; private Database $database;
private bool $parseOnDerivedDataUpdates; private bool $parseOnDerivedDataUpdates;
/**
* This should match Parsoid's PageConfig::hasLintableContentModel()
*/
public const LINTABLE_CONTENT_MODELS = [ CONTENT_MODEL_WIKITEXT, 'proofread-page' ];
/** /**
* @param LinkRenderer $linkRenderer * @param LinkRenderer $linkRenderer
* @param JobQueueGroup $jobQueueGroup * @param JobQueueGroup $jobQueueGroup
@ -150,44 +151,6 @@ class Hooks implements
}, __METHOD__ ); }, __METHOD__ );
} }
/**
* This should match Parsoid's PageConfig::hasLintableContentModel()
*/
public const LINTABLE_CONTENT_MODELS = [ CONTENT_MODEL_WIKITEXT, 'proofread-page' ];
/**
* Hook: RevisionFromEditComplete
*
* Remove entries from the linter table upon page content model change away from wikitext
*
* @param WikiPage $wikiPage
* @param RevisionRecord $newRevisionRecord
* @param bool|int $originalRevId
* @param UserIdentity $user
* @param string[] &$tags
*/
public function onRevisionFromEditComplete(
$wikiPage, $newRevisionRecord, $originalRevId, $user, &$tags
) {
// This is just a stop-gap to deal with callers that aren't complying
// with the advertised hook signature.
if ( !is_array( $tags ) ) {
return;
}
if (
in_array( "mw-blank", $tags ) ||
( in_array( "mw-contentmodelchange", $tags ) &&
!in_array( $wikiPage->getContentModel(), self::LINTABLE_CONTENT_MODELS ) )
) {
$this->totalsLookup->updateStats(
$this->database->setForPage(
$wikiPage->getId(), $wikiPage->getNamespace(), []
)
);
}
}
/** /**
* Hook: APIQuerySiteInfoGeneralInfo * Hook: APIQuerySiteInfoGeneralInfo
* *

View file

@ -0,0 +1,44 @@
<?php
namespace MediaWiki\Linter;
use MediaWiki\DomainEvent\EventSubscriberBase;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Storage\PageUpdatedEvent;
class LintSubscriber extends EventSubscriberBase {
private TotalsLookup $totalsLookup;
private Database $database;
public function __construct( TotalsLookup $totalsLookup, Database $database ) {
$this->totalsLookup = $totalsLookup;
$this->database = $database;
}
/**
* Remove entries from the linter table upon page content model change away from wikitext
*
* @noinspection PhpUnused
* @param PageUpdatedEvent $event
* @return void
*/
public function handlePageUpdatedEventAfterCommit( PageUpdatedEvent $event ) {
$page = $event->getPage();
$tags = $event->getTags();
if (
in_array( "mw-blank", $tags ) ||
(
in_array( "mw-contentmodelchange", $tags ) &&
!in_array(
$event->getNewRevision()->getSlot( SlotRecord::MAIN )->getModel(),
Hooks::LINTABLE_CONTENT_MODELS
)
)
) {
$this->totalsLookup->updateStats(
$this->database->setForPage( $page->getId(), $page->getNamespace(), [] )
);
}
}
}