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.
 
 
 

230 lines
7.5 KiB

  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. });
  47. if (extra)
  48. elem.append($("<span/>", {
  49. addClass: "tfdclerk-error-extra",
  50. text: extra
  51. }));
  52. var contact = $("<a/>", {
  53. href: TFDClerk.author_url,
  54. title: TFDClerk.author_url.split("/").pop().replace(/_/g, " "),
  55. text: "contact me"
  56. }), file_bug = $("<a/>", {
  57. href: TFDClerk.github_url,
  58. title: TFDClerk.github_url.split("/").splice(-2).join("/"),
  59. text: "file a bug report"
  60. });
  61. elem.append($("<br/>"))
  62. .append($("<small/>", {
  63. html: "This may be caused by an edit conflict or other " +
  64. "intermittent problem. Try refreshing the page. If the error " +
  65. "persists, you can " + contact.prop("outerHTML") + " or " +
  66. file_bug.prop("outerHTML") + "."
  67. }));
  68. if (this.box.find(".tfdclerk-error"))
  69. this.box.find(".tfdclerk-error").remove();
  70. elem.insertAfter(this.box.find("h5"));
  71. this._block_submit("error");
  72. };
  73. TFD.prototype._get_section_edit_tag = function() {
  74. return this.head
  75. .find(".mw-editsection a:not(.mw-editsection-visualeditor)").first();
  76. };
  77. TFD.prototype._get_discussion_page = function() {
  78. var url = this._get_section_edit_tag().prop("href");
  79. var match = url.match(/title=(.*?)(\&|$)/);
  80. return match ? match[1] : null;
  81. };
  82. TFD.prototype._get_section_number = function() {
  83. var url = this._get_section_edit_tag().prop("href");
  84. var match = url.match(/section=(.*?)(\&|$)/);
  85. return match ? match[1] : null;
  86. };
  87. TFD.prototype._with_content = function(callback) {
  88. var title = this._get_discussion_page(),
  89. section = this._get_section_number();
  90. if (title === null || section === null)
  91. return this._error("couldn't find discussion section");
  92. if (this._wikitext !== undefined) {
  93. if (this._wikitext === null)
  94. this._wikitext_callbacks.push(callback);
  95. else
  96. callback.call(this, this._wikitext);
  97. return;
  98. }
  99. this._wikitext = null;
  100. TFDClerk.api_get(this, {
  101. action: "query",
  102. prop: "revisions",
  103. rvprop: "content",
  104. indexpageids: "",
  105. rvsection: section,
  106. titles: title
  107. }, function(data) {
  108. var pageid = data.query.pageids[0];
  109. var content = data.query.pages[pageid].revisions[0]["*"];
  110. this._wikitext = content;
  111. callback.call(this, content);
  112. for (var i in this._wikitext_callbacks)
  113. this._wikitext_callbacks[i].call(this, content);
  114. }, null, function() {
  115. this._wikitext_callbacks = [];
  116. });
  117. };
  118. TFD.prototype._remove_option_box = function() {
  119. this.box.remove();
  120. this.box = null;
  121. this._guard = false;
  122. this._submit_blockers = [];
  123. };
  124. TFD.prototype._add_option_box = function(verb, title, callback, options) {
  125. var self = this;
  126. this.box = $("<div/>", {
  127. id: "tfdclerk-" + verb + "-box-" + this.id,
  128. addClass: "tfdclerk-box tfdclerk-" + verb + "-box"
  129. })
  130. .append($("<div/>", {addClass: "tfdclerk-box-info"})
  131. .append($("<a/>", {
  132. href: TFDClerk.script_url,
  133. title: "tfdclerk.js",
  134. text: "tfdclerk.js"
  135. }))
  136. .append($("<span/>", {text: " version " + TFDClerk.version})))
  137. .append($("<h5/>", {
  138. addClass: "tfdclerk-box-heading",
  139. text: title
  140. }));
  141. options.call(this);
  142. this.box.append($("<button/>", {
  143. text: verb.charAt(0).toUpperCase() + verb.slice(1),
  144. addClass: "tfdclerk-submit mw-ui-button mw-ui-progressive",
  145. disabled: this._submit_blockers.length > 0,
  146. click: function() {
  147. self._block_submit("submitting");
  148. self.box.find(".tfdclerk-cancel").prop("disabled", true);
  149. callback.call(self);
  150. }
  151. }))
  152. .append($("<button/>", {
  153. text: "Cancel",
  154. addClass: "tfdclerk-cancel mw-ui-button",
  155. click: function() { self._remove_option_box(); }
  156. }))
  157. .insertAfter(this.head);
  158. };
  159. TFD.prototype._add_option_table = function(options) {
  160. var table = $("<table/>", {addClass: "tfdclerk-options"});
  161. $.each(options, function(i, opt) {
  162. table.append($("<tr/>")
  163. .append(
  164. $("<td/>", {addClass: "tfdclerk-option-name"}).append(opt[0]))
  165. .append(
  166. $("<td/>", {addClass: "tfdclerk-option-value"}).append(opt[1]))
  167. );
  168. });
  169. this.box.append(table);
  170. };
  171. TFD.prototype._build_loading_node = function(node, text) {
  172. return $("<" + node + "/>", {
  173. addClass: "tfdclerk-loading",
  174. text: text + "..."
  175. });
  176. };
  177. TFD.prototype._build_hook = function(verb, callback) {
  178. var self = this;
  179. return $("<a/>", {
  180. href: "#",
  181. text: verb,
  182. title: "tfdclerk: " + verb + " discussion",
  183. click: function() {
  184. callback.call(self);
  185. return false;
  186. }
  187. });
  188. };
  189. TFD.prototype._build_hook_divider = function() {
  190. return $("<span/>", {
  191. addClass: "tfdclerk-hook-divider mw-editsection-divider",
  192. text: " | "
  193. });
  194. };
  195. TFD.prototype.add_hooks = function() {
  196. $("<span/>", {addClass: "tfdclerk-hooks"})
  197. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "["}))
  198. .append(this._build_hook("close", this.close))
  199. .append(this._build_hook_divider())
  200. .append(this._build_hook("relist", this.relist))
  201. .append($("<span/>", {addClass: "mw-editsection-bracket", text: "]"}))
  202. .appendTo(this.head.find(".mw-editsection"));
  203. };