A Wikipedia user script to automate common TfD operations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

269 rivejä
9.2 KiB

  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.ui", "jquery.ui.core"], function() {
  25. if (mw.config.get("wgNamespaceNumber") != 4 || (
  26. mw.config.get("wgTitle") != "Templates for discussion" &&
  27. mw.config.get("wgTitle").indexOf("Templates for discussion/Log/2") != 0))
  28. return;
  29. TFDClerk = {
  30. sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0,
  31. counter: 1
  32. };
  33. TFDClerk._get_today = function() {
  34. return new Date().toISOString().slice(0, 10);
  35. };
  36. TFDClerk._guard = function(head) {
  37. if (head.data("guard"))
  38. return false;
  39. head.data("guard", true);
  40. return true;
  41. };
  42. TFDClerk._unguard = function(head) {
  43. head.removeData("guard");
  44. };
  45. TFDClerk._remove_option_box = function(box) {
  46. var head = box.prev("h4");
  47. box.remove();
  48. TFDClerk._unguard(head);
  49. };
  50. TFDClerk._add_option_box = function(head, verb, title, callback, options) {
  51. var box = $("<div/>", {
  52. id: "tfdclerk-" + verb + "-box-" + TFDClerk.counter++,
  53. addClass: "tfdclerk-" + verb + "-box"
  54. })
  55. .css("border", "1px solid #AAA")
  56. .css("background-color", "#F9F9F9")
  57. .css("margin", "0.5em 0")
  58. .css("padding", "1em")
  59. .append($("<h5/>", {text: title, style: "padding-top: 0;"}));
  60. options(box, head, box.prop("id") + "-");
  61. box.append($("<button/>", {
  62. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  63. addClass: "mw-ui-button mw-ui-progressive",
  64. style: "margin-right: 0.5em;",
  65. click: function() {
  66. callback(head, box);
  67. }
  68. }))
  69. .append($("<button/>", {
  70. text: "Cancel",
  71. addClass: "mw-ui-button",
  72. click: function() {
  73. TFDClerk._remove_option_box(box);
  74. }
  75. }))
  76. .insertAfter(head);
  77. };
  78. TFDClerk._add_option_table = function(box, options) {
  79. var table = $("<table/>");
  80. $.each(options, function(i, opt) {
  81. table.append($("<tr/>")
  82. .append(
  83. $("<td/>", {
  84. style: "padding: 0 0.5em 0.75em 0;"
  85. }).append(opt[0]))
  86. .append(
  87. $("<td/>", {
  88. style: "padding-bottom: 0.75em;"
  89. }).append(opt[1]))
  90. );
  91. });
  92. box.append(table);
  93. };
  94. TFDClerk._do_close = function(head, box) {
  95. // TODO
  96. TFDClerk._remove_option_box(box);
  97. };
  98. TFDClerk._do_relist = function(head, box) {
  99. // TODO
  100. TFDClerk._remove_option_box(box);
  101. };
  102. TFDClerk._is_merge = function(head) {
  103. return head.nextUntil("h4").filter("p").first().find("b")
  104. .text() == "Propose merging";
  105. };
  106. TFDClerk._build_close_results = function(box, head, prefix) {
  107. if (TFDClerk._is_merge(head))
  108. var choices = ["Merge", "Do not merge", "No consensus"];
  109. else
  110. var choices = ["Delete", "Keep", "Redirect", "No consensus"];
  111. var elems = $("<div/>");
  112. $("<label/>").append($("<input/>", {
  113. name: "result-speedy",
  114. type: "checkbox",
  115. value: "true"
  116. })).append($("<span/>", {
  117. text: "Speedy",
  118. style: "margin-right: 1em;"
  119. })).appendTo(elems);
  120. $.each(choices, function(i, choice) {
  121. $("<label/>").append($("<input/>", {
  122. name: "result",
  123. type: "radio",
  124. value: choice
  125. })).append($("<span/>", {
  126. text: choice,
  127. style: "margin-right: 1em;"
  128. })).appendTo(elems);
  129. });
  130. $("<label/>").append($("<input/>", {
  131. name: "result",
  132. type: "radio",
  133. value: "Other"
  134. })).append($("<span/>", {
  135. text: "Other: "
  136. })).append($("<input/>", {
  137. name: "result-other",
  138. type: "text"
  139. })).appendTo(elems);
  140. return elems;
  141. };
  142. TFDClerk._build_close_actions = function(box, head, prefix) {
  143. // TODO
  144. };
  145. TFDClerk.close = function(head) {
  146. if (!TFDClerk._guard(head))
  147. return;
  148. TFDClerk._add_option_box(
  149. head, "close", "Closing discussion", TFDClerk._do_close,
  150. function(box, head, prefix) {
  151. TFDClerk._add_option_table(box, [
  152. [
  153. $("<span/>", {text: "Result:"}),
  154. TFDClerk._build_close_results(box, head, prefix)
  155. ],
  156. [
  157. $("<label/>", {
  158. for: prefix + "comments",
  159. text: "Comments:"
  160. }),
  161. $("<textarea/>", {
  162. id: prefix + "comments",
  163. name: "comments",
  164. rows: 2,
  165. cols: 60,
  166. placeholder: "Optional. Do not sign."
  167. })
  168. ],
  169. [
  170. $("<span/>", {text: "Actions:"}),
  171. TFDClerk._build_close_actions(box, head, prefix)
  172. ]
  173. ]);
  174. });
  175. };
  176. TFDClerk.relist = function(head) {
  177. if (!TFDClerk._guard(head))
  178. return;
  179. TFDClerk._add_option_box(
  180. head, "relist", "Relisting discussion", TFDClerk._do_relist,
  181. function(box, head, prefix) {
  182. TFDClerk._add_option_table(box, [
  183. [
  184. $("<label/>", {
  185. for: prefix + "date",
  186. text: "New date:"
  187. }),
  188. $("<input/>", {
  189. id: prefix + "date",
  190. name: "date",
  191. type: "date",
  192. value: TFDClerk._get_today()
  193. })
  194. ],
  195. [
  196. $("<label/>", {
  197. for: prefix + "comments",
  198. text: "Comments:"
  199. }),
  200. $("<textarea/>", {
  201. id: prefix + "comments",
  202. name: "comments",
  203. rows: 2,
  204. cols: 60,
  205. placeholder: "Optional. Do not sign."
  206. })
  207. ]
  208. ]);
  209. });
  210. };
  211. TFDClerk._build_hook = function(head, verb, callback) {
  212. return $("<span/>", {style: "margin-left: 1em;"})
  213. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  214. .append($("<a/>", {
  215. href: "#",
  216. text: verb,
  217. title: "tfdclerk: " + verb + " discussion",
  218. click: function() {
  219. callback($(head));
  220. return false;
  221. }
  222. }))
  223. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}));
  224. };
  225. TFDClerk.install = function() {
  226. $("h4").each(function(i, head) {
  227. if ($(head).next().hasClass("tfd-closed"))
  228. return;
  229. $("<span/>", {addClass: "tfdclerk-hooks"})
  230. .append(TFDClerk._build_hook(head, "close", TFDClerk.close))
  231. .append(TFDClerk._build_hook(head, "relist", TFDClerk.relist))
  232. .appendTo($(head).find(".mw-editsection"));
  233. });
  234. };
  235. $(TFDClerk.install);
  236. });