Update hooks to use PageSaveComplete hook in MW 1.35+

Bug: T250566
Change-Id: Ieccc1cae8c4b53a582391066a1a6336eae6c9277
This commit is contained in:
DannyS712 2020-06-25 17:10:43 +00:00
parent 314bf0933f
commit f5ecc23c17
2 changed files with 74 additions and 17 deletions

View file

@ -83,18 +83,17 @@
"GadgetDefinitionDeletionUpdate": "includes/content/GadgetDefinitionDeletionUpdate.php"
},
"Hooks": {
"PageContentSaveComplete": "GadgetHooks::onPageContentSaveComplete",
"BeforePageDisplay": "GadgetHooks::beforePageDisplay",
"CodeEditorGetPageLanguage": "GadgetHooks::onCodeEditorGetPageLanguage",
"ContentHandlerDefaultModelFor": "GadgetHooks::onContentHandlerDefaultModelFor",
"EditFilterMergedContent": "GadgetHooks::onEditFilterMergedContent",
"PageContentInsertComplete": "GadgetHooks::onPageContentInsertComplete",
"UserGetDefaultOptions": "GadgetHooks::userGetDefaultOptions",
"GetPreferences": "GadgetHooks::getPreferences",
"ResourceLoaderRegisterModules": "GadgetHooks::registerModules",
"wgQueryPages": "GadgetHooks::onwgQueryPages",
"DeleteUnknownPreferences": "GadgetHooks::onDeleteUnknownPreferences"
},
"callback": "GadgetHooks::onRegistration",
"config": {
"GadgetsRepoClass": {
"value": "MediaWikiGadgetsDefinitionRepo"

View file

@ -24,18 +24,88 @@ use Wikimedia\Rdbms\IDatabase;
use Wikimedia\WrappedString;
class GadgetHooks {
/**
* Callback on extension registration
*
* Register hooks based on version to keep support for mediawiki versions before 1.35
*/
public static function onRegistration() {
global $wgHooks;
if ( version_compare( MW_VERSION, '1.35', '>=' ) ) {
// Use PageSaveComplete
$wgHooks['PageSaveComplete'][] = 'GadgetHooks::onPageSaveComplete';
} else {
// Use both PageContentInsertComplete and PageContentSaveComplete
$wgHooks['PageContentSaveComplete'][] = 'GadgetHooks::onPageContentSaveComplete';
$wgHooks['PageContentInsertComplete'][] = 'GadgetHooks::onPageContentInsertComplete';
}
}
/**
* PageContentSaveComplete hook handler.
*
* Only run in versions of mediawiki before 1.35; in 1.35+, ::onPageSaveComplete is used
*
* @note Hook provides other parameters, but only the wikipage is needed
* @param WikiPage $wikiPage
* @param User $user
* @param Content $content New page content
*/
public static function onPageContentSaveComplete( WikiPage $wikiPage, $user, $content ) {
public static function onPageContentSaveComplete( WikiPage $wikiPage ) {
// update cache if MediaWiki:Gadgets-definition was edited
GadgetRepo::singleton()->handlePageUpdate( $wikiPage->getTitle() );
}
/**
* After a new page is created in the Gadget definition namespace,
* invalidate the list of gadget ids
*
* Only run in versions of mediawiki before 1.35; in 1.35+, ::onPageSaveComplete is used
*
* @note Hook provides other parameters, but only the wikipage is needed
* @param WikiPage $page
*/
public static function onPageContentInsertComplete( WikiPage $page ) {
if ( $page->getTitle()->inNamespace( NS_GADGET_DEFINITION ) ) {
GadgetRepo::singleton()->handlePageCreation( $page->getTitle() );
}
}
/**
* PageSaveComplete hook handler
*
* Only run in versions of mediawiki begining 1.35; before 1.35, ::onPageContentSaveComplete
* and ::onPageContentInsertComplete are used
*
* @note paramaters include classes not available before 1.35, so for those typehints
* are not used. The variable name reflects the class
*
* @param WikiPage $wikiPage
* @param mixed $userIdentity unused
* @param string $summary
* @param int $flags
* @param mixed $revisionRecord unused
* @param mixed $editResult unused
*/
public static function onPageSaveComplete(
WikiPage $wikiPage,
$userIdentity,
string $summary,
int $flags,
$revisionRecord,
$editResult
) {
$title = $wikiPage->getTitle();
$repo = GadgetRepo::singleton();
if ( $flags & EDIT_NEW ) {
if ( $title->inNamespace( NS_GADGET_DEFINITION ) ) {
$repo->handlePageCreation( $title );
}
}
$repo->handlePageUpdate( $title );
}
/**
* UserGetDefaultOptions hook handler
* @param array &$defaultOptions Array of default preference keys and values
@ -258,18 +328,6 @@ class GadgetHooks {
return true;
}
/**
* After a new page is created in the Gadget definition namespace,
* invalidate the list of gadget ids
*
* @param WikiPage $page
*/
public static function onPageContentInsertComplete( WikiPage $page ) {
if ( $page->getTitle()->inNamespace( NS_GADGET_DEFINITION ) ) {
GadgetRepo::singleton()->handlePageCreation( $page->getTitle() );
}
}
/**
* Mark the Title as having a content model of javascript or css for pages
* in the Gadget namespace based on their file extension