Index: trunk/extensions/WikiLove/wikiLove.js |
— | — | @@ -12,14 +12,15 @@ |
13 | 13 | descr: 'Original barnstar', // description in the menu |
14 | 14 | text: '{{subst:The Original Barnstar|$1 ~~~~}}', // message text, $1 is replaced by the user message |
15 | 15 | template: 'The Original Barnstar', // template that is used, for statistics |
16 | | - message: 'Hello '+wgTitle+'!\n\nI just awarded you a barnstar.' // message to use in email notification |
| 16 | + mail: 'Hello $2!\n\nI just awarded you a barnstar.' // message to use in email notification |
| 17 | + // $2 is replaced by the recipient's username |
17 | 18 | }, |
18 | 19 | 'special': { |
19 | 20 | title: null, // no predefined title, allows the user to enter a title |
20 | 21 | descr: 'Special barnstar', |
21 | 22 | text: '{{subst:The Special Barnstarl|$1 ~~~~}}', |
22 | 23 | template: 'The Special Barnstar', |
23 | | - message: 'Hello '+wgTitle+'!\n\nI just awarded you the special barnstar.' |
| 24 | + mail: 'Hello $2!\n\nI just awarded you the special barnstar.' |
24 | 25 | } |
25 | 26 | }, |
26 | 27 | email: true, // add email notices as an option for each award of this type |
— | — | @@ -28,13 +29,14 @@ |
29 | 30 | 'cat': { |
30 | 31 | descr: 'Cat', |
31 | 32 | title: null, |
32 | | - text: "[[$2|left|150px]]\n$1\n\n~~~~", |
| 33 | + text: "[[$3|left|150px]]\n$1\n\n~~~~", // $3 is the image filename |
33 | 34 | template: '', |
34 | 35 | gallery: { |
| 36 | + // right now we can only query the local wiki (not e.g. commons) |
35 | 37 | category: 'Category:Tropical', |
36 | | - total: 100, |
37 | | - num: 3, |
38 | | - width: 150 |
| 38 | + total: 100, // total number of pictures to retrieve, and to randomise |
| 39 | + num: 3, // number of pictures to show from the randomised set |
| 40 | + width: 150 // width of each picture in pixels in the interface (not in the template) |
39 | 41 | } |
40 | 42 | }, |
41 | 43 | // default type, nice to leave this one in place when adding other types |
— | — | @@ -63,8 +65,10 @@ |
64 | 66 | // Load local configuration |
65 | 67 | var wikiLoveConfigUrl = wgServer + wgScript + '?' + $.param( { 'title': 'MediaWiki:WikiLove.js', 'action': 'raw', 'ctype': 'text/javascript' } ); |
66 | 68 | mw.loader.load( wikiLoveConfigUrl ); |
| 69 | + |
67 | 70 | // Find out if we can email the user |
68 | 71 | $.wikiLove.getEmailable(); |
| 72 | + |
69 | 73 | // Build a type list like this: |
70 | 74 | // <ul id="wlTypes"> |
71 | 75 | // <li tabindex="0"><span>Barnstar</span></li> |
— | — | @@ -257,11 +261,17 @@ |
258 | 262 | } |
259 | 263 | }, |
260 | 264 | |
| 265 | + /* |
| 266 | + * Find out whether we can e-mail this user. Probably needs to be moved to the API. |
| 267 | + */ |
261 | 268 | getEmailable: function() { |
262 | 269 | // Test to see if the 'E-mail this user' link exists |
263 | 270 | $.wikiLove.emailable = $('#t-emailuser').length ? true : false; |
264 | 271 | }, |
265 | 272 | |
| 273 | + /* |
| 274 | + * Actually send the notification e-mail. Probably needs to be moved to the API. |
| 275 | + */ |
266 | 276 | sendEmail: function( subject, text ) { |
267 | 277 | $.ajax({ |
268 | 278 | url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?', |
— | — | @@ -322,25 +332,24 @@ |
323 | 333 | if( $( '#wlMessage' ).val().length <= 0 ) { |
324 | 334 | $.wikiLove.showError( 'wikilove-err-msg' ); return false; |
325 | 335 | } |
| 336 | + if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) { |
| 337 | + if ( !$.wikiLove.imageTitle ) { |
| 338 | + $.wikiLove.showError( 'wikilove-err-img' ); return false; |
| 339 | + } |
| 340 | + } |
326 | 341 | |
327 | | - var title = '==' + $( '#wlTitle' ).val() + "==\n"; |
328 | 342 | var rawMessage = $( '#wlMessage' ).val(); |
| 343 | + |
329 | 344 | // If there isn't a signature already in the message, throw an error |
330 | 345 | if ( rawMessage.indexOf( '~~~' ) >= 0 ) { |
331 | 346 | $.wikiLove.showError( 'wikilove-err-sig' ); return false; |
332 | 347 | } |
333 | | - var msg = $.wikiLove.currentTypeOrSubtype.text.replace( '$1', rawMessage ); |
334 | | - if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) { |
335 | | - if ( $.wikiLove.imageTitle ) { |
336 | | - msg = msg.replace( '$2', $.wikiLove.imageTitle ); |
337 | | - } |
338 | | - else { |
339 | | - $.wikiLove.showError( 'wikilove-err-img' ); return false; |
340 | | - } |
341 | | - } |
342 | | - $.wikiLove.doPreview( title + msg ); |
| 348 | + |
| 349 | + var msg = $.wikiLove.prepareMsg( rawMessage ); |
| 350 | + |
| 351 | + $.wikiLove.doPreview( '==' + $( '#wlTitle' ).val() + "==\n" + msg ); |
343 | 352 | $.wikiLove.previewData = { |
344 | | - 'title': title, |
| 353 | + 'title': $( '#wlTitle' ).val(), |
345 | 354 | 'msg': msg, |
346 | 355 | 'type': $.wikiLove.currentTypeId |
347 | 356 | + ($.wikiLove.currentSubtypeId != null ? '-' + $.wikiLove.currentSubtypeId : ''), |
— | — | @@ -355,6 +364,27 @@ |
356 | 365 | }, |
357 | 366 | |
358 | 367 | /* |
| 368 | + * Prepares a message or e-mail body by replacing placeholders. |
| 369 | + * $1: message entered by the user |
| 370 | + * $2: username of the recipient |
| 371 | + * $3: title of the chosen image |
| 372 | + */ |
| 373 | + prepareMsg: function( msg ) { |
| 374 | + // replace the raw message |
| 375 | + msg = msg.replace( '$1', $( '#wlMessage' ).val() ); |
| 376 | + |
| 377 | + // replace the username we're sending to |
| 378 | + msg = msg.replace( '$2', wgTitle ); |
| 379 | + |
| 380 | + // replace the image filename |
| 381 | + if ( $.wikiLove.imageTitle ) { |
| 382 | + msg = msg.replace( '$3', $.wikiLove.imageTitle ); |
| 383 | + } |
| 384 | + |
| 385 | + return msg; |
| 386 | + }, |
| 387 | + |
| 388 | + /* |
359 | 389 | * Fires AJAX request for previewing wikitext. |
360 | 390 | */ |
361 | 391 | doPreview: function( wikitext ) { |
— | — | @@ -421,7 +451,10 @@ |
422 | 452 | mw.log( 'wgPageName: ' + mw.config.get( 'wgPageName' ) ); |
423 | 453 | |
424 | 454 | if ( notify && $.wikiLove.emailable ) { |
425 | | - $.wikiLove.sendEmail( $.wikiLove.currentTypeOrSubtype.title, $.wikiLove.currentTypeOrSubtype.message ); |
| 455 | + $.wikiLove.sendEmail( |
| 456 | + $.wikiLove.currentTypeOrSubtype.title, |
| 457 | + $.wikiLove.prepareMsg( $.wikiLove.currentTypeOrSubtype.mail ) |
| 458 | + ); |
426 | 459 | } |
427 | 460 | |
428 | 461 | $( '#wlPreview .wlSpinner' ).fadeOut( 200 ); |
— | — | @@ -445,11 +478,17 @@ |
446 | 479 | }); |
447 | 480 | }, |
448 | 481 | |
| 482 | + /* |
| 483 | + * This is a bit of a hack to show some random images. A predefined set of image infos are |
| 484 | + * retrieved using the API. Then we randomise this set ourselves and select some images to |
| 485 | + * show. Eventually we probably want to make a custom API call that does this properly and |
| 486 | + * also allows for using remote galleries such as Commons, which is now prohibited by JS. |
| 487 | + */ |
449 | 488 | makeGallery: function() { |
450 | 489 | $( '#wlGallery' ).html( '' ); |
451 | 490 | $.wikiLove.gallery = {}; |
452 | 491 | $.ajax({ |
453 | | - url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?', |
| 492 | + url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php', |
454 | 493 | data: { |
455 | 494 | 'action' : 'query', |
456 | 495 | 'format' : 'json', |
— | — | @@ -465,19 +504,35 @@ |
466 | 505 | dataType: 'json', |
467 | 506 | type: 'POST', |
468 | 507 | success: function( data ) { |
| 508 | + // clear |
469 | 509 | $( '#wlGallery' ).html( '' ); |
470 | 510 | $.wikiLove.gallery = {}; |
| 511 | + |
| 512 | + // if we have any images at all |
471 | 513 | if( data.query) { |
| 514 | + // get the page keys which are just ids |
472 | 515 | var keys = Object.keys( data.query.pages ); |
| 516 | + |
| 517 | + // try to find "num" images to show |
473 | 518 | for( var i=0; i<$.wikiLove.currentTypeOrSubtype.gallery.num; i++ ) { |
| 519 | + // continue looking for a new image until we have found one thats valid |
| 520 | + // or until we run out of images |
474 | 521 | while( keys.length > 0 ) { |
| 522 | + // get a random page |
475 | 523 | var id = Math.floor( Math.random() * keys.length ); |
476 | 524 | var page = data.query.pages[keys[id]]; |
| 525 | + |
| 526 | + // remove the random page from the keys array |
477 | 527 | keys.splice(id, 1); |
| 528 | + |
| 529 | + // only add the image if it's actually an image |
478 | 530 | if( page.imageinfo[0].mime.substr(0,5) == 'image' ) { |
| 531 | + // build an image tag with the correct url and width |
479 | 532 | $img = $( '<img/>' ); |
480 | 533 | $img.attr( 'src', page.imageinfo[0].url ); |
481 | 534 | $img.attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width ); |
| 535 | + |
| 536 | + // append the image to the gallery and also make sure it's selectable |
482 | 537 | $( '#wlGallery' ).append( |
483 | 538 | $( '<a href="#"></a>' ) |
484 | 539 | .attr( 'id', 'wlGalleryImg' + i ) |
— | — | @@ -490,13 +545,15 @@ |
491 | 546 | return false; |
492 | 547 | }) |
493 | 548 | ); |
| 549 | + |
| 550 | + // save the page title into an array so we know which image id maps to which title |
494 | 551 | $.wikiLove.gallery['wlGalleryImg' + i] = page.title; |
495 | 552 | break; |
496 | 553 | } |
497 | 554 | } |
498 | 555 | } |
499 | 556 | } |
500 | | - if( $( '#wlGallery' ).html().length <= 0 ) { |
| 557 | + if( $.wikiLove.gallery.length <= 0 ) { |
501 | 558 | $( '#wlGallery' ).hide(); |
502 | 559 | $( '#wlGalleryTitle' ).hide(); |
503 | 560 | } |