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.
 
 
 

294 lines
9.8 KiB

  1. // @TFDCLERK_HEADER_START@
  2. /*
  3. Copyright (C) 2015 Ben Kurtovic <ben.kurtovic@gmail.com>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights to
  7. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  8. of the Software, and to permit persons to whom the Software is furnished to do
  9. so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. // @TFDCLERK_HEADER_END@
  21. TFD.prototype._do_close = function() {
  22. // TODO
  23. // rough mockup:
  24. // - post-submit ui updates
  25. // - result options -> result string
  26. // - disable comments box
  27. // - collapse/disable/fix actions
  28. // - add progress info area
  29. // - "Closing discussion..."
  30. // - add discussion close tags with result and comment, optional nac
  31. // - " done ([[diff]])"
  32. // - interface for closing each template
  33. // - replace gray buttons with progressive refresh button
  34. };
  35. TFD.prototype._is_merge = function() {
  36. return this.head.nextUntil("h4").filter("p").first().find("b")
  37. .text() == "Propose merging";
  38. };
  39. TFD.prototype._get_close_action_choices = function() {
  40. // TODO
  41. return [{
  42. id: "none",
  43. name: "Do nothing",
  44. help: "The script will not modify this template or pages related to " +
  45. "it. You will need to carry out the closure yourself.",
  46. on_select: null,
  47. on_close: null,
  48. default: true
  49. }, {
  50. id: "holding-cell",
  51. name: "Move to holding cell",
  52. help: "The script will add {{being deleted}} to the template and " +
  53. "add an entry to the holding cell ([[WP:TFD/H]]).",
  54. on_select: null,
  55. on_close: null
  56. }];
  57. };
  58. TFD.prototype._on_close_result_change = function() {
  59. this._unblock_submit("no-close-result");
  60. // TODO: possibly disable/enable individual close actions
  61. };
  62. TFD.prototype._build_close_results = function() {
  63. if (this._is_merge())
  64. var choices = ["Merge", "Do not merge", "No consensus", "Other"];
  65. else
  66. var choices = ["Delete", "Keep", "Redirect", "No consensus", "Other"];
  67. var self = this;
  68. var elems = $("<div/>");
  69. $("<label/>").append($("<input/>", {
  70. name: "result-speedy",
  71. type: "checkbox",
  72. value: "true"
  73. })).append($("<span/>", {
  74. text: "Speedy",
  75. style: "margin: 0 1.25em 0 0.25em;"
  76. })).appendTo(elems);
  77. $.each(choices, function(i, choice) {
  78. $("<label/>").append($("<input/>", {
  79. name: "result",
  80. type: "radio",
  81. value: choice.toLowerCase(),
  82. change: function() { self._on_close_result_change(); }
  83. })).append($("<span/>", {
  84. text: choice,
  85. style: "margin: 0 1.25em 0 0.25em;"
  86. })).appendTo(elems);
  87. });
  88. var other = elems.children().last();
  89. other.find("span").text("Other:").css("margin", "0 0.25em");
  90. other.append($("<input/>", {
  91. name: "result-other",
  92. type: "text"
  93. }));
  94. this._block_submit("no-close-result");
  95. return elems;
  96. };
  97. TFD.prototype._on_backlink_summary = function(page, tlinfo, ntrans, nmtrans,
  98. nlinks) {
  99. tlinfo.empty().append($("<li/>").append($("<a/>", {
  100. href: mw.util.getUrl("Special:WhatLinksHere/" + page, {
  101. namespace: "",
  102. hidelinks: 1,
  103. hideredirs: 1,
  104. hidetrans: 0
  105. }),
  106. title: "Transclusions of " + page,
  107. text: ntrans + " transclusions"
  108. })));
  109. if (ntrans != 0)
  110. tlinfo.append($("<li/>").append($("<a/>", {
  111. href: mw.util.getUrl("Special:WhatLinksHere/" + page, {
  112. namespace: 0,
  113. hidelinks: 1,
  114. hideredirs: 1,
  115. hidetrans: 0
  116. }),
  117. title: "Mainspace transclusions of " + page,
  118. text: nmtrans + " in mainspace"
  119. })));
  120. if (nlinks != 0)
  121. tlinfo.append($("<li/>").append($("<a/>", {
  122. href: mw.util.getUrl("Special:WhatLinksHere/" + page, {
  123. namespace: "",
  124. hidelinks: 0,
  125. hideredirs: 0,
  126. hidetrans: 1
  127. }),
  128. title: "Mainspace links to " + page,
  129. text: nlinks + " mainspace links"
  130. })));
  131. };
  132. TFD.prototype._load_backlink_summary = function(page, tlinfo) {
  133. var limit = TFDClerk._sysop ? 5000 : 500;
  134. TFDClerk.api_get(this, {
  135. action: "query",
  136. list: "embeddedin|backlinks",
  137. eititle: page,
  138. eilimit: limit,
  139. bltitle: page,
  140. blnamespace: "0|10",
  141. bllimit: limit,
  142. blredirect: ""
  143. }, function(data) {
  144. var ntrans = data.query.embeddedin.length;
  145. var nmtrans = data.query.embeddedin.filter(function(pg) {
  146. return pg.ns == 0;
  147. }).length;
  148. var nlinks = data.query.backlinks.reduce(function(acc, pg) {
  149. var c = 0;
  150. if (pg.ns == 0)
  151. c++;
  152. if (pg.redirlinks)
  153. c += pg.redirlinks.filter(function(rl) {
  154. return rl.ns == 0;
  155. }).length;
  156. return acc + c;
  157. }, 0);
  158. if (data["continue"]) {
  159. if (data["continue"].eicontinue) {
  160. ntrans += "+";
  161. nmtrans += "+";
  162. }
  163. if (data["continue"].blcontinue)
  164. nlinks += "+";
  165. }
  166. this._on_backlink_summary(page, tlinfo, ntrans, nmtrans, nlinks);
  167. });
  168. };
  169. TFD.prototype._build_close_action_entry = function(page) {
  170. var self = this;
  171. var redlink = this.head.nextUntil("h4").filter("ul").first()
  172. .find("a").filter(function() { return $(this).text() == page; })
  173. .hasClass("new");
  174. var tlinfo = $("<ul/>", {style: "display: inline;"})
  175. .append(this._build_loading_node("li", "Fetching transclusions"));
  176. this._load_backlink_summary(page, tlinfo);
  177. var select_extra = $("<span/>");
  178. var help = $("<abbr/>", {text: "?"});
  179. var select = $("<select/>", {
  180. disabled: redlink,
  181. change: function() {
  182. var option = select.find("option:selected");
  183. help.prop("title", option.data("help"));
  184. if (option.data("select"))
  185. option.data("select").call(self, select_extra);
  186. }
  187. });
  188. $.each(this._get_close_action_choices(), function(i, choice) {
  189. select.append($("<option/>", {
  190. value: choice.id,
  191. text: choice.name,
  192. selected: choice.default
  193. }).data({
  194. help: choice.help,
  195. select: choice.on_select,
  196. close: choice.on_close
  197. }));
  198. if (choice.default)
  199. help.prop("title", choice.help);
  200. });
  201. return $("<li/>").append($("<a/>", {
  202. href: mw.util.getUrl(page),
  203. title: page,
  204. text: page,
  205. addClass: redlink ? "new" : "",
  206. style: "font-weight: bold;"
  207. })).append($("<span/>", {text: ": "}))
  208. .append(select)
  209. .append(select_extra)
  210. .append($("<span/>", {text: " ("}))
  211. .append(help)
  212. .append($("<span/>", {text: ") ("}))
  213. .append($("<div/>", {addClass: "hlist", style: "display: inline;"})
  214. .append(tlinfo))
  215. .append($("<span/>", {text: ")"}));
  216. };
  217. TFD.prototype._add_close_actions = function() {
  218. this._block_submit("add-close-actions");
  219. this._with_content(function(content) {
  220. var regex = /\*\s*\{\{tfd links\|(.*?)(\||\}\})/gi,
  221. match = regex.exec(content);
  222. if (match === null)
  223. return this._error("no templates found in section");
  224. var list = $("<ul/>", {style: "margin: 0 0 0 1em;"});
  225. do {
  226. var page = "Template:" + match[1];
  227. this._build_close_action_entry(page).appendTo(list);
  228. } while ((match = regex.exec(content)) !== null);
  229. this.box.find(".tfdclerk-actions").empty().append(list);
  230. this._unblock_submit("add-close-actions");
  231. });
  232. };
  233. TFD.prototype.close = function() {
  234. if (this._guard)
  235. return;
  236. this._guard = true;
  237. this._add_option_box("close", "Closing discussion", this._do_close,
  238. function() {
  239. this._add_option_table([
  240. [
  241. $("<span/>", {text: "Result:"}),
  242. this._build_close_results()
  243. ],
  244. [
  245. $("<label/>", {
  246. for: "tfdclerk-comments-" + this.id,
  247. text: "Comments:"
  248. }),
  249. $("<textarea/>", {
  250. id: "tfdclerk-comments-" + this.id,
  251. name: "comments",
  252. rows: 2,
  253. cols: 60,
  254. placeholder: "Optional. Do not sign."
  255. })
  256. ],
  257. [
  258. $("<span/>", {text: "Actions:"}),
  259. $("<div/>", {addClass: "tfdclerk-actions"})
  260. .append(this._build_loading_node("span", "Fetching")),
  261. ]
  262. ]);
  263. this._add_close_actions();
  264. });
  265. };