wiki.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. const util = require('util');
  2. const {defaultSettings, wikiProjects} = require('./default.json');
  3. /**
  4. * A wiki.
  5. * @class Wiki
  6. */
  7. class Wiki extends URL {
  8. /**
  9. * Creates a new wiki.
  10. * @param {String|URL|Wiki} [wiki] - The wiki script path.
  11. * @param {String|URL|Wiki} [base] - The base for the wiki.
  12. * @constructs Wiki
  13. */
  14. constructor(wiki = defaultSettings.wiki, base) {
  15. super(wiki, base);
  16. this.protocol = 'https';
  17. let articlepath = '/index.php?title=$1';
  18. if ( this.isFandom() ) articlepath = this.pathname + 'wiki/$1';
  19. if ( this.isGamepedia() ) articlepath = '/$1';
  20. let project = wikiProjects.find( project => this.hostname.endsWith( project.name ) );
  21. if ( project ) {
  22. let regex = ( this.host + this.pathname ).match( new RegExp( '^' + project.regex + project.scriptPath + '$' ) );
  23. if ( regex ) articlepath = 'https://' + regex[1] + project.articlePath + '$1';
  24. }
  25. this.articlepath = articlepath;
  26. this.mainpage = '';
  27. }
  28. /**
  29. * @type {String}
  30. */
  31. get articlepath() {
  32. return this.articleURL.pathname + this.articleURL.search;
  33. }
  34. set articlepath(path) {
  35. this.articleURL = new articleURL(path, this);
  36. }
  37. /**
  38. * @type {String}
  39. */
  40. get mainpage() {
  41. return this.articleURL.mainpage;
  42. }
  43. set mainpage(title) {
  44. this.articleURL.mainpage = title;
  45. }
  46. /**
  47. * Updates the wiki url.
  48. * @param {Object} siteinfo - Siteinfo from the wiki API.
  49. * @param {String} siteinfo.server - Server of the wiki with protocol. (For legacy Fandom wikis)
  50. * @param {String} siteinfo.servername - Hostname of the wiki.
  51. * @param {String} siteinfo.scriptpath - Scriptpath of the wiki.
  52. * @param {String} siteinfo.articlepath - Articlepath of the wiki.
  53. * @param {String} siteinfo.mainpage - Main page of the wiki.
  54. * @returns {Wiki}
  55. */
  56. updateWiki({server, servername, scriptpath, articlepath, mainpage}) {
  57. if ( servername ) this.hostname = servername;
  58. else this.hostname = server.replace( /^(?:https?:)?\/\//, '' );
  59. this.pathname = scriptpath + '/';
  60. this.articlepath = articlepath;
  61. this.mainpage = mainpage;
  62. return this;
  63. }
  64. /**
  65. * Check for a Fandom wiki.
  66. * @returns {Boolean}
  67. */
  68. isFandom() {
  69. return ( this.hostname.endsWith( '.fandom.com' ) || this.hostname.endsWith( '.wikia.org' ) );
  70. }
  71. /**
  72. * Check for a Gamepedia wiki.
  73. * @returns {Boolean}
  74. */
  75. isGamepedia() {
  76. return this.hostname.endsWith( '.gamepedia.com' );
  77. }
  78. /**
  79. * Check if a wiki is missing.
  80. * @param {String} [message] - Error message or response url.
  81. * @returns {Boolean}
  82. */
  83. noWiki(message = '') {
  84. if ( !( this.isGamepedia() || this.isFandom() ) ) return false;
  85. if ( this.hostname.startsWith( 'www.' ) || message.startsWith( 'https://www.' ) ) return true;
  86. return [
  87. 'https://community.fandom.com/wiki/Community_Central:Not_a_valid_community?from=' + this.hostname,
  88. this + 'language-wikis'
  89. ].includes( message.replace( /Unexpected token < in JSON at position 0 in "([^ ]+)"/, '$1' ) );
  90. }
  91. /**
  92. * Get an URI encoded link.
  93. * @param {String} [title] - Name of the page.
  94. * @returns {String}
  95. */
  96. toDescLink(title = this.mainpage) {
  97. return this.articleURL.href.replace( '$1', encodeURIComponent( title.replace( / /g, '_' ) ) );
  98. }
  99. /**
  100. * Get a page link.
  101. * @param {String} [title] - Name of the page.
  102. * @param {URLSearchParams} [querystring] - Query arguments of the page.
  103. * @param {String} [fragment] - Fragment of the page.
  104. * @param {Boolean} [isMarkdown] - Use the link in markdown.
  105. * @returns {String}
  106. */
  107. toLink(title = '', querystring = '', fragment = '', isMarkdown = false) {
  108. querystring = new URLSearchParams(querystring);
  109. if ( !querystring.toString().length ) title = ( title || this.mainpage );
  110. title = title.replace( / /g, '_' );
  111. let link = new URL(this.articleURL);
  112. link.pathname = link.pathname.replace( '$1', title.replace( /\\/g, '%5C' ) );
  113. link.searchParams.forEach( (value, name, searchParams) => {
  114. if ( value.includes( '$1' ) ) {
  115. if ( !title ) searchParams.delete(name);
  116. else searchParams.set(name, value.replace( '$1', title ));
  117. }
  118. } );
  119. querystring.forEach( (value, name) => {
  120. link.searchParams.append(name, value);
  121. } );
  122. let output = decodeURI( link ).replace( /\\/g, '%5C' ).replace( /@(here|everyone)/g, '%40$1' );
  123. if ( isMarkdown ) output = output.replace( /([\(\)])/g, '\\$1' );
  124. return output + Wiki.toSection(fragment);
  125. }
  126. /**
  127. * Encode a page title.
  128. * @param {String} [title] - Title of the page.
  129. * @returns {String}
  130. * @static
  131. */
  132. static toTitle(title = '') {
  133. return title.replace( / /g, '_' ).replace( /[?&%\\]/g, (match) => {
  134. return '%' + match.charCodeAt().toString(16).toUpperCase();
  135. } ).replace( /@(here|everyone)/g, '%40$1' ).replace( /[()]/g, '\\$&' );
  136. };
  137. /**
  138. * Encode a link section.
  139. * @param {String} [fragment] - Fragment of the page.
  140. * @returns {String}
  141. * @static
  142. */
  143. static toSection(fragment = '') {
  144. if ( !fragment ) return '';
  145. fragment = fragment.replace( / /g, '_' );
  146. if ( !/['"`^{}<>|\\]|@(everyone|here)/.test(fragment) ) return '#' + fragment;
  147. return '#' + encodeURIComponent( fragment ).replace( /[!'()*~]/g, (match) => {
  148. return '%' + match.charCodeAt().toString(16).toUpperCase();
  149. } ).replace( /%3A/g, ':' ).replace( /%/g, '.' );
  150. }
  151. [util.inspect.custom](depth, opts) {
  152. if ( typeof depth === 'number' && depth < 0 ) return this;
  153. const wiki = {
  154. href: this.href,
  155. origin: this.origin,
  156. protocol: this.protocol,
  157. username: this.username,
  158. password: this.password,
  159. host: this.host,
  160. hostname: this.hostname,
  161. port: this.port,
  162. pathname: this.pathname,
  163. search: this.search,
  164. searchParams: this.searchParams,
  165. hash: this.hash,
  166. articlepath: this.articlepath,
  167. articleURL: this.articleURL,
  168. mainpage: this.mainpage
  169. }
  170. return 'Wiki ' + util.inspect(wiki, opts);
  171. }
  172. }
  173. /**
  174. * An article URL.
  175. * @class articleURL
  176. */
  177. class articleURL extends URL {
  178. /**
  179. * Creates a new article URL.
  180. * @param {String|URL|Wiki} [articlepath] - The article path.
  181. * @param {String|URL|Wiki} [wiki] - The wiki.
  182. * @constructs articleURL
  183. */
  184. constructor(articlepath = '/index.php?title=$1', wiki) {
  185. super(articlepath, wiki);
  186. this.protocol = 'https';
  187. this.mainpage = '';
  188. }
  189. [util.inspect.custom](depth, opts) {
  190. if ( typeof depth === 'number' && depth < 0 ) return this;
  191. if ( typeof depth === 'number' && depth < 2 ) {
  192. var link = this.href;
  193. var mainpage = link.replace( '$1', ( this.mainpage || 'Main Page' ).replace( / /g, '_' ) );
  194. return 'articleURL { ' + util.inspect(link, opts) + ' => ' + util.inspect(mainpage, opts) + ' }';
  195. }
  196. return super[util.inspect.custom](depth, opts);
  197. }
  198. }
  199. module.exports = Wiki;