İçeriğe atla

MediaWiki:Gadget-ChooseResolution.js

Vikipedi, özgür ansiklopedi

Not: Sayfayı kaydettikten sonra değişiklikleri görebilmek için tarayıcınızın önbelleğinizi temizlemeniz gerekir. Google Chrome, Firefox, Microsoft Edge ve Safari: ⇧ Shift tuşuna basılı tutun ve Yeniden Yükle araç çubuğu düğmesine tıklayın. Ayrıntılar ve diğer tarayıcılara yönelik yönergeler için Vikipedi:Önbelleğinizi atlayın sayfasını inceleyin.

if ( mw.config.get('wgNamespaceNumber') == 6 && mw.config.get('wgAction') == "view" ) $( choose_sizes_init ) ;

var csid_w = 0 ;
var csid_h = 0 ;
var csid_tn_w = 0 ;
var csid_th_h = 0 ;
var cs_full_url = "" ;
var cs_image_type = "" ;
var cs_author = "" ;
var cs_licenses = new Array () ;

var cs_license_cats = new Array (
  "GFDL",
  "CC-",
  "PD"
);

function choose_sizes_open_html_window ( html ) {
  html = decodeURIComponent ( html ) ;
  html = unescape ( html ) ;

  var html2 = html ;

  html = html.split("<").join("&lt;") ;
  html = html.split("\n").join("<br/>") ;

  OpenWindow=window.open("", "newwin", "height=550, width=800,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,status=no") ;
  OpenWindow.document.write("<TITLE>" + mw.config.get('wgTitle') + "</TITLE>") ;
  OpenWindow.document.write("<BODY>") ;
  OpenWindow.document.write("<h1>Copy the following HTML to your page:</h1>") ;
  OpenWindow.document.write( "<small>" + html + "</small>" ) ;
  OpenWindow.document.write( "<hr/>This will look like:<br/>" + html2 ) ;
  OpenWindow.document.write("</BODY>") ;
  OpenWindow.document.write("</HTML>") ;

  OpenWindow.document.close() ;
  self.name="main" ;
  return false ;
}

function choose_sizes_get_td () {
  var td = document.createElement ( "TD" ) ;
  td.style.textAlign = "center" ;
  td.style.borderLeft = "1px solid grey" ;
//  td.style.fontSize = "100%" ;
  td.style.lineHeight = "110%" ;
  td.style.marginLeft = "15px" ;
  td.style.marginRight = "15px" ;
  return td ;
}

function choose_sizes_get_thumbnail_link ( width ) {
  var u = cs_full_url.split ( "/" ) ;
  while ( u[0] != "commons" ) u.shift() ;
  u.shift() ;
  u = "//upload.wikimedia.org/wikipedia/commons/thumb/" + u.join ( "/" ) ;
  u += "/" + width + "px-" ;
//  var i = mw.config.get('wgPageName').split ( ":" ) ;
//  i.shift() ;
//  u += i.join ( ":" ) ;
  u += encodeURIComponent ( mw.config.get('wgTitle') ) ;
  if ( cs_image_type == "svg" ) u += ".png" ;
  return u ;
}

function choose_sizes_add_resolution ( ul , width , height ) {
  var urltitle = encodeURI ( mw.config.get('wgTitle').split(" ").join("_") ) ;
  var url = choose_sizes_get_thumbnail_link ( width ) ;
  var fullres = ( width == csid_w || height == csid_h ) ;
  if ( fullres && cs_image_type == "svg" ) return document.createElement ( "TD" ) ; // No fullres for SVG
  if ( fullres ) url = cs_full_url ;

  var html = "<div style='text-align:center'>\n" +
    "<img src=\"" + url + "\"><br/>\n" +
    "<a href=\"" + mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(mw.config.get('wgPageName')) + "\">From Wikimedia Commons</a>" ;
//  html += " |\n<a href=\"" + cs_full_url + "\">Full resolution</a>" ;

  if ( cs_author != "" ) html += " |\nAuthor : " + cs_author.split("\n").join(" ")

  if ( cs_licenses.length > 0 ) {
    html += " |\nLicensing :" ;
    for ( var i = 0 ; i < cs_licenses.length ; i++ ) {
      if ( i > 0 ) html += ", " ;
      html += "\n<a href=\"" + "//en.wikipedia.org/wiki/" + encodeURIComponent ( cs_licenses[i] ) +
//        mw.config.get('wgServer') + mw.config.get('wgArticlePath').split("$1").join(encodeURIComponent("Category:"+cs_licenses[i])) + 
        "\">" + cs_licenses[i] + "</a>" ;
    }
  } else html += " |\nSee Source page for license" ;

  html += "\n</div>" ;

  var a1text = width + "×" + height ;
  if ( fullres ) a1text = "Full resolution"
  var a1 = document.createElement ( "A" ) ;
  a1.href = url ;
  a1.alt = "Open image" ;
  a1.appendChild ( document.createTextNode ( a1text ) ) ;

  var a2 = document.createElement ( "A" ) ;
  a2.href = "#" ;
  a2.alt = "Get HTML to include this image on your own website" ;
  a2.appendChild ( document.createTextNode ( "HTML" ) ) ;
  a2.onclick = function() { return choose_sizes_open_html_window( encodeURIComponent ( escape ( html ) ) ) } ;

  var td = choose_sizes_get_td () ;
  if ( fullres ) td.style.fontWeight = "bold" ;
  td.appendChild ( a1 ) ;
  if ( !fullres ) { // No HTML link for fullres
    td.appendChild ( document.createElement ( "BR" ) ) ;
    td.appendChild ( document.createTextNode ( "(" ) ) ;
    td.appendChild ( a2 ) ;
    td.appendChild ( document.createTextNode ( ")" ) ) ;
  }
  ul.appendChild ( td) ;
}

function choose_sizes_add_resolution_w ( ul , width ) {
  if ( csid_w >= width ) choose_sizes_add_resolution ( ul , width , Math.floor ( width * csid_h / csid_w ) ) ;
}

function choose_sizes_add_resolution_h ( ul , height ) {
  if ( csid_h >= height ) choose_sizes_add_resolution ( ul , Math.floor ( height * csid_w / csid_h ) , height ) ;
}

function choose_sizes_init () {
  var type = mw.config.get('wgTitle').split(".").pop().toLowerCase() ;
  if ( type != "png" && type != "jpg" && type != "jpeg" && type != "svg" && type != "gif" ) return ; // No image type
  cs_image_type = type ;
  choose_sizes_get_image_dimensions () ;
  if ( type == "svg" ) {
    csid_w = csid_tn_w ;
    csid_h = csid_tn_h ;
  }
  if ( csid_w * csid_h <= 0 ) return ; // Sanity check
//  if ( csid_w == csid_tn_w ) return ; // Thumbnail already as large as image, no need for more resolution

  var sb = document.getElementById ( "p-tb" ) ;
  if ( !sb ) return ; // Sanity check
  sb = sb.getElementsByTagName("UL")[0] ;
  if ( !sb ) return ; // Sanity check

  choose_sizes_detemine_author () ;
  choose_sizes_detemine_licenses () ;
  
  var ul = document.createElement ( "tr" ) ;
  if ( csid_w >= csid_h ) { // Horizontal image
    choose_sizes_add_resolution_w ( ul , 250 ) ;
//    choose_sizes_add_resolution_w ( ul , 500 ) ;
//    choose_sizes_add_resolution_w ( ul , 640 ) ;
//    choose_sizes_add_resolution_w ( ul , 800 ) ;
//    choose_sizes_add_resolution_w ( ul , 1024 ) ;
//    choose_sizes_add_resolution_w ( ul , 1280 ) ;
//    choose_sizes_add_resolution_w ( ul , 2048 ) ;
    choose_sizes_add_resolution_w ( ul , csid_w ) ;
  } else { // Vertical image
    choose_sizes_add_resolution_h ( ul , 250 ) ;
//    choose_sizes_add_resolution_h ( ul , 480 ) ;
//    choose_sizes_add_resolution_h ( ul , 600 ) ;
//    choose_sizes_add_resolution_h ( ul , 768 ) ;
//    choose_sizes_add_resolution_h ( ul , 1024 ) ;
//    choose_sizes_add_resolution_h ( ul , 2048 ) ;
    choose_sizes_add_resolution_h ( ul , csid_h ) ;
  }
  if ( !ul.firstChild ) return ; // No higher resolutions

  var file = document.getElementById ( "file" ) ;
  var table = document.createElement ( "table" ) ;
  var tbody = document.createElement ( "tbody" ) ;
  var td = choose_sizes_get_td () ;
  td.innerHTML = "<b>Use this image<br/>on your webpage</b>" ;
  td.style.textAlign = "left" ;
  td.style.borderLeft = "" ;
  ul.insertBefore ( td , ul.firstChild ) ;
  tbody.appendChild ( ul ) ;
  table.appendChild ( tbody ) ;
  file.insertBefore ( table , file.firstChild ) ;
}

function choose_sizes_get_image_dimensions () {
  var file = document.getElementById ( "file" ) ;
  if (!file) return;

  var linkinfile ;
  var linkinfiles = file.getElementsByTagName ( "a" ) ;
  for ( var i = 0 ; i < linkinfiles.length ; i++ ) {
    var s = linkinfiles[i].href ;
    if ( s.substr ( 0 , 14 ) != "http://upload." ) continue ;
    if ( s.substr ( 0 , 15 ) != "https://upload." ) continue ;
    linkinfile = linkinfiles[i] ;
    break ;
  }

  var bc = document.getElementById ( "bodyContent" ) ;
  var img = file.getElementsByTagName ( "img" ) [0] ;
  cs_full_url = linkinfile.href ;

  if ( !img ) return ;

  csid_tn_w = img.width ;
  csid_tn_h = img.height ;
 
  // Find image size in text
  for ( var i = 0 ; i < file.childNodes.length ; i++ ) {
    var j = file.childNodes[i].nodeValue ;
    if ( !j ) continue ;
    j = j.split("(",2).pop() ;
    j = j.split(" × ",2) ;
    csid_w = parseInt ( j.shift() ) ;
    csid_h = j.pop().split(" ",2).shift() ;
    break ;
  }
}


function choose_sizes_get_recursive_text ( obj ) {
  var ret = "" ;
  if ( obj.nodeValue ) ret += obj.nodeValue + " " ;  
  for ( var i = obj.firstChild ; i ; i = i.nextSibling ) {
    ret += choose_sizes_get_recursive_text ( i );
  }
  if ( ret != "" ) ret += " " ;
  return ret ;
}

function choose_sizes_detemine_author () {
  // Try Information template
  var td , th = document.getElementById ( "fileinfotpl_aut" ) ;
  if ( th ) td = th.parentNode.getElementsByTagName("TD")[0] ;
  if ( td ) cs_author = choose_sizes_get_recursive_text ( td ) ;
  if ( cs_author != "" ) return ;
}

function choose_sizes_detemine_licenses () {
  var cl = document.getElementById ( "catlinks" ) ;
  if ( !cl ) return ;
  var as = cl.getElementsByTagName ( "A" ) ;
  for ( var i = 0 ; i < as.length ; i++ ) {
    var title = as[i].title.split(":") ;
    if ( title[0].toLowerCase() != "category" ) continue ;
    title.shift();
    title = title.join(":") ;
    for ( var j = 0 ; j < cs_license_cats.length ; j++ ) {
      if ( title.substr(0,cs_license_cats[j].length).toLowerCase() != cs_license_cats[j].toLowerCase() ) continue ;
      if ( title.substr ( 0 , 2 ) == "PD" ) title = "Public domain" ;
      for ( var k = 0 ; k < cs_licenses.length ; k++ ) {
        if ( cs_licenses[k] != title ) continue ;
        title = "" ; // Already had that...
      }
      if ( title != "" ) cs_licenses.push ( title ) ;
      break ;
    }
  }
}