From 0ae317bcc7c70d69af17139dad495be62e6e8795 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Tue, 24 Jul 2018 15:05:22 -0700 Subject: [PATCH] New hook ApiOptions We need it to inform API clients they're changing globally overridden preferences. Bug: T198913 Change-Id: I692a1fb42b73d72814f31bfe9bd400c65f59d83c --- RELEASE-NOTES-1.33 | 1 + docs/hooks.txt | 9 +++++++++ includes/api/ApiOptions.php | 14 +++++++++++--- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/RELEASE-NOTES-1.33 b/RELEASE-NOTES-1.33 index 59443dfda4..00a693829a 100644 --- a/RELEASE-NOTES-1.33 +++ b/RELEASE-NOTES-1.33 @@ -34,6 +34,7 @@ production. * … === Action API changes in 1.33 === +* (T198913) Added 'ApiOptions' hook. * … === Action API internal changes in 1.33 === diff --git a/docs/hooks.txt b/docs/hooks.txt index fd7b3005d7..90b2b05f1e 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -473,6 +473,15 @@ can alter or append to the array. (url), 'width', 'height', 'alt', 'align'. - url: Url for the given title. +'ApiOptions': Called by action=options before applying changes to user +preferences. +$apiModule: Calling ApiOptions object +$user: User object whose preferences are being changed +$changes: Associative array of preference name => value +$resetKinds: Array of strings specifying which options kinds to reset. + See User::resetOptions() and User::getOptionKinds() for possible + values. + 'ApiParseMakeOutputPage': Called when preparing the OutputPage object for ApiParse. This is mainly intended for calling OutputPage::addContentOverride() or OutputPage::addContentOverrideCallback(). diff --git a/includes/api/ApiOptions.php b/includes/api/ApiOptions.php index 3ea827c13a..c4de31f581 100644 --- a/includes/api/ApiOptions.php +++ b/includes/api/ApiOptions.php @@ -52,9 +52,9 @@ class ApiOptions extends ApiBase { $this->dieWithError( [ 'apierror-missingparam', 'optionname' ] ); } - if ( $params['reset'] ) { - $this->resetPreferences( $params['resetkinds'] ); - $changed = true; + $resetKinds = $params['resetkinds']; + if ( !$params['reset'] ) { + $resetKinds = []; } $changes = []; @@ -68,6 +68,14 @@ class ApiOptions extends ApiBase { $newValue = $params['optionvalue'] ?? null; $changes[$params['optionname']] = $newValue; } + + Hooks::run( 'ApiOptions', [ $this, $user, $changes, $resetKinds ] ); + + if ( $resetKinds ) { + $this->resetPreferences( $resetKinds ); + $changed = true; + } + if ( !$changed && !count( $changes ) ) { $this->dieWithError( 'apierror-nochanges' ); } -- 2.20.1