New hook ApiOptions
authorMax Semenik <maxsem.wiki@gmail.com>
Tue, 24 Jul 2018 22:05:22 +0000 (15:05 -0700)
committerMax Semenik <maxsem.wiki@gmail.com>
Thu, 18 Oct 2018 01:02:20 +0000 (18:02 -0700)
We need it to inform API clients they're changing globally overridden
preferences.

Bug: T198913
Change-Id: I692a1fb42b73d72814f31bfe9bd400c65f59d83c

RELEASE-NOTES-1.33
docs/hooks.txt
includes/api/ApiOptions.php

index 59443df..00a6938 100644 (file)
@@ -34,6 +34,7 @@ production.
 * …
 
 === Action API changes in 1.33 ===
+* (T198913) Added 'ApiOptions' hook.
 * …
 
 === Action API internal changes in 1.33 ===
index fd7b300..90b2b05 100644 (file)
@@ -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().
index 3ea827c..c4de31f 100644 (file)
@@ -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' );
                }