A Wikipedia user script to automate common TfD operations
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

tfdclerk.js 11 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. mw.loader.using(["mediawiki.api", "mediawiki.ui", "jquery.ui.core"], function() {
  25. if (mw.config.get("wgAction") != "view" ||
  26. mw.config.get("wgIsProbablyEditable") != true ||
  27. mw.config.get("wgRevisionId") != mw.config.get("wgCurRevisionId") ||
  28. mw.config.get("wgNamespaceNumber") != 4 || (
  29. mw.config.get("wgTitle") != "Templates for discussion" &&
  30. mw.config.get("wgTitle").indexOf("Templates for discussion/Log/2") != 0))
  31. return;
  32. TFDClerk = {
  33. api: new mw.Api(),
  34. sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0,
  35. counter: 1
  36. };
  37. TFDClerk._get_today = function() {
  38. return new Date().toISOString().slice(0, 10);
  39. };
  40. TFDClerk._guard = function(head) {
  41. if (head.data("guard"))
  42. return false;
  43. head.data("guard", true);
  44. return true;
  45. };
  46. TFDClerk._unguard = function(head) {
  47. head.removeData("guard");
  48. };
  49. TFDClerk._get_section = function(head) {
  50. var url = head.find(".mw-editsection a").first().prop("href");
  51. var match = url.match(/section=(.*?)(\&|$)/);
  52. return match ? match[1] : null;
  53. };
  54. TFDClerk._error = function(box, msg, extra) {
  55. var elem = $("<span/>", {
  56. text: "Error: " + (extra ? msg + ": " : msg),
  57. style: "color: #A00;"
  58. });
  59. if (extra)
  60. elem.append($("<span/>", {
  61. text: extra,
  62. style: "font-family: monospace;"
  63. }));
  64. elem.insertAfter(box.find("h5"));
  65. $("#" + box.prop("id") + "-submit").prop("disabled", true);
  66. };
  67. TFDClerk._remove_option_box = function(box) {
  68. var head = box.prev("h4");
  69. box.remove();
  70. TFDClerk._unguard(head);
  71. };
  72. TFDClerk._add_option_box = function(head, verb, title, callback, options) {
  73. var box_id = "tfdclerk-" + verb + "-box-" + TFDClerk.counter++;
  74. var box = $("<div/>", {
  75. id: box_id,
  76. addClass: "tfdclerk-" + verb + "-box"
  77. })
  78. .css("border", "1px solid #AAA")
  79. .css("color", "#000")
  80. .css("background-color", "#F9F9F9")
  81. .css("margin", "0.5em 0")
  82. .css("padding", "1em")
  83. .append($("<h5/>", {
  84. text: title,
  85. style: "margin: 0; padding: 0 0 0.25em 0;"
  86. }));
  87. options(box, head, box_id + "-");
  88. box.append($("<button/>", {
  89. id: box_id + "-submit",
  90. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  91. addClass: "mw-ui-button mw-ui-progressive",
  92. style: "margin-right: 0.5em;",
  93. click: function() {
  94. callback(head, box);
  95. }
  96. }))
  97. .append($("<button/>", {
  98. id: box_id + "-cancel",
  99. text: "Cancel",
  100. addClass: "mw-ui-button",
  101. click: function() {
  102. TFDClerk._remove_option_box(box);
  103. }
  104. }))
  105. .insertAfter(head);
  106. };
  107. TFDClerk._add_option_table = function(box, options) {
  108. var table = $("<table/>", {style: "border-spacing: 0;"});
  109. $.each(options, function(i, opt) {
  110. table.append($("<tr/>")
  111. .append(
  112. $("<td/>", {
  113. style: "padding-bottom: 0.75em; padding-right: 0.5em;"
  114. }).append(opt[0]))
  115. .append(
  116. $("<td/>", {
  117. style: "padding-bottom: 0.75em;"
  118. }).append(opt[1]))
  119. );
  120. });
  121. box.append(table);
  122. };
  123. TFDClerk._do_close = function(head, box) {
  124. // TODO
  125. TFDClerk._remove_option_box(box);
  126. };
  127. TFDClerk._do_relist = function(head, box) {
  128. // TODO
  129. TFDClerk._remove_option_box(box);
  130. };
  131. TFDClerk._is_merge = function(head) {
  132. return head.nextUntil("h4").filter("p").first().find("b")
  133. .text() == "Propose merging";
  134. };
  135. TFDClerk._build_close_results = function(head) {
  136. if (TFDClerk._is_merge(head))
  137. var choices = ["Merge", "Do not merge", "No consensus"];
  138. else
  139. var choices = ["Delete", "Keep", "Redirect", "No consensus"];
  140. var elems = $("<div/>");
  141. $("<label/>").append($("<input/>", {
  142. name: "result-speedy",
  143. type: "checkbox",
  144. value: "true"
  145. })).append($("<span/>", {
  146. text: "Speedy",
  147. style: "margin: 0 1.25em 0 0.25em;"
  148. })).appendTo(elems);
  149. $.each(choices, function(i, choice) {
  150. $("<label/>").append($("<input/>", {
  151. name: "result",
  152. type: "radio",
  153. value: choice
  154. })).append($("<span/>", {
  155. text: choice,
  156. style: "margin: 0 1.25em 0 0.25em;"
  157. })).appendTo(elems);
  158. });
  159. $("<label/>").append($("<input/>", {
  160. name: "result",
  161. type: "radio",
  162. value: "Other"
  163. })).append($("<span/>", {
  164. text: "Other: ",
  165. style: "margin: 0 0.25em;"
  166. })).append($("<input/>", {
  167. name: "result-other",
  168. type: "text"
  169. })).appendTo(elems);
  170. return elems;
  171. };
  172. TFDClerk._add_close_actions = function(box, head, prefix) { // prefix?
  173. var section = TFDClerk._get_section(head);
  174. if (section === null)
  175. return TFDClerk._error(box, "couldn't get section number");
  176. TFDClerk.api.get({
  177. action: "query",
  178. prop: "revisions",
  179. rvprop: "content",
  180. rvsection: section,
  181. revids: mw.config.get("wgRevisionId")
  182. }).done(function(data) {
  183. // TODO
  184. console.log(data);
  185. }).fail(function(err) {
  186. TFDClerk._error(box, "API query failure", err);
  187. });
  188. };
  189. TFDClerk.close = function(head) {
  190. if (!TFDClerk._guard(head))
  191. return;
  192. TFDClerk._add_option_box(
  193. head, "close", "Closing discussion", TFDClerk._do_close,
  194. function(box, head, prefix) {
  195. TFDClerk._add_option_table(box, [
  196. [
  197. $("<span/>", {text: "Result:"}),
  198. TFDClerk._build_close_results(head)
  199. ],
  200. [
  201. $("<label/>", {
  202. for: prefix + "comments",
  203. text: "Comments:"
  204. }),
  205. $("<textarea/>", {
  206. id: prefix + "comments",
  207. name: "comments",
  208. rows: 2,
  209. cols: 60,
  210. placeholder: "Optional. Do not sign."
  211. })
  212. ],
  213. [
  214. $("<span/>", {text: "Actions:"}),
  215. $("<div/>", {id: prefix + "actions"}).append(
  216. $("<span/>", {
  217. text: "Fetching...",
  218. style: "font-style: italic; color: #777;"
  219. })),
  220. ]
  221. ]);
  222. TFDClerk._add_close_actions(box, head, prefix);
  223. });
  224. };
  225. TFDClerk.relist = function(head) {
  226. if (!TFDClerk._guard(head))
  227. return;
  228. TFDClerk._add_option_box(
  229. head, "relist", "Relisting discussion", TFDClerk._do_relist,
  230. function(box, head, prefix) {
  231. TFDClerk._add_option_table(box, [
  232. [
  233. $("<label/>", {
  234. for: prefix + "date",
  235. text: "New date:"
  236. }),
  237. $("<input/>", {
  238. id: prefix + "date",
  239. name: "date",
  240. type: "date",
  241. value: TFDClerk._get_today()
  242. })
  243. ],
  244. [
  245. $("<label/>", {
  246. for: prefix + "comments",
  247. text: "Comments:"
  248. }),
  249. $("<textarea/>", {
  250. id: prefix + "comments",
  251. name: "comments",
  252. rows: 2,
  253. cols: 60,
  254. placeholder: "Optional. Do not sign."
  255. })
  256. ]
  257. ]);
  258. });
  259. };
  260. TFDClerk._build_hook = function(head, verb, callback) {
  261. return $("<span/>", {style: "margin-left: 1em;"})
  262. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  263. .append($("<a/>", {
  264. href: "#",
  265. text: verb,
  266. title: "tfdclerk: " + verb + " discussion",
  267. click: function() {
  268. callback($(head));
  269. return false;
  270. }
  271. }))
  272. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}));
  273. };
  274. TFDClerk.install = function() {
  275. $("h4").each(function(i, head) {
  276. if ($(head).next().hasClass("tfd-closed"))
  277. return;
  278. $("<span/>", {addClass: "tfdclerk-hooks"})
  279. .append(TFDClerk._build_hook(head, "close", TFDClerk.close))
  280. .append(TFDClerk._build_hook(head, "relist", TFDClerk.relist))
  281. .appendTo($(head).find(".mw-editsection"));
  282. });
  283. };
  284. $(TFDClerk.install);
  285. });