wiki.js 6.9 KB

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