A Wikipedia user script to automate common TfD operations
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 

496 řádky
16 KiB

  1. // Templates for discussion clerk script by [[User:The Earwig]]
  2. // Version: @TFDCLERK_VERSION_FULL@
  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. script_url: "https://en.wikipedia.org/wiki/User:The_Earwig/tfdclerk.js",
  44. author_url: "https://en.wikipedia.org/wiki/User_talk:The_Earwig",
  45. github_url: "https://github.com/earwig/tfdclerk",
  46. tfds: [],
  47. _api: new mw.Api(),
  48. _sysop: $.inArray("sysop", mw.config.get("wgUserGroups")) >= 0
  49. // TODO: access time?
  50. };
  51. TFD = function(id, head) {
  52. this.id = id;
  53. this.head = head;
  54. this.box = null;
  55. this._guard = false;
  56. this._submit_blockers = [];
  57. this._wikitext = undefined;
  58. this._wikitext_callbacks = [];
  59. };
  60. TFDClerk.api_get = function(tfd, params, done, fail, always) {
  61. TFDClerk._api.get(params)
  62. .done(function(data) {
  63. if (done !== undefined)
  64. done.call(tfd, data);
  65. })
  66. .fail(function(error) {
  67. if (done !== undefined)
  68. fail.call(tfd, error);
  69. })
  70. .always(function() {
  71. if (always !== undefined)
  72. always.call(tfd);
  73. });
  74. };
  75. TFDClerk.install = function() {
  76. $("h4").each(function(i, elem) {
  77. var head = $(elem);
  78. if (head.next().hasClass("tfd-closed"))
  79. return;
  80. if (head.find(".mw-editsection").length == 0)
  81. return;
  82. var tfd = new TFD(TFDClerk.tfds.length, head);
  83. TFDClerk.tfds.push(tfd);
  84. tfd.add_hooks();
  85. });
  86. };
  87. Date.prototype.toDatePickerFormat = function() {
  88. return this.toISOString().slice(0, 10);
  89. }
  90. TFD.prototype._block_submit = function(reason) {
  91. if (this._submit_blockers.indexOf(reason) < 0)
  92. this._submit_blockers.push(reason);
  93. this.box.find(".tfdclerk-submit").prop("disabled", true);
  94. };
  95. TFD.prototype._unblock_submit = function(reason) {
  96. var index = this._submit_blockers.indexOf(reason);
  97. if (index >= 0)
  98. this._submit_blockers.splice(index, 1);
  99. if (this._submit_blockers.length == 0)
  100. this.box.find(".tfdclerk-submit").prop("disabled", false);
  101. };
  102. TFD.prototype._error = function(msg, extra) {
  103. var elem = $("<span/>", {
  104. text: "Error: " + (extra ? msg + ": " : msg),
  105. style: "color: #A00;"
  106. });
  107. if (extra)
  108. elem.append($("<span/>", {
  109. text: extra,
  110. style: "font-family: monospace;"
  111. }));
  112. var contact = $("<a/>", {
  113. href: TFDClerk.author_url,
  114. title: TFDClerk.author_url.split("/").pop().replace(/_/g, " "),
  115. text: "contact me"
  116. }), file_bug = $("<a/>", {
  117. href: TFDClerk.github_url,
  118. title: TFDClerk.github_url.split("/").splice(-2).join("/"),
  119. text: "file a bug report"
  120. });
  121. elem.append($("<br/>"))
  122. .append($("<small/>", {
  123. html: "This may be caused by an edit conflict or other " +
  124. "intermittent problem. Try refreshing the page. If the error " +
  125. "persists, you can " + contact.prop("outerHTML") + " or " +
  126. file_bug.prop("outerHTML") + "."
  127. }));
  128. elem.insertAfter(this.box.find("h5"));
  129. this._block_submit("error");
  130. };
  131. TFD.prototype._get_section_number = function() {
  132. var url = this.head.find(".mw-editsection a").first().prop("href");
  133. var match = url.match(/section=(.*?)(\&|$)/);
  134. return match ? match[1] : null;
  135. };
  136. TFD.prototype._with_content = function(callback) {
  137. var section = this._get_section_number();
  138. if (section === null)
  139. return this._error("couldn't get section number");
  140. if (this._wikitext !== undefined) {
  141. if (this._wikitext === null)
  142. this._wikitext_callbacks.push(callback);
  143. else
  144. callback.call(this, this._wikitext);
  145. return;
  146. }
  147. this._wikitext = null;
  148. TFDClerk.api_get(this, {
  149. action: "query",
  150. prop: "revisions",
  151. rvprop: "content",
  152. rvsection: section,
  153. revids: mw.config.get("wgRevisionId")
  154. }, function(data) {
  155. var pageid = mw.config.get("wgArticleId");
  156. var content = data.query.pages[pageid].revisions[0]["*"];
  157. this._wikitext = content;
  158. callback.call(this, content);
  159. for (var i in this._wikitext_callbacks)
  160. this._wikitext_callbacks[i].call(this, content);
  161. }, function(error) {
  162. this._error("API query failure", error);
  163. }, function() {
  164. this._wikitext_callbacks = [];
  165. });
  166. };
  167. TFD.prototype._remove_option_box = function() {
  168. this.box.remove();
  169. this.box = null;
  170. this._guard = false;
  171. this._submit_blockers = [];
  172. };
  173. TFD.prototype._add_option_box = function(verb, title, callback, options) {
  174. var self = this;
  175. this.box = $("<div/>", {
  176. id: "tfdclerk-" + verb + "-box-" + this.id,
  177. addClass: "tfdclerk-" + verb + "-box"
  178. })
  179. .css("position", "relative")
  180. .css("border", "1px solid #AAA")
  181. .css("color", "#000")
  182. .css("background-color", "#F9F9F9")
  183. .css("margin", "0.5em 0")
  184. .css("padding", "1em")
  185. .append($("<div/>")
  186. .css("position", "absolute")
  187. .css("right", "1em")
  188. .css("top", "0.5em")
  189. .css("font-size", "75%")
  190. .css("color", "#777")
  191. .append($("<a/>", {
  192. href: TFDClerk.script_url,
  193. title: "tfdclerk.js",
  194. text: "tfdclerk.js"
  195. }))
  196. .append($("<span/>", {text: " version @TFDCLERK_VERSION@"})))
  197. .append($("<h5/>", {
  198. text: title,
  199. style: "margin: 0; padding: 0 0 0.25em 0;"
  200. }));
  201. options.call(this);
  202. this.box.append($("<button/>", {
  203. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  204. addClass: "tfdclerk-submit mw-ui-button mw-ui-progressive",
  205. style: "margin-right: 0.5em;",
  206. disabled: this._submit_blockers.length > 0,
  207. click: function() {
  208. self._block_submit("submitting");
  209. self.box.find(".tfdclerk-cancel").prop("disabled", true);
  210. callback.call(self);
  211. }
  212. }))
  213. .append($("<button/>", {
  214. text: "Cancel",
  215. addClass: "tfdclerk-cancel mw-ui-button",
  216. click: function() { self._remove_option_box(); }
  217. }))
  218. .insertAfter(this.head);
  219. };
  220. TFD.prototype._add_option_table = function(options) {
  221. var table = $("<table/>", {style: "border-spacing: 0;"});
  222. $.each(options, function(i, opt) {
  223. table.append($("<tr/>")
  224. .append(
  225. $("<td/>", {
  226. style: "padding-bottom: 0.75em; padding-right: 0.5em;"
  227. }).append(opt[0]))
  228. .append(
  229. $("<td/>", {
  230. style: "padding-bottom: 0.75em;"
  231. }).append(opt[1]))
  232. );
  233. });
  234. this.box.append(table);
  235. };
  236. TFD.prototype._do_close = function() {
  237. // TODO
  238. // rough mockup:
  239. // - post-submit ui updates
  240. // - result options -> result string
  241. // - disable comments box
  242. // - collapse/disable/fix actions
  243. // - add progress info area
  244. // - "Closing discussion..."
  245. // - add discussion close tags with result and comment, optional nac
  246. // - " done ([[diff]])"
  247. // - interface for closing each template
  248. // - replace gray buttons with progressive refresh button
  249. };
  250. TFD.prototype._do_relist = function() {
  251. // TODO
  252. // rough mockup:
  253. // - post-submit ui updates
  254. // - date input box -> link to new discussion log page
  255. // - disable comments box
  256. // - add progress info area
  257. // - "Adding discussion to new date..."
  258. // - add discussion to new date, plus relist template
  259. // - " done ([[diff]])"
  260. // - "Removing discussion from old date..."
  261. // - replace contents of section with {{relisted}}
  262. // - " done ([[diff]])"
  263. // - "Updating discussion link on template(s):\n* [[Template:A]]..."
  264. // - update |link param of {{template for discussion}}
  265. // - " done ([[diff]])"
  266. // - replace gray buttons with progressive refresh button
  267. };
  268. TFD.prototype._is_merge = function() {
  269. return this.head.nextUntil("h4").filter("p").first().find("b")
  270. .text() == "Propose merging";
  271. };
  272. TFD.prototype._build_close_results = function() {
  273. if (this._is_merge())
  274. var choices = ["Merge", "Do not merge", "No consensus"];
  275. else
  276. var choices = ["Delete", "Keep", "Redirect", "No consensus"];
  277. var elems = $("<div/>");
  278. $("<label/>").append($("<input/>", {
  279. name: "result-speedy",
  280. type: "checkbox",
  281. value: "true"
  282. })).append($("<span/>", {
  283. text: "Speedy",
  284. style: "margin: 0 1.25em 0 0.25em;"
  285. })).appendTo(elems);
  286. $.each(choices, function(i, choice) {
  287. $("<label/>").append($("<input/>", {
  288. name: "result",
  289. type: "radio",
  290. value: choice
  291. })).append($("<span/>", {
  292. text: choice,
  293. style: "margin: 0 1.25em 0 0.25em;"
  294. })).appendTo(elems);
  295. });
  296. $("<label/>").append($("<input/>", {
  297. name: "result",
  298. type: "radio",
  299. value: "Other"
  300. })).append($("<span/>", {
  301. text: "Other: ",
  302. style: "margin: 0 0.25em;"
  303. })).append($("<input/>", {
  304. name: "result-other",
  305. type: "text"
  306. })).appendTo(elems);
  307. return elems;
  308. };
  309. TFD.prototype._add_close_actions = function() {
  310. this._block_submit("add-close-actions");
  311. this._with_content(function(content) {
  312. var regex = /\{\{tfd links\|(.*?)(\||\}\})/gi,
  313. match = regex.exec(content);
  314. if (match === null)
  315. return this._error("no templates found in section");
  316. var list = $("<ul/>", {style: "margin: 0 0 0 1em;"});
  317. do {
  318. var page = "Template:" + match[1];
  319. $("<li/>").append($("<a/>", {
  320. href: mw.util.getUrl(page),
  321. title: page,
  322. text: page
  323. })).appendTo(list);
  324. } while ((match = regex.exec(content)) !== null);
  325. this.box.find(".tfdclerk-actions").empty().append(list);
  326. this._unblock_submit("add-close-actions");
  327. });
  328. };
  329. TFD.prototype._get_relist_date = function() {
  330. var months = [
  331. "January", "February", "March", "April", "May", "June", "July",
  332. "August", "September", "October", "November", "December"];
  333. var date = new Date($("#tfdclerk-date-" + this.id).val());
  334. var month = months[date.getUTCMonth()];
  335. return date.getUTCFullYear() + " " + month + " " + date.getUTCDate();
  336. };
  337. TFD.prototype._on_date_change = function() {
  338. var date = this._get_relist_date();
  339. var this_date = mw.config.get("wgTitle").split("/Log/")[1];
  340. if (date == null || date == this_date)
  341. this._block_submit("bad-date");
  342. else
  343. this._unblock_submit("bad-date");
  344. };
  345. TFD.prototype.close = function() {
  346. if (this._guard)
  347. return;
  348. this._guard = true;
  349. this._add_option_box("close", "Closing discussion", this._do_close,
  350. function() {
  351. this._add_option_table([
  352. [
  353. $("<span/>", {text: "Result:"}),
  354. this._build_close_results()
  355. ],
  356. [
  357. $("<label/>", {
  358. for: "tfdclerk-comments-" + this.id,
  359. text: "Comments:"
  360. }),
  361. $("<textarea/>", {
  362. id: "tfdclerk-comments-" + this.id,
  363. name: "comments",
  364. rows: 2,
  365. cols: 60,
  366. placeholder: "Optional. Do not sign."
  367. })
  368. ],
  369. [
  370. $("<span/>", {text: "Actions:"}),
  371. $("<div/>", {addClass: "tfdclerk-actions"}).append(
  372. $("<span/>", {
  373. text: "Fetching...",
  374. style: "font-style: italic; color: #777;"
  375. })),
  376. ]
  377. ]);
  378. this._add_close_actions();
  379. });
  380. };
  381. TFD.prototype.relist = function() {
  382. var self = this;
  383. if (this._guard)
  384. return;
  385. this._guard = true;
  386. this._add_option_box("relist", "Relisting discussion", this._do_relist,
  387. function() {
  388. this._add_option_table([
  389. [
  390. $("<label/>", {
  391. for: "tfdclerk-date-" + this.id,
  392. text: "New date:"
  393. }),
  394. $("<input/>", {
  395. id: "tfdclerk-date-" + this.id,
  396. name: "date",
  397. type: "date",
  398. value: new Date().toDatePickerFormat(),
  399. change: function() { self._on_date_change(); }
  400. })
  401. ],
  402. [
  403. $("<label/>", {
  404. for: "tfdclerk-comments-" + this.id,
  405. text: "Comments:"
  406. }),
  407. $("<textarea/>", {
  408. id: "tfdclerk-comments-" + this.id,
  409. name: "comments",
  410. rows: 2,
  411. cols: 60,
  412. placeholder: "Optional. Do not sign."
  413. })
  414. ]
  415. ]);
  416. });
  417. };
  418. TFD.prototype._build_hook = function(verb, callback) {
  419. var self = this;
  420. return $("<span/>", {style: "margin-left: 1em;"})
  421. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  422. .append($("<a/>", {
  423. href: "#",
  424. text: verb,
  425. title: "tfdclerk: " + verb + " discussion",
  426. click: function() {
  427. callback.call(self);
  428. return false;
  429. }
  430. }))
  431. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}));
  432. };
  433. TFD.prototype.add_hooks = function() {
  434. $("<span/>", {addClass: "tfdclerk-hooks"})
  435. .append(this._build_hook("close", this.close))
  436. .append(this._build_hook("relist", this.relist))
  437. .appendTo(this.head.find(".mw-editsection"));
  438. };
  439. $(TFDClerk.install);
  440. /* Main script ends here */
  441. });
  442. }