2014-03-19 02:00:26 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MediaViewer.
|
|
|
|
*
|
|
|
|
* MediaViewer is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* MediaViewer is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2023-05-19 20:26:45 +00:00
|
|
|
const { HtmlUtils } = require( 'mmv.bootstrap' );
|
|
|
|
|
2018-11-12 16:33:24 +00:00
|
|
|
( function () {
|
2014-03-21 20:29:55 +00:00
|
|
|
var EFFP;
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts data in various formats needed by the Embed sub-dialog
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-19 02:00:26 +00:00
|
|
|
* @class mw.mmv.EmbedFileFormatter
|
|
|
|
* @constructor
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
function EmbedFileFormatter() {
|
2023-05-19 20:26:45 +00:00
|
|
|
/** @property {HtmlUtils} htmlUtils - */
|
|
|
|
this.htmlUtils = new HtmlUtils();
|
2014-03-21 20:29:55 +00:00
|
|
|
}
|
|
|
|
EFFP = EmbedFileFormatter.prototype;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the caption of the image (possibly a fallback generated from image metadata).
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
|
|
|
* @param {string} [info.caption]
|
2014-03-21 20:29:55 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
EFFP.getCaption = function ( info ) {
|
|
|
|
if ( info.caption ) {
|
|
|
|
return this.htmlUtils.htmlToText( info.caption );
|
|
|
|
} else {
|
|
|
|
return info.imageInfo.title.getNameText();
|
|
|
|
}
|
|
|
|
};
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to generate thumbnail wikicode
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-19 02:00:26 +00:00
|
|
|
* @param {mw.Title} title
|
|
|
|
* @param {number} [width]
|
|
|
|
* @param {string} [caption]
|
2014-12-30 05:10:27 +00:00
|
|
|
* @param {string} [alt]
|
2014-03-19 02:00:26 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2014-12-30 05:10:27 +00:00
|
|
|
EFFP.getThumbnailWikitext = function ( title, width, caption, alt ) {
|
|
|
|
var widthSection, captionSection, altSection;
|
2014-03-19 02:00:26 +00:00
|
|
|
|
|
|
|
widthSection = width ? '|' + width + 'px' : '';
|
|
|
|
captionSection = caption ? '|' + caption : '';
|
2014-12-30 05:10:27 +00:00
|
|
|
altSection = alt ? '|alt=' + alt : '';
|
2014-03-19 02:00:26 +00:00
|
|
|
|
2014-12-30 05:10:27 +00:00
|
|
|
return '[[File:' + title.getMainText() + widthSection + '|thumb' + captionSection + altSection + ']]';
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to generate thumbnail wikicode
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
2014-03-19 02:00:26 +00:00
|
|
|
* @param {number} [width]
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EFFP.getThumbnailWikitextFromEmbedFileInfo = function ( info, width ) {
|
2014-12-30 05:10:27 +00:00
|
|
|
return this.getThumbnailWikitext( info.imageInfo.title, width, this.getCaption( info ), info.alt );
|
2014-03-19 02:00:26 +00:00
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
|
|
|
* Byline construction
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2014-03-21 20:29:55 +00:00
|
|
|
* @param {string} [author] author name (can contain HTML)
|
|
|
|
* @param {string} [source] source name (can contain HTML)
|
2014-12-28 21:04:22 +00:00
|
|
|
* @param {string} [attribution] custom attribution line (can contain HTML)
|
2014-06-12 23:59:06 +00:00
|
|
|
* @param {Function} [formatterFunction] Format function for the text - defaults to whitelisting HTML links, but all else sanitized.
|
|
|
|
* @return {string} Byline (can contain HTML)
|
2014-03-19 03:05:36 +00:00
|
|
|
*/
|
2014-12-28 21:04:22 +00:00
|
|
|
EFFP.getByline = function ( author, source, attribution, formatterFunction ) {
|
2014-06-12 23:59:06 +00:00
|
|
|
var formatter = this;
|
2014-03-25 22:18:15 +00:00
|
|
|
|
2014-06-12 23:59:06 +00:00
|
|
|
formatterFunction = formatterFunction || function ( txt ) {
|
|
|
|
return formatter.htmlUtils.htmlToTextWithLinks( txt );
|
|
|
|
};
|
|
|
|
|
2014-12-28 21:04:22 +00:00
|
|
|
if ( attribution ) {
|
|
|
|
attribution = attribution && formatterFunction( attribution );
|
|
|
|
return attribution;
|
2014-03-21 20:29:55 +00:00
|
|
|
} else {
|
2014-12-28 21:04:22 +00:00
|
|
|
author = author && formatterFunction( author );
|
|
|
|
source = source && formatterFunction( source );
|
|
|
|
|
|
|
|
if ( author && source ) {
|
|
|
|
return mw.message(
|
|
|
|
'multimediaviewer-credit',
|
|
|
|
author,
|
|
|
|
source
|
|
|
|
).parse();
|
|
|
|
} else {
|
|
|
|
return author || source;
|
|
|
|
}
|
2014-03-19 03:05:36 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-12 23:59:06 +00:00
|
|
|
/**
|
|
|
|
* Generates the plain text embed code for the image credit line.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
2014-06-12 23:59:06 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
EFFP.getCreditText = function ( info ) {
|
|
|
|
var creditText, creditParams,
|
|
|
|
formatter = this,
|
2015-12-24 20:35:52 +00:00
|
|
|
shortURL = info.imageInfo.descriptionShortUrl,
|
|
|
|
license = info.imageInfo.license,
|
2014-12-28 21:04:22 +00:00
|
|
|
byline = this.getByline( info.imageInfo.author, info.imageInfo.source, info.imageInfo.attribution, function ( txt ) {
|
2014-06-12 23:59:06 +00:00
|
|
|
return formatter.htmlUtils.htmlToText( txt );
|
|
|
|
} );
|
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
// If both the byline and licence are missing, the credit text is simply the URL
|
|
|
|
if ( !byline && !license ) {
|
|
|
|
return shortURL;
|
|
|
|
}
|
|
|
|
|
2014-06-12 23:59:06 +00:00
|
|
|
creditParams = [
|
2015-12-24 20:35:52 +00:00
|
|
|
'multimediaviewer-text-embed-credit-text-'
|
2014-06-12 23:59:06 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
if ( byline ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
creditParams[ 0 ] += 'b';
|
2014-06-12 23:59:06 +00:00
|
|
|
creditParams.push( byline );
|
|
|
|
}
|
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
if ( license ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
creditParams[ 0 ] += 'l';
|
2015-12-24 20:35:52 +00:00
|
|
|
creditParams.push( this.htmlUtils.htmlToText( license.getShortName() ) );
|
2014-11-24 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
creditParams.push( shortURL );
|
2014-06-12 23:59:06 +00:00
|
|
|
creditText = mw.message.apply( mw, creditParams ).plain();
|
|
|
|
|
|
|
|
return creditText;
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
|
|
|
* Generates the HTML embed code for the image credit line.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
2014-03-19 03:05:36 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EFFP.getCreditHtml = function ( info ) {
|
2014-06-12 23:59:06 +00:00
|
|
|
var creditText, creditParams,
|
2015-12-24 20:35:52 +00:00
|
|
|
shortURL = info.imageInfo.descriptionShortUrl,
|
2016-09-04 04:49:58 +00:00
|
|
|
shortLink = this.htmlUtils.makeLinkText( mw.message( 'multimediaviewer-html-embed-credit-link-text' ), { href: shortURL } ),
|
2015-12-24 20:35:52 +00:00
|
|
|
license = info.imageInfo.license,
|
2014-12-28 21:04:22 +00:00
|
|
|
byline = this.getByline( info.imageInfo.author, info.imageInfo.source, info.imageInfo.attribution );
|
2014-03-19 03:05:36 +00:00
|
|
|
|
2015-12-24 20:35:52 +00:00
|
|
|
if ( !byline && !license ) {
|
2016-06-19 15:13:24 +00:00
|
|
|
return shortLink;
|
2015-12-24 20:35:52 +00:00
|
|
|
}
|
|
|
|
|
2014-06-12 23:59:06 +00:00
|
|
|
creditParams = [
|
2015-12-24 20:35:52 +00:00
|
|
|
'multimediaviewer-html-embed-credit-text-'
|
2014-06-12 23:59:06 +00:00
|
|
|
];
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
if ( byline ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
creditParams[ 0 ] += 'b';
|
2014-03-21 20:29:55 +00:00
|
|
|
creditParams.push( byline );
|
2014-03-19 03:05:36 +00:00
|
|
|
}
|
2015-12-24 20:35:52 +00:00
|
|
|
if ( license ) {
|
2016-07-18 13:49:27 +00:00
|
|
|
creditParams[ 0 ] += 'l';
|
2015-12-24 20:35:52 +00:00
|
|
|
creditParams.push( license.getShortLink() );
|
2014-11-24 22:09:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-19 15:13:24 +00:00
|
|
|
creditParams.push( shortLink );
|
2014-03-19 20:14:15 +00:00
|
|
|
creditText = mw.message.apply( mw, creditParams ).plain();
|
2014-03-19 03:05:36 +00:00
|
|
|
|
|
|
|
return creditText;
|
|
|
|
};
|
|
|
|
|
2014-03-21 20:29:55 +00:00
|
|
|
/**
|
|
|
|
* Returns HTML code for a link to the site of the image.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
2014-03-21 20:29:55 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
EFFP.getSiteLink = function ( info ) {
|
|
|
|
var siteName = info.repoInfo.displayName,
|
|
|
|
siteUrl = info.repoInfo.getSiteLink();
|
|
|
|
|
|
|
|
if ( siteUrl ) {
|
|
|
|
return this.htmlUtils.jqueryToHtml(
|
|
|
|
$( '<a>' ).prop( 'href', siteUrl ).text( siteName )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return siteName;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-03-19 03:05:36 +00:00
|
|
|
/**
|
|
|
|
* Generates the HTML embed code for the image.
|
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
2014-03-19 03:05:36 +00:00
|
|
|
* @param {string} imgUrl URL to the file itself.
|
|
|
|
* @param {number} [width] Width to put into the image element.
|
|
|
|
* @param {number} [height] Height to put into the image element.
|
|
|
|
* @return {string} Embed code.
|
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EFFP.getThumbnailHtml = function ( info, imgUrl, width, height ) {
|
|
|
|
return this.htmlUtils.jqueryToHtml(
|
2014-03-19 03:05:36 +00:00
|
|
|
$( '<p>' ).append(
|
|
|
|
$( '<a>' )
|
2014-03-19 20:14:15 +00:00
|
|
|
.attr( 'href', this.getLinkUrl( info ) )
|
2014-03-19 03:05:36 +00:00
|
|
|
.append(
|
|
|
|
$( '<img>' )
|
|
|
|
.attr( 'src', imgUrl )
|
2014-12-30 05:10:27 +00:00
|
|
|
.attr( 'alt', info.alt || info.imageInfo.title.getMainText() )
|
2014-03-19 03:05:36 +00:00
|
|
|
.attr( 'height', height )
|
|
|
|
.attr( 'width', width )
|
|
|
|
),
|
|
|
|
$( '<br>' ),
|
|
|
|
this.getCreditHtml( info )
|
|
|
|
)
|
2014-03-21 20:29:55 +00:00
|
|
|
);
|
2014-03-19 03:05:36 +00:00
|
|
|
};
|
|
|
|
|
2014-03-19 20:14:15 +00:00
|
|
|
/**
|
2014-04-14 23:22:28 +00:00
|
|
|
* Generate a link which we will be using for sharing stuff.
|
2016-07-18 13:49:27 +00:00
|
|
|
*
|
2019-02-05 21:52:48 +00:00
|
|
|
* @param {Object} info
|
|
|
|
* @param {mw.mmv.model.Image} info.imageInfo
|
2016-12-14 13:09:10 +00:00
|
|
|
* @return {string} URL
|
2014-03-19 20:14:15 +00:00
|
|
|
*/
|
2014-03-21 20:29:55 +00:00
|
|
|
EFFP.getLinkUrl = function ( info ) {
|
2019-05-16 13:19:44 +00:00
|
|
|
return info.imageInfo.descriptionUrl + mw.mmv.getMediaHash( info.imageInfo.title );
|
2014-03-19 20:14:15 +00:00
|
|
|
};
|
|
|
|
|
2023-05-19 14:47:34 +00:00
|
|
|
module.exports = EmbedFileFormatter;
|
2018-11-12 16:33:24 +00:00
|
|
|
}() );
|