跳转到内容

User:Hamish/massmove.js

维基百科,自由的百科全书
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**
* @description: 大量移動子頁面之工具。
* @usage / configuration:
  在使用者common.js中添加:
 mw.loader.load( '//wiki.gdrain.workers.dev/w/index.php?title=User:Hamish/massmove.js&action=raw&ctype=text/javascript' );
 mw.util.addPortletLink("p-tb", "//wiki.gdrain.workers.dev/wiki/Special:Massmove", "Mass move", "p-massmove", "Mass move");

  前往[[Special:Massmove]]填寫批次移動表單,僅限擁有suppressredirect之使用者使用。

* @revision: 03:00, 2 February 2019 (UTC)
**/

mw.loader.using(['mediawiki.api', 'mediawiki.Title'], function () {
 "use strict";

 var config = mw.config.get(['wgNamespaceNumber', 'wgTitle', 'wgUserGroups', 'skin']);

 function removeBlanks(arr) {
  var ret = [];
  var i, len;
  for (i = 0, len = arr.length; i < len; i++) {
   var s = arr[i];
   s = s.trim();
   if (s) {
    ret.push(s);
   }
  }
  return ret;
 }

 function replacePrefix(s, oldPrefix, newPrefix) {
  if (s.indexOf(oldPrefix) === 0) {
   s = s.substr(oldPrefix.length);
  }
  return newPrefix + s;
 }

 function doMassMove() {
  document.getElementById("wpMassMoveSubmit").disabled = true;
  var articles = document.getElementById("wpMassMovePages").value.split("\n");
  articles = removeBlanks(articles);
  if (!articles.length) {
   return;
  }
  var
   api = new mw.Api(),
   wpMassMoveReason = document.getElementById("wpMassMoveReason").value,
   wpMassMovePrefix1 = document.getElementById("wpMassMovePrefix1").value,
   wpMassMovePrefix2 = document.getElementById("wpMassMovePrefix2").value,
   wpMassMoveWatch = document.getElementById("wpMassMoveWatch").value,
   wpMassMoveNoRedirect = document.getElementById("wpMassMoveNoRedirect").checked,
   wpMassMoveMoveTalk = document.getElementById("wpMassMoveMoveTalk").checked,
   moved = 0,
   failed = [],
   error = [],
   deferreds = [],
   onSuccess = function () {
    moved++;
    document.getElementById("wpMassMoveSubmit").value = "(" + moved + ")";
   };

  function makeMoveFunc(article) {
   return function () {
    return $.Deferred(function (deferred) {
     var options = {
      format: 'json',
      action: 'move',
      watchlist: wpMassMoveWatch,
      from: article,
      to: replacePrefix(article, wpMassMovePrefix1, wpMassMovePrefix2),
      reason: wpMassMoveReason
     };
     if (wpMassMoveNoRedirect) {
      options.noredirect = '';
     }
     if (wpMassMoveMoveTalk) {
      options.movetalk = '';
     }
     var promise = api.postWithToken('move', options);
     promise.done(onSuccess);
     promise.fail(function (code, obj) {
      failed.push(article);
      error.push(obj.error.info);
     });
     promise.always(function () {
      deferred.resolve();
     });
    });
   };
  }

  
  var deferred = makeMoveFunc(articles[0])();
  for (var i = 1, len = articles.length; i < len; i++) {
   deferred = deferred.then(makeMoveFunc(articles[i]));
  }

  $.when(deferred).then(function () {
   document.getElementById("wpMassMoveSubmit").value = "Done (" + moved + ")";
   if (failed.length) {
    var $failedList = $('<ul>');
    for(var x = 0; x < failed.length; x++) {
  
     var failedTitle = mw.Title.newFromText(failed[x]);
     var $failedItem = $('<li>');
     if (failedTitle) {
      $failedItem.append( $('<a>')
       .attr('href', failedTitle.getUrl())
       .text(failed[x])
      );
     } else {
      $failedItem.text(failed[x]);
     }
     $failedItem.append(document.createTextNode(': ' + error[x]));
     $failedList.append($failedItem);
    }
    $('#wpMassMoveFailedContainer')
     .append($('<br />'))
     .append($('<b>')
      .text('Failed moves:')
     )
     .append($failedList);
   }
  });
 }
 
 function massMoveForm() {
  var bodyContent = (config.skin == "cologneblue" ? "article" : "bodyContent");
  document.getElementsByTagName("h1")[0].textContent = "批次移動頁面";
  document.title = "移動頁面";
  document.getElementById(bodyContent).innerHTML = '<div id="siteSub">維基百科,自由的百科全書</div>' +
   '<p>大量移動子頁面之工具,請勿濫用,以免遭致封禁。</p>' +
   '<br /><br />' +
   '<form id="wpMassMove" name="wpMassMove">' +
   '<b>如有濫用之行為,後果由<u>閣下</u>承擔。</b>' +
   '<div id="wpMassMoveFailedContainer"></div>' +
   '<br /><br />' +
'移動清單(每行一個頁面,不加[[]]):<br />' +
     '<textarea tabindex="1" accesskey="," name="wpMassMovePages" id="wpMassMovePages" rows="10" cols="80"></textarea>' +
    '<br /><br /><table style="background-color:transparent">' +
    '<tr><td>去除前綴(例如「Template:」):</td>' +
     '<td><input type="text" id="wpMassMovePrefix1" name="wpMassMovePrefix1" maxlength="255" /></td></tr>' +
    '<tr><td>加入前綴(例如「User:Jimbo Wales/」):</td>' +
     '<td><input type="text" id="wpMassMovePrefix2" name="wpMassMovePrefix2" maxlength="255" /></td></tr>' +
    '<tr><td>移動討論頁</td>' +
     '<td><input type="checkbox" id="wpMassMoveMoveTalk" name="wpMassMoveMoveTalk"/></td></tr>' +
    '<tr><td>不留重新導向</td>' +
     '<td><input type="checkbox" id="wpMassMoveNoRedirect" name="wpMassMoveNoRedirect"/></td></tr>' +
    '<tr><td>監視頁面</td>' +
      '<td><select id="wpMassMoveWatch">' +
       '<option value="nochange">維持原有設置</option>' +
       '<option value="preferences">參數設置</option>' +
       '<option value="watch">加入監視列表</option>' +
       '<option value="unwatch">取消監視</option>' +
      '</select></td></tr>' +
    '<tr><td>編輯摘要:</td>' +
     '<td><input type="text" id="wpMassMoveReason" name="wpMassMoveReason" maxlength="255" /></td></tr>' +
     '<tr><td><input type="button" id="wpMassMoveSubmit" name="wpMassMoveSubmit" value="移動" /></td>' +
   '</form>';
  document.getElementById("wpMassMoveSubmit").addEventListener("click", function (e) {
   doMassMove();
  });
 }
  
 if (config.wgNamespaceNumber == -1 &&
  config.wgTitle.toLowerCase() == "massmove" &&
  /sysop|patroller|rollbacker/.test(config.wgUserGroups)
 ) {
  massMoveForm();
 }

});