رُکُن:Sakura emad/Gadget-CatMaker.js

وِکیٖپیٖڈیا پؠٹھٕ، اَکھ آزاد اِنسایکلوپیٖڈیا

نوٹ: شایع کَرنہٕ پَتہ یِم تبدیلی وُچِھنہٕ خٲطرٕ کٔرِو براؤزرُک کیش (cache) صاف۔

  • فائرفاکس/ سفاری: ییلہ Reload پؠٹھ کلِک کٔرِو تہٕ Shift تھٲوِو دَبٲیِتھ، یا Ctrl-F5 یا Ctrl-R دَبٲیِو (Mac پؠٹھ R-⌘)
  • گوٗگُل کروم: Ctrl-Shift-R دَبٲیِو (Mac پؠٹھ Shift-R-⌘)
  • اِنٹَرنیٹ ایکسپلورَر: ییلہ Refresh پؠٹھ کِلِک کٔرِو Ctrl یا Ctrl-F5 تھٲوِو دَبٲیِتھ
  • اوپیرا: Tools → Preferences مَنٛز گٔژھو تہٕ کیش (cache) کٔرِو صاف
/*jslint browser: true, indent: 2*/
/*global mediaWiki, jQuery*/
/*!
 * @author User:Ebraminio, User:Yamaha5
 */
(function ($, mw) {
  'use strict';
//mw.notify('version '+'3');

  function createPage(title, text, summary, callback) {
    return new mw.Api().post({
      action: 'edit',
      title: title,
      text: text,
      summary: summary,
      createonly: '',
      minor: '',
      token: mw.user.tokens.get('csrfToken')
    }).then(
      function (data) {
        if (data.error && data.error.info) {
        	mw.notify(title + ' not done: ' + data.error.info);
        } else {
          callback();
        }
        return data;
      },
      function (data) {
      	mw.notify(title + ' Not done: ' + data);
        return data;
      }
    );
  }

  function faWikiName(title) {
    return $.ajax({
      url: '//en.wikipedia.org/w/api.php',
      data: {
        action: 'query',
        prop: 'langlinks',
        titles: title,
        redirects: 1,
        format: 'json',
        lllimit: 500
      },
      dataType: 'jsonp'
    }).then(function (data) {
      try {
        return $.grep($.map(data.query.pages, function (x) { return x; })[0].langlinks, function (x) {
          return x.lang === 'ks';
        })[0]['*'];
      } catch (ignore) { }
    });
  }

  function catParents(title) {
    return $.ajax({
      url: '//en.wikipedia.org/w/api.php',
      data: {
        action: 'query',
        prop: 'categories',
        titles: title,
        clshow: '!hidden',
        cllimit: 500,
        format: 'json'
      },
      dataType: 'jsonp'
    }).then(function (data) {
      try {
        return $.map($.map(data.query.pages, function (x) { return x; })[0].categories, function (x) {
          return x.title;
        });
      } catch (ignore) { }
    });
  }

  function getWikidataEntity(entitle) {
    return new mw.Api({ ajax: { url: '//www.wikidata.org/w/api.php' } }).get({
      action: 'wbgetentities',
      format: 'json',
      sites: 'enwiki',
      titles: entitle,
      normalize: 'true',
      origin: window.location.protocol + '//' + window.location.hostname
    }).then(function (x) { return $.map(x.entities, function (x) { return x; })[0]; });
  }

  function commonsCatFromEntity(entity) {
    try {
      return entity.claims.P373[0].mainsnak.datavalue.value;
    } catch (ignore) { }
  }

  function startCat(falink, enlink) {
    enlink=enlink.replace(/\_/g, ' ')
    return faWikiName(enlink).then(function (x) {
      if (x) {
        mw.notify('Note Done:the page was existed before');
        return;
      }
      return catParents(enlink).then(function (x) {
        if (!x) {
          mw.notify('page "' + enlink + '"does not exist on the specifed wiki');
          return;
        }
        return $.when.apply(null, $.map(x, function (y) {
          return faWikiName(y);
        })).then(function () {
          var catText = $.map(arguments, function (x) {
              return '[[' + x + ']]';
            }).join('\n') + '\n[[en:' + enlink + ']]';
          catText = catText.replace(/\[\[undefined\]\]/g, '');
          catText = catText.replace(/\n\n/g, '\n');
          catText=catText .trim()
          return createPage(
            falink,
            catText,
           'creating a cat equivalent to [[:en:' + enlink + ']] by Cat-creator tool',
            function () {
             mw.notify('creating ' + falink + ' Successfully executed');
            }
          );
        });
      });
    });
  }

  function init(e) {
    e.preventDefault();
    $('<div>' +
      '<div style="width: 40em">Write the class creation requests on separate lines:<br>زٲژ1@Cateogry1<br>زٲژ2@Cateogry2<br>زٲژ3@Cateogry3</div>' +
      '<textarea style="width: 100%; background-color: white;" placeholder="زٲژ1@Cateogry1" id="catmakerinput" rows="7"></textarea>' +
      '</div>'
      ).dialog({
      modal: true,
      width: 700,
      buttons: [{
        id: 'catmaker-button-import',
text: 'proceed',
click: function () {
          var formInput = $('#catmakerinput').val();
if (formInput === '') { mw.notify('Please fill the form properly'); return; }
$.when.apply(null, $.map(formInput.split('\n'), function (x) {
var req = x.replace(/زٲژ:/, '').replace(/Category:/i, '').split('@');
if (req.length !== 2) { return; }
return startCat('زٲژ:' + req[0], 'Category:' + req[1]);
})).then(function () {
            mw.notify('Category creation finished, you can discuss it now.');
            window.location = '/wiki/Special:Contributions/' + mw.config.get('wgUserName');
          });
        }
      }]
    });
  }

  mw.loader.using(['jquery.ui'], function () {
    $(function () {
      mw.util.addCSS('#t-catmaker { font-weight: normal; }');
      $(mw.util.addPortletLink('p-tb', '#', 'Cat-creator', 't-catmaker', 'automatically create cats')).click(init);
    });
  });
}(jQuery, mediaWiki));