r98018 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98017‎ | r98018 | r98019 >
Date:19:55, 24 September 2011
Author:wikinaut
Status:deferred
Tags:
Comment:
new version detects PND and GND numbers and shows names via Toolserver API
Modified paths:
  • /trunk/tools/viaf/README (modified) (history)
  • /trunk/tools/viaf/viaf.user.js (modified) (history)

Diff [purge]

Index: trunk/tools/viaf/README
@@ -10,7 +10,7 @@
1111 http://de.wikipedia.org/wiki/Wikipedia:WikiConvention/ \
1212 Programm/VIAF#Greasemonkey-Skript (de)
1313
14 -Version: 0.309
 14+Version: 0.400
1515 README last updated: 20110924
1616
1717 What are VIAF numbers ?
@@ -123,8 +123,42 @@
124124 ]}
125125 ]
126126
 127+Example 5
 128+request and response shows mixed viaf and pnd number for one person
 129+sent to server
 130+http://toolserver.org/~apper/pd/x.php?format=json&data=name&for=[{"viaf":["100272799"]},{"pnd":["118588664"]}]
 131+
 132+received from server:
 133+[
 134+{"viaf":["100272799"],
 135+"names":[
 136+{"lang":"de","name":"Peter Noll"}]},
 137+{"pnd":["118588664"],
 138+"names":[
 139+{"lang":"de","name":"Peter Noll"}
 140+]}
 141+]
 142+
 143+the same response in array notation:
 144+'0' ...
 145+ 'viaf' ...
 146+ '0' => "100272799"
 147+ 'names' ...
 148+ '0' ...
 149+ 'lang' => "de"
 150+ 'name' => "Peter Noll"
 151+'1' ...
 152+ 'pnd' ...
 153+ '0' => "118588664"
 154+ 'names' ...
 155+ '0' ...
 156+ 'lang' => "de"
 157+ 'name' => "Peter Noll"
 158+
127159 == solved ==
128160 + searching now also PND and GND number
 161++ Toolserver API changed, it returns names for VIAF, PND, GND numbers
 162+ if they are available
129163 + add a ajax request module to fetch author names for numbers from toolserver
130164 and show them live on the current page; define a JSONP data format
131165 (0.306)
@@ -132,7 +166,6 @@
133167 fixed in r95940 (0.204)
134168
135169 == To Do ==
136 -* adding Toolserver software and requests to also fetch names for PND/GMD number
137170 * add a user-interface or mechanism to enable/disable the summary alert box
138171 without the need to change program code
139172 * revise and enable the update mechanism
Index: trunk/tools/viaf/viaf.user.js
@@ -4,11 +4,11 @@
55 // @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js
66 // @require http://svn.wikimedia.org/svnroot/mediawiki/trunk/tools/viaf/jquery.cookie.js
77 // @require http://svn.wikimedia.org/svnroot/mediawiki/trunk/tools/viaf/jquery.ba-replacetext.js
8 -// @description locate VIAF numbers in texts and urls on web pages, fetch corresponding names from toolserver. (c)T.Gries Version 0.309 201109241230
 8+// @description locate VIAF numbers in texts and urls on web pages, fetch corresponding names from toolserver. (c)T.Gries Version 0.400 201109242100
99 // @include *
1010 // ==/UserScript==
1111
12 -var VERSION = "0.309";
 12+var VERSION = "0.400";
1313 /***
1414 * Copyright (c) 2011 T. Gries
1515 *
@@ -50,7 +50,9 @@
5151 * debug parameter bitwise
5252 * 20110920 removed parent() and add the new links close after the place
5353 * where the VIAF was found; jQuery 1.6.4
54 - * 20110923 beginning to detect and link PND numbers as well
 54+ * 20110924 detecting and linking PND and GND numbers as well
 55+ * Toolserver API adapted: names now returned for VIAF, PND, GND
 56+ * numbers; detected GND numbers are treated as PND
5557 *
5658 ***/
5759
@@ -89,38 +91,35 @@
9092 }
9193
9294 function updateCheck(verbose) {
 95+ try {
 96+ GM_xmlhttpRequest({
 97+ method: 'GET',
 98+ url: SCRIPT_URL + "?rnd="+Math.random(),
 99+ onload: function(result) {
 100+ if (result.status != 200) throw "status="+result.status;
93101
94 - try {
95 - GM_xmlhttpRequest({
96 - method: 'GET',
97 - url: SCRIPT_URL + "?rnd="+Math.random(),
98 - onload: function(result) {
99 - if (result.status != 200) throw "status="+result.status;
 102+ var tmp = /VERSION[\s=]+"(.*?)"/.exec(result.responseText);
 103+ if (tmp == null) throw "parse error";
100104
101 - var tmp = /VERSION[\s=]+"(.*?)"/.exec(result.responseText);
102 - if (tmp == null) throw "parse error";
 105+ if (VERSION < tmp[1]) {
 106+ if ( ($.cookie("updatecheck", {path:'/'} ) == null) || ( $("#updateinfo").length == 0) ) if (window.confirm("A newer version of the Greasemonkey script "+SCRIPT_NAME+" is available. You currently have version "+VERSION+".\n\nDo you want to update to version "+tmp[1]+" from "+SCRIPT_URL+" now ?\n\nPress Ctrl+F5 after the installation has finished to clear your browser chache.")) location.href = SCRIPT_URL;
103107
104 - if (VERSION < tmp[1]) {
105 - if ( ($.cookie("updatecheck", {path:'/'} ) == null) || ( $("#updateinfo").length == 0) ) if (window.confirm("A newer version of the Greasemonkey script "+SCRIPT_NAME+" is available. You currently have version "+VERSION+".\n\nDo you want to update to version "+tmp[1]+" from "+SCRIPT_URL+" now ?\n\nPress Ctrl+F5 after the installation has finished to clear your browser chache.")) location.href = SCRIPT_URL;
 108+ $("#updateinfo")
 109+ .css("background","yellow")
 110+ .text(" New version of Greasemonkey script "+SCRIPT_NAME+" available.")
 111+ .attr("title","Click here to update to "+SCRIPT_NAME+" version "+tmp[1]+". Your current version is "+VERSION)
 112+ .mouseover( function(){ $(this).css("cursor","pointer") } )
 113+ .click( function() { $(this).fadeOut(2000); location.href=SCRIPT_URL } );
 114+ } else {
 115+ if (verbose) alert("There is no newer version of the Greasemonkey script "+SCRIPT_NAME+" available. Feel lucky, because you already have the latest version "+VERSION+".") ;
 116+ }
106117
107 - $("#updateinfo")
108 - .css("background","yellow")
109 - .text(" New version of Greasemonkey script "+SCRIPT_NAME+" available.")
110 - .attr("title","Click here to update to "+SCRIPT_NAME+" version "+tmp[1]+". Your current version is "+VERSION)
111 - .mouseover( function(){ $(this).css("cursor","pointer") } )
112 - .click( function() { $(this).fadeOut(2000); location.href=SCRIPT_URL } );
113 -
114 - } else {
115 - if (verbose) alert("There is no newer version of the Greasemonkey script "+SCRIPT_NAME+" available. Feel lucky, because you already have the latest version "+VERSION+".") ;
116 - }
117 -
118 - $.cookie("updatecheck", "1", {expires:1, path:'/'} );
119 -
120 - }
121 - });
122 - } catch (error) {
123 - alert('Error updating: '+error);
124 - }
 118+ $.cookie("updatecheck", "1", {expires:1, path:'/'} );
 119+ }
 120+ });
 121+ } catch (error) {
 122+ alert('Error updating: '+error);
 123+ }
125124 }
126125
127126 // utility functions
@@ -147,6 +146,8 @@
148147 *
149148 **/
150149 function dump( arr, depth ) {
 150+ var cr = "\n";
 151+ // var cr = "<br/>";
151152 var dumped_text = "";
152153 if( !depth ) depth = 0;
153154
@@ -162,10 +163,10 @@
163164 var value = arr[item];
164165
165166 if ( typeof( value ) == 'object' ) { //If it is an array,
166 - dumped_text += depth_padding + "'" + item + "' ...\n";
 167+ dumped_text += depth_padding + "'" + item + "' ..."+cr;
167168 dumped_text += dump( value, depth+1 );
168169 } else {
169 - dumped_text += depth_padding + "'" + item + "' => \"" + value + "\"\n";
 170+ dumped_text += depth_padding + "'" + item + "' => \"" + value + "\""+cr;
170171 }
171172 }
172173 } else { //Strings/Chars/Numbers etc.
@@ -213,9 +214,37 @@
214215 var items_str = JSON.stringify( items );
215216 if ( debug & 1 ) output = "sent to server:\n"+items_str;
216217
217 - // get names via Greasemonkey_xmlhttpRequest across domain borders
218 - // example using "GET"
219 - // http://toolserver.org/~apper/pd/x.php?format=json&data=name&for=[{"viaf":["15571981"]},{"viaf":["79410188"]},{"viaf":["2878675"]},{"viaf":["122255788"]}]
 218+ /***
 219+ * get names via Greasemonkey_xmlhttpRequest across domain borders
 220+ *
 221+ * example usages using "GET" requests
 222+ *
 223+ * example 1 request shows several viaf numbers for the same persons
 224+ * http://toolserver.org/~apper/pd/x.php?format=json&data=name&for=[{"viaf":["15571981"]},{"viaf":["79410188"]},{"viaf":["2878675"]},{"viaf":["122255788"]}]
 225+ *
 226+ * example 2 request and response shows mixed viaf and pnd number for one person
 227+ * http://toolserver.org/~apper/pd/x.php?format=json&data=name&for=[{"viaf":["100272799"]},{"pnd":["118588664"]}]
 228+ *
 229+ * example 2 JSON response:
 230+ * [{"viaf":["100272799"],"names":[{"lang":"de","name":"Peter Noll"}]},{"pnd":["118588664"],"names":[{"lang":"de","name":"Peter Noll"}]}]
 231+ *
 232+ * example 2 response in array notation as produced by the enclosed dump() function:
 233+ * '0' ...
 234+ * 'viaf' ...
 235+ * '0' => "100272799"
 236+ * 'names' ...
 237+ * '0' ...
 238+ * 'lang' => "de"
 239+ * 'name' => "Peter Noll"
 240+ * '1' ...
 241+ * 'pnd' ...
 242+ * '0' => "118588664"
 243+ * 'names' ...
 244+ * '0' ...
 245+ * 'lang' => "de"
 246+ * 'name' => "Peter Noll"
 247+ *
 248+ ***/
220249
221250 // http://wiki.greasespot.net/GM_xmlhttpRequest#POST_request
222251 GM_xmlhttpRequest({
@@ -242,41 +271,57 @@
243272 function cb_updateFromServer( data ){
244273 if ( debug & 2 ) output += "\n\nreceived from server:\n"+ data;
245274 var data_js = JSON.parse( data );
246 - if ( debug & 4 ) alert( dump( data_js ) );
 275+ if ( debug & 4 ) output += dump( data_js );
247276
248 - var names = '';
 277+ var names,numType,number;
249278
250 - // for all returned viaf numbers do
251 - for ( viaf_i in data_js ) {
 279+ // for all returned viaf and pnd numbers do
 280+ for ( record_nr in data_js ) {
252281
253 - // get the first viaf number per record,
 282+ // get the first viaf or pnd number per record,
254283 // as this was the one which was found on the web page.
255284 //
256 - // All occurences are already marked as class "viaf-<viafnr>"
 285+ // All occurences in the web page are already
 286+ // marked as class "viaf-<viafnr>" or "pnd-<pndnr>"
257287 // when coming back here.
258288 //
259 - // Remark: the server response may contain further numbers for
260 - // persons having more than one viaf number.
261 -
262 - var viaf = data_js[viaf_i]['viaf'][0];
263 -
264 - if ( data_js[viaf_i]['names'].length > 0 ) {
 289+ // The server may return more numbers for a person than were
 290+ // sent, because there are authors which have more than
 291+ // one viaf or pnd number in the database (i.e. doublures).
265292
266 - // for all available names for that viaf do
267 - for ( name_i in data_js[viaf_i]['names'] ) {
268 - names += data_js[viaf_i]['names'][name_i]['name']+" ";
 293+ switch ( true ) {
 294+ case ( typeof data_js[record_nr]['viaf'] != "undefined" ):
 295+ numType = "viaf";
 296+ number = data_js[record_nr]['viaf'][0];
 297+ break;
 298+ case ( typeof data_js[record_nr]['pnd'] != "undefined" ):
 299+ numType = "pnd";
 300+ number = data_js[record_nr]['pnd'][0];
 301+ break;
 302+ default:
 303+ numType = "";
 304+ number = -1;
 305+ }
 306+
 307+ names = "";
 308+ if ( data_js[record_nr]['names'].length > 0 ) {
 309+
 310+ // for all available names for that number do
 311+ for ( name_j in data_js[record_nr]['names'] ) {
 312+ names += data_js[record_nr]['names'][name_j]['name']+" ";
269313 }
270 -
271 - // replace the class "viaf-<viafnr>"
 314+
 315+ // replace the class "viaf-<viafnr>" or "pnd-<pndnr>"
272316 // with all names string or with the first name only
273 - var nameString = ( showAllNames ) ? names : data_js[viaf_i]['names'][0]['name'];
 317+ var nameString = ( showAllNames ) ? names : data_js[record_nr]['names'][0]['name'];
274318
275 - $(".viaf-" + viaf )
 319+ $("."+numType+"-" + number )
276320 .text( " " + nameString + " " )
277321 .css( markNamesFromServer )
278322 .after( nbsp )
279323 .before( nbsp );
280324 }
 325+
281326 }
282327
283328 if ( ( debug & 8 ) && ( names.length > 0 ) ) {
@@ -288,7 +333,6 @@
289334
290335 }
291336
292 -
293337 function doAnyOtherBusiness( numberType, number ) {
294338 // add the element only if it does not exist in list
295339 if ( numberType == "viaf" ) {
@@ -299,8 +343,7 @@
300344 } else {
301345 if ( pnd.indexOf( number ) == -1 ) {
302346 pnd.push( number );
303 -// FIXME
304 - // out_js.push( { "pnd" : [number] } );
 347+ out_js.push( { "pnd" : [number] } );
305348 }
306349 }
307350 }
@@ -315,15 +358,14 @@
316359 // but don't look in an active textareas like mediawiki input textarea
317360
318361 $("body *:not(textarea)")
319 - .replaceText( /(viaf[1-9]?)(:|\/|%2B|%3A|%2F|\s|ID:|=|%3D)+\s*([0-9]+)/gi, "<span class='viaf viaf-in-text' viaf='$3'>$1$2$3</span>" )
320 -// .replaceText( /([pg]nd[1-9]?)(:|\/|%2B|%3A|%2F|\s|ID:|=|%3D)+\s*([0-9]+x?)/gi, "<span class='pnd pnd-in-text' viaf='$3'>$1$2$3</span>" );
321 - .replaceText( /([pg]nd[1-9]?)(:|\/|%2B|%3A|%2F|\s|ID:|=|%3D)+\s*([0-9]+x?)/gi, function( s0, s1, s2, s3 ){
 362+ .replaceText( /(viaf[1-9]?)\s*(:|\/|%2B|%3A|%2F|\s|ID:|=|%3D)+\s*([0-9]+)/gi, "<span class='viaf viaf-in-text' viaf='$3'>$1$2$3</span>" )
 363+ .replaceText( /([pg]nd[1-9]?)\s*(:|\/|%2B|%3A|%2F|\s|ID:|=|%3D)+\s*([0-9]+x?)/gi, function( s0, s1, s2, s3 ){
322364 s3 = s3.toUpperCase();
323365 return "<span class='pnd pnd-in-text' pnd='" + s3 + "'>" + s1 + s2 + s3 + "</span>"
324366 });
325367
326368 // PASS 2
327 -// try to retrieve viaf numbers in urls
 369+// try to retrieve viaf or pnd or gnd numbers from urls
328370
329371 $("a").each(function(){
330372 var $this = $(this);
@@ -331,14 +373,22 @@
332374 if ( $this.find(".pnd").length != 0 ) return; // in PASS 2, skip all entries which have this attribute from PASS 1
333375 var url = $this.attr("href");
334376
 377+ /* VIAF */
335378 var magicUrlRegExp = new RegExp( /(http:\/\/viaf.org\/(viaf\/)?(\d+)|http:\/\/www.librarything.de\/commonknowledge\/search.php?f=13&exact=1&q=VIAF%3A(\d)+)/gi );
336 -
337379 if ( typeof url != "undefined" && url.match( magicUrlRegExp ) ) {
338380 if ( markUrlDetectedItems ) $this.css( markUrlDetectedItemsCSS );
339381 var viaf = RegExp.$1.replace( /[\D]*/g, '' );
340382 $this.after( $("<span class='viaf viaf-in-url' viaf='"+viaf+"'>&nbsp;"+viaf+"</span>") );
341383 }
342384
 385+ /* PND, GND */
 386+ var magicUrlRegExp = new RegExp( /(http:\/\/d-nb.info\/([gp]nd\/)?(\d+))/gi );
 387+ if ( typeof url != "undefined" && url.match( magicUrlRegExp ) ) {
 388+ if ( markUrlDetectedItems ) $this.css( markUrlDetectedItemsCSS );
 389+ var pnd = RegExp.$1.replace( /[\D]*/g, '' );
 390+ $this.after( $("<span class='pnd pnd-in-url' pnd='"+pnd+"'>&nbsp;"+pnd+"</span>") );
 391+ }
 392+
343393 })
344394
345395 // PASS 3
@@ -418,6 +468,11 @@
419469 // numberToNames( "pnd", out_js );
420470
421471 // show a summary of the collected numbers
 472+
 473+/***
 474+ *
 475+ * FIXME
 476+ *
422477 if ( out.length > 0 ) {
423478
424479 out.sort( numSort );
@@ -436,5 +491,5 @@
437492
438493
439494 }
440 -
 495+***/
441496 }) ( jQuery );

Status & tagging log