Manual:Hooks/PageContentSave

From Linux Web Expert

Revision as of 22:06, 3 July 2023 by imported>RobinHood70 (→‎Details: Spelling)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

PageContentSave
<translate> Available from <tvar name=1><translate> version <tvar </tvar></translate>
Before an article is saved.
<translate> Define function:</translate>
public static function onPageContentSave( $wikiPage, $user, $content, &$summary,
$isMinor, $isWatch, $section, $flags, $status ) { ... }
<translate> Attach hook:</translate> <translate> In <tvar name=1>extension.json</tvar>:</translate>
{
	"Hooks": {
		"PageContentSave": "MediaWiki\\Extension\\MyExtension\\Hooks::onPageContentSave"
	}
}
<translate> Called from:</translate> <translate> File(s):</translate> Storage/PageUpdater.php
<translate> Interface:</translate> PageContentSaveHook.php

<translate> For more information about attaching hooks, see <tvar name=1>Manual:Hooks </tvar>.</translate>
<translate> For examples of extensions using this hook, see <tvar name=cat>Category:PageContentSave extensions</tvar>.</translate>


Details

  • $wikiPage: the WikiPage (object) being saved
  • $user: the User (object) saving the article
  • $content: the new article content, as a Content object
  • &$summary: CommentStoreComment object containing the edit comment. Can be replaced with a new one.
  • $isMinor: Boolean flag specifying if the edit was marked as minor.
  • $isWatch: Previously a watch flag. Currently unused, always null.
  • $section: Previously the section number being edited. Currently unused, always null.
  • $flags: All EDIT_… flags (including EDIT_MINOR) as an integer number. See WikiPage::doEditContent documentation for flags' definition.
  • $status: StatusValue object for the hook handlers resulting status. Either set $hookStatus->fatal() or return false to abort the save action.
  • Return value: Return false to abort saving the page.

Earlier versions of MediaWiki (somewhat unintentionally) allowed changing parameters other than $summary. Since 1.32 that does not work anymore; there is currently no exact replacement. If you own the content type, you can use a pre-save transform to change the content.

Examples

# Return false to cancel a save and use $status to provide an error message.
$wgHooks['PageContentSave'][] = function( $wikiPage, $user, $content, $summary, $isMinor, $isWatch, $section, $flags, $status) {
	if ( true ) {
		$status->fatal( new RawMessage( "Your Error Message Here!" ) );
	}
	return false;
}

See also