// Remove stub tags from article 
// Originally by ], on a request by ].
// (])
 
// Modified slightly by CBM
 
// <source lang="javascript">
$(function(){
  var wgNamespaceNumber = mw.config.get('wgNamespaceNumber');
  var wgAction = mw.config.get('wgAction');
  if(wgNamespaceNumber==0&&wgAction=="view")
  {
    if(document.getElementById("ca-edit"))
        mw.loader.using( 'mediawiki.api', function () {
	       ( new mw.Api() ).get( {
               prop: 'revisions',
               rvprop: 'content',
               rvlimit: 1,
               indexpageids: true,
               titles: mw.config.get('wgPageName')
	       } ).done( function ( json ) {
            $.each(json.query.pages, function (pageid, page) {
               page = page.revisions;
               var len = page.length;
               var found = page.match(/\{\{ynkä.*?\}\}/);
               if (found !== null && len < 1000) {
                   var newText1 = 'Pituus: ' + len + ' tavua; Tynkä: <span style="color: yellow;">' + found + '</span>';
                   $('#contentSub').append(newText1);
               } else if (found !== null && len < 2000) {
                   var newText2 = 'Pituus: ' + len + ' tavua; Tynkä: <span style="color: orange;">' + found + '</span>';
                   $('#contentSub').append(newText2);
               } else if (found !== null && len < 3000) {
                   var newText3 = 'Pituus: ' + len + ' tavua; Tynkä: <span style="color: red;">' + found + '</span>';
                   $('#contentSub').append(newText3);
               } else if (found !== null && len > 3000) {
                   var newText4 = 'Pituus: ' + len + ' tavua; Tynkä: <span style="color: red; font-weight: bold;">' + found + '</span>';
                   $('#contentSub').append(newText4);
               }
            });
        });
    });
      mw.util.addPortletLink('p-cactions', 'javascript:rmstubtagtab()', 'Epätynkä', 'ca-nostubtag',
                                   'Poista tynkämerkintä tältä sivulta', '');
  }
  if(location.href.indexOf("&autormstubtag=")!=-1&&  
                 document.getElementById('wpTextbox1')!=null)
  {
    var x=decodeURIComponent(location.href.split("&autormstubtag="));
    if(x=="") x="stub"; else x+="-stub";
    var text = document.getElementById('wpTextbox1').value;
    text = text.replace(/\{\{ynkä.*?\}\}/,  '');
    document.getElementById('wpTextbox1').value = text ;
    document.getElementById('wpSummary').value=
      "ei tynkä";
    document.getElementById('wpDiff').click();
  }
});
 
function rmstubtagtab()
{
  location.href=mw.config.get('wgServer')+mw.config.get('wgScript')+"?title="+encodeURIComponent(mw.config.get('wgPageName'))+                                  
                "&action=edit&autormstubtag=1";
}