jenkins-bot has submitted this change and it was merged.

Change subject: Fix some Special:Random bugs
......................................................................


Fix some Special:Random bugs

1.  If a user asked for a random file in a namespace (Special:Random/File
for example), then we'd use the namespace to pick an index but not as a
filter.  This caused searches in the main namespace to work ok but not
those in, say, File.  They'd get results from User_talk and other crazy
stuff.
2.  Searches in the File: namespace were picking up results from commons.
While we want searches to pick up results from commons we probably don't
want Special:Random to do it.

Bug: 66643

Change-Id: I4a8e6eb0979584866ff3b9aa3aeddf646544c779
---
M includes/Hooks.php
M includes/Searcher.php
A tests/browser/features/special_random.feature
M tests/browser/features/step_definitions/page_steps.rb
M tests/browser/features/step_definitions/search_steps.rb
M tests/browser/features/support/hooks.rb
6 files changed, 65 insertions(+), 3 deletions(-)

Approvals:
  Chad: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/Hooks.php b/includes/Hooks.php
index e6c0760..19a3e8f 100644
--- a/includes/Hooks.php
+++ b/includes/Hooks.php
@@ -404,6 +404,7 @@
 
                        $searcher = new Searcher( 0, 1, $namespaces,
                                RequestContext::getMain()->getUser() );
+                       $searcher->limitSearchToLocalWiki( true );
                        $randSearch = $searcher->randomSearch( $seed );
                        if ( $randSearch->isOk() ) {
                                $results = $randSearch->getValue();
diff --git a/includes/Searcher.php b/includes/Searcher.php
index 5cf9167..7b723b0 100644
--- a/includes/Searcher.php
+++ b/includes/Searcher.php
@@ -152,6 +152,11 @@
        private $escaper;
 
        /**
+        * @var boolean limit the search to the local wiki.  Defaults to false.
+        */
+       private $limitSearchToLocalWiki = false;
+
+       /**
         * Constructor
         * @param int $offset Offset the results by this much
         * @param int $limit Limit the results to this many
@@ -184,6 +189,14 @@
         */
        public function setSort( $sort ) {
                $this->sort = $sort;
+       }
+
+       /**
+        * Should this search limit results to the local wiki?  If not called 
the default is false.
+        * @param boolean $limitSearchToLocalWiki should the results be limited?
+        */
+       public function limitSearchToLocalWiki( $limitSearchToLocalWiki ) {
+               $this->limitSearchToLocalWiki = $limitSearchToLocalWiki;
        }
 
        /**
@@ -814,8 +827,10 @@
                        ) ) );
                        break;
                case 'random':
+                       // Random scoring is funky - you have to wrap the query 
in a FunctionScore query.
                        $funcScore = new \Elastica\Query\FunctionScore();
                        $funcScore->setRandomScore( $for );
+                       $funcScore->setQuery( $this->query );
                        $query->setQuery( $funcScore );
                        break;
                default:
@@ -1015,6 +1030,9 @@
         * @return array(string)
         */
        protected function getAndFilterExtraIndexes() {
+               if ( $this->limitSearchToLocalWiki ) {
+                       return array();
+               }
                $extraIndexes = OtherIndexes::getExtraIndexesForNamespaces( 
$this->namespaces );
                if ( $extraIndexes ) {
                        $this->notFilters[] = new \Elastica\Filter\Term(
diff --git a/tests/browser/features/special_random.feature 
b/tests/browser/features/special_random.feature
new file mode 100644
index 0000000..9f63ebf
--- /dev/null
+++ b/tests/browser/features/special_random.feature
@@ -0,0 +1,23 @@
+@clean @phantomjs @special_random
+Feature: Cirrus powered Special:Random
+  Scenario: Special:Random gives a page in the main namespace by default
+    When I am at a random page
+    Then I am on a page in the main namespace
+
+  # Repeats test three times because failure is, well, random
+  Scenario: Special:Random/User gives a page in the user namespace
+    When I am at a random User page
+    Then I am on a page in the User namespace
+    When I am at a random User page
+    Then I am on a page in the User namespace
+    When I am at a random User page
+    Then I am on a page in the User namespace
+
+  # Repeats test three times because failure is, well, random
+  Scenario: Special:Random/User_talk gives a page in the user namespace
+    When I am at a random User_talk page
+    Then I am on a page in the User talk namespace
+    When I am at a random User_talk page
+    Then I am on a page in the User talk namespace
+    When I am at a random User_talk page
+    Then I am on a page in the User talk namespace
diff --git a/tests/browser/features/step_definitions/page_steps.rb 
b/tests/browser/features/step_definitions/page_steps.rb
index 9e38783..3d15915 100644
--- a/tests/browser/features/step_definitions/page_steps.rb
+++ b/tests/browser/features/step_definitions/page_steps.rb
@@ -24,6 +24,9 @@
     result.body.should_not include '{"error":{"code"'
   end
 end
+When(/^I am at a random (.+) page$/) do |namespace|
+  visit(ArticlePage, using_params: {page_name: "Special:Random/#{namespace}"})
+end
 When(/^I edit (.+) to add (.+)$/) do |title, text|
   edit_page(title, text, true)
 end
@@ -56,6 +59,16 @@
 Then(/^there is a software version row for (.+)$/) do |name|
   on(SpecialVersion).software_table_row(name).exists?
 end
+Then(/^I am on a page titled (.*)$/) do |title|
+  on(ArticlePage).title.should == title
+end
+Then(/^I am on a page in the (.*) namespace$/) do |namespace|
+  if namespace == 'main' then
+    on(ArticlePage).title.index(":").should == nil
+  else
+    on(ArticlePage).title.index("#{namespace}:").should_not == nil
+  end
+end
 
 def edit_page(title, text, add)
   if text.start_with?("@")
diff --git a/tests/browser/features/step_definitions/search_steps.rb 
b/tests/browser/features/step_definitions/search_steps.rb
index ccca030..c03ab2d 100644
--- a/tests/browser/features/step_definitions/search_steps.rb
+++ b/tests/browser/features/step_definitions/search_steps.rb
@@ -100,9 +100,6 @@
 Then(/^I should be offered to search for (.+)$/) do |term|
   on(SearchPage).search_special.should == "containing...\n" + term
 end
-Then(/^I am on a page titled (.*)$/) do |title|
-  on(ArticlePage).title.should == title
-end
 Then(/^there is a search result$/) do
   on(SearchResultsPage).first_result_element.should exist
 end
diff --git a/tests/browser/features/support/hooks.rb 
b/tests/browser/features/support/hooks.rb
index 059ef7e..147f3b6 100644
--- a/tests/browser/features/support/hooks.rb
+++ b/tests/browser/features/support/hooks.rb
@@ -435,3 +435,13 @@
   end
   $js_and_css = true
 end
+
+Before("@special_random") do
+  if !$special_random
+    steps %Q{
+      Given a page named User:Random Test exists
+      And a page named User_talk:Random Test exists
+    }
+  end
+  $special_random = true
+end

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4a8e6eb0979584866ff3b9aa3aeddf646544c779
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/CirrusSearch
Gerrit-Branch: master
Gerrit-Owner: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: Chad <ch...@wikimedia.org>
Gerrit-Reviewer: Manybubbles <never...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to