| 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 HtmlUtils = require( '../mmv.HtmlUtils.js' ); |
| 19 | |
| 20 | /** |
| 21 | * Class for storing license information about an image. For available fields, see |
| 22 | * TemplateParser::$licenseFieldClasses in the CommonsMetadata extension. |
| 23 | */ |
| 24 | class License { |
| 25 | /** |
| 26 | * @param {string} shortName see {@link #shortName} |
| 27 | * @param {string} [internalName] see {@link #internalName} |
| 28 | * @param {string} [longName] see {@link #longName} |
| 29 | * @param {string} [deedUrl] see {@link #deedUrl} |
| 30 | * @param {boolean} [attributionRequired] see {@link #attributionRequired} |
| 31 | * @param {boolean} [nonFree] see {@link #nonFree} |
| 32 | */ |
| 33 | constructor( |
| 34 | shortName, |
| 35 | internalName, |
| 36 | longName, |
| 37 | deedUrl, |
| 38 | attributionRequired, |
| 39 | nonFree |
| 40 | ) { |
| 41 | if ( !shortName ) { |
| 42 | throw new Error( 'License: shortName is required' ); |
| 43 | } |
| 44 | |
| 45 | /** @property {string} shortName short (abbreviated) name of the license (e.g. CC-BY-SA-3.0) */ |
| 46 | this.shortName = shortName; |
| 47 | |
| 48 | /** @property {string} internalName internal name of the license, used for localization (e.g. cc-by-sa ) */ |
| 49 | this.internalName = internalName; |
| 50 | |
| 51 | /** @property {string} longName full name of the license (e.g. Creative Commons etc. etc.) */ |
| 52 | this.longName = longName; |
| 53 | |
| 54 | /** @property {string} deedUrl URL to the description of the license (e.g. the CC deed) */ |
| 55 | this.deedUrl = deedUrl; |
| 56 | |
| 57 | /** @property {boolean} attributionRequired does the author need to be attributed on reuse? */ |
| 58 | this.attributionRequired = attributionRequired; |
| 59 | |
| 60 | /** @property {boolean} nonFree is this a non-free license? */ |
| 61 | this.nonFree = nonFree; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Check whether this is a Creative Commons license. |
| 66 | * |
| 67 | * @return {boolean} |
| 68 | */ |
| 69 | isCc() { |
| 70 | return this.internalName ? this.internalName.slice( 0, 2 ) === 'cc' : false; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Check whether this is a public domain "license". |
| 75 | * |
| 76 | * @return {boolean} |
| 77 | */ |
| 78 | isPd() { |
| 79 | return this.internalName === 'pd'; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Check whether this is a free license. |
| 84 | * |
| 85 | * @return {boolean} |
| 86 | */ |
| 87 | isFree() { |
| 88 | // licenses with missing nonfree information are assumed free |
| 89 | return !this.nonFree; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Check whether reusers need to attribute the author |
| 94 | * |
| 95 | * @return {boolean} |
| 96 | */ |
| 97 | needsAttribution() { |
| 98 | // to be on the safe side, if the attribution required flag is not set, it is assumed to be true |
| 99 | return !this.isPd() && this.attributionRequired !== false; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns the short name of the license: |
| 104 | * - if we have interface messages for this license (basically just CC and PD), use those |
| 105 | * - otherwise use the short name from the license template (might or might not be translated |
| 106 | * still, depending on how the template is set up) |
| 107 | * |
| 108 | * @return {string} |
| 109 | * FIXME a model should not depend on an i18n class. We should probably use view models. |
| 110 | */ |
| 111 | getShortName() { |
| 112 | const message = `multimediaviewer-license-${ this.internalName || '' }`; |
| 113 | if ( mw.messages.exists( message ) ) { |
| 114 | // The following messages are used here: |
| 115 | // * multimediaviewer-license-cc-by-1.0 |
| 116 | // * multimediaviewer-license-cc-sa-1.0 |
| 117 | // * multimediaviewer-license-cc-by-sa-1.0 |
| 118 | // * multimediaviewer-license-cc-by-2.0 |
| 119 | // * multimediaviewer-license-cc-by-sa-2.0 |
| 120 | // * multimediaviewer-license-cc-by-2.1 |
| 121 | // * multimediaviewer-license-cc-by-sa-2.1 |
| 122 | // * multimediaviewer-license-cc-by-2.5 |
| 123 | // * multimediaviewer-license-cc-by-sa-2.5 |
| 124 | // * multimediaviewer-license-cc-by-3.0 |
| 125 | // * multimediaviewer-license-cc-by-sa-3.0 |
| 126 | // * multimediaviewer-license-cc-by-4.0 |
| 127 | // * multimediaviewer-license-cc-by-sa-4.0 |
| 128 | // * multimediaviewer-license-cc-pd |
| 129 | // * multimediaviewer-license-cc-zero |
| 130 | // * multimediaviewer-license-pd |
| 131 | // * multimediaviewer-license-default |
| 132 | return mw.msg( message ); |
| 133 | } else { |
| 134 | return this.shortName; |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Returns a short HTML representation of the license. |
| 140 | * |
| 141 | * @return {string} |
| 142 | */ |
| 143 | getShortLink() { |
| 144 | const shortName = this.getShortName(); |
| 145 | |
| 146 | if ( this.deedUrl ) { |
| 147 | return HtmlUtils.makeLinkText( shortName, { |
| 148 | href: this.deedUrl, |
| 149 | title: this.longName || shortName |
| 150 | } ); |
| 151 | } else { |
| 152 | return shortName; |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | module.exports = License; |