wiki.js 9.5 KB

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