wiki.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. this.gamepedia = this.hostname.endsWith( '.gamepedia.com' );
  20. if ( this.isGamepedia() ) articlepath = '/$1';
  21. let project = wikiProjects.find( project => this.hostname.endsWith( project.name ) );
  22. if ( project ) {
  23. let regex = ( this.host + this.pathname ).match( new RegExp( '^' + project.regex + project.scriptPath + '$' ) );
  24. if ( regex ) articlepath = 'https://' + regex[1] + project.articlePath + '$1';
  25. }
  26. this.articlepath = articlepath;
  27. this.mainpage = '';
  28. this.centralauth = 'local';
  29. this.miraheze = this.hostname.endsWith( '.miraheze.org' );
  30. }
  31. /**
  32. * @type {String}
  33. */
  34. get articlepath() {
  35. return this.articleURL.pathname + this.articleURL.search;
  36. }
  37. set articlepath(path) {
  38. this.articleURL = new articleURL(path, this);
  39. }
  40. /**
  41. * @type {String}
  42. */
  43. get mainpage() {
  44. return this.articleURL.mainpage;
  45. }
  46. set mainpage(title) {
  47. this.articleURL.mainpage = title;
  48. }
  49. /**
  50. * Updates the wiki url.
  51. * @param {Object} siteinfo - Siteinfo from the wiki API.
  52. * @param {String} siteinfo.servername - Hostname of the wiki.
  53. * @param {String} siteinfo.scriptpath - Scriptpath of the wiki.
  54. * @param {String} siteinfo.articlepath - Articlepath of the wiki.
  55. * @param {String} siteinfo.mainpage - Main page of the wiki.
  56. * @param {String} siteinfo.centralidlookupprovider - Central auth of the wiki.
  57. * @param {String} siteinfo.logo - Logo of the wiki.
  58. * @param {String} [siteinfo.gamepedia] - If the wiki is a Gamepedia wiki.
  59. * @returns {Wiki}
  60. */
  61. updateWiki({servername, scriptpath, articlepath, mainpage, centralidlookupprovider, logo, gamepedia = 'false'}) {
  62. this.hostname = servername;
  63. this.pathname = scriptpath + '/';
  64. this.articlepath = articlepath;
  65. this.mainpage = mainpage;
  66. this.centralauth = centralidlookupprovider;
  67. this.miraheze = /^(?:https?:)?\/\/static\.miraheze\.org\//.test(logo);
  68. this.gamepedia = ( gamepedia === 'true' ? true : this.hostname.endsWith( '.gamepedia.com' ) );
  69. return this;
  70. }
  71. /**
  72. * Check for a Fandom wiki.
  73. * @param {Boolean} [includeGP] - If Gamepedia wikis are included.
  74. * @returns {Boolean}
  75. */
  76. isFandom(includeGP = true) {
  77. return ( this.hostname.endsWith( '.fandom.com' ) || this.hostname.endsWith( '.wikia.org' )
  78. || ( includeGP && this.isGamepedia() ) );
  79. }
  80. /**
  81. * Check for a Gamepedia wiki.
  82. * @returns {Boolean}
  83. */
  84. isGamepedia() {
  85. return this.gamepedia;
  86. }
  87. /**
  88. * Check for a Miraheze wiki.
  89. * @returns {Boolean}
  90. */
  91. isMiraheze() {
  92. return this.miraheze;
  93. }
  94. /**
  95. * Check for CentralAuth.
  96. * @returns {Boolean}
  97. */
  98. hasCentralAuth() {
  99. return this.centralauth === 'CentralAuth';
  100. }
  101. /**
  102. * Check if a wiki is missing.
  103. * @param {String} [message] - Error message or response url.
  104. * @param {Number} [statusCode] - Status code of the response.
  105. * @returns {Boolean}
  106. */
  107. noWiki(message = '', statusCode = 0) {
  108. if ( statusCode === 410 || statusCode === 404 ) return true;
  109. if ( !this.isFandom() ) return false;
  110. if ( this.hostname.startsWith( 'www.' ) || message.startsWith( 'https://www.' ) ) return true;
  111. return [
  112. 'https://community.fandom.com/wiki/Community_Central:Not_a_valid_community?from=' + this.hostname,
  113. this + 'language-wikis'
  114. ].includes( message.replace( /Unexpected token < in JSON at position 0 in "([^ ]+)"/, '$1' ) );
  115. }
  116. /**
  117. * Get a page link.
  118. * @param {String} [title] - Name of the page.
  119. * @param {URLSearchParams} [querystring] - Query arguments of the page.
  120. * @param {String} [fragment] - Fragment of the page.
  121. * @param {Boolean} [isMarkdown] - Use the link in markdown.
  122. * @returns {String}
  123. */
  124. toLink(title = '', querystring = '', fragment = '', isMarkdown = false) {
  125. querystring = new URLSearchParams(querystring);
  126. if ( !querystring.toString().length ) title = ( title || this.mainpage );
  127. title = title.replace( / /g, '_' ).replace( /%/g, '%2525' );
  128. let link = new URL(this.articleURL);
  129. link.pathname = link.pathname.replace( '$1', title.replace( /\\/g, '%5C' ) );
  130. link.searchParams.forEach( (value, name, searchParams) => {
  131. if ( value.includes( '$1' ) ) {
  132. if ( !title ) searchParams.delete(name);
  133. else searchParams.set(name, value.replace( '$1', title ));
  134. }
  135. } );
  136. querystring.forEach( (value, name) => {
  137. link.searchParams.append(name, value);
  138. } );
  139. let output = decodeURI( link ).replace( /\\/g, '%5C' ).replace( /@(here|everyone)/g, '%40$1' ) + Wiki.toSection(fragment);
  140. if ( isMarkdown ) return output.replace( /\(/g, '%28' ).replace( /\)/g, '%29' );
  141. else return output;
  142. }
  143. /**
  144. * Encode a page title.
  145. * @param {String} [title] - Title of the page.
  146. * @returns {String}
  147. * @static
  148. */
  149. static toTitle(title = '') {
  150. return title.replace( / /g, '_' ).replace( /[?&%\\]/g, (match) => {
  151. return '%' + match.charCodeAt().toString(16).toUpperCase();
  152. } ).replace( /@(here|everyone)/g, '%40$1' ).replace( /[()]/g, '\\$&' );
  153. };
  154. /**
  155. * Encode a link section.
  156. * @param {String} [fragment] - Fragment of the page.
  157. * @param {Boolean} [simpleEncoding] - Don't fully encode the anchor.
  158. * @returns {String}
  159. * @static
  160. */
  161. static toSection(fragment = '', simpleEncoding = true) {
  162. if ( !fragment ) return '';
  163. fragment = fragment.replace( / /g, '_' );
  164. if ( simpleEncoding && !/['"`^{}<>|\\]|@(everyone|here)/.test(fragment) ) return '#' + fragment;
  165. return '#' + encodeURIComponent( fragment ).replace( /[!'()*~]/g, (match) => {
  166. return '%' + match.charCodeAt().toString(16).toUpperCase();
  167. } ).replace( /%3A/g, ':' ).replace( /%/g, '.' );
  168. }
  169. /**
  170. * Turn user input into a wiki.
  171. * @param {String} input - The user input referring to a wiki.
  172. * @returns {Wiki}
  173. * @static
  174. */
  175. static fromInput(input = '') {
  176. if ( input instanceof URL ) return new this(input);
  177. input = input.replace( /^(?:https?:)?\/\//, 'https://' );
  178. var regex = input.match( /^(?:https:\/\/)?([a-z\d-]{1,50}\.(?:gamepedia\.com|(?:fandom\.com|wikia\.org)(?:(?!\/(?:wiki|api)\/)\/[a-z-]{2,12})?))(?:\/|$)/ );
  179. if ( regex ) return new this('https://' + regex[1] + '/');
  180. if ( input.startsWith( 'https://' ) ) {
  181. let project = wikiProjects.find( project => input.split('/')[2].endsWith( project.name ) );
  182. if ( project ) {
  183. regex = input.match( new RegExp( project.regex + `(?:${project.articlePath}|${project.scriptPath}|/?$)` ) );
  184. if ( regex ) return new this('https://' + regex[1] + project.scriptPath);
  185. }
  186. let wiki = input.replace( /\/(?:api|load|index)\.php(?:|\?.*)$/, '/' );
  187. if ( !wiki.endsWith( '/' ) ) wiki += '/';
  188. return new this(wiki);
  189. }
  190. let project = wikiProjects.find( project => input.split('/')[0].endsWith( project.name ) );
  191. if ( project ) {
  192. regex = input.match( new RegExp( project.regex + `(?:${project.articlePath}|${project.scriptPath}|/?$)` ) );
  193. if ( regex ) return new this('https://' + regex[1] + project.scriptPath);
  194. }
  195. if ( /^(?:[a-z-]{2,12}\.)?[a-z\d-]{1,50}$/.test(input) ) {
  196. if ( !input.includes( '.' ) ) return new this('https://' + input + '.fandom.com/');
  197. else return new this('https://' + input.split('.')[1] + '.fandom.com/' + input.split('.')[0] + '/');
  198. }
  199. return null;
  200. }
  201. [util.inspect.custom](depth, opts) {
  202. if ( typeof depth === 'number' && depth < 0 ) return this;
  203. const wiki = {
  204. href: this.href,
  205. origin: this.origin,
  206. protocol: this.protocol,
  207. username: this.username,
  208. password: this.password,
  209. host: this.host,
  210. hostname: this.hostname,
  211. port: this.port,
  212. pathname: this.pathname,
  213. search: this.search,
  214. searchParams: this.searchParams,
  215. hash: this.hash,
  216. articlepath: this.articlepath,
  217. articleURL: this.articleURL,
  218. mainpage: this.mainpage
  219. }
  220. return 'Wiki ' + util.inspect(wiki, opts);
  221. }
  222. }
  223. /**
  224. * An article URL.
  225. * @class articleURL
  226. */
  227. class articleURL extends URL {
  228. /**
  229. * Creates a new article URL.
  230. * @param {String|URL|Wiki} [articlepath] - The article path.
  231. * @param {String|URL|Wiki} [wiki] - The wiki.
  232. * @constructs articleURL
  233. */
  234. constructor(articlepath = '/index.php?title=$1', wiki) {
  235. super(articlepath, wiki);
  236. this.protocol = 'https';
  237. this.mainpage = '';
  238. }
  239. [util.inspect.custom](depth, opts) {
  240. if ( typeof depth === 'number' && depth < 0 ) return this;
  241. if ( typeof depth === 'number' && depth < 2 ) {
  242. var link = this.href;
  243. var mainpage = link.replace( '$1', ( this.mainpage || 'Main Page' ).replace( / /g, '_' ) );
  244. return 'articleURL { ' + util.inspect(link, opts) + ' => ' + util.inspect(mainpage, opts) + ' }';
  245. }
  246. return super[util.inspect.custom](depth, opts);
  247. }
  248. }
  249. module.exports = Wiki;