A Wikipedia user script to automate common TfD operations
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 

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