Add SpecialCreateAccountsBenefits hook

Add a new hook for modifying the hero message on the signup page.

Bug: T325655
Change-Id: Ifea5cd0674f71e267a931a64a48c8ea5d052b334
This commit is contained in:
Gergő Tisza 2023-01-29 16:16:54 -08:00
parent bf6b5751db
commit 4d7f46af95
No known key found for this signature in database
GPG key ID: C34FEC97E6257F96
5 changed files with 68 additions and 2 deletions

View file

@ -85,6 +85,8 @@ For notes on 1.39.x and older releases, see HISTORY.
run on PHP 8.1 by default, up from PHP 7.4 now that that's EOL.
* Maintenance scripts can (and should) now be executed using
maintenance/run.php.
* Added 'SpecialCreateAccountBenefits' hook, to customize the signup page
content about the benefits of registering an account.
* …
=== External library changes in 1.40 ===

View file

@ -1334,6 +1334,7 @@ $wgAutoloadLocalClasses = [
'MediaWiki\\Hook\\SpecialContributionsBeforeMainOutputHook' => __DIR__ . '/includes/specials/Hook/SpecialContributionsBeforeMainOutputHook.php',
'MediaWiki\\Hook\\SpecialContributions__formatRow__flagsHook' => __DIR__ . '/includes/specials/Hook/SpecialContributions__formatRow__flagsHook.php',
'MediaWiki\\Hook\\SpecialContributions__getForm__filtersHook' => __DIR__ . '/includes/specials/Hook/SpecialContributions__getForm__filtersHook.php',
'MediaWiki\\Hook\\SpecialCreateAccountBenefitsHook' => __DIR__ . '/includes/specials/Hook/SpecialCreateAccountBenefitsHook.php',
'MediaWiki\\Hook\\SpecialExportGetExtraPagesHook' => __DIR__ . '/includes/specials/Hook/SpecialExportGetExtraPagesHook.php',
'MediaWiki\\Hook\\SpecialListusersDefaultQueryHook' => __DIR__ . '/includes/specials/Hook/SpecialListusersDefaultQueryHook.php',
'MediaWiki\\Hook\\SpecialListusersFormatRowHook' => __DIR__ . '/includes/specials/Hook/SpecialListusersFormatRowHook.php',

View file

@ -337,6 +337,7 @@ class HookRunner implements
\MediaWiki\Hook\SpecialBlockModifyFormFieldsHook,
\MediaWiki\Hook\SpecialContributionsBeforeMainOutputHook,
\MediaWiki\Hook\SpecialContributions__formatRow__flagsHook,
\MediaWiki\Hook\SpecialCreateAccountBenefitsHook,
\MediaWiki\Hook\SpecialExportGetExtraPagesHook,
\MediaWiki\Hook\SpecialContributions__getForm__filtersHook,
\MediaWiki\Hook\SpecialListusersDefaultQueryHook,
@ -3560,6 +3561,13 @@ class HookRunner implements
);
}
public function onSpecialCreateAccountBenefits( ?string &$html, array $info, array &$options ) {
return $this->container->run(
'SpecialCreateAccountBenefits',
[ &$html, $info, &$options ]
);
}
public function onSpecialExportGetExtraPages( $inputPages, &$extraPages ) {
return $this->container->run(
'SpecialExportGetExtraPages',

View file

@ -603,12 +603,33 @@ abstract class LoginSignupSpecialPage extends AuthManagerSpecialPage {
);
}
$formBlock = Html::rawElement( 'div', [ 'id' => 'userloginForm' ], $formHtml );
$formAndBenefits = $formBlock;
if ( $this->isSignup() && $this->showExtraInformation() ) {
$benefitsContainerHtml = null;
$info = [
'context' => $this->getContext(),
'form' => $this->authForm,
];
$options = [
'beforeForm' => false,
];
$this->getHookRunner()->onSpecialCreateAccountBenefits(
$benefitsContainerHtml, $info, $options
);
if ( $benefitsContainerHtml === null ) {
$benefitsContainerHtml = $this->getBenefitsContainerHtml();
}
$formAndBenefits = $options['beforeForm']
? ( $benefitsContainerHtml . $formBlock )
: ( $formBlock . $benefitsContainerHtml );
}
return Html::rawElement( 'div', [ 'class' => 'mw-ui-container' ],
$loginPrompt
. $languageLinks
. $signupStart
. Html::rawElement( 'div', [ 'id' => 'userloginForm' ], $formHtml )
. $this->getBenefitsContainerHtml()
. $formAndBenefits
);
}

View file

@ -0,0 +1,34 @@
<?php
namespace MediaWiki\Hook;
/**
* This is a hook handler interface, see docs/Hooks.md.
* Use the hook name "SpecialCreateAccountBenefits" to register handlers implementing this interface.
*
* @stable to implement
* @ingroup Hooks
*/
interface SpecialCreateAccountBenefitsHook {
/**
* Replace the default signup page content about the benefits of registering an account
* ("Wikipedia is made by people like you...") on Special:CreateAccount.
*
* @param string|null &$html HTML to use instead of the default .mw-createacct-benefits-container
* block. By default, this is null, which means the default content will be used.
* @param array $info Array of information:
* - context: (IContextSource) Context object.
* - form: (HTMLForm) The signup form. Read-only - the form HTML has already been generated.
* @phan-param array{context:\IContextSource,form:\HTMLForm} $info
* @param array &$options Array of modifiable options:
* - beforeForm: (bool, default false) Whether to insert the HTML before the form. This is
* mainly useful on mobile (where the login form might push the benefits out of view; but
* also, a long benefits block might push the form out of view).
* @phan-param array{beforeForm:bool} &$options
* @return bool|void True or no return value to continue or false to abort.
* @since 1.40
*/
public function onSpecialCreateAccountBenefits( ?string &$html, array $info, array &$options );
}