JGonera has submitted this change and it was merged.

Change subject: Story 436: Use file name for images with descriptions with 
templates
......................................................................


Story 436: Use file name for images with descriptions with templates

Modified juliusz's original patchset rather than abandoning it
since we now want a slight variation of this

Renders description as filename
Minus File: prefix, date and extension.

Now avoids unnecessary api call

Also addresses:
Bug: 45579

Change-Id: Ie1fc7f0591b6312fbdd45d9889895d74b25ded02
---
M javascripts/specials/uploads.js
M tests/javascripts/specials/test_uploads.js
2 files changed, 36 insertions(+), 61 deletions(-)

Approvals:
  JGonera: Verified; Looks good to me, approved
  jenkins-bot: Checked



diff --git a/javascripts/specials/uploads.js b/javascripts/specials/uploads.js
index 612e88a..a4ee232 100644
--- a/javascripts/specials/uploads.js
+++ b/javascripts/specials/uploads.js
@@ -1,5 +1,5 @@
 ( function( M, $ ) {
-var api = M.require( 'api' ),
+var
        photo = M.require( 'photo' ),
        popup = M.require( 'notifications' ),
        View = M.require( 'view' ),
@@ -41,52 +41,29 @@
                } ),
                userGallery;
 
+       /**
+        * Returns a description based on the file name using
+        * a regular expression that strips the file type suffix,
+        * namespace prefix and any
+        * date suffix in format YYYY-MM-DD HH-MM
+        * @param {string} title Title of file
+        * @return {string} Description for file
+        */
+       function getDescription( title ) {
+               title = title.replace( /\.[^\. ]+$/, '' ); // replace filename 
suffix
+               // strip namespace: prefix and date suffix from remainder
+               return title.replace( /^[^:]*:/, '').replace( / 
\d{4}-\d{2}-\d{2} \d{2}-\d{2}$/, '' );
+       }
+
        function getImageDataFromPage( page ) {
                var img = page.imageinfo[0];
                return {
                        url: img.thumburl,
                        title: page.title,
                        timestamp: img.timestamp,
+                       description: getDescription( page.title ),
                        descriptionUrl: img.descriptionurl
                };
-       }
-
-       function extractDescription( text ) {
-               var index, summary = '';
-               // FIXME: assumes wikimedia commons - this should be 
customisable
-               index = text.indexOf( '== {{int:filedesc}} ==' );
-               if ( index > - 1 ) {
-                       summary = $.trim( text.substr( index ).split( '==' )[ 2 
] );
-               }
-               return summary;
-       }
-       function appendDescriptions( imageData, callback ) {
-               var options,
-                       data, titles = $.map( imageData, function( i ) {
-                               return i.title;
-                       } );
-
-               data = {
-                       action: 'query',
-                       titles: titles,
-                       origin: corsUrl ? M.getOrigin() : undefined,
-                       prop: 'revisions',
-                       rvprop: 'content'
-               };
-
-               if ( corsUrl ) {
-                       options = { url: corsUrl };
-               }
-               api.ajax( data, options ).done( function( resp ) {
-                       var pages = $.map( resp.query.pages, function ( v ) {
-                               return v;
-                       } );
-                       $( pages ).each( function() {
-                               imageData[ this.title ].description = 
extractDescription( this.revisions[0]['*'] ) ||
-                                       mw.msg( 
'mobile-frontend-listed-image-no-description' );
-                       } );
-                       callback( imageData );
-               } );
        }
 
        function showGallery( username ) {
@@ -111,25 +88,19 @@
                                'withCredentials': true
                        }
                } ).done( function( resp ) {
-                       var pages = [], data = {};
+                       var pages = [];
 
                        if ( resp.query && resp.query.pages ) {
                                pages = resp.query.pages;
-                               $.each( pages, function () {
-                                       data[ this.title ] = 
getImageDataFromPage( this );
+                               pages = $.map( pages, function ( p ) {
+                                       return getImageDataFromPage( p );
                                } );
-                               appendDescriptions( data, function( imageData ) 
{
-                                       var fileArray = [];
-                                       // FIXME: API work around - in an ideal 
world imageData would be an array
-                                       $.each( imageData, function() {
-                                               fileArray.push( this );
-                                       } );
-                                       fileArray = fileArray.sort( function( 
a, b ) {
-                                               return a.timestamp > 
b.timestamp ? 1 : -1;
-                                       } );
-                                       $.each( fileArray, function() {
-                                               userGallery.addPhoto( this );
-                                       } );
+                               // FIXME: API work around - in an ideal world 
imageData would be an array
+                               pages = pages.sort( function( a, b ) {
+                                       return a.timestamp > b.timestamp ? 1 : 
-1;
+                               } );
+                               $.each( pages, function() {
+                                       userGallery.addPhoto( this );
                                } );
                        }
 
@@ -188,7 +159,7 @@
        }
 
        return {
-               extractDescription: extractDescription,
+               getDescription: getDescription,
                init: init
        };
 }() );
diff --git a/tests/javascripts/specials/test_uploads.js 
b/tests/javascripts/specials/test_uploads.js
index 52031e2..f172d07 100644
--- a/tests/javascripts/specials/test_uploads.js
+++ b/tests/javascripts/specials/test_uploads.js
@@ -3,16 +3,20 @@
 var m = M.require( 'userGallery' );
 QUnit.module( 'MobileFrontend donate image' );
 
-QUnit.test( 'extractDescription', function() {
+QUnit.test( 'getDescription', function() {
        var tests = [
-                       [ '== {{int:filedesc}} ==\nHello world', 'Hello world' 
],
-                       [ '==Foo 1==\nbar 1\n==Foo 2==\nbar 2\n== 
{{int:filedesc}} ==\npicture of cat\n', 'picture of cat' ],
-                       [ '==Foo 1==\nbar 1\n== {{int:filedesc}} ==\npicture of 
dog\n==Foo 2==\nbar 2\n', 'picture of dog' ],
-                       [ '== Yo ==\nother text', '' ]
+                       [ 'File:Pirates in SF 2013-04-03 15-44.png', 'Pirates 
in SF' ],
+                       [ 'File:Jon lies next to volcano 2013-03-18 
13-37.jpeg', 'Jon lies next to volcano' ],
+                       [ 'hello world 37.jpg', 'hello world 37' ],
+                       [ 'hello world again.jpeg', 'hello world again' ],
+                       [ 'Fichier:French Photo Timestamp 2013-04-03 
15-44.jpg', 'French Photo Timestamp' ],
+                       [ 'Fichier:Full stop. Photo.unknownfileextension', 
'Full stop. Photo' ],
+                       [ 'File:No file extension but has a . in the title', 
'No file extension but has a . in the title' ],
+                       [ 'Fichier:French Photo.jpg', 'French Photo' ]
                ];
        QUnit.expect( tests.length );
        $( tests ).each( function( i ) {
-               var val = m.extractDescription( this[0] );
+               var val = m.getDescription( this[0] );
                strictEqual( val, this[1], 'test ' + i );
        } );
 } );

-- 
To view, visit https://gerrit.wikimedia.org/r/56613
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ie1fc7f0591b6312fbdd45d9889895d74b25ded02
Gerrit-PatchSet: 9
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: JGonera <[email protected]>
Gerrit-Reviewer: Brion VIBBER <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: awjrichards <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to