Add DeleteUnknownPreferences hook
authorSam Wilson <sam@samwilson.id.au>
Tue, 3 Apr 2018 03:56:18 +0000 (11:56 +0800)
committerNikerabbit <niklas.laxstrom@gmail.com>
Tue, 3 Apr 2018 06:56:18 +0000 (06:56 +0000)
This makes it possible for extensions to add to the WHERE clause
with which unknown preferences are deleted.

Bug: T188966
Change-Id: Ifb22ca42207956f2b07333da026ff0b215a83930

docs/hooks.txt
maintenance/cleanupPreferences.php

index 4e8474b..f35d610 100644 (file)
@@ -1209,6 +1209,14 @@ $row: the DB row for this line
   Currently only data attributes reserved to MediaWiki are allowed
   (see Sanitizer::isReservedDataAttribute).
 
+'DeleteUnknownPreferences': Called by the cleanupPreferences.php maintenance script to build a WHERE clause with which
+to delete preferences that are not known about. This hook is used by extensions that have dynamically-named preferences
+that should not be deleted in the usual cleanup process. For example, the Gadgets extension creates preferences prefixed
+with 'gadget-', and so anything with that prefix is excluded from the deletion.
+&where: An array that will be passed as the $cond parameter to IDatabase::select() to determine what will be deleted
+  from the user_properties table.
+$db: The IDatabase object, useful for accessing $db->buildLike() etc.
+
 'DifferenceEngineAfterLoadNewText': called in DifferenceEngine::loadNewText()
 after the new revision's content has been loaded into the class member variable
 $differenceEngine->mNewContent but before returning true from this function.
index a21bc73..33cc0ca 100644 (file)
@@ -87,15 +87,14 @@ class CleanupPreferences extends Maintenance {
                // Remove unknown preferences. Special-case gadget- and userjs- as we can't
                // control those names.
                if ( $unknown ) {
-                       $this->deleteByWhere(
-                               $dbw,
-                               'Dropping unknown preferences',
-                               [
-                                       'up_property NOT' . $dbw->buildLike( 'gadget-', $dbw->anyString() ),
-                                       'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
-                                       'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
-                               ]
-                       );
+                       $where = [
+                               'up_property NOT' . $dbw->buildLike( 'gadget-', $dbw->anyString() ),
+                               'up_property NOT' . $dbw->buildLike( 'userjs-', $dbw->anyString() ),
+                               'up_property NOT IN (' . $dbw->makeList( array_keys( $wgDefaultUserOptions ) ) . ')',
+                       ];
+                       // Allow extensions to add to the where clause to prevent deletion of their own prefs.
+                       Hooks::run( 'DeleteUnknownPreferences', [ &$where, $dbw ] );
+                       $this->deleteByWhere( $dbw, 'Dropping unknown preferences', $where );
                }
 
                // Something something phase 3