wiki.js 11 KB

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