A Wikipedia user script to automate common TfD operations
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

434 linhas
14 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. this._guard = false;
  53. this._submit_blockers = [];
  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._block_submit = function(reason) {
  88. if (this._submit_blockers.indexOf(reason) < 0)
  89. this._submit_blockers.push(reason);
  90. this.box.find(".tfdclerk-submit").prop("disabled", true);
  91. };
  92. TFD.prototype._unblock_submit = function(reason) {
  93. var index = this._submit_blockers.indexOf(reason);
  94. if (index >= 0)
  95. this._submit_blockers.splice(index, 1);
  96. if (this._submit_blockers.length == 0)
  97. this.box.find(".tfdclerk-submit").prop("disabled", false);
  98. };
  99. TFD.prototype._error = function(msg, extra) {
  100. // TODO: expand: advise refreshing, reporting persistent errors
  101. var elem = $("<span/>", {
  102. text: "Error: " + (extra ? msg + ": " : msg),
  103. style: "color: #A00;"
  104. });
  105. if (extra)
  106. elem.append($("<span/>", {
  107. text: extra,
  108. style: "font-family: monospace;"
  109. }));
  110. elem.insertAfter(this.box.find("h5"));
  111. this._block_submit("error");
  112. };
  113. TFD.prototype._get_section_number = function() {
  114. var url = this.head.find(".mw-editsection a").first().prop("href");
  115. var match = url.match(/section=(.*?)(\&|$)/);
  116. return match ? match[1] : null;
  117. };
  118. TFD.prototype._with_content = function(callback) {
  119. var section = this._get_section_number();
  120. if (section === null)
  121. return this._error("couldn't get section number");
  122. if (this._wikitext !== undefined) {
  123. if (this._wikitext === null)
  124. this._wikitext_callbacks.push(callback);
  125. else
  126. callback.call(this, this._wikitext);
  127. return;
  128. }
  129. this._wikitext = null;
  130. TFDClerk.api_get(this, {
  131. action: "query",
  132. prop: "revisions",
  133. rvprop: "content",
  134. rvsection: section,
  135. revids: mw.config.get("wgRevisionId")
  136. }, function(data) {
  137. var pageid = mw.config.get("wgArticleId");
  138. var content = data.query.pages[pageid].revisions[0]["*"];
  139. this._wikitext = content;
  140. callback.call(this, content);
  141. for (var i in this._wikitext_callbacks)
  142. this._wikitext_callbacks[i].call(this, content);
  143. }, function(error) {
  144. this._error("API query failure", error);
  145. }, function() {
  146. this._wikitext_callbacks = [];
  147. });
  148. };
  149. TFD.prototype._remove_option_box = function() {
  150. this.box.remove();
  151. this.box = null;
  152. this._guard = false;
  153. };
  154. TFD.prototype._add_option_box = function(verb, title, callback, options) {
  155. var self = this;
  156. this.box = $("<div/>", {
  157. id: "tfdclerk-" + verb + "-box-" + this.id,
  158. addClass: "tfdclerk-" + verb + "-box"
  159. })
  160. .css("border", "1px solid #AAA")
  161. .css("color", "#000")
  162. .css("background-color", "#F9F9F9")
  163. .css("margin", "0.5em 0")
  164. .css("padding", "1em")
  165. .append($("<h5/>", {
  166. text: title,
  167. style: "margin: 0; padding: 0 0 0.25em 0;"
  168. }));
  169. options.call(this);
  170. this.box.append($("<button/>", {
  171. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  172. addClass: "tfdclerk-submit mw-ui-button mw-ui-progressive",
  173. style: "margin-right: 0.5em;",
  174. disabled: this._submit_blockers.length > 0,
  175. click: function() { callback.call(self); }
  176. }))
  177. .append($("<button/>", {
  178. text: "Cancel",
  179. addClass: "mw-ui-button",
  180. click: function() { self._remove_option_box(); }
  181. }))
  182. .insertAfter(this.head);
  183. };
  184. TFD.prototype._add_option_table = function(options) {
  185. var table = $("<table/>", {style: "border-spacing: 0;"});
  186. $.each(options, function(i, opt) {
  187. table.append($("<tr/>")
  188. .append(
  189. $("<td/>", {
  190. style: "padding-bottom: 0.75em; padding-right: 0.5em;"
  191. }).append(opt[0]))
  192. .append(
  193. $("<td/>", {
  194. style: "padding-bottom: 0.75em;"
  195. }).append(opt[1]))
  196. );
  197. });
  198. this.box.append(table);
  199. };
  200. TFD.prototype._do_close = function() {
  201. // TODO
  202. this._remove_option_box();
  203. };
  204. TFD.prototype._do_relist = function() {
  205. // TODO
  206. this._remove_option_box();
  207. };
  208. TFD.prototype._is_merge = function() {
  209. return this.head.nextUntil("h4").filter("p").first().find("b")
  210. .text() == "Propose merging";
  211. };
  212. TFD.prototype._build_close_results = function() {
  213. if (this._is_merge())
  214. var choices = ["Merge", "Do not merge", "No consensus"];
  215. else
  216. var choices = ["Delete", "Keep", "Redirect", "No consensus"];
  217. var elems = $("<div/>");
  218. $("<label/>").append($("<input/>", {
  219. name: "result-speedy",
  220. type: "checkbox",
  221. value: "true"
  222. })).append($("<span/>", {
  223. text: "Speedy",
  224. style: "margin: 0 1.25em 0 0.25em;"
  225. })).appendTo(elems);
  226. $.each(choices, function(i, choice) {
  227. $("<label/>").append($("<input/>", {
  228. name: "result",
  229. type: "radio",
  230. value: choice
  231. })).append($("<span/>", {
  232. text: choice,
  233. style: "margin: 0 1.25em 0 0.25em;"
  234. })).appendTo(elems);
  235. });
  236. $("<label/>").append($("<input/>", {
  237. name: "result",
  238. type: "radio",
  239. value: "Other"
  240. })).append($("<span/>", {
  241. text: "Other: ",
  242. style: "margin: 0 0.25em;"
  243. })).append($("<input/>", {
  244. name: "result-other",
  245. type: "text"
  246. })).appendTo(elems);
  247. return elems;
  248. };
  249. TFD.prototype._add_close_actions = function() {
  250. this._block_submit("add-close-actions");
  251. this._with_content(function(content) {
  252. var regex = /\{\{tfd links\|(.*?)(\||\}\})/gi,
  253. match = regex.exec(content);
  254. if (match === null)
  255. return this._error("no templates found in section");
  256. var list = $("<ul/>", {style: "margin: 0 0 0 1em;"});
  257. do {
  258. var page = "Template:" + match[1];
  259. $("<li/>").append($("<a/>", {
  260. href: mw.util.getUrl(page),
  261. title: page,
  262. text: page
  263. })).appendTo(list);
  264. } while ((match = regex.exec(content)) !== null);
  265. this.box.find(".tfdclerk-actions").empty().append(list);
  266. this._unblock_submit("add-close-actions");
  267. });
  268. };
  269. TFD.prototype._get_relist_date = function() {
  270. var months = [
  271. "January", "February", "March", "April", "May", "June", "July",
  272. "August", "September", "October", "November", "December"];
  273. var date = new Date($("#tfdclerk-date-" + this.id).val());
  274. var month = months[date.getUTCMonth()];
  275. return date.getUTCFullYear() + " " + month + " " + date.getUTCDate();
  276. };
  277. TFD.prototype._on_date_change = function() {
  278. var date = this._get_relist_date();
  279. var this_date = mw.config.get("wgTitle").split("/Log/")[1];
  280. if (date == null || date == this_date)
  281. this._block_submit("bad-date");
  282. else
  283. this._unblock_submit("bad-date");
  284. };
  285. TFD.prototype.close = function() {
  286. if (this._guard)
  287. return;
  288. this._guard = true;
  289. this._add_option_box("close", "Closing discussion", this._do_close,
  290. function() {
  291. this._add_option_table([
  292. [
  293. $("<span/>", {text: "Result:"}),
  294. this._build_close_results()
  295. ],
  296. [
  297. $("<label/>", {
  298. for: "tfdclerk-comments-" + this.id,
  299. text: "Comments:"
  300. }),
  301. $("<textarea/>", {
  302. id: "tfdclerk-comments-" + this.id,
  303. name: "comments",
  304. rows: 2,
  305. cols: 60,
  306. placeholder: "Optional. Do not sign."
  307. })
  308. ],
  309. [
  310. $("<span/>", {text: "Actions:"}),
  311. $("<div/>", {addClass: "tfdclerk-actions"}).append(
  312. $("<span/>", {
  313. text: "Fetching...",
  314. style: "font-style: italic; color: #777;"
  315. })),
  316. ]
  317. ]);
  318. this._add_close_actions();
  319. });
  320. };
  321. TFD.prototype.relist = function() {
  322. var self = this;
  323. if (this._guard)
  324. return;
  325. this._guard = true;
  326. this._add_option_box("relist", "Relisting discussion", this._do_relist,
  327. function() {
  328. this._add_option_table([
  329. [
  330. $("<label/>", {
  331. for: "tfdclerk-date-" + this.id,
  332. text: "New date:"
  333. }),
  334. $("<input/>", {
  335. id: "tfdclerk-date-" + this.id,
  336. name: "date",
  337. type: "date",
  338. value: new Date().toDatePickerFormat(),
  339. change: function() { self._on_date_change(); }
  340. })
  341. ],
  342. [
  343. $("<label/>", {
  344. for: "tfdclerk-comments-" + this.id,
  345. text: "Comments:"
  346. }),
  347. $("<textarea/>", {
  348. id: "tfdclerk-comments-" + this.id,
  349. name: "comments",
  350. rows: 2,
  351. cols: 60,
  352. placeholder: "Optional. Do not sign."
  353. })
  354. ]
  355. ]);
  356. });
  357. };
  358. TFD.prototype._build_hook = function(verb, callback) {
  359. var self = this;
  360. return $("<span/>", {style: "margin-left: 1em;"})
  361. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  362. .append($("<a/>", {
  363. href: "#",
  364. text: verb,
  365. title: "tfdclerk: " + verb + " discussion",
  366. click: function() {
  367. callback.call(self);
  368. return false;
  369. }
  370. }))
  371. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}));
  372. };
  373. TFD.prototype.add_hooks = function() {
  374. $("<span/>", {addClass: "tfdclerk-hooks"})
  375. .append(this._build_hook("close", this.close))
  376. .append(this._build_hook("relist", this.relist))
  377. .appendTo(this.head.find(".mw-editsection"));
  378. };
  379. $(TFDClerk.install);
  380. /* Main script ends here */
  381. });
  382. }