Участник:Claymore/Wikibugs.js

Материал из Википедии — свободной энциклопедии
/*
; Original source: http://pl.wikipedia.org/wiki/MediaWiki:Wikibugs.js
; Russian adaptation: [[:ru:User:Александр Сигачёв]], [[:ru:User:Putnik]], [[:ru:User:LEMeZza]]
; English adaptation: [[:ru:User:Claymore]]
;
; Original idea: [[:pl:User:Dodek]], [[:pl:User:Adziura]]
; Implementation: [[:pl:User:Nux]], [[:pl:User:Saper]], [[:pl:User:Beau]], [[:ru:User:Putnik]]
*/

var wb$description = "Please give us the full description of the error you found. Cite your sources if you can."
var wb$badPages = new Array(
  "Wikipedia:Error reports",
  "Main page"
)
 
$(function()
{
  var el = document.getElementById('n-bug_in_article')
 
  if (el)
    el.getElementsByTagName('a')[0].onclick= wb$popWikibug
})
 
function wb$popWikibug()
{
  var link_wiki = window.wgArticlePath.replace(/\$1/, 'wiki')
  var link_tocreate = window.wgArticlePath.replace(/\$1/, 'Wikipedia:Requested articles')
  var link_bebold = window.wgArticlePath.replace(/\$1/, 'Wikipedia:Be_bold')
  var link_buglist = window.wgArticlePath.replace(/\$1/, 'Wikipedia:Error_reports')
 
  wb$popBugBoth("Wikipedia:Error reports", '\
    <div style="float:right;width:200px;padding:4px 10px;margin:2px 0px 0px 10px;font-size:90%;border:2px solid #900"><p><strong>Do not\u00A0report</strong> \u00A0errors on\u00A0other sites (for example, <strong>Facebook</strong> or <strong>Twitter</strong>), they will be ignored.</p>\
    <p>Please do not report about articles you can't find, you can add a <a href="' + link_tocreate + '">request for its creation</a> instead.</p></div>\
    <p style="margin-top:0px">If you found an error, please try to fix it yourself, the <a href="' + link_wiki + '">wiki</a> technology on this site enables you to do this. Do not hesitate, one of the main Wikipedian rules says: <a href="' + link_bebold + '">be bold</a>"! If you can't fix the error, report it using this form.</p><p>Do not post your telephone number or e-mail.</p>\
    <ul><li><a href="' + link_buglist + '">Current error reports.</a></li></ul>\
    ')
  return false
}
 
function wb$getEditToken(page)
{
  var objhttp = (window.XMLHttpRequest) ? new XMLHttpRequest(): new ActiveXObject('Microsoft.XMLHTTP')
  if (!objhttp)
    return
  objhttp.onreadystatechange=function() {
    if (objhttp.readyState == 4) {
      if (objhttp.status == 200) {
        var r_sti = /value="(\d+)" name=["']wpStarttime["']/
        var r_eti = /value="(\d+)" name=["']wpEdittime["']/
        var r_etk = /value="(.*?)" name=["']wpEditToken["']/
        var r_asm = /name="wpAutoSummary" type="hidden" value="(.*?)"/
        sti = r_sti.exec(objhttp.responseText)
        eri = r_eti.exec(objhttp.responseText)
        etk = r_etk.exec(objhttp.responseText)
        asm = r_asm.exec(objhttp.responseText)
        document.getElementById('Starttime').value=sti[1]
        document.getElementById('Edittime').value=eri[1]
        document.getElementById('EditToken').value=etk[1]
        document.getElementById('AutoSummary').value=asm[1]
      }
      else
        alert(objhttp.status)
    }
  }
  objhttp.open("GET", mw.config.get('wgServer')+mw.config.get('wgScript')+"?title="+encodeURIComponent(page)+"&action=edit")
  objhttp.send("")
}
 
function wb$isValidPageName(name)
{
  if (name == "")
    return false
  if (name.substr(0, 10) == "Special:")
    return false
  name = name.replace(/_/g, " ")
  for (var i = 0; i < wb$badPages.length; i++)
    if (name == wb$badPages[i])
      return false
 
  return true
}
 
function wb$checkForm(form)
{
  var page = form.wpSummary.value
  var content = form.wpTextbox1.value

  if (content=="" || content==wb$description || content.length<20 || !content.match(' ')) {
    alert("The description is too short, please expand it.")
    form.wpTextbox1.focus()
    return false
  }
 
  page = page.replace(/^http:\/\/en\.wikipedia\.org\/wiki\/(.+)$/, "$1")
  page = page.replace(/_/g, " ")
  page = decodeURIComponent(page)
 
  if (page == wgPageName.replace(/_/g, " ") && wb$isValidPageName(wgPageName)) {
    if (wgNamespaceNumber == 6) {
      page = "[[:File:"+wgTitle+"|"+wgTitle+"]]"
      content = "[[File:"+wgTitle+"|thumb|left|100px]]\n* "+content+"\r\n{{clear}}"
    }
    else {
      page = page.replace(/^(Category:|File:|\/)/, ":$1")
      page = "[["+page+"]]"
    }
  }
  else {
    page = page.replace(/\[\[([^\[\]\|]+)\|[^\[\]\|]+\]\]/g, "$1")
    page = page.replace(/[\[\]\|]/g, "")
    page = page.replace(/^\s+/g, "")
    page = page.replace(/\s+$/g, "")
 
    if (! wb$isValidPageName(page) ) {
      alert("Enter the page name.")
      if ( wb$isValidPageName(wgPageName) )
        form.wpSummary.value = wgPageName
      else {
        form.wpSummary.value = ""
        form.wpSummary.focus()
      }
      return false
    }
    if (page.indexOf(':') > 0)
      page = '[[:'+page+']]'
    else
      page = '[['+page+']]'
  }
 
  form.submit.disabled = 'disabled'
 
  if (wgUserName != null) 
    content += '\r\n\r\nPosted by: ~~'+'~~'
  else
    content += '\r\n\r\nPosted by: '+form.author.value+' ~~'+'~~'
 
  form.wpTextbox1.value = content
  form.wpSummary.value = page
 
  return true
}
 
function wb$goToEditPage()
{
  var edit_el = document.getElementById('ca-edit')
  var edit_href = window.wgArticlePath.replace(/\$1/, 'Wikipedia:Error_reports')
  if (edit_el)
    edit_href = edit_el.getElementsByTagName('a')[0].href
  window.location = edit_href
}
 
function wb$popBugBoth(action_page, infoHTML)
{
  var glob = document.body
 
  // shadow
  var nel = document.createElement('div')
  nel.id='specpop-globhidden'
  nel.style.cssText = 'background:white;filter:alpha(opacity=75);opacity:0.75;position:absolute;left:0px;top:0px;z-index:2000'
  nel.style.width = document.documentElement.scrollWidth+'px'
  nel.style.height= document.documentElement.scrollHeight+'px'
  glob.appendChild(nel)
 
  // move the dialog
  window.scroll(0, 150)
 
  // information
  var edit_el = document.getElementById('ca-edit')
  if (edit_el)
    var can_edit = true
  else
    var can_edit = false
 
  nel = document.createElement('div')
  nel.id='specpop-info'
  nel.style.cssText = 'font-size:13px;background:white;padding:21px 30px;border:1px solid black;position:absolute;width:500px;min-height:300px;top:200px;z-index:2002'
  if (nel.style.maxHeight==undefined)
    nel.style.height='300px' // IE blah
  var tmp = Math.floor(glob.clientWidth/2)-300
  if (tmp<5)
    tmp = 5
  nel.style.left = tmp+'px'
 
  nel.innerHTML	= infoHTML
  if (window.wgUserName == null)
    nel.innerHTML = nel.innerHTML + '<p><strong>Attention.</strong> Your IP address will be logged in the page history.</p>'
  nel.innerHTML	= nel.innerHTML + '<p style="text-align:center;margin-top:15px">\
    '+(can_edit ? '<input type="button" value="Fix it myselft" onclick="wb$goToEditPage()" />' : '')+ '\
    <input type="button" value="Report the error" onclick="wb$elementsRemove(\'specpop-info\')" />&nbsp;&nbsp;&nbsp;\
    <input type="button" value="Cancel" onclick="wb$elementsRemove(\'specpop-info\',\'specpop-form\',\'specpop-globhidden\',\'specpop-pos\')" />\
    </p>'
  glob.appendChild(nel)
 
  var action_url = window.wgServer + window.wgScript + "?title=" +encodeURIComponent(action_page)  + "&action=submit"
 
  // form
  nel = document.createElement('div')
  nel.id = 'specpop-form'
  nel.style.cssText = 'background:white;padding:5px 10px;border:1px solid black;position:absolute;width:330px;min-height:300px;top:200px;z-index:2001'
  if (nel.style.maxHeight==undefined)
    nel.style.height='300px' // IE blah
  nel.style.left = (Math.floor(glob.clientWidth/2)-165)+'px'
  //nel.style.top = (this.offsetTop-100)+'px'
  nel.innerHTML	= '<form id="fm1" action="'+action_url+'" method="post" enctype="multipart/form-data" onsubmit="return wb$checkForm(this)">\
    Page name:<br /><input type="text" name="wpSummary" id="wpSummary" style="width:320px" /><br />\
    <input type="hidden" name="wpSection" value="new" />\
    <input type="hidden" name="wpSave" value="Save" />\
    <input type="hidden" id="Starttime" name="wpStarttime" value="" />\
    <input type="hidden" id="Edittime" name="wpEdittime" value="" />\
    <input type="hidden" id="EditToken" name="wpEditToken" value="" />\
    <input type="hidden" id="AutoSummary" name="wpAutoSummary" value="" />\
    <input type="hidden" name="wpScrolltop" value="0" />\
    Message:<br /><textarea id="TextBox" name="wpTextbox1" style="width:320px;height:200px" onfocus="if (this.value == wb$description) {this.value = \'\'}">' + wb$description + '</textarea><br />\
    Signature:<input type="text" name="author" id="wikibug-input-author" /><br />\
    <input type="submit" id="submit" value="Send" /> &nbsp; \
    <input type="button" value="Cancel" onclick="wb$elementsRemove(\'specpop-form\',\'specpop-globhidden\',\'specpop-pos\')" />\
    </form>'
  glob.appendChild(nel)
  if (wb$isValidPageName(wgPageName))
    document.getElementById('wpSummary').value = wgPageName.replace(/_/g, " ")
 
  if (wgUserName != null) {
    var author = document.getElementById("wikibug-input-author")
    author.value = '~~'+'~~'
    author.disabled = 'disabled'
  }
  wb$getEditToken(action_page)
}
 
function wb$elementsRemove()
{
  var el
  for (var i=arguments.length-1; i>=0; i--) {
    el = document.getElementById(arguments[i])
    if (el)
      el.parentNode.removeChild(el)
  }
}