r88175 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88174‎ | r88175 | r88176 >
Date:13:20, 15 May 2011
Author:janpaul123
Status:ok
Tags:
Comment:
Documentation, cleanup
Modified paths:
  • /trunk/extensions/WikiLove/wikiLove.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiLove/wikiLove.js
@@ -12,14 +12,15 @@
1313 descr: 'Original barnstar', // description in the menu
1414 text: '{{subst:The Original Barnstar|$1 ~~~~}}', // message text, $1 is replaced by the user message
1515 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
1718 },
1819 'special': {
1920 title: null, // no predefined title, allows the user to enter a title
2021 descr: 'Special barnstar',
2122 text: '{{subst:The Special Barnstarl|$1 ~~~~}}',
2223 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.'
2425 }
2526 },
2627 email: true, // add email notices as an option for each award of this type
@@ -28,13 +29,14 @@
2930 'cat': {
3031 descr: 'Cat',
3132 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
3334 template: '',
3435 gallery: {
 36+ // right now we can only query the local wiki (not e.g. commons)
3537 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)
3941 }
4042 },
4143 // default type, nice to leave this one in place when adding other types
@@ -63,8 +65,10 @@
6466 // Load local configuration
6567 var wikiLoveConfigUrl = wgServer + wgScript + '?' + $.param( { 'title': 'MediaWiki:WikiLove.js', 'action': 'raw', 'ctype': 'text/javascript' } );
6668 mw.loader.load( wikiLoveConfigUrl );
 69+
6770 // Find out if we can email the user
6871 $.wikiLove.getEmailable();
 72+
6973 // Build a type list like this:
7074 // <ul id="wlTypes">
7175 // <li tabindex="0"><span>Barnstar</span></li>
@@ -257,11 +261,17 @@
258262 }
259263 },
260264
 265+ /*
 266+ * Find out whether we can e-mail this user. Probably needs to be moved to the API.
 267+ */
261268 getEmailable: function() {
262269 // Test to see if the 'E-mail this user' link exists
263270 $.wikiLove.emailable = $('#t-emailuser').length ? true : false;
264271 },
265272
 273+ /*
 274+ * Actually send the notification e-mail. Probably needs to be moved to the API.
 275+ */
266276 sendEmail: function( subject, text ) {
267277 $.ajax({
268278 url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?',
@@ -322,25 +332,24 @@
323333 if( $( '#wlMessage' ).val().length <= 0 ) {
324334 $.wikiLove.showError( 'wikilove-err-msg' ); return false;
325335 }
 336+ if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) {
 337+ if ( !$.wikiLove.imageTitle ) {
 338+ $.wikiLove.showError( 'wikilove-err-img' ); return false;
 339+ }
 340+ }
326341
327 - var title = '==' + $( '#wlTitle' ).val() + "==\n";
328342 var rawMessage = $( '#wlMessage' ).val();
 343+
329344 // If there isn't a signature already in the message, throw an error
330345 if ( rawMessage.indexOf( '~~~' ) >= 0 ) {
331346 $.wikiLove.showError( 'wikilove-err-sig' ); return false;
332347 }
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 );
343352 $.wikiLove.previewData = {
344 - 'title': title,
 353+ 'title': $( '#wlTitle' ).val(),
345354 'msg': msg,
346355 'type': $.wikiLove.currentTypeId
347356 + ($.wikiLove.currentSubtypeId != null ? '-' + $.wikiLove.currentSubtypeId : ''),
@@ -355,6 +364,27 @@
356365 },
357366
358367 /*
 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+ /*
359389 * Fires AJAX request for previewing wikitext.
360390 */
361391 doPreview: function( wikitext ) {
@@ -421,7 +451,10 @@
422452 mw.log( 'wgPageName: ' + mw.config.get( 'wgPageName' ) );
423453
424454 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+ );
426459 }
427460
428461 $( '#wlPreview .wlSpinner' ).fadeOut( 200 );
@@ -445,11 +478,17 @@
446479 });
447480 },
448481
 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+ */
449488 makeGallery: function() {
450489 $( '#wlGallery' ).html( '' );
451490 $.wikiLove.gallery = {};
452491 $.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',
454493 data: {
455494 'action' : 'query',
456495 'format' : 'json',
@@ -465,19 +504,35 @@
466505 dataType: 'json',
467506 type: 'POST',
468507 success: function( data ) {
 508+ // clear
469509 $( '#wlGallery' ).html( '' );
470510 $.wikiLove.gallery = {};
 511+
 512+ // if we have any images at all
471513 if( data.query) {
 514+ // get the page keys which are just ids
472515 var keys = Object.keys( data.query.pages );
 516+
 517+ // try to find "num" images to show
473518 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
474521 while( keys.length > 0 ) {
 522+ // get a random page
475523 var id = Math.floor( Math.random() * keys.length );
476524 var page = data.query.pages[keys[id]];
 525+
 526+ // remove the random page from the keys array
477527 keys.splice(id, 1);
 528+
 529+ // only add the image if it's actually an image
478530 if( page.imageinfo[0].mime.substr(0,5) == 'image' ) {
 531+ // build an image tag with the correct url and width
479532 $img = $( '<img/>' );
480533 $img.attr( 'src', page.imageinfo[0].url );
481534 $img.attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width );
 535+
 536+ // append the image to the gallery and also make sure it's selectable
482537 $( '#wlGallery' ).append(
483538 $( '<a href="#"></a>' )
484539 .attr( 'id', 'wlGalleryImg' + i )
@@ -490,13 +545,15 @@
491546 return false;
492547 })
493548 );
 549+
 550+ // save the page title into an array so we know which image id maps to which title
494551 $.wikiLove.gallery['wlGalleryImg' + i] = page.title;
495552 break;
496553 }
497554 }
498555 }
499556 }
500 - if( $( '#wlGallery' ).html().length <= 0 ) {
 557+ if( $.wikiLove.gallery.length <= 0 ) {
501558 $( '#wlGallery' ).hide();
502559 $( '#wlGalleryTitle' ).hide();
503560 }

Status & tagging log