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.
 
 
 

347 lines
12 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: disable action options until transcluion info loads...
  41. // TODO: restrictions for close result
  42. // - ["delete", "keep", "redirect", "merge", "nomerge", "nocon", "other"];
  43. return [{
  44. id: "none",
  45. name: "Do nothing",
  46. help: "The script will not modify this template or pages related to " +
  47. "it. You will need to carry out the closure yourself.",
  48. on_select: null,
  49. on_close: null,
  50. restrict: {},
  51. default: true
  52. }, {
  53. id: "holding-cell",
  54. name: "Move to holding cell",
  55. help: "The script will add {{being deleted}} to the template and " +
  56. "add an entry to the holding cell ([[WP:TFD/H]]).",
  57. on_select: null, // TODO: auto-select "to merge" if merging
  58. on_close: null, // TODO
  59. restrict: {results: ["delete", "redirect", "merge", "nocon", "other"]}
  60. }, {
  61. id: "keep",
  62. name: "Keep", // TODO: clarify? esp. for merges
  63. help: "The script will remove {{tfd}} or {{tfm}} from the template " +
  64. "and add {{Old TfD}} to its talk page.",
  65. on_select: null,
  66. on_close: null, // TODO
  67. restrict: {results: ["keep", "merge", "nomerge", "nocon", "other"]} // SPLIT OFF "merge" FOR "merge target"?
  68. }, {
  69. id: "redirect",
  70. name: "Redirect", // TODO: clarify?
  71. help: "TODO",
  72. // redirect main and subpages and double-redirects to user-selected
  73. // target; add {{TFD end}} on talk [or give option to redirect talk
  74. // instead?]; "Ensure page protection for the redirect matches that of
  75. // the template which the page now redirects to, and that page
  76. // protection for the target template is appropriate for the number of
  77. // transclusions which may have increased after the redirection.")
  78. on_select: null,
  79. on_close: null,
  80. restrict: {merge: false} // TODO: allow for merges?
  81. }, {
  82. id: "review-and-delete",
  83. name: "Review and delete",
  84. help: "TODO",
  85. on_select: null,
  86. on_close: null,
  87. restrict: {merge: false, used: true}
  88. }, {
  89. id: "orphan-and-delete",
  90. name: "Orphan and delete",
  91. help: "TODO",
  92. on_select: null,
  93. on_close: null,
  94. restrict: {merge: false, used: true}
  95. }, {
  96. id: "substitute-and-delete",
  97. name: "Substitute and delete",
  98. help: "TODO",
  99. on_select: null,
  100. on_close: null,
  101. restrict: {merge: false, used: true}
  102. }, {
  103. id: "delete",
  104. name: "Delete",
  105. help: "TODO",
  106. on_select: null,
  107. on_close: null,
  108. restrict: {merge: false, used: false}
  109. }];
  110. };
  111. TFD.prototype._on_close_result_change = function() {
  112. this._unblock_submit("no-close-result");
  113. // TODO: possibly disable/enable individual close actions
  114. };
  115. TFD.prototype._build_close_results = function() {
  116. if (this._is_merge())
  117. var choices = [["Merge"], ["Do not merge", "nomerge"]];
  118. else
  119. var choices = [["Delete"], ["Keep"], ["Redirect"]];
  120. choices.push(["No consensus", "nocon"], ["Other"]);
  121. var self = this;
  122. var elems = $("<div/>");
  123. $("<label/>").append($("<input/>", {
  124. name: "result-speedy",
  125. type: "checkbox",
  126. value: "true"
  127. })).append($("<span/>", {
  128. addClass: "tfdclerk-result-option",
  129. text: "Speedy"
  130. })).appendTo(elems);
  131. $.each(choices, function(i, choice) {
  132. $("<label/>").append($("<input/>", {
  133. name: "result",
  134. type: "radio",
  135. value: choice[1] || choice[0].toLowerCase(),
  136. change: function() { self._on_close_result_change(); }
  137. })).append($("<span/>", {
  138. addClass: "tfdclerk-result-option",
  139. text: choice[0]
  140. })).appendTo(elems);
  141. });
  142. var other = elems.children().last();
  143. other.find("span").addClass("tfdclerk-result-other").text("Other:");
  144. other.append($("<input/>", {
  145. name: "result-other",
  146. type: "text"
  147. }));
  148. this._block_submit("no-close-result");
  149. return elems;
  150. };
  151. TFD.prototype._on_backlink_summary = function(page, tlinfo, ntrans, nmtrans,
  152. nlinks) {
  153. tlinfo.empty().append($("<li/>").append($("<a/>", {
  154. href: mw.util.getUrl("Special:WhatLinksHere/" + page, {
  155. namespace: "",
  156. hidelinks: 1,
  157. hideredirs: 1,
  158. hidetrans: 0
  159. }),
  160. title: "Transclusions of " + page,
  161. text: ntrans + " transclusions"
  162. })));
  163. if (ntrans != 0)
  164. tlinfo.append($("<li/>").append($("<a/>", {
  165. href: mw.util.getUrl("Special:WhatLinksHere/" + page, {
  166. namespace: 0,
  167. hidelinks: 1,
  168. hideredirs: 1,
  169. hidetrans: 0
  170. }),
  171. title: "Mainspace transclusions of " + page,
  172. text: nmtrans + " in mainspace"
  173. })));
  174. if (nlinks != 0)
  175. tlinfo.append($("<li/>").append($("<a/>", {
  176. href: mw.util.getUrl("Special:WhatLinksHere/" + page, {
  177. namespace: "",
  178. hidelinks: 0,
  179. hideredirs: 0,
  180. hidetrans: 1
  181. }),
  182. title: "Mainspace links to " + page,
  183. text: nlinks + " mainspace links"
  184. })));
  185. };
  186. TFD.prototype._load_backlink_summary = function(page, tlinfo) {
  187. var limit = TFDClerk._sysop ? 5000 : 500;
  188. TFDClerk.api_get(this, {
  189. action: "query",
  190. list: "embeddedin|backlinks",
  191. eititle: page,
  192. eilimit: limit,
  193. bltitle: page,
  194. blnamespace: "0|10",
  195. bllimit: limit,
  196. blredirect: ""
  197. }, function(data) {
  198. var ntrans = data.query.embeddedin.length;
  199. var nmtrans = data.query.embeddedin.filter(function(pg) {
  200. return pg.ns == 0;
  201. }).length;
  202. var nlinks = data.query.backlinks.reduce(function(acc, pg) {
  203. var c = 0;
  204. if (pg.ns == 0)
  205. c++;
  206. if (pg.redirlinks)
  207. c += pg.redirlinks.filter(function(rl) {
  208. return rl.ns == 0;
  209. }).length;
  210. return acc + c;
  211. }, 0);
  212. if (data["continue"]) {
  213. if (data["continue"].eicontinue) {
  214. ntrans += "+";
  215. nmtrans += "+";
  216. }
  217. if (data["continue"].blcontinue)
  218. nlinks += "+";
  219. }
  220. this._on_backlink_summary(page, tlinfo, ntrans, nmtrans, nlinks);
  221. });
  222. };
  223. TFD.prototype._build_close_action_entry = function(page) {
  224. var self = this;
  225. var redlink = this.head.nextUntil("h4").filter("ul").first()
  226. .find("a").filter(function() { return $(this).text() == page; })
  227. .hasClass("new");
  228. var tlinfo = $("<ul/>", {addClass: "tfdclerk-backlink-sum-list"})
  229. .append(this._build_loading_node("li", "Fetching transclusions"));
  230. this._load_backlink_summary(page, tlinfo);
  231. var select_extra = $("<span/>");
  232. var help = $("<abbr/>", {text: "?"});
  233. var select = $("<select/>", {
  234. disabled: redlink,
  235. change: function() {
  236. var option = select.find("option:selected");
  237. help.prop("title", option.data("help"));
  238. if (option.data("select"))
  239. option.data("select").call(self, select_extra);
  240. }
  241. });
  242. $.each(this._get_close_action_choices(), function(i, choice) {
  243. select.append($("<option/>", {
  244. value: choice.id,
  245. text: choice.name,
  246. selected: choice.default
  247. }).data({
  248. help: choice.help,
  249. select: choice.on_select,
  250. close: choice.on_close
  251. }));
  252. if (choice.default)
  253. help.prop("title", choice.help);
  254. });
  255. return $("<li/>").append($("<a/>", {
  256. href: mw.util.getUrl(page),
  257. title: page,
  258. text: page,
  259. addClass: "tfdclerk-action-page" + (redlink ? " new" : "")
  260. })).append($("<span/>", {text: ": "}))
  261. .append(select)
  262. .append(select_extra)
  263. .append($("<span/>", {text: " ("}))
  264. .append(help)
  265. .append($("<span/>", {text: ") ("}))
  266. .append($("<div/>", {addClass: "tfdclerk-backlink-sum-div hlist"})
  267. .append(tlinfo))
  268. .append($("<span/>", {text: ")"}));
  269. };
  270. TFD.prototype._add_close_actions = function() {
  271. this._block_submit("add-close-actions");
  272. this._with_content(function(content) {
  273. var regex = /\*\s*\{\{tfd links\|(.*?)(\||\}\})/gi,
  274. match = regex.exec(content);
  275. if (match === null)
  276. return this._error("no templates found in section");
  277. var list = $("<ul/>", {addClass: "tfdclerk-close-actions"});
  278. do {
  279. var page = "Template:" + match[1];
  280. this._build_close_action_entry(page).appendTo(list);
  281. } while ((match = regex.exec(content)) !== null);
  282. this.box.find(".tfdclerk-actions").empty().append(list);
  283. this._unblock_submit("add-close-actions");
  284. });
  285. };
  286. TFD.prototype.close = function() {
  287. if (this._guard)
  288. return;
  289. this._guard = true;
  290. this._add_option_box("close", "Closing discussion", this._do_close,
  291. function() {
  292. this._add_option_table([
  293. [
  294. $("<span/>", {text: "Result:"}),
  295. this._build_close_results()
  296. ],
  297. [
  298. $("<label/>", {
  299. for: "tfdclerk-comments-" + this.id,
  300. text: "Comments:"
  301. }),
  302. $("<textarea/>", {
  303. id: "tfdclerk-comments-" + this.id,
  304. name: "comments",
  305. rows: 2,
  306. cols: 60,
  307. placeholder: "Optional. Do not sign."
  308. })
  309. ],
  310. [
  311. $("<span/>", {text: "Actions:"}),
  312. $("<div/>", {addClass: "tfdclerk-actions"})
  313. .append(this._build_loading_node("span", "Fetching")),
  314. ]
  315. ]);
  316. this._add_close_actions();
  317. });
  318. };