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.
 
 
 

393 lines
12 KiB

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