A Wikipedia user script to automate common TfD operations
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

tfdclerk.js 12 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // Templates for discussion clerk script by [[User:The Earwig]]
  2. // Development and bug reports: https://github.com/earwig/tfdclerk
  3. // To install, add:
  4. // importScript("User:The Earwig/tfdclerk.js"); // [[User:The Earwig/tfdclerk.js]]
  5. // to [[Special:MyPage/common.js]] or [[Special:MyPage/skin.js]]
  6. /*
  7. Copyright (C) 2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  8. Permission is hereby granted, free of charge, to any person obtaining a copy
  9. of this software and associated documentation files (the "Software"), to deal
  10. in the Software without restriction, including without limitation the rights to
  11. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  12. of the Software, and to permit persons to whom the Software is furnished to do
  13. so, subject to the following conditions:
  14. The above copyright notice and this permission notice shall be included in all
  15. copies or substantial portions of the Software.
  16. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. SOFTWARE.
  23. */
  24. var dependencies = [
  25. "mediawiki.api",
  26. "mediawiki.ui",
  27. "mediawiki.util",
  28. "jquery.ui.core"
  29. ];
  30. var is_tfd_page = function() {
  31. return mw.config.get("wgAction") == "view" &&
  32. mw.config.get("wgIsProbablyEditable") == true &&
  33. mw.config.get("wgRevisionId") == mw.config.get("wgCurRevisionId") &&
  34. mw.config.get("wgNamespaceNumber") == 4 && (
  35. mw.config.get("wgTitle") == "Templates for discussion" ||
  36. mw.config.get("wgTitle").indexOf("Templates for discussion/Log/2") == 0);
  37. }
  38. if (is_tfd_page()) {
  39. mw.loader.using(dependencies, function() {
  40. /* Main script starts here */
  41. TFDClerk = {
  42. api: new mw.Api(),
  43. sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0,
  44. // TODO: access time?
  45. _counter: 1,
  46. _wikitext_cache: {},
  47. _wikitext_cache_extra: {}
  48. };
  49. TFDClerk._get_today = function() {
  50. return new Date().toISOString().slice(0, 10);
  51. };
  52. TFDClerk._guard = function(head) {
  53. if (head.data("guard"))
  54. return false;
  55. head.data("guard", true);
  56. return true;
  57. };
  58. TFDClerk._unguard = function(head) {
  59. head.removeData("guard");
  60. };
  61. TFDClerk._get_section_number = function(head) {
  62. var url = head.find(".mw-editsection a").first().prop("href");
  63. var match = url.match(/section=(.*?)(\&|$)/);
  64. return match ? match[1] : null;
  65. };
  66. TFDClerk._error = function(box, msg, extra) {
  67. var elem = $("<span/>", {
  68. text: "Error: " + (extra ? msg + ": " : msg),
  69. style: "color: #A00;"
  70. });
  71. if (extra)
  72. elem.append($("<span/>", {
  73. text: extra,
  74. style: "font-family: monospace;"
  75. }));
  76. elem.insertAfter(box.find("h5"));
  77. box.find(".tfdclerk-submit").prop("disabled", true);
  78. };
  79. TFDClerk._with_section_content = function(head, box, callback) {
  80. var section = TFDClerk._get_section_number(head);
  81. if (section === null)
  82. return TFDClerk._error(box, "couldn't get section number");
  83. var cache = TFDClerk._wikitext_cache,
  84. extra_cache = TFDClerk._wikitext_cache_extra;
  85. if (section in cache) {
  86. if (cache[section] === null) {
  87. if (section in extra_cache)
  88. extra_cache[section].push(callback);
  89. else
  90. extra_cache[section] = [callback];
  91. } else
  92. callback(cache[section]);
  93. return;
  94. }
  95. cache[section] = null;
  96. TFDClerk.api.get({
  97. action: "query",
  98. prop: "revisions",
  99. rvprop: "content",
  100. rvsection: section,
  101. revids: mw.config.get("wgRevisionId")
  102. }).done(function(data) {
  103. var pageid = mw.config.get("wgArticleId");
  104. var content = data.query.pages[pageid].revisions[0]["*"];
  105. cache[section] = content;
  106. callback(content);
  107. if (section in extra_cache) {
  108. for (var i in extra_cache[section])
  109. extra_cache[section][i](content);
  110. }
  111. }).fail(function(err) {
  112. TFDClerk._error(box, "API query failure", err);
  113. }).always(function() {
  114. if (section in extra_cache)
  115. delete extra_cache[section];
  116. });
  117. };
  118. TFDClerk._remove_option_box = function(box) {
  119. var head = box.prev("h4");
  120. box.remove();
  121. TFDClerk._unguard(head);
  122. };
  123. TFDClerk._add_option_box = function(head, verb, title, callback, options) {
  124. var box_id = "tfdclerk-" + verb + "-box-" + TFDClerk._counter++;
  125. var box = $("<div/>", {
  126. id: box_id,
  127. addClass: "tfdclerk-" + verb + "-box"
  128. })
  129. .css("border", "1px solid #AAA")
  130. .css("color", "#000")
  131. .css("background-color", "#F9F9F9")
  132. .css("margin", "0.5em 0")
  133. .css("padding", "1em")
  134. .append($("<h5/>", {
  135. text: title,
  136. style: "margin: 0; padding: 0 0 0.25em 0;"
  137. }));
  138. options(box, head, box_id + "-");
  139. box.append($("<button/>", {
  140. id: box_id + "-submit",
  141. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  142. addClass: "tfdclerk-submit mw-ui-button mw-ui-progressive",
  143. style: "margin-right: 0.5em;",
  144. click: function() {
  145. callback(head, box);
  146. }
  147. }))
  148. .append($("<button/>", {
  149. id: box_id + "-cancel",
  150. text: "Cancel",
  151. addClass: "mw-ui-button",
  152. click: function() {
  153. TFDClerk._remove_option_box(box);
  154. }
  155. }))
  156. .insertAfter(head);
  157. };
  158. TFDClerk._add_option_table = function(box, options) {
  159. var table = $("<table/>", {style: "border-spacing: 0;"});
  160. $.each(options, function(i, opt) {
  161. table.append($("<tr/>")
  162. .append(
  163. $("<td/>", {
  164. style: "padding-bottom: 0.75em; padding-right: 0.5em;"
  165. }).append(opt[0]))
  166. .append(
  167. $("<td/>", {
  168. style: "padding-bottom: 0.75em;"
  169. }).append(opt[1]))
  170. );
  171. });
  172. box.append(table);
  173. };
  174. TFDClerk._do_close = function(head, box) {
  175. // TODO
  176. TFDClerk._remove_option_box(box);
  177. };
  178. TFDClerk._do_relist = function(head, box) {
  179. // TODO
  180. TFDClerk._remove_option_box(box);
  181. };
  182. TFDClerk._is_merge = function(head) {
  183. return head.nextUntil("h4").filter("p").first().find("b")
  184. .text() == "Propose merging";
  185. };
  186. TFDClerk._build_close_results = function(head) {
  187. if (TFDClerk._is_merge(head))
  188. var choices = ["Merge", "Do not merge", "No consensus"];
  189. else
  190. var choices = ["Delete", "Keep", "Redirect", "No consensus"];
  191. var elems = $("<div/>");
  192. $("<label/>").append($("<input/>", {
  193. name: "result-speedy",
  194. type: "checkbox",
  195. value: "true"
  196. })).append($("<span/>", {
  197. text: "Speedy",
  198. style: "margin: 0 1.25em 0 0.25em;"
  199. })).appendTo(elems);
  200. $.each(choices, function(i, choice) {
  201. $("<label/>").append($("<input/>", {
  202. name: "result",
  203. type: "radio",
  204. value: choice
  205. })).append($("<span/>", {
  206. text: choice,
  207. style: "margin: 0 1.25em 0 0.25em;"
  208. })).appendTo(elems);
  209. });
  210. $("<label/>").append($("<input/>", {
  211. name: "result",
  212. type: "radio",
  213. value: "Other"
  214. })).append($("<span/>", {
  215. text: "Other: ",
  216. style: "margin: 0 0.25em;"
  217. })).append($("<input/>", {
  218. name: "result-other",
  219. type: "text"
  220. })).appendTo(elems);
  221. return elems;
  222. };
  223. TFDClerk._add_close_actions = function(box, head) {
  224. TFDClerk._with_section_content(head, box, function(content) {
  225. var regex = /\{\{tfd links\|(.*?)(\||\}\})/gi,
  226. match = regex.exec(content);
  227. if (match === null)
  228. return TFDClerk._error(box, "no templates found in section");
  229. var actions = box.find(".tfdclerk-actions"),
  230. list = $("<ul/>", {style: "margin: 0 0 0 1em;"});
  231. do {
  232. var page = "Template:" + match[1];
  233. $("<li/>").append($("<a/>", {
  234. href: mw.util.getUrl(page),
  235. title: page,
  236. text: page
  237. })).appendTo(list);
  238. } while ((match = regex.exec(content)) !== null);
  239. actions.empty();
  240. actions.append(list);
  241. });
  242. };
  243. TFDClerk.close = function(head) {
  244. if (!TFDClerk._guard(head))
  245. return;
  246. TFDClerk._add_option_box(
  247. head, "close", "Closing discussion", TFDClerk._do_close,
  248. function(box, head, prefix) {
  249. TFDClerk._add_option_table(box, [
  250. [
  251. $("<span/>", {text: "Result:"}),
  252. TFDClerk._build_close_results(head)
  253. ],
  254. [
  255. $("<label/>", {
  256. for: prefix + "comments",
  257. text: "Comments:"
  258. }),
  259. $("<textarea/>", {
  260. id: prefix + "comments",
  261. name: "comments",
  262. rows: 2,
  263. cols: 60,
  264. placeholder: "Optional. Do not sign."
  265. })
  266. ],
  267. [
  268. $("<span/>", {text: "Actions:"}),
  269. $("<div/>", {addClass: "tfdclerk-actions"}).append(
  270. $("<span/>", {
  271. text: "Fetching...",
  272. style: "font-style: italic; color: #777;"
  273. })),
  274. ]
  275. ]);
  276. TFDClerk._add_close_actions(box, head);
  277. });
  278. };
  279. TFDClerk.relist = function(head) {
  280. if (!TFDClerk._guard(head))
  281. return;
  282. TFDClerk._add_option_box(
  283. head, "relist", "Relisting discussion", TFDClerk._do_relist,
  284. function(box, head, prefix) {
  285. TFDClerk._add_option_table(box, [
  286. [
  287. $("<label/>", {
  288. for: prefix + "date",
  289. text: "New date:"
  290. }),
  291. $("<input/>", {
  292. id: prefix + "date",
  293. name: "date",
  294. type: "date",
  295. value: TFDClerk._get_today()
  296. })
  297. ],
  298. [
  299. $("<label/>", {
  300. for: prefix + "comments",
  301. text: "Comments:"
  302. }),
  303. $("<textarea/>", {
  304. id: prefix + "comments",
  305. name: "comments",
  306. rows: 2,
  307. cols: 60,
  308. placeholder: "Optional. Do not sign."
  309. })
  310. ]
  311. ]);
  312. });
  313. };
  314. TFDClerk._build_hook = function(head, verb, callback) {
  315. return $("<span/>", {style: "margin-left: 1em;"})
  316. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  317. .append($("<a/>", {
  318. href: "#",
  319. text: verb,
  320. title: "tfdclerk: " + verb + " discussion",
  321. click: function() {
  322. callback($(head));
  323. return false;
  324. }
  325. }))
  326. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}));
  327. };
  328. TFDClerk.install = function() {
  329. $("h4").each(function(i, head) {
  330. if ($(head).next().hasClass("tfd-closed"))
  331. return;
  332. $("<span/>", {addClass: "tfdclerk-hooks"})
  333. .append(TFDClerk._build_hook(head, "close", TFDClerk.close))
  334. .append(TFDClerk._build_hook(head, "relist", TFDClerk.relist))
  335. .appendTo($(head).find(".mw-editsection"));
  336. });
  337. };
  338. $(TFDClerk.install);
  339. /* Main script ends here */
  340. });
  341. }