Jonas Kress (WMDE) has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/366655 )

Change subject: [WIP] Extract tags in JS
......................................................................

[WIP] Extract tags in JS

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


  git pull ssh://gerrit.wikimedia.org:29418/wikidata/query/gui 
refs/changes/55/366655/1

diff --git a/wikibase/queryService/api/QuerySamples.js 
b/wikibase/queryService/api/QuerySamples.js
index 2f92872..9259d1c 100644
--- a/wikibase/queryService/api/QuerySamples.js
+++ b/wikibase/queryService/api/QuerySamples.js
@@ -97,6 +97,16 @@
                return prev;
        };
 
+       SELF.prototype._extractTagsFromSPARQL = function( sparql ) {
+               var tags =  sparql.replace( /\n/g, '' ).match(  /(Q|P)[0-9]+/g  
);
+
+               if( !tags ) {
+                       return [];
+               }
+
+               return tags;
+       };
+
        /**
         * Get list of tags from UL list
         *
@@ -148,7 +158,7 @@
                                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..346934a 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'
        };
 
        /**
@@ -90,6 +98,28 @@
                return this._query( QUERY_LANGUGES );
        };
 
+
+       /**
+        * 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
         */
diff --git a/wikibase/queryService/ui/QueryExampleDialog.js 
b/wikibase/queryService/ui/QueryExampleDialog.js
index 787a895..d200a8a 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,13 +154,14 @@
                var self = this,
                        jQCloudTags = [];
 
-               $.each( this._getCloudTags(), function( tag, weight ) {
+               $.each( this._getCloudTags(), function( tag, d ) {
                        jQCloudTags.push( {
                                text: tag,
-                               weight: weight,
+                               weight: d.weight,
                                link: '#',
                                html: {
-                                       title: weight + ' match(es)'
+                                       title: d.weight + ' match(es)',
+                                       'data-id': tag
                                },
                                handlers: {
                                        click: function( e ) {
@@ -209,13 +218,27 @@
                                }
 
                                if ( !tagCloud[tag] ) {
-                                       tagCloud[tag] = 1;
+                                       tagCloud[tag] = { id:tag, weight: 1 };
                                } else {
-                                       tagCloud[tag]++;
+                                       tagCloud[tag].weight++;
                                }
                        } );
                } );
 
+               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 );
+
+               this._wikibaseApi.getLabels( tagCloud.map( function(v){ return 
v.id } ) ).done( function( labels ){
+                       console.log( labels );
+               } );
+
                return tagCloud;
        };
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic9bce71837e2a2d92aa3f6660460d465da0d4a30
Gerrit-PatchSet: 1
Gerrit-Project: wikidata/query/gui
Gerrit-Branch: master
Gerrit-Owner: Jonas Kress (WMDE) <[email protected]>

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

Reply via email to