Brion VIBBER has submitted this change and it was merged.

Change subject: Re-added Wikidata descriptions to search results.
......................................................................


Re-added Wikidata descriptions to search results.

No longer needs separate query to get them! Also now use
index property for ordering.

Note: Will need to route these descriptions to the article
record as soon as dust settles a bit more on the data layer
refactor. Although, just doing a search doesn't create an
article record, so we may not add descriptions to article
records until an article is actually loaded.

Change-Id: I5ebd0be0e2cd1d02f692818a9258fed4762d5f86
---
M wikipedia/Networking/Fetchers/SearchResultFetcher.m
M wikipedia/View Controllers/SearchResults/SearchResultAttributedString.m
M wikipedia/View Controllers/SearchResults/SearchResultsController.m
3 files changed, 43 insertions(+), 124 deletions(-)

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



diff --git a/wikipedia/Networking/Fetchers/SearchResultFetcher.m 
b/wikipedia/Networking/Fetchers/SearchResultFetcher.m
index 2e8ef6f..eb1f4c8 100644
--- a/wikipedia/Networking/Fetchers/SearchResultFetcher.m
+++ b/wikipedia/Networking/Fetchers/SearchResultFetcher.m
@@ -12,8 +12,6 @@
 #import "ArticleCoreDataObjects.h"
 #import "NSManagedObjectContext+SimpleFetch.h"
 
-#define GET_SNIPPET_WITH_IN_ARTICLE_RESULTS YES
-
 @interface SearchResultFetcher()
 
 @property (strong, nonatomic) NSString *domain;
@@ -128,15 +126,11 @@
                      @"gpssearch": self.searchTerm,
                      @"gpsnamespace": @0,
                      @"gpslimit": @(SEARCH_MAX_RESULTS),
-                     @"prop": @"pageprops|pageimages",
-                     @"ppprop": @"wikibase_item",
+                     @"prop": @"pageterms|pageimages",
                      @"piprop": @"thumbnail",
+                     @"wbptterms": @"description",
                      @"pithumbsize" : @(SEARCH_THUMBNAIL_WIDTH),
                      @"pilimit": @(SEARCH_MAX_RESULTS),
-                     @"list": @"prefixsearch",
-                     @"pssearch": self.searchTerm,
-                     @"pslimit": @(SEARCH_MAX_RESULTS),
-                     @"psnamespace": @0,
                      @"format": @"json"
                      };
             break;
@@ -144,8 +138,8 @@
         case SEARCH_TYPE_IN_ARTICLES:
             return @{
                      @"action": @"query",
-                     @"prop": @"pageprops|pageimages",
-                     @"ppprop": @"wikibase_item",
+                     @"prop": @"pageterms|pageimages",
+                     @"wbptterms": @"description",
                      @"generator": @"search",
                      @"gsrsearch": self.searchTerm,
                      @"gsrnamespace": @0,
@@ -154,14 +148,6 @@
                      @"gsrprop": @"redirecttitle",
                      @"gsroffset": @0,
                      @"gsrlimit": @(SEARCH_MAX_RESULTS),
-                     @"list": @"search",
-                     @"srsearch": self.searchTerm,
-                     @"srnamespace": @0,
-                     @"srwhat": @"text",
-                     @"srinfo": @"suggestion",
-                     @"srprop": (GET_SNIPPET_WITH_IN_ARTICLE_RESULTS ? 
@"snippet" : @""),
-                     @"sroffset": @0,
-                     @"srlimit": @(SEARCH_MAX_RESULTS),
                      @"piprop": @"thumbnail",
                      @"pithumbsize" : @(SEARCH_THUMBNAIL_WIDTH),
                      @"pilimit": @(SEARCH_MAX_RESULTS),
@@ -183,65 +169,46 @@
         if (query) {
         
             NSDictionary *pages = (NSDictionary *)query[@"pages"];
+
+            NSSortDescriptor *sortByIndex = [NSSortDescriptor 
sortDescriptorWithKey: @"index"
+                                                                          
ascending: YES];
+            if (!pages) return output;
             
-            NSString *searchTypeString = (self.searchType == 
SEARCH_TYPE_TITLES) ? @"prefixsearch" : @"search";
-            
-            NSArray *pagesOrdered = (NSArray *)query[searchTypeString];
-            
-            if (pages && pagesOrdered) {
+            NSArray *pagesOrdered = [pages.allValues 
sortedArrayUsingDescriptors:@[sortByIndex]];
 
-                // Loop through the prefixsearch results (rather than the 
pages results) so we maintain correct order.
-                // Based on 
https://gerrit.wikimedia.org/r/#/c/158011/2/javascripts/modules/search/SearchApi.js
-                for (NSDictionary *prefixPage in pagesOrdered) {
-
-                    // "dictionaryWithDictionary" used because it creates a 
deep mutable copy of the __NSCFDictionary.
-                    NSMutableDictionary *mutablePrefixPage = 
[NSMutableDictionary dictionaryWithDictionary:prefixPage];
-                    
-                    // Add thumb placeholder.
-                    mutablePrefixPage[@"thumbnail"] = @{}.mutableCopy;
-                    
-                    NSString *snippet = prefixPage[@"snippet"] ? 
prefixPage[@"snippet"] : @"";
-                    // Strip HTML and collapse repeating spaces in snippet.
-                    if (snippet.length > 0) {
-                        snippet = [snippet getStringWithoutHTML];
-                        snippet = [self.spaceCollapsingRegex 
stringByReplacingMatchesInString: snippet
-                                                                               
       options: 0
-                                                                               
         range: NSMakeRange(0, [snippet length])
-                                                                               
  withTemplate: @" "];
-                    }
-                    mutablePrefixPage[@"snippet"] = snippet;
-
-                    // Grab thumbnail and pageprops info from non-prefixsearch 
result for this pageid.
-                    for (NSDictionary *page in pages.allValues) {
-
-                        // Grab thumbnail info.
-                        id pageTitle = page[@"title"];
-                        id prefixPageTitle = mutablePrefixPage[@"title"];
-                        if (pageTitle && prefixPageTitle && [prefixPageTitle 
isKindOfClass:[NSString class]] && [pageTitle isKindOfClass:[NSString class]]){
-                            if ([prefixPageTitle isEqualToString:pageTitle]) {
-                                
-                                if (page[@"thumbnail"]){
-                                    mutablePrefixPage[@"thumbnail"] = 
page[@"thumbnail"];
-                                }
-                                
-                                // Grab wiki data id.
-                                id pageprops = page[@"pageprops"];
-                                if (pageprops && [pageprops 
isKindOfClass:[NSDictionary class]]){
-                                    if (pageprops[@"wikibase_item"]){
-                                        mutablePrefixPage[@"wikibase_item"] = 
pageprops[@"wikibase_item"];
-                                    }
-                                }
-                                
-                                break;
-                            }
-                        }
-                        
-                    }
-
-                    mutablePrefixPage[@"title"] = mutablePrefixPage[@"title"] 
? [mutablePrefixPage[@"title"] wikiTitleWithoutUnderscores] : @"";
-                    
-                    if (mutablePrefixPage) [output 
addObject:mutablePrefixPage];
+            for (NSDictionary *prefixPage in pagesOrdered) {
+                
+                // Use "dictionaryWithDictionary" because it creates
+                // a deep mutable copy of the __NSCFDictionary.
+                NSMutableDictionary *mutablePrefixPage =
+                [NSMutableDictionary dictionaryWithDictionary:prefixPage];
+                
+                NSString *snippet = prefixPage[@"snippet"] ? 
prefixPage[@"snippet"] : @"";
+                // Strip HTML and collapse repeating spaces in snippet.
+                if (snippet.length > 0) {
+                    snippet = [snippet getStringWithoutHTML];
+                    snippet = [self.spaceCollapsingRegex 
stringByReplacingMatchesInString: snippet
+                                                                               
   options: 0
+                                                                               
     range: NSMakeRange(0, [snippet length])
+                                                                             
withTemplate: @" "];
                 }
+                mutablePrefixPage[@"snippet"] = snippet;
+                
+                mutablePrefixPage[@"title"] = mutablePrefixPage[@"title"] ? 
[mutablePrefixPage[@"title"] wikiTitleWithoutUnderscores] : @"";
+
+                NSString *description = @"";
+                NSDictionary *terms = mutablePrefixPage[@"terms"];
+                if(terms && terms[@"description"]){
+                    NSArray *descriptions = terms[@"description"];
+                    if (descriptions && (descriptions.count > 0)) {
+                        description = descriptions[0];
+                        description = [description capitalizeFirstLetter];
+                    }
+                    [mutablePrefixPage removeObjectForKey:@"terms"];
+                }
+                mutablePrefixPage[@"description"] = description;
+
+                if (mutablePrefixPage) [output addObject:mutablePrefixPage];
             }
         }
     }
diff --git a/wikipedia/View 
Controllers/SearchResults/SearchResultAttributedString.m b/wikipedia/View 
Controllers/SearchResults/SearchResultAttributedString.m
index eb49bdc..31ced60 100644
--- a/wikipedia/View Controllers/SearchResults/SearchResultAttributedString.m
+++ b/wikipedia/View Controllers/SearchResults/SearchResultAttributedString.m
@@ -44,7 +44,7 @@
     // Append/style Wikidata description.
     if ((description.length > 0)) {
         NSAttributedString *attributedDesc =
-        [[NSAttributedString alloc] initWithString: [@"\n" 
stringByAppendingString:[description capitalizeFirstLetter]]
+        [[NSAttributedString alloc] initWithString: [@"\n" 
stringByAppendingString:description]
                                         attributes: attributesDescription];
         [outputString appendAttributedString:attributedDesc];
     }
diff --git a/wikipedia/View Controllers/SearchResults/SearchResultsController.m 
b/wikipedia/View Controllers/SearchResults/SearchResultsController.m
index a8e662f..1ecc820 100644
--- a/wikipedia/View Controllers/SearchResults/SearchResultsController.m
+++ b/wikipedia/View Controllers/SearchResults/SearchResultsController.m
@@ -21,7 +21,6 @@
 #import "TopMenuTextFieldContainer.h"
 #import "TopMenuTextField.h"
 #import "SearchDidYouMeanButton.h"
-#import "WikiDataShortDescriptionFetcher.h"
 #import "SearchMessageLabel.h"
 #import "RecentSearchesViewController.h"
 #import "NSArray+Predicate.h"
@@ -345,7 +344,7 @@
         SearchResultAttributedString *attributedResult =
         [SearchResultAttributedString initWithTitle: result[@"title"]
                                             snippet: result[@"snippet"]
-                                wikiDataDescription: 
result[@"wikidata_description"]
+                                wikiDataDescription: result[@"description"]
                                      highlightWords: 
self.searchStringWordsToHighlight
                                          searchType: searchType
                                     attributesTitle: self.attributesTitle
@@ -381,24 +380,6 @@
                 // We have search titles! Show them right away!
                 // NSLog(@"FIRE ONE! Show search result titles.");
                 [self.searchResultsTable reloadData];
-
-                // Get WikiData Id's to pass to 
WikiDataShortDescriptionFetcher.
-                NSMutableArray *wikiDataIds = @[].mutableCopy;
-                for (NSDictionary *page in self.searchResults) {
-                    id wikiDataId = page[@"wikibase_item"];
-                    if(wikiDataId && [wikiDataId isKindOfClass:[NSString 
class]]){
-                        [wikiDataIds addObject:wikiDataId];
-                    }
-                }
-
-                // Fetch WikiData short descriptions.
-                if ((wikiDataIds.count > 0) && ENABLE_WIKIDATA_DESCRIPTIONS){
-                    (void)[[WikiDataShortDescriptionFetcher alloc] 
initAndFetchDescriptionsForIds: wikiDataIds
-                                                                               
        searchType: searchResultFetcher.searchType
-                                                                               
       withManager: [QueuesSingleton sharedInstance].searchResultsFetchManager
-                                                                               
thenNotifyDelegate: self];
-                }
-
             }
                 break;
             case FETCH_FINAL_STATUS_CANCELLED:
@@ -473,35 +454,6 @@
                         break;
                     }
                 }
-            }
-                break;
-            case FETCH_FINAL_STATUS_CANCELLED:
-                break;
-            case FETCH_FINAL_STATUS_FAILED:
-                break;
-        }
-    }else if ([sender isKindOfClass:[WikiDataShortDescriptionFetcher class]]) {
-        WikiDataShortDescriptionFetcher *wikiDataShortDescriptionFetcher = 
(WikiDataShortDescriptionFetcher*)sender;
-        switch (status) {
-            case FETCH_FINAL_STATUS_SUCCEEDED:{
-                NSDictionary *wikiDataShortDescriptions = (NSDictionary 
*)fetchedData;
-
-                // Add wikidata descriptions to respective search results.
-                for (NSMutableDictionary *d in self.searchResults) {
-                    NSString *wikiDataId = d[@"wikibase_item"];
-                    if(wikiDataId){
-                        if ([wikiDataShortDescriptions 
objectForKey:wikiDataId]) {
-                            NSString *shortDesc = 
wikiDataShortDescriptions[wikiDataId];
-                            if (shortDesc) {
-                                d[@"wikidata_description"] = shortDesc;
-                            }
-                        }
-                    }
-                }
-
-                [self 
updateAttributeTextForSearchType:wikiDataShortDescriptionFetcher.searchType];
-                
-                [self.searchResultsTable reloadData];
             }
                 break;
             case FETCH_FINAL_STATUS_CANCELLED:

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I5ebd0be0e2cd1d02f692818a9258fed4762d5f86
Gerrit-PatchSet: 4
Gerrit-Project: apps/ios/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Mhurd <mh...@wikimedia.org>
Gerrit-Reviewer: Brion VIBBER <br...@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