Index: trunk/tools/viaf/README |
— | — | @@ -10,7 +10,7 @@ |
11 | 11 | http://de.wikipedia.org/wiki/Wikipedia:WikiConvention/ \ |
12 | 12 | Programm/VIAF#Greasemonkey-Skript (de) |
13 | 13 | |
14 | | -Version: 0.309 |
| 14 | +Version: 0.400 |
15 | 15 | README last updated: 20110924 |
16 | 16 | |
17 | 17 | What are VIAF numbers ? |
— | — | @@ -123,8 +123,42 @@ |
124 | 124 | ]} |
125 | 125 | ] |
126 | 126 | |
| 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 | + |
127 | 159 | == solved == |
128 | 160 | + searching now also PND and GND number |
| 161 | ++ Toolserver API changed, it returns names for VIAF, PND, GND numbers |
| 162 | + if they are available |
129 | 163 | + add a ajax request module to fetch author names for numbers from toolserver |
130 | 164 | and show them live on the current page; define a JSONP data format |
131 | 165 | (0.306) |
— | — | @@ -132,7 +166,6 @@ |
133 | 167 | fixed in r95940 (0.204) |
134 | 168 | |
135 | 169 | == To Do == |
136 | | -* adding Toolserver software and requests to also fetch names for PND/GMD number |
137 | 170 | * add a user-interface or mechanism to enable/disable the summary alert box |
138 | 171 | without the need to change program code |
139 | 172 | * revise and enable the update mechanism |
Index: trunk/tools/viaf/viaf.user.js |
— | — | @@ -4,11 +4,11 @@ |
5 | 5 | // @require https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js |
6 | 6 | // @require http://svn.wikimedia.org/svnroot/mediawiki/trunk/tools/viaf/jquery.cookie.js |
7 | 7 | // @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 |
9 | 9 | // @include * |
10 | 10 | // ==/UserScript== |
11 | 11 | |
12 | | -var VERSION = "0.309"; |
| 12 | +var VERSION = "0.400"; |
13 | 13 | /*** |
14 | 14 | * Copyright (c) 2011 T. Gries |
15 | 15 | * |
— | — | @@ -50,7 +50,9 @@ |
51 | 51 | * debug parameter bitwise |
52 | 52 | * 20110920 removed parent() and add the new links close after the place |
53 | 53 | * 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 |
55 | 57 | * |
56 | 58 | ***/ |
57 | 59 | |
— | — | @@ -89,38 +91,35 @@ |
90 | 92 | } |
91 | 93 | |
92 | 94 | 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; |
93 | 101 | |
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"; |
100 | 104 | |
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; |
103 | 107 | |
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 | + } |
106 | 117 | |
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 | + } |
125 | 124 | } |
126 | 125 | |
127 | 126 | // utility functions |
— | — | @@ -147,6 +146,8 @@ |
148 | 147 | * |
149 | 148 | **/ |
150 | 149 | function dump( arr, depth ) { |
| 150 | + var cr = "\n"; |
| 151 | + // var cr = "<br/>"; |
151 | 152 | var dumped_text = ""; |
152 | 153 | if( !depth ) depth = 0; |
153 | 154 | |
— | — | @@ -162,10 +163,10 @@ |
163 | 164 | var value = arr[item]; |
164 | 165 | |
165 | 166 | if ( typeof( value ) == 'object' ) { //If it is an array, |
166 | | - dumped_text += depth_padding + "'" + item + "' ...\n"; |
| 167 | + dumped_text += depth_padding + "'" + item + "' ..."+cr; |
167 | 168 | dumped_text += dump( value, depth+1 ); |
168 | 169 | } else { |
169 | | - dumped_text += depth_padding + "'" + item + "' => \"" + value + "\"\n"; |
| 170 | + dumped_text += depth_padding + "'" + item + "' => \"" + value + "\""+cr; |
170 | 171 | } |
171 | 172 | } |
172 | 173 | } else { //Strings/Chars/Numbers etc. |
— | — | @@ -213,9 +214,37 @@ |
214 | 215 | var items_str = JSON.stringify( items ); |
215 | 216 | if ( debug & 1 ) output = "sent to server:\n"+items_str; |
216 | 217 | |
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 | + ***/ |
220 | 249 | |
221 | 250 | // http://wiki.greasespot.net/GM_xmlhttpRequest#POST_request |
222 | 251 | GM_xmlhttpRequest({ |
— | — | @@ -242,41 +271,57 @@ |
243 | 272 | function cb_updateFromServer( data ){ |
244 | 273 | if ( debug & 2 ) output += "\n\nreceived from server:\n"+ data; |
245 | 274 | var data_js = JSON.parse( data ); |
246 | | - if ( debug & 4 ) alert( dump( data_js ) ); |
| 275 | + if ( debug & 4 ) output += dump( data_js ); |
247 | 276 | |
248 | | - var names = ''; |
| 277 | + var names,numType,number; |
249 | 278 | |
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 ) { |
252 | 281 | |
253 | | - // get the first viaf number per record, |
| 282 | + // get the first viaf or pnd number per record, |
254 | 283 | // as this was the one which was found on the web page. |
255 | 284 | // |
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>" |
257 | 287 | // when coming back here. |
258 | 288 | // |
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). |
265 | 292 | |
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']+" "; |
269 | 313 | } |
270 | | - |
271 | | - // replace the class "viaf-<viafnr>" |
| 314 | + |
| 315 | + // replace the class "viaf-<viafnr>" or "pnd-<pndnr>" |
272 | 316 | // 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']; |
274 | 318 | |
275 | | - $(".viaf-" + viaf ) |
| 319 | + $("."+numType+"-" + number ) |
276 | 320 | .text( " " + nameString + " " ) |
277 | 321 | .css( markNamesFromServer ) |
278 | 322 | .after( nbsp ) |
279 | 323 | .before( nbsp ); |
280 | 324 | } |
| 325 | + |
281 | 326 | } |
282 | 327 | |
283 | 328 | if ( ( debug & 8 ) && ( names.length > 0 ) ) { |
— | — | @@ -288,7 +333,6 @@ |
289 | 334 | |
290 | 335 | } |
291 | 336 | |
292 | | - |
293 | 337 | function doAnyOtherBusiness( numberType, number ) { |
294 | 338 | // add the element only if it does not exist in list |
295 | 339 | if ( numberType == "viaf" ) { |
— | — | @@ -299,8 +343,7 @@ |
300 | 344 | } else { |
301 | 345 | if ( pnd.indexOf( number ) == -1 ) { |
302 | 346 | pnd.push( number ); |
303 | | -// FIXME |
304 | | - // out_js.push( { "pnd" : [number] } ); |
| 347 | + out_js.push( { "pnd" : [number] } ); |
305 | 348 | } |
306 | 349 | } |
307 | 350 | } |
— | — | @@ -315,15 +358,14 @@ |
316 | 359 | // but don't look in an active textareas like mediawiki input textarea |
317 | 360 | |
318 | 361 | $("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 ){ |
322 | 364 | s3 = s3.toUpperCase(); |
323 | 365 | return "<span class='pnd pnd-in-text' pnd='" + s3 + "'>" + s1 + s2 + s3 + "</span>" |
324 | 366 | }); |
325 | 367 | |
326 | 368 | // PASS 2 |
327 | | -// try to retrieve viaf numbers in urls |
| 369 | +// try to retrieve viaf or pnd or gnd numbers from urls |
328 | 370 | |
329 | 371 | $("a").each(function(){ |
330 | 372 | var $this = $(this); |
— | — | @@ -331,14 +373,22 @@ |
332 | 374 | if ( $this.find(".pnd").length != 0 ) return; // in PASS 2, skip all entries which have this attribute from PASS 1 |
333 | 375 | var url = $this.attr("href"); |
334 | 376 | |
| 377 | + /* VIAF */ |
335 | 378 | 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 | | - |
337 | 379 | if ( typeof url != "undefined" && url.match( magicUrlRegExp ) ) { |
338 | 380 | if ( markUrlDetectedItems ) $this.css( markUrlDetectedItemsCSS ); |
339 | 381 | var viaf = RegExp.$1.replace( /[\D]*/g, '' ); |
340 | 382 | $this.after( $("<span class='viaf viaf-in-url' viaf='"+viaf+"'> "+viaf+"</span>") ); |
341 | 383 | } |
342 | 384 | |
| 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+"'> "+pnd+"</span>") ); |
| 391 | + } |
| 392 | + |
343 | 393 | }) |
344 | 394 | |
345 | 395 | // PASS 3 |
— | — | @@ -418,6 +468,11 @@ |
419 | 469 | // numberToNames( "pnd", out_js ); |
420 | 470 | |
421 | 471 | // show a summary of the collected numbers |
| 472 | + |
| 473 | +/*** |
| 474 | + * |
| 475 | + * FIXME |
| 476 | + * |
422 | 477 | if ( out.length > 0 ) { |
423 | 478 | |
424 | 479 | out.sort( numSort ); |
— | — | @@ -436,5 +491,5 @@ |
437 | 492 | |
438 | 493 | |
439 | 494 | } |
440 | | - |
| 495 | +***/ |
441 | 496 | }) ( jQuery ); |