Debian Codegraph @

extensions/MultimediaViewer/resources/mmv.ui.reuse/mmv.EmbedFileFormatter.js

raw symbols SCIP
1 /*
2 * This file is part of the MediaWiki extension MediaViewer.
3 *
4 * MediaViewer is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * MediaViewer is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with MediaViewer. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 const { Config } = require( 'mmv.bootstrap' );
19 const { HtmlUtils } = require( 'mmv' );
20
21 /**
22 * Converts data in various formats needed by the Embed sub-dialog
23 */
24 class EmbedFileFormatter {
25 /**
26 * Helper function to generate thumbnail wikicode
27 *
28 * @param {mw.Title} title
29 * @param {number} [width]
30 * @param {string} [caption]
31 * @param {string} [alt]
32 * @return {string}
33 */
34 getThumbnailWikitext( title, width, caption, alt ) {
35 const widthSection = width ? `|${ width }px` : '';
36 const captionSection = caption ? `|${ caption }` : '';
37 const altSection = alt ? `|alt=${ alt }` : '';
38
39 return `[[${ title.getPrefixedText() }${ widthSection }|thumb${ captionSection }${ altSection }]]`;
40 }
41
42 /**
43 * Helper function to generate thumbnail wikicode
44 *
45 * @param {Object} info
46 * @param {ImageModel} info.imageInfo
47 * @param {number} [width]
48 * @return {string}
49 */
50 getThumbnailWikitextFromEmbedFileInfo( info, width ) {
51 const caption = info.caption ? HtmlUtils.htmlToText( info.caption ) : info.imageInfo.title.getNameText();
52 return this.getThumbnailWikitext( info.imageInfo.title, width, caption, info.alt );
53 }
54
55 /**
56 * Byline construction
57 *
58 * @param {string} [author] author name (can contain HTML)
59 * @param {string} [source] source name (can contain HTML)
60 * @param {string} [attribution] custom attribution line (can contain HTML)
61 * @param {Function} [formatterFunction] Format function for the text - defaults to allowlisting HTML links, but all else sanitized.
62 * @return {string} Byline (can contain HTML)
63 */
64 getByline( author, source, attribution, formatterFunction ) {
65 formatterFunction = formatterFunction || ( ( txt ) => HtmlUtils.htmlToTextWithLinks( txt ) );
66
67 if ( attribution ) {
68 attribution = attribution && formatterFunction( attribution );
69 return attribution;
70 } else {
71 author = author && formatterFunction( author );
72 source = source && formatterFunction( source );
73
74 if ( author && source ) {
75 return mw.msg( 'multimediaviewer-credit', author, source );
76 } else {
77 return author || source;
78 }
79 }
80 }
81
82 /**
83 * Generates the plain text embed code for the image credit line.
84 *
85 * @param {ImageModel} imageInfo
86 * @return {string}
87 */
88 getCreditText( imageInfo ) {
89 const shortURL = imageInfo.descriptionShortUrl;
90 const license = imageInfo.license;
91 const byline = this.getByline(
92 imageInfo.author,
93 imageInfo.source,
94 imageInfo.attribution,
95 ( txt ) => HtmlUtils.htmlToText( txt )
96 );
97
98 // If both the byline and licence are missing, the credit text is simply the URL
99 if ( !byline && !license ) {
100 return shortURL;
101 }
102
103 // The following messages are used here:
104 // * multimediaviewer-text-embed-credit-text-bl
105 // * multimediaviewer-text-embed-credit-text-b
106 // * multimediaviewer-text-embed-credit-text-l
107 const creditParams = [
108 'multimediaviewer-text-embed-credit-text-'
109 ];
110
111 if ( byline ) {
112 creditParams[ 0 ] += 'b';
113 creditParams.push( byline );
114 }
115
116 if ( license ) {
117 creditParams[ 0 ] += 'l';
118 creditParams.push( HtmlUtils.htmlToText( license.getShortName() ) );
119 }
120
121 creditParams.push( shortURL );
122 return mw.message.apply( mw, creditParams ).plain();
123 }
124
125 /**
126 * Generates the HTML embed code for the image credit line.
127 *
128 * @param {ImageModel} imageInfo
129 * @return {string}
130 */
131 getCreditHtml( imageInfo ) {
132 const shortURL = imageInfo.descriptionShortUrl;
133 const shortLink = HtmlUtils.makeLinkText( mw.message( 'multimediaviewer-html-embed-credit-link-text' ), { href: shortURL } );
134 const license = imageInfo.license;
135 const byline = this.getByline( imageInfo.author, imageInfo.source, imageInfo.attribution );
136
137 if ( !byline && !license ) {
138 return shortLink;
139 }
140
141 // The following messages are used here:
142 // * multimediaviewer-html-embed-credit-text-bl
143 // * multimediaviewer-html-embed-credit-text-b
144 // * multimediaviewer-html-embed-credit-text-l
145 const creditParams = [
146 'multimediaviewer-html-embed-credit-text-'
147 ];
148
149 if ( byline ) {
150 creditParams[ 0 ] += 'b';
151 creditParams.push( byline );
152 }
153 if ( license ) {
154 creditParams[ 0 ] += 'l';
155 creditParams.push( license.getShortLink() );
156 }
157
158 creditParams.push( shortLink );
159 return mw.message.apply( mw, creditParams ).plain();
160 }
161
162 /**
163 * Generates the HTML embed code for the image.
164 *
165 * @param {Object} info
166 * @param {ImageModel} info.imageInfo
167 * @param {string} imgUrl URL to the file itself.
168 * @param {number} [width] Width to put into the image element.
169 * @param {number} [height] Height to put into the image element.
170 * @return {string} Embed code.
171 */
172 getThumbnailHtml( info, imgUrl, width, height ) {
173 return HtmlUtils.jqueryToHtml(
174 $( '<p>' ).append(
175 $( '<a>' )
176 .attr( 'href', info.imageInfo.descriptionUrl + Config.getMediaHash( info.imageInfo.title ) )
177 .append(
178 $( '<img>' )
179 .attr( 'src', imgUrl )
180 .attr( 'alt', info.alt || info.imageInfo.title.getMainText() )
181 .attr( 'height', height )
182 .attr( 'width', width )
183 ),
184 $( '<br>' ),
185 this.getCreditHtml( info.imageInfo )
186 )
187 );
188 }
189 }
190
191 module.exports = EmbedFileFormatter;