Difference between revisions of "MediaWiki:Gadget-LinkMissingGalleryFilesToUpload.js"
(→top: Restoring revision 4572131 by User:Becasita on 2020-11-06 03:02:49. Update timestamp.) (Tag: New user edit) |
(Add template preload here, from MediaWiki:Gadget-CardImageUploadPreLoads.js.) |
||
Line 6: | Line 6: | ||
( function _gadgetLinkMissingGalleryFilesToUpload( window, $, mw, console ) { | ( function _gadgetLinkMissingGalleryFilesToUpload( window, $, mw, console ) { | ||
"use strict"; | "use strict"; | ||
− | var LAST_LOG = ' | + | |
+ | var LAST_LOG = '12:16, 27 January 2024 (UTC)'; | ||
+ | |||
+ | var config = mw.config.get( [ | ||
+ | 'wgNamespaceNumber', | ||
+ | 'wgTitle', | ||
+ | 'wgNamespaceIds', | ||
+ | ] ); | ||
+ | |||
+ | var NS = { | ||
+ | CARD_GALLERY: config.wgNamespaceIds.card_gallery, | ||
+ | SET_CARD_GALLERIES: config.wgNamespaceIds.set_card_galleries, | ||
+ | }; | ||
+ | |||
+ | var acceptedNamespacesForTemplatePreload = [ NS.CARD_GALLERY, NS.SET_CARD_GALLERIES ]; | ||
+ | |||
+ | function getCardName( $thumb ) { | ||
+ | return $thumb.siblings( '.gallerytext' ) | ||
+ | .children( 'p' ).first() | ||
+ | .children( 'br' ).first() | ||
+ | .next( 'a' ).attr( 'title' ); | ||
+ | } | ||
+ | |||
+ | function getTemplatePreload( $thumb ) { | ||
+ | if ( !acceptedNamespacesForTemplatePreload.includes( config.wgNamespaceNumber ) ) { | ||
+ | return ''; | ||
+ | } | ||
+ | |||
+ | var cardName = config.wgNamespaceNumber === NS.CARD_GALLERY | ||
+ | ? config.wgTitle | ||
+ | : getCardName( $thumb ); | ||
+ | |||
+ | // <pre> | ||
+ | return '{{OCG-TCG card image\n| name = ' + cardName + '\n}}'; | ||
+ | // </pre> | ||
+ | } | ||
+ | |||
function linkEmptyGalleries( $content ) { | function linkEmptyGalleries( $content ) { | ||
$content.find( '.thumb' ).each( function() { | $content.find( '.thumb' ).each( function() { | ||
var $thisThumb = $( this ); | var $thisThumb = $( this ); | ||
+ | |||
if ( !$thisThumb.children().length ) { | if ( !$thisThumb.children().length ) { | ||
var missingFileName = $thisThumb.text(); | var missingFileName = $thisThumb.text(); | ||
− | + | ||
− | + | var queryParams = new URLSearchParams( { | |
− | + | title: 'Special:Upload', | |
− | + | wpDestFile: missingFileName, | |
− | + | wpUploadDescription: getTemplatePreload( $thisThumb ), | |
− | + | } ); | |
− | ); | + | |
+ | var $uploadLink = $( '<a>', { | ||
+ | 'class': 'noFile', | ||
+ | href: '/index.php?' + queryParams.toString(), | ||
+ | text: missingFileName, | ||
+ | } ); | ||
+ | |||
+ | $thisThumb.text( '' ).append( $uploadLink ); | ||
} | } | ||
} ); | } ); | ||
+ | |||
mw.hook( 'ext.gadget.LinkMissingGalleryFilesToUpload' ).fire( $content ); | mw.hook( 'ext.gadget.LinkMissingGalleryFilesToUpload' ).fire( $content ); | ||
} | } | ||
+ | |||
mw.hook( 'wikipage.content' ).add( linkEmptyGalleries ); | mw.hook( 'wikipage.content' ).add( linkEmptyGalleries ); | ||
+ | |||
console.log( '[Gadget] LinkMissingGalleryFilesToUpload last updated at', LAST_LOG ); | console.log( '[Gadget] LinkMissingGalleryFilesToUpload last updated at', LAST_LOG ); | ||
+ | |||
} )( window, window.jQuery, window.mediaWiki, window.console ); | } )( window, window.jQuery, window.mediaWiki, window.console ); |
Latest revision as of 12:17, 27 January 2024
/**
* Links missing files on galleries to the Special:Upload page.
* @author Becasita
* @contact [[User talk:Becasita]]
*/
( function _gadgetLinkMissingGalleryFilesToUpload( window, $, mw, console ) {
"use strict";
var LAST_LOG = '12:16, 27 January 2024 (UTC)';
var config = mw.config.get( [
'wgNamespaceNumber',
'wgTitle',
'wgNamespaceIds',
] );
var NS = {
CARD_GALLERY: config.wgNamespaceIds.card_gallery,
SET_CARD_GALLERIES: config.wgNamespaceIds.set_card_galleries,
};
var acceptedNamespacesForTemplatePreload = [ NS.CARD_GALLERY, NS.SET_CARD_GALLERIES ];
function getCardName( $thumb ) {
return $thumb.siblings( '.gallerytext' )
.children( 'p' ).first()
.children( 'br' ).first()
.next( 'a' ).attr( 'title' );
}
function getTemplatePreload( $thumb ) {
if ( !acceptedNamespacesForTemplatePreload.includes( config.wgNamespaceNumber ) ) {
return '';
}
var cardName = config.wgNamespaceNumber === NS.CARD_GALLERY
? config.wgTitle
: getCardName( $thumb );
// <pre>
return '{{OCG-TCG card image\n| name = ' + cardName + '\n}}';
// </pre>
}
function linkEmptyGalleries( $content ) {
$content.find( '.thumb' ).each( function() {
var $thisThumb = $( this );
if ( !$thisThumb.children().length ) {
var missingFileName = $thisThumb.text();
var queryParams = new URLSearchParams( {
title: 'Special:Upload',
wpDestFile: missingFileName,
wpUploadDescription: getTemplatePreload( $thisThumb ),
} );
var $uploadLink = $( '<a>', {
'class': 'noFile',
href: '/index.php?' + queryParams.toString(),
text: missingFileName,
} );
$thisThumb.text( '' ).append( $uploadLink );
}
} );
mw.hook( 'ext.gadget.LinkMissingGalleryFilesToUpload' ).fire( $content );
}
mw.hook( 'wikipage.content' ).add( linkEmptyGalleries );
console.log( '[Gadget] LinkMissingGalleryFilesToUpload last updated at', LAST_LOG );
} )( window, window.jQuery, window.mediaWiki, window.console );