1
0

wiki.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 = defaultSettings.wiki) {
  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. * @param {Boolean} [includeGP] - If Gamepedia wikis are included.
  67. * @returns {Boolean}
  68. */
  69. isFandom(includeGP = true) {
  70. return ( this.hostname.endsWith( '.fandom.com' ) || this.hostname.endsWith( '.wikia.org' )
  71. || ( includeGP && this.hostname.endsWith( '.gamepedia.com' ) ) );
  72. }
  73. /**
  74. * Check for a Gamepedia wiki.
  75. * @returns {Boolean}
  76. */
  77. isGamepedia() {
  78. return this.hostname.endsWith( '.gamepedia.com' );
  79. }
  80. /**
  81. * Check if a wiki is missing.
  82. * @param {String} [message] - Error message or response url.
  83. * @returns {Boolean}
  84. */
  85. noWiki(message = '') {
  86. if ( !this.isFandom() ) return false;
  87. if ( this.hostname.startsWith( 'www.' ) || message.startsWith( 'https://www.' ) ) return true;
  88. return [
  89. 'https://community.fandom.com/wiki/Community_Central:Not_a_valid_community?from=' + this.hostname,
  90. this + 'language-wikis'
  91. ].includes( message.replace( /Unexpected token < in JSON at position 0 in "([^ ]+)"/, '$1' ) );
  92. }
  93. /**
  94. * Get an URI encoded link.
  95. * @param {String} [title] - Name of the page.
  96. * @returns {String}
  97. */
  98. toDescLink(title = this.mainpage) {
  99. return this.articleURL.href.replace( '$1', encodeURIComponent( title.replace( / /g, '_' ) ) );
  100. }
  101. /**
  102. * Get a page link.
  103. * @param {String} [title] - Name of the page.
  104. * @param {URLSearchParams} [querystring] - Query arguments of the page.
  105. * @param {String} [fragment] - Fragment of the page.
  106. * @param {Boolean} [isMarkdown] - Use the link in markdown.
  107. * @returns {String}
  108. */
  109. toLink(title = '', querystring = '', fragment = '', isMarkdown = false) {
  110. querystring = new URLSearchParams(querystring);
  111. if ( !querystring.toString().length ) title = ( title || this.mainpage );
  112. title = title.replace( / /g, '_' );
  113. let link = new URL(this.articleURL);
  114. link.pathname = link.pathname.replace( '$1', title.replace( /\\/g, '%5C' ) );
  115. link.searchParams.forEach( (value, name, searchParams) => {
  116. if ( value.includes( '$1' ) ) {
  117. if ( !title ) searchParams.delete(name);
  118. else searchParams.set(name, value.replace( '$1', title ));
  119. }
  120. } );
  121. querystring.forEach( (value, name) => {
  122. link.searchParams.append(name, value);
  123. } );
  124. let output = decodeURI( link ).replace( /\\/g, '%5C' ).replace( /@(here|everyone)/g, '%40$1' );
  125. if ( isMarkdown ) output = output.replace( /([\(\)])/g, '\\$1' );
  126. return output + Wiki.toSection(fragment);
  127. }
  128. /**
  129. * Encode a page title.
  130. * @param {String} [title] - Title of the page.
  131. * @returns {String}
  132. * @static
  133. */
  134. static toTitle(title = '') {
  135. return title.replace( / /g, '_' ).replace( /[?&%\\]/g, (match) => {
  136. return '%' + match.charCodeAt().toString(16).toUpperCase();
  137. } ).replace( /@(here|everyone)/g, '%40$1' ).replace( /[()]/g, '\\$&' );
  138. };
  139. /**
  140. * Encode a link section.
  141. * @param {String} [fragment] - Fragment of the page.
  142. * @returns {String}
  143. * @static
  144. */
  145. static toSection(fragment = '') {
  146. if ( !fragment ) return '';
  147. fragment = fragment.replace( / /g, '_' );
  148. if ( !/['"`^{}<>|\\]|@(everyone|here)/.test(fragment) ) return '#' + fragment;
  149. return '#' + encodeURIComponent( fragment ).replace( /[!'()*~]/g, (match) => {
  150. return '%' + match.charCodeAt().toString(16).toUpperCase();
  151. } ).replace( /%3A/g, ':' ).replace( /%/g, '.' );
  152. }
  153. [util.inspect.custom](depth, opts) {
  154. if ( typeof depth === 'number' && depth < 0 ) return this;
  155. const wiki = {
  156. href: this.href,
  157. origin: this.origin,
  158. protocol: this.protocol,
  159. username: this.username,
  160. password: this.password,
  161. host: this.host,
  162. hostname: this.hostname,
  163. port: this.port,
  164. pathname: this.pathname,
  165. search: this.search,
  166. searchParams: this.searchParams,
  167. hash: this.hash,
  168. articlepath: this.articlepath,
  169. articleURL: this.articleURL,
  170. mainpage: this.mainpage
  171. }
  172. return 'Wiki ' + util.inspect(wiki, opts);
  173. }
  174. }
  175. /**
  176. * An article URL.
  177. * @class articleURL
  178. */
  179. class articleURL extends URL {
  180. /**
  181. * Creates a new article URL.
  182. * @param {String|URL|Wiki} [articlepath] - The article path.
  183. * @param {String|URL|Wiki} [wiki] - The wiki.
  184. * @constructs articleURL
  185. */
  186. constructor(articlepath = '/index.php?title=$1', wiki) {
  187. super(articlepath, wiki);
  188. this.protocol = 'https';
  189. this.mainpage = '';
  190. }
  191. [util.inspect.custom](depth, opts) {
  192. if ( typeof depth === 'number' && depth < 0 ) return this;
  193. if ( typeof depth === 'number' && depth < 2 ) {
  194. var link = this.href;
  195. var mainpage = link.replace( '$1', ( this.mainpage || 'Main Page' ).replace( / /g, '_' ) );
  196. return 'articleURL { ' + util.inspect(link, opts) + ' => ' + util.inspect(mainpage, opts) + ' }';
  197. }
  198. return super[util.inspect.custom](depth, opts);
  199. }
  200. }
  201. module.exports = Wiki;