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.

tfd.js 8.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 = function(id, head) {
  22. this.id = id;
  23. this.head = head;
  24. this.box = null;
  25. this._guard = false;
  26. this._submit_blockers = [];
  27. this._wikitext = undefined;
  28. this._wikitext_callbacks = [];
  29. };
  30. TFD.prototype._block_submit = function(reason) {
  31. if (this._submit_blockers.indexOf(reason) < 0)
  32. this._submit_blockers.push(reason);
  33. this.box.find(".tfdclerk-submit").prop("disabled", true);
  34. };
  35. TFD.prototype._unblock_submit = function(reason) {
  36. var index = this._submit_blockers.indexOf(reason);
  37. if (index >= 0)
  38. this._submit_blockers.splice(index, 1);
  39. if (this._submit_blockers.length == 0)
  40. this.box.find(".tfdclerk-submit").prop("disabled", false);
  41. };
  42. TFD.prototype._error = function(msg, extra) {
  43. var elem = $("<span/>", {
  44. addClass: "tfdclerk-error",
  45. html: "<strong>Error:</strong> " + (extra ? msg + ": " : msg),
  46. style: "color: #A00;"
  47. });
  48. if (extra)
  49. elem.append($("<span/>", {
  50. text: extra,
  51. style: "font-family: monospace;"
  52. }));
  53. var contact = $("<a/>", {
  54. href: TFDClerk.author_url,
  55. title: TFDClerk.author_url.split("/").pop().replace(/_/g, " "),
  56. text: "contact me"
  57. }), file_bug = $("<a/>", {
  58. href: TFDClerk.github_url,
  59. title: TFDClerk.github_url.split("/").splice(-2).join("/"),
  60. text: "file a bug report"
  61. });
  62. elem.append($("<br/>"))
  63. .append($("<small/>", {
  64. html: "This may be caused by an edit conflict or other " +
  65. "intermittent problem. Try refreshing the page. If the error " +
  66. "persists, you can " + contact.prop("outerHTML") + " or " +
  67. file_bug.prop("outerHTML") + "."
  68. }));
  69. if (this.box.find(".tfdclerk-error"))
  70. this.box.find(".tfdclerk-error").remove();
  71. elem.insertAfter(this.box.find("h5"));
  72. this._block_submit("error");
  73. };
  74. TFD.prototype._get_section_edit_tag = function() {
  75. return this.head
  76. .find(".mw-editsection a:not(.mw-editsection-visualeditor)").first();
  77. };
  78. TFD.prototype._get_discussion_page = function() {
  79. var url = this._get_section_edit_tag().prop("href");
  80. var match = url.match(/title=(.*?)(\&|$)/);
  81. return match ? match[1] : null;
  82. };
  83. TFD.prototype._get_section_number = function() {
  84. var url = this._get_section_edit_tag().prop("href");
  85. var match = url.match(/section=(.*?)(\&|$)/);
  86. return match ? match[1] : null;
  87. };
  88. TFD.prototype._with_content = function(callback) {
  89. var title = this._get_discussion_page(),
  90. section = this._get_section_number();
  91. if (title === null || section === null)
  92. return this._error("couldn't find discussion section");
  93. if (this._wikitext !== undefined) {
  94. if (this._wikitext === null)
  95. this._wikitext_callbacks.push(callback);
  96. else
  97. callback.call(this, this._wikitext);
  98. return;
  99. }
  100. this._wikitext = null;
  101. TFDClerk.api_get(this, {
  102. action: "query",
  103. prop: "revisions",
  104. rvprop: "content",
  105. indexpageids: "",
  106. rvsection: section,
  107. titles: title
  108. }, function(data) {
  109. var pageid = data.query.pageids[0];
  110. var content = data.query.pages[pageid].revisions[0]["*"];
  111. this._wikitext = content;
  112. callback.call(this, content);
  113. for (var i in this._wikitext_callbacks)
  114. this._wikitext_callbacks[i].call(this, content);
  115. }, null, function() {
  116. this._wikitext_callbacks = [];
  117. });
  118. };
  119. TFD.prototype._remove_option_box = function() {
  120. this.box.remove();
  121. this.box = null;
  122. this._guard = false;
  123. this._submit_blockers = [];
  124. };
  125. TFD.prototype._add_option_box = function(verb, title, callback, options) {
  126. var self = this;
  127. this.box = $("<div/>", {
  128. id: "tfdclerk-" + verb + "-box-" + this.id,
  129. addClass: "tfdclerk-" + verb + "-box"
  130. })
  131. .css("position", "relative")
  132. .css("border", "1px solid #AAA")
  133. .css("color", "#000")
  134. .css("background-color", "#F9F9F9")
  135. .css("margin", "0.5em 0")
  136. .css("padding", "1em")
  137. .append($("<div/>")
  138. .css("position", "absolute")
  139. .css("right", "1em")
  140. .css("top", "0.5em")
  141. .css("font-size", "75%")
  142. .css("color", "#777")
  143. .append($("<a/>", {
  144. href: TFDClerk.script_url,
  145. title: "tfdclerk.js",
  146. text: "tfdclerk.js"
  147. }))
  148. .append($("<span/>", {text: " version " + TFDClerk.version})))
  149. .append($("<h5/>", {
  150. text: title,
  151. style: "margin: 0; padding: 0 0 0.25em 0;"
  152. }));
  153. options.call(this);
  154. this.box.append($("<button/>", {
  155. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  156. addClass: "tfdclerk-submit mw-ui-button mw-ui-progressive",
  157. style: "margin-right: 0.5em;",
  158. disabled: this._submit_blockers.length > 0,
  159. click: function() {
  160. self._block_submit("submitting");
  161. self.box.find(".tfdclerk-cancel").prop("disabled", true);
  162. callback.call(self);
  163. }
  164. }))
  165. .append($("<button/>", {
  166. text: "Cancel",
  167. addClass: "tfdclerk-cancel mw-ui-button",
  168. click: function() { self._remove_option_box(); }
  169. }))
  170. .insertAfter(this.head);
  171. };
  172. TFD.prototype._add_option_table = function(options) {
  173. var table = $("<table/>", {style: "border-spacing: 0;"});
  174. $.each(options, function(i, opt) {
  175. table.append($("<tr/>")
  176. .append(
  177. $("<td/>", {
  178. style: "padding-bottom: 0.75em; padding-right: 0.5em;"
  179. }).append(opt[0]))
  180. .append(
  181. $("<td/>", {
  182. style: "padding-bottom: 0.75em;"
  183. }).append(opt[1]))
  184. );
  185. });
  186. this.box.append(table);
  187. };
  188. TFD.prototype._build_loading_node = function(node, text) {
  189. return $("<" + node + "/>", {
  190. text: text + "...",
  191. style: "font-style: italic; color: #777;"
  192. });
  193. };
  194. TFD.prototype._build_hook = function(verb, callback) {
  195. var self = this;
  196. return $("<a/>", {
  197. href: "#",
  198. text: verb,
  199. title: "tfdclerk: " + verb + " discussion",
  200. click: function() {
  201. callback.call(self);
  202. return false;
  203. }
  204. });
  205. };
  206. TFD.prototype._build_hook_divider = function() {
  207. return $("<span/>", {
  208. addClass: "mw-editsection-divider",
  209. style: "display: inline;",
  210. text: " | "
  211. });
  212. };
  213. TFD.prototype.add_hooks = function() {
  214. $("<span/>", {addClass: "tfdclerk-hooks", style: "margin-left: 1em;"})
  215. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  216. .append(this._build_hook("close", this.close))
  217. .append(this._build_hook_divider())
  218. .append(this._build_hook("relist", this.relist))
  219. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}))
  220. .appendTo(this.head.find(".mw-editsection"));
  221. };