wiki.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. const util = require('util');
  2. const {defaultSettings, wikiProjects} = require('./default.json');
  3. var allSites = [];
  4. const getAllSites = require('../util/allSites.js');
  5. getAllSites.then( sites => allSites = sites );
  6. /**
  7. * A wiki.
  8. * @class Wiki
  9. */
  10. class Wiki extends URL {
  11. /**
  12. * Creates a new wiki.
  13. * @param {String|URL|Wiki} [wiki] - The wiki script path.
  14. * @param {String|URL|Wiki} [base] - The base for the wiki.
  15. * @constructs Wiki
  16. */
  17. constructor(wiki = defaultSettings.wiki, base = defaultSettings.wiki) {
  18. super(wiki, base);
  19. this.protocol = 'https';
  20. let articlepath = '/index.php?title=$1';
  21. if ( this.isFandom() ) articlepath = this.pathname + 'wiki/$1';
  22. if ( this.isGamepedia() ) articlepath = '/$1';
  23. let project = wikiProjects.find( project => this.hostname.endsWith( project.name ) );
  24. if ( project ) {
  25. let regex = ( this.host + this.pathname ).match( new RegExp( '^' + project.regex + project.scriptPath + '$' ) );
  26. if ( regex ) articlepath = 'https://' + regex[1] + project.articlePath + '$1';
  27. }
  28. this.articlepath = articlepath;
  29. this.mainpage = '';
  30. this.centralauth = 'local';
  31. this.miraheze = this.hostname.endsWith( '.miraheze.org' );
  32. }
  33. /**
  34. * @type {String}
  35. */
  36. get articlepath() {
  37. return this.articleURL.pathname + this.articleURL.search;
  38. }
  39. set articlepath(path) {
  40. this.articleURL = new articleURL(path, this);
  41. }
  42. /**
  43. * @type {String}
  44. */
  45. get mainpage() {
  46. return this.articleURL.mainpage;
  47. }
  48. set mainpage(title) {
  49. this.articleURL.mainpage = title;
  50. }
  51. /**
  52. * Updates the wiki url.
  53. * @param {Object} siteinfo - Siteinfo from the wiki API.
  54. * @param {String} siteinfo.servername - Hostname of the wiki.
  55. * @param {String} siteinfo.scriptpath - Scriptpath of the wiki.
  56. * @param {String} siteinfo.articlepath - Articlepath of the wiki.
  57. * @param {String} siteinfo.mainpage - Main page of the wiki.
  58. * @param {String} siteinfo.centralidlookupprovider - Central auth of the wiki.
  59. * @param {String} siteinfo.logo - Logo of the wiki.
  60. * @returns {Wiki}
  61. */
  62. updateWiki({servername, scriptpath, articlepath, mainpage, centralidlookupprovider, logo}) {
  63. this.hostname = servername;
  64. this.pathname = scriptpath + '/';
  65. this.articlepath = articlepath;
  66. this.mainpage = mainpage;
  67. this.centralauth = centralidlookupprovider;
  68. this.miraheze = /^(?:https?:)?\/\/static\.miraheze\.org\//.test(logo);
  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.hostname.endsWith( '.gamepedia.com' ) ) );
  79. }
  80. /**
  81. * Check for a Gamepedia wiki.
  82. * @returns {Boolean}
  83. */
  84. isGamepedia() {
  85. return this.hostname.endsWith( '.gamepedia.com' );
  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 an URI encoded link.
  118. * @param {String} [title] - Name of the page.
  119. * @returns {String}
  120. */
  121. toDescLink(title = this.mainpage) {
  122. return this.articleURL.href.replace( '$1', encodeURIComponent( title.replace( / /g, '_' ) ) );
  123. }
  124. /**
  125. * Get a page link.
  126. * @param {String} [title] - Name of the page.
  127. * @param {URLSearchParams} [querystring] - Query arguments of the page.
  128. * @param {String} [fragment] - Fragment of the page.
  129. * @param {Boolean} [isMarkdown] - Use the link in markdown.
  130. * @returns {String}
  131. */
  132. toLink(title = '', querystring = '', fragment = '', isMarkdown = false) {
  133. querystring = new URLSearchParams(querystring);
  134. if ( !querystring.toString().length ) title = ( title || this.mainpage );
  135. title = title.replace( / /g, '_' ).replace( /%/g, '%2525' );
  136. let link = new URL(this.articleURL);
  137. link.pathname = link.pathname.replace( '$1', title.replace( /\\/g, '%5C' ) );
  138. link.searchParams.forEach( (value, name, searchParams) => {
  139. if ( value.includes( '$1' ) ) {
  140. if ( !title ) searchParams.delete(name);
  141. else searchParams.set(name, value.replace( '$1', title ));
  142. }
  143. } );
  144. querystring.forEach( (value, name) => {
  145. link.searchParams.append(name, value);
  146. } );
  147. let output = decodeURI( link ).replace( /\\/g, '%5C' ).replace( /@(here|everyone)/g, '%40$1' ) + Wiki.toSection(fragment);
  148. if ( isMarkdown ) return output.replace( /[()]/g, '\\$&' );
  149. else return output;
  150. }
  151. /**
  152. * Encode a page title.
  153. * @param {String} [title] - Title of the page.
  154. * @returns {String}
  155. * @static
  156. */
  157. static toTitle(title = '') {
  158. return title.replace( / /g, '_' ).replace( /[?&%\\]/g, (match) => {
  159. return '%' + match.charCodeAt().toString(16).toUpperCase();
  160. } ).replace( /@(here|everyone)/g, '%40$1' ).replace( /[()]/g, '\\$&' );
  161. };
  162. /**
  163. * Encode a link section.
  164. * @param {String} [fragment] - Fragment of the page.
  165. * @param {Boolean} [simpleEncoding] - Don't fully encode the anchor.
  166. * @returns {String}
  167. * @static
  168. */
  169. static toSection(fragment = '', simpleEncoding = true) {
  170. if ( !fragment ) return '';
  171. fragment = fragment.replace( / /g, '_' );
  172. if ( simpleEncoding && !/['"`^{}<>|\\]|@(everyone|here)/.test(fragment) ) return '#' + fragment;
  173. return '#' + encodeURIComponent( fragment ).replace( /[!'()*~]/g, (match) => {
  174. return '%' + match.charCodeAt().toString(16).toUpperCase();
  175. } ).replace( /%3A/g, ':' ).replace( /%/g, '.' );
  176. }
  177. /**
  178. * Turn user input into a wiki.
  179. * @param {String} input - The user input referring to a wiki.
  180. * @returns {Wiki}
  181. * @static
  182. */
  183. static fromInput(input = '') {
  184. if ( input instanceof URL ) return new this(input);
  185. input = input.replace( /^(?:https?:)?\/\//, 'https://' );
  186. var regex = input.match( /^(?:https:\/\/)?([a-z\d-]{1,50}\.(?:gamepedia\.com|(?:fandom\.com|wikia\.org)(?:(?!\/(?:wiki|api)\/)\/[a-z-]{2,12})?))(?:\/|$)/ );
  187. if ( regex ) return new this('https://' + regex[1] + '/');
  188. if ( input.startsWith( 'https://' ) ) {
  189. let project = wikiProjects.find( project => input.split('/')[2].endsWith( project.name ) );
  190. if ( project ) {
  191. regex = input.match( new RegExp( project.regex + `(?:${project.articlePath}|${project.scriptPath}|/?$)` ) );
  192. if ( regex ) return new this('https://' + regex[1] + project.scriptPath);
  193. }
  194. let wiki = input.replace( /\/(?:api|load|index)\.php(?:|\?.*)$/, '/' );
  195. if ( !wiki.endsWith( '/' ) ) wiki += '/';
  196. return new this(wiki);
  197. }
  198. let project = wikiProjects.find( project => input.split('/')[0].endsWith( project.name ) );
  199. if ( project ) {
  200. regex = input.match( new RegExp( project.regex + `(?:${project.articlePath}|${project.scriptPath}|/?$)` ) );
  201. if ( regex ) return new this('https://' + regex[1] + project.scriptPath);
  202. }
  203. if ( allSites.some( site => site.wiki_domain === input + '.gamepedia.com' ) ) {
  204. return new this('https://' + input + '.gamepedia.com/');
  205. }
  206. if ( /^(?:[a-z-]{2,12}\.)?[a-z\d-]{1,50}$/.test(input) ) {
  207. if ( !input.includes( '.' ) ) return new this('https://' + input + '.fandom.com/');
  208. else return new this('https://' + input.split('.')[1] + '.fandom.com/' + input.split('.')[0] + '/');
  209. }
  210. return null;
  211. }
  212. [util.inspect.custom](depth, opts) {
  213. if ( typeof depth === 'number' && depth < 0 ) return this;
  214. const wiki = {
  215. href: this.href,
  216. origin: this.origin,
  217. protocol: this.protocol,
  218. username: this.username,
  219. password: this.password,
  220. host: this.host,
  221. hostname: this.hostname,
  222. port: this.port,
  223. pathname: this.pathname,
  224. search: this.search,
  225. searchParams: this.searchParams,
  226. hash: this.hash,
  227. articlepath: this.articlepath,
  228. articleURL: this.articleURL,
  229. mainpage: this.mainpage
  230. }
  231. return 'Wiki ' + util.inspect(wiki, opts);
  232. }
  233. }
  234. /**
  235. * An article URL.
  236. * @class articleURL
  237. */
  238. class articleURL extends URL {
  239. /**
  240. * Creates a new article URL.
  241. * @param {String|URL|Wiki} [articlepath] - The article path.
  242. * @param {String|URL|Wiki} [wiki] - The wiki.
  243. * @constructs articleURL
  244. */
  245. constructor(articlepath = '/index.php?title=$1', wiki) {
  246. super(articlepath, wiki);
  247. this.protocol = 'https';
  248. this.mainpage = '';
  249. }
  250. [util.inspect.custom](depth, opts) {
  251. if ( typeof depth === 'number' && depth < 0 ) return this;
  252. if ( typeof depth === 'number' && depth < 2 ) {
  253. var link = this.href;
  254. var mainpage = link.replace( '$1', ( this.mainpage || 'Main Page' ).replace( / /g, '_' ) );
  255. return 'articleURL { ' + util.inspect(link, opts) + ' => ' + util.inspect(mainpage, opts) + ' }';
  256. }
  257. return super[util.inspect.custom](depth, opts);
  258. }
  259. }
  260. module.exports = Wiki;