MediaWiki:ImageLinks.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// <source lang="javascript">

/**
 * Helper function to normalize date
 */
function getISODate() {
 var date = new Date();
 // UTC
 var dd = date.getUTCDate();
 if (dd < 10) {
  dd = '0' + dd.toString();
 }
 var mm = date.getUTCMonth() + 1;
 if (mm < 10) {
  mm = '0' + mm.toString();
 }
 var YYYY = date.getUTCFullYear();
 var ISOdate = YYYY + '-' + mm + '-' + dd;
 return ISOdate;
}

// Guard against double inclusions
if ( typeof ImageLinks === 'undefined' ) {

	importScript( 'MediaWiki:Utilities.js' );
	importScript( 'MediaWiki:LAPI.js' );

	if ( typeof( image_links_close_windows ) == 'undefined' )
		var image_links_close_windows = false;

	if ( typeof( image_deletion_script ) == 'undefined' )
		var image_deletion_script = '&withJS=MediaWiki:Gadget-DelReqHandler.js';

	var image_links_desc = {
		flickr: {
			text: 'Flickr check',
			tip: 'Check if this image is on Flickr'
		},
		pbase: {
			text: 'pbase check',
			tip: 'Check if this file is on pbase'
		},
		google: {
			text: 'Google img search',
			tip: 'Search for this image using Google'
		},
		tineye: {
			text: 'Tineye search',
			tip: 'Search for the image on Tineye'
		}
	};

	if ( mw.config.get( 'wgUserGroups' )
		.join( ' ' )
		.indexOf( 'sysop' ) >= 0 ) { // Only for sysops

		importScript( 'MediaWiki:MD5.js' );

		var image_links_del_links = [ {
			text: 'Del\xa0copyvio',
			tip: 'Delete copvio (general)',
			reason: 'copyright violation; see [[COM:L]]'
		}, {
			text: 'Del\xa0fair use',
			tip: 'Delete a fair use file',
			reason: 'fair use: not allowed; see [[COM:L]]'
		}, {
			text: 'Del\xa0nc/nd',
			tip: 'Delete non-commercial/non-derivative uses file',
			reason: 'non-commercial or no-derivatives licenses not allowed; see [[COM:L]]'
		}, {
			text: 'Del\xa0derivative',
			tip: 'Delete a derivative work (but mind FOP!)',
			reason: 'depicts a copyrighted work; see [[COM:DW]]'
		}, {
			text: 'Del\xa0album/CD/DVD',
			tip: 'Delete an album cover',
			reason: 'album/DVD covers not allowed; see [[COM:L]] and [[COM:DW]]'
		}, {
			text: 'Del\xa0book/magazine',
			tip: 'Delete a book or magazine cover',
			reason: 'copyrighted book or magazine covers not allowed; see [[COM:L]] and [[COM:DW]]'
		}, {
			text: 'Del\xa0WP\xa0only',
			tip: 'Delete wikipedia-only file',
			reason: '"Wikipedia-only" licenses not allowed; see [[COM:L]]'
		}, {
			text: 'Del\xa0nsd/nld',
			tip: 'Delete a file without source or license',
			reason: 'no source, no license, or both'
		}, {
			text: 'Del\xa0no\xa0permission',
			tip: 'Delete a file without permission for more than 7 days',
			reason: 'no permission for more than 7 days'
		}, {
			text: 'Del\xa0Flickr',
			tip: 'Delete a file on which Flickr review failed',
			reason: 'Flickr review failed; image is all rights reserved or NC/ND only at Flickr'
		}, {
			text: 'Del\xa0screenshot',
			tip: 'Delete a non-free screenshot',
			reason: 'Non-free screen shot or TV capture'
		} ];

		var image_links_admin_lks = {
			delreason: {
				text: 'Del\xa0reason...',
				tip: 'Delete file, prompting for a reason'
			},
			notify: {
				text: 'Notify\xa0uploader',
				tip: 'Notify uploader of speedy deletion'
			},
			flickr: {
				text: 'Successful\xa0flickrreview',
				tip: 'Fill in the flickrreview template with your user name and a timestamp'
			}
		};

		var AdminImageLinks = {
			get_links: function( title, user, rev_id, img_url ) {
				var links = [];
				if ( !title ) return links;
				title = title.replace( / /g, '_' );
				var is_img_page = ( mw.config.get( 'wgNamespaceNumber' ) == 6 && mw.config.get( 'wgAction' ) == 'view' );

				// Deletion section
				for ( var i = 0; i < image_links_del_links.length; i++ ) {
					links[ links.length ] =
						makeRawLink(
							image_links_del_links[ i ].text, 'javascript:AdminImageLinks.execute ("' + stringifyJS( title ) + '", "' + stringifyJS( image_links_del_links[ i ].reason ) + '");', null, image_links_del_links[ i ].tooltip
						);
				}
				links[ links.length ] =
					makeRawLink(
						image_links_admin_lks.delreason.text, 'javascript:AdminImageLinks.promptForReason ("' + stringifyJS( title ) + '");', null, image_links_admin_lks.delreason.tip
					);
				if ( user && user.length > 0 || is_img_page ) {
					links[ links.length ] =
						makeRawLink(
							image_links_admin_lks.notify.text, 'javascript:AdminImageLinks.notify ("' + stringifyJS( title ) + '", "' + ( user && user.length > 0 ? stringifyJS( user ) : "" ) + '");', null, image_links_admin_lks.notify.tip
						);
				}
				var all_links = [];
				all_links[ all_links.length ] = links;

				// Flickr review section
				links = [];
				links[ links.length ] =
					makeRawLink(
						image_links_admin_lks.flickr.text, 'javascript:AdminImageLinks.flickrreview ("' + stringifyJS( title ) + '", "' + ( rev_id || "" ) + '", ' + is_img_page + ');', null, image_links_admin_lks.flickr.tip
					);
				if ( links.length > 0 )
					all_links[ all_links.length ] = links;
				return all_links;
			},

			last_del_reason: "",

			promptForReason: function( filename ) {
				var reason = prompt( 'Deletion reason:', AdminImageLinks.last_del_reason );
				if ( reason && reason.length > 0 ) {
					AdminImageLinks.last_del_reason = reason;
					AdminImageLinks.execute( filename, reason );
				}
			},

			notify: function( page_name, user ) {
				Notifier.notify(
					'\{\{subst:speedywhat|1=' + page_name + '\}\}\~\~\~\~', user, 'Please do not upload copyright violations', false // Do save
					, !image_links_close_windows, false // Use separate window
				);
			},

			flickrreview: function( page_name, rev_id, same_window ) {
				var edit_lk = mw.config.get( 'wgServer' ) + mw.config.get( 'wgScript' ) + '?title=' + encodeURIComponent( page_name ) + '&action=edit';
				QuickModify.execute(
					edit_lk, QuickModify.actions(
						( rev_id ? [ 'h', rev_id ] : null ), [ 'r', /\{\{[Ff]lickr?review\}\}|(\{\{User:FlickreviewR\/reviewed-(error|fail)[^\}]*\}\})/g, '\{\{flickrreview|' + mw.config.get( 'wgUserName' ) + '|' + getISODate() + '\}\}' ], [ ( image_links_close_windows && !same_window ? 'c' : 's' ), 'Flickr review passed' ]
					), same_window
				);
			},

			execute: function( filename, reason ) {
				if ( filename ) {
					var href = mw.config.get( 'wgServer' ) + mw.config.get( 'wgArticlePath' )
						.replace( '$1', encodeURI( filename ) ) + '?action=delete' + image_deletion_script + '&delreason=' + encodeURIComponent( reason ) + '&deltoken=' + AdminImageLinks.get_token( filename );
					window.open( href, '_blank' );
				}
			},

			get_cookie: function( cookie_name ) {
				if ( !document.cookie || !cookie_name ) return "";
				var start = document.cookie.indexOf( cookie_name + '=' );
				if ( start < 0 ) return "";
				start = start + cookie_name.length + 1;
				var end = document.cookie.indexOf( ';', start );
				if ( end < 0 )
					return document.cookie.substring( start );
				else
					return document.cookie.substring( start, end );
			},

			get_token: function( filename ) {
				if ( !filename ) return "";
				var now = new Date();
				now = Math.floor( now.getTime() / ( 30 * 1000 ) ); // 30 seconds
				var sessionID = AdminImageLinks.get_cookie( 'wiki_session' );
				return hex_md5( filename + sessionID + mw.config.get( 'wgUserName' ) + now );
			}

		};

	} else { // Dummy object for non-sysops

		var AdminImageLinks = {
			get_links: function( title, user, rev_id, img_url ) {
				var result = [];
				return result;
			}
		};

	} // end if (sysop)

	// Localization hook.
	if ( mw.config.get( 'wgUserLanguage' ) != 'en' ) importScript( 'MediaWiki:ImageLinks.js/' + mw.config.get( 'wgUserLanguage' ) );

	var ImageLinks = {
		get_links: function( title, user, rev_id, img_url ) {
			var links = [];
			if ( !title ) return links;
			var is_img_page = ( mw.config.get( 'wgNamespaceNumber' ) == 6 && mw.config.get( 'wgAction' ) == 'view' );

			var plain_title = title.substr( title.indexOf( ':' ) + 1 ); // Strip namespace

			var digits = plain_title.match( /^(\d{6,10})[ _].*\.jpg$/i );
			if ( digits ) {
				// File name starts with ten digits: add link for static Flickr check
				links[ links.length ] =
					makeRawLink(
						image_links_desc.flickr.text, 'http://www.flickr.com/photo.gne?id=' + digits[ 1 ], '_blank', image_links_desc.flickr.tip
					);
				// and one for pbase.com
				links[ links.length ] =
					makeRawLink(
						image_links_desc.pbase.text, 'http://www.pbase.com/image/' + digits[ 1 ], '_blank', image_links_desc.pbase.tip
					);
			}
			if ( img_url || is_img_page ) {
				if ( !img_url ) {
					var img = LAPI.WP.getPreviewImage();
					if ( img && img.parentNode.nodeName.toLowerCase() == 'a' ) {
						img_url = img.parentNode.href;
					}
				}
				if ( img_url ) {
					links[ links.length ] =
						makeRawLink(
							image_links_desc.google.text, 'https://www.google.com/searchbyimage?image_url=' + encodeURIComponent( img_url ), '_blank', image_links_desc.google.tip
						);
					links[ links.length ] =
						makeRawLink(
							image_links_desc.tineye.text, 'https://tineye.com/search?url=' + encodeURIComponent( img_url ), '_blank', image_links_desc.tineye.tip
						);
				}
			}
			var all_links = [];
			all_links[ all_links.length ] = links;
			var result =
				all_links.concat( AdminImageLinks.get_links( title, user, rev_id, img_url ) );
			return result;
		}
	};

} // end if (guard)

// </source>