r90930 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90929‎ | r90930 | r90931 >
Date:02:08, 28 June 2011
Author:kaldari
Status:ok
Tags:
Comment:
have WikiLove check to make sure that user entered image titles actually exist on Commons
Modified paths:
  • /trunk/extensions/WikiLove/WikiLove.i18n.php (modified) (history)
  • /trunk/extensions/WikiLove/WikiLove.php (modified) (history)
  • /trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js (modified) (history)

Diff [purge]

Index: trunk/extensions/WikiLove/WikiLove.php
@@ -118,6 +118,8 @@
119119 'wikilove-err-title',
120120 'wikilove-err-msg',
121121 'wikilove-err-image',
 122+ 'wikilove-err-image-bad',
 123+ 'wikilove-err-image-api',
122124 'wikilove-err-sig',
123125 'wikilove-err-gallery',
124126 'wikilove-err-gallery-again',
Index: trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js
@@ -126,7 +126,7 @@
127127 $( '#mw-wikilove-gallery-error-again' ).click( $.wikiLove.showGallery );
128128 $( '#mw-wikilove-types a' ).click( $.wikiLove.clickType );
129129 $( '#mw-wikilove-subtype' ).change( $.wikiLove.changeSubtype );
130 - $( '#mw-wikilove-preview-form' ).submit( $.wikiLove.submitPreview );
 130+ $( '#mw-wikilove-preview-form' ).submit( $.wikiLove.validatePreviewForm );
131131 $( '#mw-wikilove-send-form' ).click( $.wikiLove.submitSend );
132132 $( '#mw-wikilove-message' ).elastic(); // have the message textarea grow automatically
133133 }
@@ -262,21 +262,13 @@
263263 },
264264
265265 /*
266 - * Handler for clicking the preview button. Builds data for AJAX request.
 266+ * Handler for clicking the preview button.
267267 */
268 - submitPreview: function( e ) {
 268+ validatePreviewForm: function( e ) {
269269 e.preventDefault();
270270 $( '#mw-wikilove-preview' ).hide();
271271 $( '#mw-wikilove-dialog' ).find( '.mw-wikilove-error' ).remove();
272272
273 -
274 - if ( $( '#mw-wikilove-image' ).val().length <= 0 ) {
275 - if( typeof currentTypeOrSubtype.gallery == 'object' ) {
276 - $.wikiLove.showError( 'wikilove-err-image' ); return false;
277 - } else {
278 - $( '#mw-wikilove-image' ).val( options.defaultImage );
279 - }
280 - }
281273 if( $( '#mw-wikilove-header' ).val().length <= 0 ) {
282274 $.wikiLove.showError( 'wikilove-err-header' ); return false;
283275 }
@@ -290,6 +282,53 @@
291283 $.wikiLove.showError( 'wikilove-err-sig' ); return false;
292284 }
293285
 286+ // Split image validation depending on whether or not it is a gallery
 287+ if ( typeof currentTypeOrSubtype.gallery == 'undefined' ) { // not a gallery
 288+ if ( $( '#mw-wikilove-image' ).val().length <= 0 ) {
 289+ // Give them the default image and continue with preview.
 290+ $( '#mw-wikilove-image' ).val( options.defaultImage );
 291+ $.wikiLove.submitPreview();
 292+ } else {
 293+ // Make sure the image exists
 294+ var imageTitle = $.wikiLove.addFilePrefix( $( '#mw-wikilove-image' ).val() );
 295+ $.ajax( {
 296+ url: mw.util.wikiScript( 'api' ),
 297+ data: {
 298+ 'action': 'query',
 299+ 'format': 'json',
 300+ 'titles': imageTitle,
 301+ 'prop': 'imageinfo'
 302+ },
 303+ dataType: 'json',
 304+ success: function( data ) {
 305+ if ( !data.query.pages[-1].imageinfo ) {
 306+ // Image does not exist
 307+ $.wikiLove.showError( 'wikilove-err-image-bad' );
 308+ } else {
 309+ // Image exists. Proceed with preview.
 310+ $.wikiLove.submitPreview();
 311+ }
 312+ },
 313+ error: function( xhr ) {
 314+ $.wikiLove.showError( 'wikilove-err-image-api' );
 315+ }
 316+ } );
 317+ }
 318+ } else { // a gallery
 319+ if ( $( '#mw-wikilove-image' ).val().length <= 0 ) {
 320+ // Display an error telling them to select an image.
 321+ $.wikiLove.showError( 'wikilove-err-image' ); return false;
 322+ } else {
 323+ // Proceed with preview.
 324+ $.wikiLove.submitPreview();
 325+ }
 326+ }
 327+ },
 328+
 329+ /*
 330+ * After the form is validated, perform preview, and build data for the final AJAX request.
 331+ */
 332+ submitPreview: function() {
294333 var text = $.wikiLove.prepareMsg( currentTypeOrSubtype.text || options.defaultText );
295334
296335 $.wikiLove.doPreview( '==' + $( '#mw-wikilove-header' ).val() + "==\n" + text );
@@ -376,7 +415,7 @@
377416 },
378417
379418 /*
380 - * Handler for the send (final submit) button. Builds data for AJAX request.
 419+ * Handler for the send (final submit) button.
381420 * The type sent for statistics is 'typeId-subtypeId' when using subtypes,
382421 * or simply 'typeId' otherwise.
383422 */
Index: trunk/extensions/WikiLove/WikiLove.i18n.php
@@ -39,6 +39,8 @@
4040 'wikilove-err-title' => 'Please enter a title.',
4141 'wikilove-err-msg' => 'Please enter a message.',
4242 'wikilove-err-image' => 'Please select an image.',
 43+ 'wikilove-err-image-bad' => 'Image does not exist.',
 44+ 'wikilove-err-image-api' => 'Image check failed.',
4345 'wikilove-err-sig' => 'Please do not include a signature in the message.',
4446 'wikilove-err-gallery' => 'Something went wrong when loading the images!',
4547 'wikilove-err-gallery-again' => 'Try again',

Status & tagging log