jenkins-bot has submitted this change and it was merged. ( 
https://gerrit.wikimedia.org/r/366655 )

Change subject: Extract tags for example dialog in JS
......................................................................


Extract tags for example dialog in JS

Extracts entity IDs as tags from SPARQL query.
Sorts them by weight and use first 50.
Then fetches labels via Wikibase API and builds tag cloud with labels.


Change-Id: Ic9bce71837e2a2d92aa3f6660460d465da0d4a30
---
M wikibase/queryService/api/QuerySamples.js
M wikibase/queryService/api/Wikibase.js
M wikibase/queryService/ui/QueryExampleDialog.js
3 files changed, 108 insertions(+), 43 deletions(-)

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



diff --git a/wikibase/queryService/api/QuerySamples.js 
b/wikibase/queryService/api/QuerySamples.js
index 2f92872..8e62ed6 100644
--- a/wikibase/queryService/api/QuerySamples.js
+++ b/wikibase/queryService/api/QuerySamples.js
@@ -97,15 +97,14 @@
                return prev;
        };
 
-       /**
-        * Get list of tags from UL list
-        *
-        * @param {Element} tagUL
-        * @return {string[]}
-     * @private
-     */
-       SELF.prototype._extractTagsFromUL = function( tagUL ) {
-               return tagUL.find( 'a[rel="mw:WikiLink"]' ).map( function() { 
return $( this ).text().trim(); } ).get();
+       SELF.prototype._extractTagsFromSPARQL = function ( sparql ) {
+               var tags = sparql.replace( /\n/g, '' ).match( /(Q|P)[0-9]+/g );
+
+               if ( !tags ) {
+                       return [];
+               }
+
+               return tags;
        };
 
        SELF.prototype._parseHTML = function ( html ) {
@@ -141,14 +140,12 @@
                                return null;
                        }
                        var title = titleEl.text().trim();
-                       // Get UL elements between header and query text
-                       var tagUL = $this.prevUntil( titleEl ).filter( 'ul' );
 
                        return {
                                title:    title,
                                query:    query,
                                href:     PAGE_URL + '#' + encodeURIComponent( 
title.replace( / /g, '_' ) ).replace( /%/g, '.' ),
-                               tags:     self._extractTagsFromUL( tagUL ),
+                               tags:     self._extractTagsFromSPARQL( query ),
                                category: self._findPrevHeader( titleEl 
).text().trim()
                        };
                } ).get();
diff --git a/wikibase/queryService/api/Wikibase.js 
b/wikibase/queryService/api/Wikibase.js
index c84bc99..4a9dac9 100644
--- a/wikibase/queryService/api/Wikibase.js
+++ b/wikibase/queryService/api/Wikibase.js
@@ -12,7 +12,7 @@
                action: 'wbsearchentities',
                format: 'json',
                continue: 0,
-               language: LANGUAGE,
+               languages: LANGUAGE,
                uselang: LANGUAGE
        };
 
@@ -21,6 +21,14 @@
                meta: 'siteinfo',
                format: 'json',
                siprop: 'languages'
+       };
+
+       var QUERY_LABELS = {
+               action: 'wbgetentities',
+               props: 'labels',
+               format: 'json',
+               languages: LANGUAGE,
+               languagefallback: '1'
        };
 
        /**
@@ -91,6 +99,27 @@
        };
 
        /**
+        * Get labels for given entities
+        *
+        * @return {jQuery.Promise}
+        */
+       SELF.prototype.getLabels = function( ids ) {
+
+               if ( typeof ids === 'string' ) {
+                       ids = [ ids ];
+               }
+
+               var query = QUERY_LABELS;
+               query.ids = ids.join( '|' );
+
+               if ( this._language  ) {
+                       query.languages = this._language;
+               }
+
+               return this._query( query );
+       };
+
+       /**
         * @private
         */
        SELF.prototype._query = function( query ) {
diff --git a/wikibase/queryService/ui/QueryExampleDialog.js 
b/wikibase/queryService/ui/QueryExampleDialog.js
index 30c9948..6ad7e0b 100644
--- a/wikibase/queryService/ui/QueryExampleDialog.js
+++ b/wikibase/queryService/ui/QueryExampleDialog.js
@@ -38,6 +38,12 @@
        SELF.prototype._querySamplesApi = null;
 
        /**
+        * @property {wikibase.queryService.api.Wikibase}
+        * @private
+        */
+       SELF.prototype._wikibaseApi = null;
+
+       /**
         * @property {Function}
         * @private
         */
@@ -74,6 +80,8 @@
                if ( !this._trackingApi ) {
                        this._trackingApi = new 
wikibase.queryService.api.Tracking();
                }
+
+               this._wikibaseApi = new wikibase.queryService.api.Wikibase();
 
                this._initFilter();
                this._initExamples();
@@ -146,32 +154,38 @@
                var self = this,
                        jQCloudTags = [];
 
-               $.each( this._getCloudTags(), function( tag, weight ) {
-                       jQCloudTags.push( {
-                               text: tag,
-                               weight: weight,
-                               link: '#',
-                               html: {
-                                       title: weight + ' match(es)'
-                               },
-                               handlers: {
-                                       click: function( e ) {
-                                               self._$element.find( 
'.tagFilter' ).tags().addTag( $( this ).text() );
-                                               self._drawTagCloud( true );
-                                               return false;
+               this._getCloudTags().done( function ( tags ) {
+                       $.each( tags, function ( i, tag ) {
+                               var label =  tag.label + ' (' + tag.id + ')';
+
+                               jQCloudTags.push( {
+                                       text: label,
+                                       weight: tag.weight,
+                                       link: '#',
+                                       html: {
+                                               title: '(' + tag.weight + ')',
+                                               'data-id': tag.id
+                                       },
+                                       handlers: {
+                                               click: function ( e ) {
+                                                       self._$element.find( 
'.tagFilter' ).tags().addTag( $( this ).text() );
+                                                       self._drawTagCloud( 
true );
+                                                       return false;
+                                               }
                                        }
-                               }
+                               } );
                        } );
-               } );
 
-               if ( redraw ) {
-                       $( '.tagCloud' ).jQCloud( 'update', jQCloudTags );
-                       return;
-               }
+                       if ( redraw ) {
+                               $( '.tagCloud' ).jQCloud( 'update', jQCloudTags 
);
+                               return;
+                       }
 
-               $( '.tagCloud' ).jQCloud( jQCloudTags, {
-                       delayedMode: true,
-                       autoResize: true
+                       $( '.tagCloud' ).jQCloud( jQCloudTags, {
+                               delayedMode: true,
+                               autoResize: true
+                       } );
+
                } );
        };
 
@@ -182,17 +196,20 @@
                var self = this;
 
                // filter tags that don't effect the filter for examples
-               var tagsFilter = function( tags ) {
+               var tagsFilter = function ( tags ) {
                        var selectedTags = self._$element.find( '.tagFilter' 
).tags().getTags();
 
                        return selectedTags.every( function ( selectedTag ) {
-                               return tags.indexOf( selectedTag ) !== -1;
+                               return tags.indexOf( selectedTag.match( 
/\((.*)\)/ )[1] ) !== -1;
                        } );
                };
 
                // filter selected tags from tag cloud
-               var tagFilter = function( tag ) {
-                       var selectedTags = self._$element.find( '.tagFilter' 
).tags().getTags();
+               var tagFilter = function ( tag ) {
+                       var selectedTags = self._$element.find( '.tagFilter' 
).tags().getTags().map(
+                                       function ( v ) {
+                                               return v.match( /\((.*)\)/ )[1];
+                                       } );
 
                        return selectedTags.indexOf( tag ) !== -1;
                };
@@ -209,14 +226,36 @@
                                }
 
                                if ( !tagCloud[tag] ) {
-                                       tagCloud[tag] = 1;
+                                       tagCloud[tag] = { id: tag, weight: 1 };
                                } else {
-                                       tagCloud[tag]++;
+                                       tagCloud[tag].weight++;
                                }
                        } );
                } );
 
-               return tagCloud;
+               tagCloud = _.compact( tagCloud ).sort( function ( a, b ) {
+                       if ( a.weight > b.weight ) {
+                               return -1;
+                       }
+                       if ( a.weight < b.weight ) {
+                               return 1;
+                       }
+                       return 0;
+               } ).slice( 0, 50 );
+
+               var deferred = $.Deferred();
+               this._wikibaseApi.getLabels( tagCloud.map( function ( v ) {
+                       return v.id;
+               } ) ).done( function ( data ) {
+                       tagCloud.forEach( function ( tag ) {
+                               tag.label = _.compact( 
data.entities[tag.id].labels )[0].value;
+
+                       } );
+
+                       deferred.resolve( tagCloud );
+               } );
+
+               return deferred.promise();
        };
 
        /**
@@ -293,7 +332,7 @@
                        text = text.toLowerCase();
 
                        $.each( tags, function( key, tag ) {
-                               if ( text.indexOf( tag.toLowerCase() ) === -1 ) 
{
+                               if ( text.indexOf( tag.toLowerCase().match( 
/\((.*)\)/ )[1] ) === -1 ) {
                                        matches = false;
                                }
                        } );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ic9bce71837e2a2d92aa3f6660460d465da0d4a30
Gerrit-PatchSet: 3
Gerrit-Project: wikidata/query/gui
Gerrit-Branch: master
Gerrit-Owner: Jonas Kress (WMDE) <jonas.kr...@wikimedia.de>
Gerrit-Reviewer: Smalyshev <smalys...@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