summaryrefslogtreecommitdiff
path: root/extensions/TimedMediaHandler/resources/mw.MediaWikiPlayerSupport.js
blob: 16ce367c22c4f33b381293c4204074296b9ff82d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
( function ( mw, $ ) {
	/**
	 * Merge in the default video attributes supported by embedPlayer:
	 */
	mw.mergeConfig( 'EmbedPlayer.Attributes', {
		// A apiTitleKey for looking up subtitles, credits and related videos
		'data-mwtitle': null,

		// The apiProvider where to lookup the title key
		'data-mwprovider': null
	} );

	// Add mediaWiki player support to target embedPlayer
	$( mw ).bind( 'EmbedPlayerNewPlayer', function ( event, embedPlayer ) {
		mw.addMediaWikiPlayerSupport( embedPlayer );
	});

	/**
	 * Closure function wraps mediaWiki embedPlayer bindings
	 */
	mw.addMediaWikiPlayerSupport = function ( embedPlayer ) {
		var apiTitleKey, apiProvider, $creditsCache = false;
		// Set some local variables:
		if ( !embedPlayer['data-mwtitle'] ) {
			return false;
		} else {
			apiTitleKey = embedPlayer['data-mwtitle'];
			// legacy support ( set as attribute )
			embedPlayer.apiTitleKey = apiTitleKey;
		}
		// Set local apiProvider via config if not defined
		apiProvider = embedPlayer['data-mwprovider'];
		if ( !apiProvider ) {
			apiProvider = mw.config.get( 'EmbedPlayer.ApiProvider' );
		}

		/**
		 * Loads mediaWiki sources for a given embedPlayer
		 * @param {function} callback Function called once player sources have been added
		 */
		function loadPlayerSources( callback ) {
			// Setup the request
			var request = {
				'prop': 'imageinfo',
				// In case the user added File: or Image: to the apiKey:
				'titles': 'File:' + decodeURIComponent( apiTitleKey ).replace( /^(File:|Image:)/, '' ),
				'iiprop': 'url|size|dimensions|metadata',
				'iiurlwidth': embedPlayer.getWidth(),
				'redirects': true // automatically resolve redirects
			};

			// Run the request:
			mw.getJSON( mw.getApiProviderURL( apiProvider ), request, function ( data ) {
				var i, page, imageinfo;
				if ( data.query.pages ) {
					for ( i in data.query.pages ) {
						if ( i === '-1' ) {
							callback( false );
							return ;
						}
						page = data.query.pages[i];
					}
				} else {
					callback( false );
					return ;
				}
				// Make sure we have imageinfo:
				if ( !page.imageinfo || !page.imageinfo[0] ) {
					callback( false );
					return ;
				}
				imageinfo = page.imageinfo[0];

				// TODO these should call public methods rather than update internals:

				// Update the poster
				embedPlayer.poster = imageinfo.thumburl;

				// Add the media src
				embedPlayer.mediaElement.tryAddSource(
					$( '<source />' )
					.attr( 'src', imageinfo.url )
					.get( 0 )
				);

				// Set the duration
				if ( imageinfo.metadata[2].name === 'length' ) {
					embedPlayer.duration = imageinfo.metadata[2].value;
				}

				// Set the width height
				// Make sure we have an accurate aspect ratio
				if ( imageinfo.height !== 0 && imageinfo.width !== 0 ) {
					embedPlayer.height = Math.floor( embedPlayer.width * ( imageinfo.height / imageinfo.width ) );
				}

				// Update the css for the player interface
				$( embedPlayer ).css( 'height', embedPlayer.height );

				callback();
			});
		}

		/**
		* Build a clip credit from the resource wikiText page
		*
		* TODO parse the resource page template
		*
		* @param {String} resourceHTML Resource wiki text page contents
		*/
		function doCreditLine( resourceHTML, articleUrl ) {
			var authUrl, $page, $author, $authorText, $links, $date, $authorLink,
				imgSize = {},
				// Get the title string ( again a "Title" like js object could help out here. )
				titleStr = embedPlayer.apiTitleKey.replace( /_/g, ' ' ),
				// Setup the initial credits line:
				$creditLine = $( '<div />' );

			// Add the title:
			$creditLine.append(
				$( '<span>' ).html(
					mw.msg( 'mwe-embedplayer-credit-title',
						// get the link
						$( '<div>' ).append(
							$( '<a/>' ).attr( {
								'href': articleUrl,
								'title': titleStr
							} )
							.text( titleStr )
						)[0].innerHTML
					)
				)
			);

			// Parse some data from the page info template if possible:
			$page = $( resourceHTML );

			// Look for author:
			$author = $page.find( '#fileinfotpl_aut' );
			if ( $author.length ) {
				// Get the real author sibling of fileinfotpl_aut
				$authorText = $author.next();
				// Remove white space:
				$authorText.find( 'br' ).remove();

				// Update link to be absolute per page url context:
				$links = $authorText.find( 'a' );
				if ( $links.length ) {
					$links.each( function ( i, authorLink ) {
						$authorLink = $( authorLink );
						authUrl = $authorLink.attr( 'href' );
						authUrl = mw.absoluteUrl( authUrl,  articleUrl );
						$authorLink.attr( 'href', authUrl );
					});
				}
				$creditLine.append( $( '<br />' ),
					mw.msg( 'mwe-embedplayer-credit-author', $authorText.html() )
				);
			}

			// Look for date:
			$date = $page.find( '#fileinfotpl_date' );
			if ( $date.length ) {
				// Get the real date sibling of fileinfotpl_date
				$date = $date.next();

				// remove white space:
				$date.find( 'br' ).remove();
				$creditLine.append(  $( '<br />' ),
					mw.msg( 'mwe-embedplayer-credit-date', $date.html() )
				);
			}

			// Build out the image and credit line
			if ( embedPlayer.isAudio() ) {
				imgSize.height = imgSize.width = ( embedPlayer.controlBuilder.getOverlayWidth() < 250 ) ? 45 : 80;
			} else {
				imgSize.width = ( embedPlayer.controlBuilder.getOverlayWidth() < 250 ) ? 45 : 120;
				imgSize.height = Math.floor( imgSize.width * ( embedPlayer.getHeight() / embedPlayer.getWidth() ) );
			}
			return $( '<div/>' ).addClass( 'creditline' )
				.append(
					$( '<a/>' ).attr( {
						'href': articleUrl,
						'title': titleStr
					} ).html(
						$( '<img/>' ).attr( {
							'border': 0,
							'src': embedPlayer.poster
						} ).css( imgSize )
					)
				)
				.append(
					$creditLine
				);
		}

		/**
		 * Issues a request to populate the credits box
		 */
		function showCredits( $target, callback ) {
			var apiUrl, fileTitle, request;
			if ( $creditsCache ) {
				$target.html( $creditsCache );
				callback( true );
				return;
			}
			// Setup shortcuts:
			apiUrl = mw.getApiProviderURL( apiProvider );
			fileTitle = 'File:' + decodeURIComponent( apiTitleKey ).replace( /^File:|^Image:/, '');

			// Get the image page ( cache for 1 hour )
			request = {
				'action': 'parse',
				'page': fileTitle,
				'smaxage': 3600,
				'maxage': 3600
			};
			mw.getJSON( apiUrl, request, function ( data ) {
				var descUrl = apiUrl.replace( 'api.php', 'index.php');
				descUrl += '?title=' + encodeURIComponent( fileTitle );
				if ( data && data.parse && data.parse.text && data.parse.text['*'] ) {
					// TODO improve provider 'concept' to support page title link
					$creditsCache = doCreditLine( data.parse.text['*'], descUrl );
				} else {
					$creditsCache = doCreditLine( false, descUrl );
				}
				$target.html( $creditsCache );
				callback( true );
			} );
		}
		/**
		 * Adds embedPlayer Bindings
		 */

		// Show credits when requested
		$( embedPlayer ).bindQueueCallback( 'showCredits', function ( $target, callback ) {
			if ( $target.data( 'playerId') !== embedPlayer.id ) {
				// bad event trigger
				return ;
			}
			// Only request the credits once:
			showCredits( $target, callback );
		});

		// Show credits on clip complete:
		$( embedPlayer ).bind( 'onEndedDone', function ( event, id ) {
			if ( embedPlayer.id !== id ) {
				// possible event trigger error. ( skip )
				return ;
			}
			// dont show credits for audio elements,
			// seek to begining instead
			if ( embedPlayer.isAudio() ) {
				embedPlayer.setCurrentTime( 0 );
				return ;
			}
			var cb = embedPlayer.controlBuilder;
			cb.checkMenuOverlay();
			cb.showMenuOverlay();
			cb.showMenuItem( 'credits' );
		});

		$( embedPlayer ).bind( 'showInlineDownloadLink', function () {
			// Add recommend HTML5 player if we have non-native playback:
			if ( embedPlayer.controlBuilder.checkNativeWarning( ) ) {
				embedPlayer.controlBuilder.addWarningBinding(
					'EmbedPlayer.ShowNativeWarning',
					mw.msg( 'mwe-embedplayer-for_best_experience',
						$( '<div>' ).append(
							$( '<a />' ).attr({
								'href': 'http://www.mediawiki.org/wiki/Extension:TimedMediaHandler/Client_download',
								'target': '_new'
							})
						)[0].innerHTML
					),
					true
				);
			}
		});

		$( embedPlayer ).bind( 'TimedText_BuildCCMenu', function ( event, $menu, id ) {
			var _this,
				pageTitle,
				addTextPage,
				$li;
			if ( id !== embedPlayer.id ) {
				_this = $( '#' + id )[0].timedText;
				embedPlayer = _this.embedPlayer;
			}
			// Put in the "Make Transcript" link if config enabled and we have an api key
			if ( embedPlayer.apiTitleKey ) {
				// check if not already there:
				if ( $menu.find( '.add-timed-text' ).length ) {
					// add text link already present
					return ;
				}

				pageTitle = 'TimedText:' +
					decodeURIComponent( embedPlayer.apiTitleKey ).replace( /^File:|^Image:/, '' );
				addTextPage =  mw.getApiProviderURL( apiProvider )
									.replace( 'api.php', 'index.php') +
									'?title=' + encodeURIComponent( pageTitle );

				$li = $.getLineItem( mw.msg( 'mwe-timedtext-upload-timed-text'), 'script', function () {
					window.location = addTextPage;
				});

				$li.addClass( 'add-timed-text')
					.find( 'a' )
					.attr( {
						'href': addTextPage,
						'target': '_new'
					} );
				$menu.append(
					$li
				);
			}
		});

		$( embedPlayer ).bindQueueCallback( 'checkPlayerSourcesEvent', function ( callback ) {
			// Only load source if none are available:
			if ( embedPlayer.mediaElement.sources.length === 0 ) {
				loadPlayerSources( callback );
			} else {
				// No source to load, issue callback directly
				callback();
			}
		} );
		$( mw ).bindQueueCallback( 'TimedText_LoadTextSource', function ( source, callback ) {
			if ( !source.mwtitle || !source.mwprovider ) {
				callback();
				return ;
			}
			// Load via api
			var apiUrl = mw.getApiProviderURL( source.mwprovider ),
			// Get the image page ( cache for 1 hour )
			request = {
				'action': 'parse',
				'page': source.mwtitle,
				'smaxage': 3600,
				'maxage': 3600
			};
			mw.getJSON( apiUrl, request, function ( data ) {
				if ( data && data.parse && data.parse.text &&  data.parse.text['*'] ) {
					source.loaded = true;
					source.mimeType = 'text/mw-srt';
					source.captions = source.getCaptions(  data.parse.text['*'] );
					callback();
				} else {
					mw.log( 'Error: MediaWiki api error in getting timed text:', data );
					callback();
				}
			});
		});

		$( embedPlayer ).bind( 'getShareIframeSrc', function ( event, callback, id ) {
			if ( id !== embedPlayer.id ) {
				embedPlayer = $( '#' + id )[0];
			}
			var iframeUrl = false;
			// Do a special check for wikimediacommons provider as a known shared reop
			if ( embedPlayer['data-mwprovider'] === 'wikimediacommons' ) {
				iframeUrl = '//commons.wikimedia.org/wiki/File:' + decodeURIComponent( embedPlayer.apiTitleKey ).replace( /^(File:|Image:)/, '' );
			} else {
				// use the local wiki:
				if ( mw.config.get( 'wgServer' ) && mw.config.get( 'wgArticlePath' ) ) {
					iframeUrl =  mw.config.get( 'wgServer' ) +
						mw.config.get( 'wgArticlePath' ).replace( /\$1/, 'File:' +
							decodeURIComponent( embedPlayer.apiTitleKey ).replace( /^(File:|Image:)/, '' ) );
				}
			}
			if ( iframeUrl ) {
				iframeUrl += '?embedplayer=yes';
			}
			callback( iframeUrl );
		});
	};

} )( mediaWiki, jQuery );