Mattflaschen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/342979 )

Change subject: Prevents duplicate filter names
......................................................................

Prevents duplicate filter names

Explicitly block two filters in the same group from having the same
name.

Before, it would be left to registerFilter, which would just cause
the second one to win.

Also, avoid a getFilter warning when the filter does not exist.

Finally, a minor related doc fix.

Change-Id: I6b3880a5c7cc381c169bbd969cd4814559b49c91
---
M docs/hooks.txt
M includes/changes/ChangesListFilter.php
M includes/changes/ChangesListFilterGroup.php
M tests/phpunit/includes/changes/ChangesListFilterGroupTest.php
M tests/phpunit/includes/changes/ChangesListFilterTest.php
5 files changed, 58 insertions(+), 7 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/79/342979/1

diff --git a/docs/hooks.txt b/docs/hooks.txt
index 149ee4b..1be9a03 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -1015,10 +1015,11 @@
 'ChangesListSpecialPageStructuredFilters': Called to allow extensions to 
register
 filters for pages inheriting from ChangesListSpecialPage (in core: 
RecentChanges,
 RecentChangesLinked, and Watchlist).  Generally, you will want to construct
-new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects.  You 
can
-then either add them to existing ChangesListFilterGroup objects (accessed 
through
-$special->getFilterGroup), or create your own.  If you create new groups, you
-must register them with $special->registerFilterGroup.
+new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects.
+
+When constructing them, you specify which group they belong to.  You can reuse
+existing groups (accessed through $special->getFilterGroup), or create your 
own.
+If you create new groups, you must register them with 
$special->registerFilterGroup.
 $special: ChangesListSpecialPage instance
 
 'ChangeTagAfterDelete': Called after a change tag has been deleted (that is,
diff --git a/includes/changes/ChangesListFilter.php 
b/includes/changes/ChangesListFilter.php
index cd74154..847bb95 100644
--- a/includes/changes/ChangesListFilter.php
+++ b/includes/changes/ChangesListFilter.php
@@ -113,7 +113,8 @@
        const RESERVED_NAME_CHAR = '_';
 
        /**
-        * Create a new filter with the specified configuration.
+        * Creates a new filter with the specified configuration, and registers 
it to the
+        * specified group.
         *
         * It infers which UI (it can be either or both) to display the filter 
on based on
         * which messages are provided.
@@ -161,6 +162,11 @@
                        );
                }
 
+               if ( $this->group->getFilter( $filterDefinition['name'] ) ) {
+                       throw new MWException( 'Two filters in a group can not 
have the ' .
+                               "same name: '{$filterDefinition['name']}'" );
+               }
+
                $this->name = $filterDefinition['name'];
 
                if ( isset( $filterDefinition['cssClassSuffix'] ) ) {
diff --git a/includes/changes/ChangesListFilterGroup.php 
b/includes/changes/ChangesListFilterGroup.php
index 30ab552..d2ad204 100644
--- a/includes/changes/ChangesListFilterGroup.php
+++ b/includes/changes/ChangesListFilterGroup.php
@@ -315,10 +315,10 @@
         * Get filter by name
         *
         * @param string $name Filter name
-        * @return ChangesListFilter Specified filter
+        * @return ChangesListFilter|null Specified filter, or null if it is 
not registered
         */
        public function getFilter( $name ) {
-               return $this->filters[$name];
+               return isset( $this->filters[$name] ) ? $this->filters[$name] : 
null;
        }
 
        /**
diff --git a/tests/phpunit/includes/changes/ChangesListFilterGroupTest.php 
b/tests/phpunit/includes/changes/ChangesListFilterGroupTest.php
index f712a2f..220c00d9 100644
--- a/tests/phpunit/includes/changes/ChangesListFilterGroupTest.php
+++ b/tests/phpunit/includes/changes/ChangesListFilterGroupTest.php
@@ -51,4 +51,24 @@
                        )
                );
        }
+
+       // Get without warnings
+       public function testGetFilter() {
+               $group = new MockChangesListFilterGroup(
+                       [
+                               'type' => 'some_type',
+                               'name' => 'groupName',
+                               'isFullCoverage' => true,
+                               'priority' => 1,
+                               'filters' => [
+                                       [ 'name' => 'foo' ],
+                               ],
+                       ]
+               );
+
+               $this->assertEquals(
+                       'foo',
+                       $group->getFilter( 'foo' )->getName()
+               );
+       }
 }
diff --git a/tests/phpunit/includes/changes/ChangesListFilterTest.php 
b/tests/phpunit/includes/changes/ChangesListFilterTest.php
index c212560..202fb5d 100644
--- a/tests/phpunit/includes/changes/ChangesListFilterTest.php
+++ b/tests/phpunit/includes/changes/ChangesListFilterTest.php
@@ -40,6 +40,30 @@
                );
        }
 
+       // @codingStandardsIgnoreStart
+       /**
+        * @expectedException MWException
+        * @expectedExceptionMessage Two filters in a group can not have the 
same name: 'somename'
+        */
+       // @codingStandardsIgnoreEnd
+       public function testDuplicateName() {
+               new MockChangesListFilter(
+                       [
+                               'group' => $this->group,
+                               'name' => 'somename',
+                               'priority' => 1,
+                       ]
+               );
+
+               new MockChangesListFilter(
+                       [
+                               'group' => $this->group,
+                               'name' => 'somename',
+                               'priority' => 2,
+                       ]
+               );
+       }
+
        /**
         * @expectedException MWException
         * @expectedExceptionMessage Supersets can only be defined for filters 
in the same group

-- 
To view, visit https://gerrit.wikimedia.org/r/342979
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6b3880a5c7cc381c169bbd969cd4814559b49c91
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Mattflaschen <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to