2014-01-30 11:28:43 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the MediaWiki extension MultimediaViewer.
|
|
|
|
*
|
|
|
|
* MultimediaViewer 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.
|
|
|
|
*
|
|
|
|
* MultimediaViewer 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 MultimediaViewer. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2014-02-19 23:10:09 +00:00
|
|
|
( function ( mw, $, oo ) {
|
2014-01-30 11:28:43 +00:00
|
|
|
/**
|
|
|
|
* File usage interface (what wiki pages is this file used on?)
|
2014-02-19 02:27:30 +00:00
|
|
|
* @class mw.mmv.ui.FileUsage
|
2014-02-19 23:10:09 +00:00
|
|
|
* @extends mw.mmv.ui.Element
|
2014-01-30 11:28:43 +00:00
|
|
|
* @constructor
|
2014-02-19 17:38:55 +00:00
|
|
|
* @inheritdoc
|
2014-01-30 11:28:43 +00:00
|
|
|
*/
|
|
|
|
function FileUsage( $container ) {
|
2014-02-19 23:10:09 +00:00
|
|
|
mw.mmv.ui.Element.call( this, $container );
|
|
|
|
|
2014-01-30 11:28:43 +00:00
|
|
|
/**
|
|
|
|
* The HTML element in which the file usage shall be shown.
|
|
|
|
* @property {jQuery}
|
|
|
|
*/
|
2014-02-19 23:10:09 +00:00
|
|
|
this.$box = $( '<div>' )
|
|
|
|
.appendTo( this.$container );
|
2014-01-30 11:28:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The title of the file usage block.
|
|
|
|
* @property {jQuery}
|
|
|
|
*/
|
|
|
|
this.$title = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The list which contains the wiki pages using the file (and also some miscellaneous
|
|
|
|
* stuff like 'view more' links).
|
|
|
|
* @property {jQuery}
|
|
|
|
*/
|
|
|
|
this.$usageList = null;
|
|
|
|
}
|
2014-02-19 23:10:09 +00:00
|
|
|
oo.inheritClass( FileUsage, mw.mmv.ui.Element );
|
2014-01-30 11:28:43 +00:00
|
|
|
|
2014-02-19 02:27:30 +00:00
|
|
|
/** @property {number} MAX_LOCAL Never show more than this many local usages */
|
2014-01-30 11:28:43 +00:00
|
|
|
FileUsage.prototype.MAX_LOCAL = 3;
|
2014-02-19 02:27:30 +00:00
|
|
|
/** @property {number} MAX_GLOBAL Never show more than this many global usages */
|
2014-01-30 11:28:43 +00:00
|
|
|
FileUsage.prototype.MAX_GLOBAL = 3;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets up the interface. Must be called before any other methods.
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.init = function() {
|
2014-02-19 23:10:09 +00:00
|
|
|
this.$title = $( '<h3>' ).appendTo( this.$box );
|
|
|
|
this.$usageList = $( '<ul>' ).appendTo( this.$box );
|
|
|
|
this.$box.addClass( 'mw-mlb-fileusage-container' );
|
2014-01-30 11:28:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears the interface.
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.empty = function() {
|
|
|
|
this.$title.text( '' );
|
|
|
|
this.$usageList.empty();
|
2014-02-19 23:10:09 +00:00
|
|
|
this.$box.addClass( 'empty' );
|
2014-01-30 11:28:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays file usage based on the passed usage objects..
|
|
|
|
* @param {mw.mmv.model.FileUsage} localUsage
|
|
|
|
* @param {mw.mmv.model.FileUsage} globalUsage
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.set = function( localUsage, globalUsage ) {
|
|
|
|
var usageCount = localUsage.totalCount + globalUsage.totalCount,
|
|
|
|
countMessage = 'multimediaviewer-fileusage-count';
|
|
|
|
|
|
|
|
if ( localUsage.totalCount || globalUsage.totalCount ) {
|
2014-02-19 23:10:09 +00:00
|
|
|
this.$box.removeClass( 'empty' );
|
2014-01-30 11:28:43 +00:00
|
|
|
|
|
|
|
if ( localUsage.totalCountIsLowerBound || globalUsage.totalCountIsLowerBound ) {
|
|
|
|
// "more than 100 uses" sounds nicer than "more than 103 uses"
|
|
|
|
usageCount = Math.max( localUsage.totalCount, globalUsage.totalCount );
|
|
|
|
countMessage = 'multimediaviewer-fileusage-count-more';
|
|
|
|
}
|
2014-01-30 13:08:33 +00:00
|
|
|
this.$title.msg( countMessage, mw.language.convertNumber( usageCount ) );
|
2014-01-30 11:28:43 +00:00
|
|
|
|
|
|
|
this.$usageList.empty();
|
|
|
|
this.addSection( localUsage, mw.mmv.model.FileUsage.Scope.LOCAL, this.MAX_LOCAL,
|
|
|
|
this.getLocalUsageUrl( localUsage.file ) );
|
|
|
|
this.addSection( globalUsage, mw.mmv.model.FileUsage.Scope.GLOBAL, this.MAX_GLOBAL,
|
|
|
|
this.getGlobalUsageUrl( globalUsage.file ) );
|
|
|
|
} else {
|
|
|
|
this.empty();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a usage section to the list - a title, a list of links, and a 'view more' link
|
|
|
|
* (some or all of these might be omitted when it makes sense).
|
|
|
|
* @param {mw.mmv.model.FileUsage} fileUsage
|
|
|
|
* @param {mw.mmv.model.FileUsage.Scope} sectionType will be used for the section title
|
|
|
|
* @param {number} limit max number of entries to show
|
|
|
|
* @param {string} viewAllLink a link for the rest of entries, if there are more than the limit
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.addSection = function( fileUsage, sectionType, limit, viewAllLink ) {
|
2014-01-31 08:30:26 +00:00
|
|
|
var $section;
|
|
|
|
|
2014-01-30 11:28:43 +00:00
|
|
|
if ( fileUsage.totalCount ) {
|
2014-01-31 08:30:26 +00:00
|
|
|
$section = $( '<li>' ).addClass( 'mw-mlb-fileusage-' + sectionType + '-section' )
|
|
|
|
.msg( 'multimediaviewer-fileusage-' + sectionType + '-section' );
|
|
|
|
|
|
|
|
this.$usageList.append( $section );
|
2014-01-30 11:28:43 +00:00
|
|
|
this.addPageLinks( fileUsage.pages.slice( 0, limit ) );
|
2014-01-31 08:30:26 +00:00
|
|
|
|
2014-01-30 11:28:43 +00:00
|
|
|
if ( fileUsage.pages.length > limit ) {
|
2014-01-31 08:30:26 +00:00
|
|
|
this.addViewAllLink( $section, viewAllLink );
|
2014-01-30 11:28:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends links pointing to the given pages to the end of the usage list.
|
|
|
|
* @param {{wiki: (string|null), page: mw.Title}[]} pages
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.addPageLinks = function( pages ) {
|
|
|
|
var ui = this;
|
|
|
|
|
|
|
|
this.$usageList.append( $.map( pages, function( item ) {
|
|
|
|
var pageUrl = ui.getFileUrl( item.page, item.wiki ),
|
|
|
|
pageLink = $( '<a>' ).attr( 'href', pageUrl ).text( item.page.getMainText() );
|
|
|
|
|
|
|
|
if ( item.wiki ) {
|
|
|
|
// external link - show the wiki name next to it
|
|
|
|
return $( '<li>' ).append( pageLink ).append(
|
|
|
|
$( '<aside>' ).text( item.wiki )
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $( '<li>' ).append( pageLink );
|
|
|
|
}
|
|
|
|
} ) );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a 'View all' link (with the given URL) to the end of the usage list.
|
2014-01-31 08:30:26 +00:00
|
|
|
* @param {jQuery} $section file usage section element
|
2014-01-30 11:28:43 +00:00
|
|
|
* @param {string} url
|
|
|
|
* @protected
|
|
|
|
*/
|
2014-01-31 08:30:26 +00:00
|
|
|
FileUsage.prototype.addViewAllLink = function( $section, url ) {
|
|
|
|
$section.append(
|
2014-01-30 11:28:43 +00:00
|
|
|
$( '<span>' ).addClass( 'mw-mlb-fileusage-view-all' ).append(
|
2014-01-30 13:08:33 +00:00
|
|
|
$( '<a>' ).msg( 'multimediaviewer-fileusage-link' )
|
2014-01-30 11:28:43 +00:00
|
|
|
.attr( 'href', url )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an URL to the given file.
|
|
|
|
* @param {mw.Title} page
|
|
|
|
* @param {string} wiki domain name
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.getFileUrl = function( page, wiki ) {
|
|
|
|
// TODO the nice way to handle this would be to have a mw.IwTitle class for interwiki links
|
|
|
|
if ( wiki ) {
|
|
|
|
return new mw.Uri( wiki + page.getUrl() ).toString();
|
|
|
|
} else {
|
|
|
|
return page.getUrl();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Special:WhatLinksHere link for the given file.
|
|
|
|
* @param {mw.Title} file
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.getLocalUsageUrl = function( file ) {
|
|
|
|
// TODO special page name should be localized
|
|
|
|
return new mw.Uri( mw.config.get( 'wgScript' ) ).extend( {
|
|
|
|
title: 'Special:WhatLinksHere/' + file.getPrefixedDb(),
|
|
|
|
hidetrans: 1,
|
|
|
|
hidelinks: 1,
|
|
|
|
hideredirs: 1
|
|
|
|
} ).toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a Special:GlobalUsage link for the given file.
|
|
|
|
* @param {mw.Title} file
|
|
|
|
* @protected
|
|
|
|
*/
|
|
|
|
FileUsage.prototype.getGlobalUsageUrl = function( file ) {
|
|
|
|
// TODO special page name should be localized
|
|
|
|
return new mw.Uri( mw.config.get( 'wgScript' ) ).extend( {
|
|
|
|
title: 'Special:GlobalUsage',
|
2014-01-31 18:46:20 +00:00
|
|
|
target: file.getMain(),
|
2014-01-30 11:28:43 +00:00
|
|
|
filterlocal: 1
|
|
|
|
} ).toString();
|
|
|
|
};
|
|
|
|
|
|
|
|
mw.mmv.ui = mw.mmv.ui || {};
|
|
|
|
mw.mmv.ui.FileUsage = FileUsage;
|
2014-02-19 23:10:09 +00:00
|
|
|
} ) ( mediaWiki, jQuery, OO );
|