wiki.js 11 KB

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