r88140 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r88139‎ | r88140 | r88141 >
Date:10:16, 15 May 2011
Author:janpaul123
Status:ok (Comments)
Tags:
Comment:
* Basic gallery functionality
* Basic error messages

This still needs major cleanup but I wanted to commit this huge thing first.
Modified paths:
  • /trunk/extensions/WikiLove/README (modified) (history)
  • /trunk/extensions/WikiLove/WikiLove.i18n.php (modified) (history)
  • /trunk/extensions/WikiLove/WikiLove.php (modified) (history)
  • /trunk/extensions/WikiLove/wikiLove.css (modified) (history)
  • /trunk/extensions/WikiLove/wikiLove.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiLove/WikiLove.php
@@ -88,6 +88,7 @@
8989 'wikilove-get-started-list-2',
9090 'wikilove-get-started-list-3',
9191 'wikilove-add-details',
 92+ 'wikilove-gallery',
9293 'wikilove-title',
9394 'wikilove-enter-message',
9495 'wikilove-omit-sig',
@@ -95,6 +96,10 @@
9697 'wikilove-preview',
9798 'wikilove-button-send',
9899 'wikilove-type-makeyourown',
 100+ 'wikilove-err-title',
 101+ 'wikilove-err-msg',
 102+ 'wikilove-err-image',
 103+ 'wikilove-err-sig',
99104 ),
100105 'dependencies' => array(
101106 'jquery.ui.dialog',
Index: trunk/extensions/WikiLove/wikiLove.css
@@ -254,23 +254,45 @@
255255 resize: none;
256256 }
257257
 258+#wikiLoveDialog #wlAddDetails #wlNotify {
 259+ float: left;
 260+}
 261+
258262 #wikiLoveDialog .wlSpinner {
259263 float: right;
260264 margin-top: 6px;
261265 display: none;
262266 }
263267
 268+/* add details gallery */
 269+#wikiLoveDialog #wlGallery img {
 270+ margin-right: 5px;
 271+ margin-bottom: 5px;
 272+ padding: 5px;
 273+}
 274+
 275+#wikiLoveDialog #wlGallery a:hover img,
 276+#wikiLoveDialog #wlGallery a:focus img {
 277+ background-color: #aacfe9;
 278+}
 279+
 280+#wikiLoveDialog #wlGallery a.selected img {
 281+ background-color: #f56427;
 282+}
 283+
264284 /* dialog preview */
265285 #wikiLoveDialog #wlPreview {
266286 float: right;
267287 width: 480px;
268288 }
269289
270 -#wikiLoveDialog #wlPreview div {
 290+#wikiLoveDialog #wlPreview #wlPreviewArea {
271291 margin-left: 15px;
 292+ width: 465px;
 293+ overflow: auto;
272294 }
273295
274 -#wikiLoveDialog #wlPreview .editsection {
 296+#wikiLoveDialog #wlPreview #wlPreviewArea .editsection {
275297 display: none;
276298 }
277299
Index: trunk/extensions/WikiLove/wikiLove.js
@@ -10,14 +10,14 @@
1111 'original': {
1212 title: 'An Original Barnstar for you!', // subject title for the message
1313 descr: 'Original barnstar', // description in the menu
14 - text: '{{subst:The Original Barnstar|$1}}', // message text, $1 is replaced by the user message
 14+ 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
1616 message: 'Hello '+wgTitle+'!\n\nI just awarded you a barnstar.' // message to use in email notification
1717 },
1818 'special': {
1919 title: null, // no predefined title, allows the user to enter a title
2020 descr: 'Special barnstar',
21 - text: '{{subst:The Special Barnstarl|$1}}',
 21+ text: '{{subst:The Special Barnstarl|$1 ~~~~}}',
2222 template: 'The Special Barnstar',
2323 message: 'Hello '+wgTitle+'!\n\nI just awarded you the special barnstar.'
2424 }
@@ -25,6 +25,18 @@
2626 email: true, // add email notices as an option for each award of this type
2727 icon: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/extensions/WikiLove/images/icons/wikilove-icon-barnstar.png'
2828 },
 29+ 'cat': {
 30+ descr: 'Cat',
 31+ title: null,
 32+ text: "[[$2|left|150px]]\n$1\n\n~~~~",
 33+ template: '',
 34+ gallery: {
 35+ category: 'Category:Tropical',
 36+ total: 100,
 37+ num: 3,
 38+ width: 150
 39+ }
 40+ },
2941 // default type, nice to leave this one in place when adding other types
3042 'makeyourown': {
3143 title: null,
@@ -40,6 +52,8 @@
4153 currentTypeOrSubtype: null, // content of the current (sub)type (i.e. an object with title, descr, text, etc.)
4254 previewData: null, // data of the currently previewed thing is set here
4355 emailable: false,
 56+ gallery: {},
 57+ imageTitle: '',
4458
4559 /*
4660 * Opens the dialog and builds it if necessary.
@@ -115,7 +129,9 @@
116130 .append( '<h3>' + mw.msg( 'wikilove-add-details' ) + '</h3>' )
117131 .append( '<label for="wlSubtype" id="wlSubtypeLabel"></label>' )
118132 .append( $( '<form id="wlPreviewForm"></form>' )
119 - .append( '<select id="wlSubtype"></select>' )
 133+ .append( '<select id="wlSubtype"></select>' )
 134+ .append( '<label id="wlGalleryLabel">' + mw.msg( 'wikilove-gallery' ) + '</label>' )
 135+ .append( '<div id="wlGallery"/>' )
120136 .append( '<label for="wlTitle" id="wlTitleLabel">' + mw.msg( 'wikilove-title' ) + '</label>' )
121137 .append( '<input type="text" class="text" id="wlTitle"/>' )
122138 .append( '<label for="wlMessage" id="wlMessageLabel">' + mw.msg( 'wikilove-enter-message' ) + '</label>' )
@@ -273,6 +289,17 @@
274290 $( '#wlTitle' ).show();
275291 $( '#wlTitle' ).val( '' );
276292 }
 293+
 294+ if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) {
 295+ $( '#wlGalleryLabel' ).show();
 296+ $( '#wlGallery' ).show();
 297+ $.wikiLove.makeGallery();
 298+ }
 299+ else {
 300+ $( '#wlGalleryLabel' ).hide();
 301+ $( '#wlGallery' ).hide();
 302+ }
 303+
277304 if( $.wikiLove.types[$.wikiLove.currentTypeId].email ) {
278305 $( '#wlNotify' ).show();
279306 } else {
@@ -286,13 +313,28 @@
287314 */
288315 submitPreview: function( e ) {
289316 e.preventDefault();
 317+ if( $( '#wlTitle' ).val().length <= 0 ) {
 318+ $.wikiLove.showError( 'wikilove-err-title' ); return false;
 319+ }
 320+ if( $( '#wlMessage' ).val().length <= 0 ) {
 321+ $.wikiLove.showError( 'wikilove-err-msg' ); return false;
 322+ }
 323+
290324 var title = '==' + $( '#wlTitle' ).val() + "==\n";
291325 var rawMessage = $( '#wlMessage' ).val();
292 - // If there isn't a signature already in the message, add one to the end.
293 - if ( rawMessage.indexOf( '~~~' ) == -1 ) {
294 - rawMessage += ' ~~~~';
 326+ // If there isn't a signature already in the message, throw an error
 327+ if ( rawMessage.indexOf( '~~~' ) >= 0 ) {
 328+ $.wikiLove.showError( 'wikilove-err-sig' ); return false;
295329 }
296330 var msg = $.wikiLove.currentTypeOrSubtype.text.replace( '$1', rawMessage );
 331+ if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) {
 332+ if ( $.wikiLove.imageTitle ) {
 333+ msg = msg.replace( '$2', $.wikiLove.imageTitle );
 334+ }
 335+ else {
 336+ $.wikiLove.showError( 'wikilove-err-img' ); return false;
 337+ }
 338+ }
297339 $.wikiLove.doPreview( title + msg );
298340 $.wikiLove.previewData = {
299341 'title': title,
@@ -305,6 +347,10 @@
306348 return false;
307349 },
308350
 351+ showError: function( errmsg ) {
 352+ $.wikiLove.showPreview( mw.msg( errmsg ) );
 353+ },
 354+
309355 /*
310356 * Fires AJAX request for previewing wikitext.
311357 */
@@ -396,6 +442,65 @@
397443 });
398444 },
399445
 446+ makeGallery: function() {
 447+ $( '#wlGallery' ).html( '' );
 448+ $.wikiLove.gallery = {};
 449+ $.ajax({
 450+ url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?',
 451+ data: {
 452+ 'action' : 'query',
 453+ 'format' : 'json',
 454+ 'prop' : 'imageinfo',
 455+ 'iiprop' : 'mime|url',
 456+ 'iiurlwidth' : $.wikiLove.currentTypeOrSubtype.gallery.width,
 457+ 'generator' : 'categorymembers',
 458+ 'gcmtitle' : $.wikiLove.currentTypeOrSubtype.gallery.category,
 459+ 'gcmnamespace': 6,
 460+ 'gcmsort' : 'timestamp',
 461+ 'gcmlimit' : $.wikiLove.currentTypeOrSubtype.gallery.total
 462+ },
 463+ dataType: 'json',
 464+ type: 'POST',
 465+ success: function( data ) {
 466+ $( '#wlGallery' ).html( '' );
 467+ $.wikiLove.gallery = {};
 468+ if( data.query) {
 469+ var keys = Object.keys( data.query.pages );
 470+ for( var i=0; i<$.wikiLove.currentTypeOrSubtype.gallery.num; i++ ) {
 471+ while( keys.length > 0 ) {
 472+ var id = Math.floor( Math.random() * keys.length );
 473+ var page = data.query.pages[keys[id]];
 474+ keys.splice(id, 1);
 475+ if( page.imageinfo[0].mime.substr(0,5) == 'image' ) {
 476+ $img = $( '<img/>' );
 477+ $img.attr( 'src', page.imageinfo[0].url );
 478+ $img.attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width );
 479+ $( '#wlGallery' ).append(
 480+ $( '<a href="#"></a>' )
 481+ .attr( 'id', 'wlGalleryImg' + i )
 482+ .append( $img )
 483+ .click( function( e ) {
 484+ e.preventDefault();
 485+ $( '#wlGallery a' ).removeClass( 'selected' );
 486+ $( this ).addClass( 'selected' );
 487+ $.wikiLove.imageTitle = $.wikiLove.gallery[$( this ).attr( 'id' )];
 488+ return false;
 489+ })
 490+ );
 491+ $.wikiLove.gallery['wlGalleryImg' + i] = page.title;
 492+ break;
 493+ }
 494+ }
 495+ }
 496+ }
 497+ if( $( '#wlGallery' ).html().length <= 0 ) {
 498+ $( '#wlGallery' ).hide();
 499+ $( '#wlGalleryTitle' ).hide();
 500+ }
 501+ }
 502+ });
 503+ },
 504+
400505 /*
401506 * Init function which is called upon page load. Binds the WikiLove icon to opening the dialog.
402507 */
Index: trunk/extensions/WikiLove/README
@@ -1,5 +1,5 @@
2 -WikiLove is an extension designed to promote the spread of WikiLove within
3 -Wikipedia.
 2+WikiLove is an extension designed to promote the spread of WikiLove,
 3+message of positive reinforcement that users can send to each other.
44
55 See documentation at the MediaWiki wiki:
66 http://www.mediawiki.org/wiki/Extension:WikiLove
Index: trunk/extensions/WikiLove/WikiLove.i18n.php
@@ -24,6 +24,7 @@
2525 'wikilove-get-started-list-2' => 'Add details to your WikiLove',
2626 'wikilove-get-started-list-3' => 'Send your WikiLove!',
2727 'wikilove-add-details' => 'Add details',
 28+ 'wikilove-gallery' => 'Picture:',
2829 'wikilove-title' => 'Title:',
2930 'wikilove-enter-message' => 'Enter a message:',
3031 'wikilove-omit-sig' => '(without a signature)',
@@ -31,5 +32,9 @@
3233 'wikilove-preview' => 'Preview',
3334 'wikilove-button-send' => 'Send WikiLove',
3435 'wikilove-type-makeyourown' => 'Make your own',
 36+ 'wikilove-err-title' => 'Please enter a title.',
 37+ 'wikilove-err-msg' => 'Please enter a message.',
 38+ 'wikilove-err-image' => 'Please select an image.',
 39+ 'wikilove-err-sig' => 'Please do not include a signature in the message.',
3540 );
3641

Comments

#Comment by Krinkle (talk | contribs)   23:22, 31 May 2011

Marking OK.

A little hint, the Message contructor in the mw.js lib supports $(N) parameters natively since a few months now. Documentation isn't great yet, but here's a basic example (look at mediawiki.js)

/**
 * suppose we have a message key "user-welcome" with value
 * "Hello $1. Welcome to $2. Last visit by $1: $3".
 * mw.message(key /* , ... */ )
 * mw.message is something like:
 * var parameters = arguments.shift(); return new Message(msgBlob, key, parameters);
 * not to confuse with mw.msg(), which is like: return mw.message(key).toString()
 */

// Get instance of Message
var welcome = mw.message('user-welcome', 'Krinkle', 'Wikipedia', 'now');

// Get it as a plain string
welcome.format = 'plain';
welcome.toString();

// Shortcut for the above
welcome.plain();

// Returns: "Hello Krinkle. Welcome to Wikipedia. Last visit by Krinkle: now"
#Comment by Krinkle (talk | contribs)   23:24, 31 May 2011

Status & tagging log