http://www.mediawiki.org/wiki/Special:Code/MediaWiki/90930

Revision: 90930
Author:   kaldari
Date:     2011-06-28 02:08:52 +0000 (Tue, 28 Jun 2011)
Log Message:
-----------
have WikiLove check to make sure that user entered image titles actually exist 
on Commons

Modified Paths:
--------------
    trunk/extensions/WikiLove/WikiLove.i18n.php
    trunk/extensions/WikiLove/WikiLove.php
    trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js

Modified: trunk/extensions/WikiLove/WikiLove.i18n.php
===================================================================
--- trunk/extensions/WikiLove/WikiLove.i18n.php 2011-06-28 01:17:19 UTC (rev 
90929)
+++ trunk/extensions/WikiLove/WikiLove.i18n.php 2011-06-28 02:08:52 UTC (rev 
90930)
@@ -39,6 +39,8 @@
        'wikilove-err-title' => 'Please enter a title.',
        'wikilove-err-msg' => 'Please enter a message.',
        'wikilove-err-image' => 'Please select an image.',
+       'wikilove-err-image-bad' => 'Image does not exist.',
+       'wikilove-err-image-api' => 'Image check failed.',
        'wikilove-err-sig' => 'Please do not include a signature in the 
message.',
        'wikilove-err-gallery' => 'Something went wrong when loading the 
images!',
        'wikilove-err-gallery-again' => 'Try again',

Modified: trunk/extensions/WikiLove/WikiLove.php
===================================================================
--- trunk/extensions/WikiLove/WikiLove.php      2011-06-28 01:17:19 UTC (rev 
90929)
+++ trunk/extensions/WikiLove/WikiLove.php      2011-06-28 02:08:52 UTC (rev 
90930)
@@ -118,6 +118,8 @@
                        'wikilove-err-title',
                        'wikilove-err-msg',
                        'wikilove-err-image',
+                       'wikilove-err-image-bad',
+                       'wikilove-err-image-api',
                        'wikilove-err-sig',
                        'wikilove-err-gallery',
                        'wikilove-err-gallery-again',

Modified: trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js
===================================================================
--- trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js 
2011-06-28 01:17:19 UTC (rev 90929)
+++ trunk/extensions/WikiLove/modules/ext.wikiLove/ext.wikiLove.core.js 
2011-06-28 02:08:52 UTC (rev 90930)
@@ -126,7 +126,7 @@
                        $( '#mw-wikilove-gallery-error-again' ).click( 
$.wikiLove.showGallery );
                        $( '#mw-wikilove-types a' ).click( $.wikiLove.clickType 
);
                        $( '#mw-wikilove-subtype' ).change( 
$.wikiLove.changeSubtype );
-                       $( '#mw-wikilove-preview-form' ).submit( 
$.wikiLove.submitPreview );
+                       $( '#mw-wikilove-preview-form' ).submit( 
$.wikiLove.validatePreviewForm );
                        $( '#mw-wikilove-send-form' ).click( 
$.wikiLove.submitSend );
                        $( '#mw-wikilove-message' ).elastic(); // have the 
message textarea grow automatically
                }
@@ -262,21 +262,13 @@
        },
        
        /*
-        * Handler for clicking the preview button. Builds data for AJAX 
request.
+        * Handler for clicking the preview button.
         */
-       submitPreview: function( e ) {
+       validatePreviewForm: function( e ) {
                e.preventDefault();
                $( '#mw-wikilove-preview' ).hide();
                $( '#mw-wikilove-dialog' ).find( '.mw-wikilove-error' 
).remove();
                
-               
-               if ( $( '#mw-wikilove-image' ).val().length <= 0 ) {
-                       if( typeof currentTypeOrSubtype.gallery == 'object' ) {
-                               $.wikiLove.showError( 'wikilove-err-image' ); 
return false;
-                       } else {
-                               $( '#mw-wikilove-image' ).val( 
options.defaultImage );
-                       }
-               }
                if( $( '#mw-wikilove-header' ).val().length <= 0 ) {
                        $.wikiLove.showError( 'wikilove-err-header' ); return 
false;
                }
@@ -290,6 +282,53 @@
                        $.wikiLove.showError( 'wikilove-err-sig' ); return 
false;
                }
                
+               // Split image validation depending on whether or not it is a 
gallery
+               if ( typeof currentTypeOrSubtype.gallery == 'undefined' ) { // 
not a gallery
+                       if ( $( '#mw-wikilove-image' ).val().length <= 0 ) {
+                               // Give them the default image and continue 
with preview.
+                               $( '#mw-wikilove-image' ).val( 
options.defaultImage );
+                               $.wikiLove.submitPreview();
+                       } else {
+                               // Make sure the image exists
+                               var imageTitle = $.wikiLove.addFilePrefix( $( 
'#mw-wikilove-image' ).val() );
+                               $.ajax( {
+                                       url: mw.util.wikiScript( 'api' ),
+                                       data: {
+                                               'action': 'query',
+                                               'format': 'json',
+                                               'titles': imageTitle,
+                                               'prop': 'imageinfo'
+                                       },
+                                       dataType: 'json',
+                                       success: function( data ) {
+                                               if ( 
!data.query.pages[-1].imageinfo ) {
+                                                       // Image does not exist
+                                                       $.wikiLove.showError( 
'wikilove-err-image-bad' );
+                                               } else {
+                                                       // Image exists. 
Proceed with preview.
+                                                       
$.wikiLove.submitPreview();
+                                               }
+                                       },
+                                       error: function( xhr ) {
+                                               $.wikiLove.showError( 
'wikilove-err-image-api' );
+                                       }
+                               } );
+                       }
+               } else { // a gallery
+                       if ( $( '#mw-wikilove-image' ).val().length <= 0 ) {
+                               // Display an error telling them to select an 
image.
+                               $.wikiLove.showError( 'wikilove-err-image' ); 
return false;
+                       } else {
+                               // Proceed with preview.
+                               $.wikiLove.submitPreview();
+                       }
+               }
+       },
+       
+       /*
+        * After the form is validated, perform preview, and build data for the 
final AJAX request.
+        */
+       submitPreview: function() {
                var text = $.wikiLove.prepareMsg( currentTypeOrSubtype.text || 
options.defaultText );
                
                $.wikiLove.doPreview( '==' + $( '#mw-wikilove-header' ).val() + 
"==\n" + text );
@@ -376,7 +415,7 @@
        },
        
        /*
-        * Handler for the send (final submit) button. Builds data for AJAX 
request.
+        * Handler for the send (final submit) button.
         * The type sent for statistics is 'typeId-subtypeId' when using 
subtypes,
         * or simply 'typeId' otherwise.
         */


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to