special_page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. const {Util} = require('discord.js');
  2. const {timeoptions} = require('../util/default.json');
  3. const overwrites = {
  4. randompage: (fn, lang, msg, wiki, reaction, spoiler) => {
  5. fn.random(lang, msg, wiki, reaction, spoiler)
  6. },
  7. diff: (fn, lang, msg, wiki, reaction, spoiler, args, embed) => {
  8. fn.diff(lang, msg, args, wiki, reaction, spoiler, embed)
  9. },
  10. statistics: (fn, lang, msg, wiki, reaction, spoiler) => {
  11. fn.overview(lang, msg, wiki, reaction, spoiler)
  12. }
  13. }
  14. const queryfunctions = {
  15. title: (query, wiki) => query.querypage.results.map( result => {
  16. return '[' + result.title.escapeFormatting() + '](' + wiki.toLink(result.title, '', '', query.general, true) + ')';
  17. } ).join('\n'),
  18. times: (query, wiki) => query.querypage.results.map( result => {
  19. return result.value + '× [' + result.title.escapeFormatting() + '](' + wiki.toLink(result.title, '', '', query.general, true) + ')';
  20. } ).join('\n'),
  21. size: (query, wiki) => query.querypage.results.map( result => {
  22. return result.value + ' bytes: [' + result.title.escapeFormatting() + '](' + wiki.toLink(result.title, '', '', query.general, true) + ')';
  23. } ).join('\n'),
  24. redirect: (query, wiki) => query.querypage.results.map( result => {
  25. return '[' + result.title.replace( / /g, '_' ).escapeFormatting() + '](' + wiki.toLink(result.title, 'redirect=no', '', query.general, true) + ')' + ( result.databaseResult && result.databaseResult.rd_title ? ' → ' + result.databaseResult.rd_title.escapeFormatting() : '' );
  26. } ).join('\n'),
  27. doubleredirect: (query, wiki) => query.querypage.results.map( result => {
  28. return '[' + result.title.replace( / /g, '_' ).escapeFormatting() + '](' + wiki.toLink(result.title, 'redirect=no', '', query.general, true) + ')' + ( result.databaseResult && result.databaseResult.b_title && result.databaseResult.c_title ? ' → ' + result.databaseResult.b_title.escapeFormatting() + ' → ' + result.databaseResult.c_title.escapeFormatting() : '' );
  29. } ).join('\n'),
  30. timestamp: (query, wiki) => query.querypage.results.map( result => {
  31. return new Date(result.timestamp).toLocaleString(lang.get('dateformat'), timeoptions).escapeFormatting() + ': [' + result.title.escapeFormatting() + '](' + wiki.toLink(result.title, '', '', query.general, true) + ')';
  32. } ).join('\n'),
  33. media: (query) => query.querypage.results.map( result => {
  34. var ms = result.title.split(';');
  35. return '**' + ms[1] + '**: ' + ms[2] + ' files (' + ms[3] + ' bytes)';
  36. } ).join('\n'),
  37. category: (query, wiki) => query.querypage.results.map( result => {
  38. return result.value + '× [' + result.title.escapeFormatting() + '](' + wiki.toLink('Category:' + result.title, '', '', query.general, true) + ')';
  39. } ).join('\n'),
  40. gadget: (query) => query.querypage.results.map( result => {
  41. result.title = result.title.replace( /^(?:.*:)?gadget-/, '' );
  42. return '**' + result.title.escapeFormatting() + '**: ' + result.value + ' users (' + result.ns + ' active)';
  43. } ).join('\n'),
  44. recentchanges: (query, wiki) => query.recentchanges.map( result => {
  45. return '[' + result.title.escapeFormatting() + '](' + wiki.toLink(result.title, ( result.type === 'edit' ? 'diff=' + result.revid + '&oldid=' + result.old_revid : '' ), '', query.general, true) + ')';
  46. } ).join('\n')
  47. }
  48. const querypages = {
  49. ancientpages: ['&list=querypage&qplimit=10&qppage=Ancientpages', queryfunctions.timestamp],
  50. brokenredirects: ['&list=querypage&qplimit=10&qppage=BrokenRedirects', queryfunctions.redirect],
  51. deadendpages: ['&list=querypage&qplimit=10&qppage=Deadendpages', queryfunctions.title],
  52. doubleredirects: ['&list=querypage&qplimit=10&qppage=DoubleRedirects', queryfunctions.doubleredirect],
  53. fewestrevisions: ['&list=querypage&qplimit=10&qppage=Fewestrevisions', queryfunctions.times],
  54. listduplicatedfiles: ['&list=querypage&qplimit=10&qppage=ListDuplicatedFiles', queryfunctions.times],
  55. listredirects: ['&list=querypage&qplimit=10&qppage=Listredirects', queryfunctions.redirect],
  56. lonelypages: ['&list=querypage&qplimit=10&qppage=Lonelypages', queryfunctions.title],
  57. longpages: ['&list=querypage&qplimit=10&qppage=Longpages', queryfunctions.size],
  58. mediastatistics: ['&list=querypage&qplimit=10&qppage=MediaStatistics', queryfunctions.media],
  59. mostcategories: ['&list=querypage&qplimit=10&qppage=Mostcategories', queryfunctions.times],
  60. mostimages: ['&list=querypage&qplimit=10&qppage=Mostimages', queryfunctions.times],
  61. mostinterwikis: ['&list=querypage&qplimit=10&qppage=Mostinterwikis', queryfunctions.times],
  62. mostlinked: ['&list=querypage&qplimit=10&qppage=Mostlinked', queryfunctions.times],
  63. mostlinkedcategories: ['&list=querypage&qplimit=10&qppage=Mostlinkedcategories', queryfunctions.times],
  64. mostlinkedtemplates: ['&list=querypage&qplimit=10&qppage=Mostlinkedtemplates', queryfunctions.times],
  65. mostrevisions: ['&list=querypage&qplimit=10&qppage=Mostrevisions', queryfunctions.times],
  66. shortpages: ['&list=querypage&qplimit=10&qppage=Shortpages', queryfunctions.size],
  67. uncategorizedcategories: ['&list=querypage&qplimit=10&qppage=Uncategorizedcategories', queryfunctions.title],
  68. uncategorizedpages: ['&list=querypage&qplimit=10&qppage=Uncategorizedpages', queryfunctions.title],
  69. uncategorizedimages: ['&list=querypage&qplimit=10&qppage=Uncategorizedimages', queryfunctions.title],
  70. uncategorizedtemplates: ['&list=querypage&qplimit=10&qppage=Uncategorizedtemplates', queryfunctions.title],
  71. unusedcategories: ['&list=querypage&qplimit=10&qppage=Unusedcategories', queryfunctions.title],
  72. unusedimages: ['&list=querypage&qplimit=10&qppage=Unusedimages', queryfunctions.title],
  73. unusedtemplates: ['&list=querypage&qplimit=10&qppage=Unusedtemplates', queryfunctions.title],
  74. unwatchedpages: ['&list=querypage&qplimit=10&qppage=Unwatchedpages', queryfunctions.title],
  75. wantedcategories: ['&list=querypage&qplimit=10&qppage=Wantedcategories', queryfunctions.times],
  76. wantedfiles: ['&list=querypage&qplimit=10&qppage=Wantedfiles', queryfunctions.times],
  77. wantedpages: ['&list=querypage&qplimit=10&qppage=Wantedpages', queryfunctions.times],
  78. wantedtemplates: ['&list=querypage&qplimit=10&qppage=Wantedtemplates', queryfunctions.times],
  79. withoutinterwiki: ['&list=querypage&qplimit=10&qppage=Withoutinterwiki', queryfunctions.title],
  80. gadgetusage: ['&list=querypage&qplimit=10&qppage=GadgetUsage', queryfunctions.gadget],
  81. recentchanges: ['&list=recentchanges&rctype=edit|new|log&rclimit=10', queryfunctions.recentchanges],
  82. disambiguations: ['&list=querypage&qplimit=10&qppage=Disambiguations', queryfunctions.title],
  83. mostpopularcategories: ['&list=querypage&qplimit=10&qppage=Mostpopularcategories', queryfunctions.category],
  84. mostlinkedfilesincontent: ['&list=querypage&qplimit=10&qppage=MostLinkedFilesInContent', queryfunctions.times],
  85. unusedvideos: ['&list=querypage&qplimit=10&qppage=UnusedVideos', queryfunctions.title],
  86. withoutimages: ['&list=querypage&qplimit=10&qppage=Withoutimages', queryfunctions.title],
  87. nonportableinfoboxes: ['&list=querypage&qplimit=10&qppage=Nonportableinfoboxes', queryfunctions.title],
  88. popularpages: ['&list=querypage&qplimit=10&qppage=Popularpages', queryfunctions.title],
  89. pageswithoutinfobox: ['&list=querypage&qplimit=10&qppage=Pageswithoutinfobox', queryfunctions.title],
  90. templateswithouttype: ['&list=querypage&qplimit=10&qppage=Templateswithouttype', queryfunctions.title],
  91. allinfoboxes: ['&list=querypage&qplimit=10&qppage=AllInfoboxes', queryfunctions.title]
  92. }
  93. const descriptions = {
  94. checkuser: 'checkuser-summary&amargs=16|19',
  95. resettokens: 'resettokens-text',
  96. allmessages: 'allmessagestext',
  97. expandtemplates: 'expand_templates_intro',
  98. apisandbox: 'apisandbox-intro',
  99. abusefilter: 'abusefilter-intro',
  100. gadgets: 'gadgets-pagetext',
  101. categorytree: 'categorytree-header',
  102. drafts: 'drafts-view-summary&amargs=30',
  103. analytics: 'analytics_confidential',
  104. mostlinkedfilesincontent: 'mostimagesincontent-summary',
  105. popularpages: 'insights-list-description-popularpages'
  106. }
  107. /**
  108. * Processes special pages.
  109. * @param {import('../util/i18n.js')} lang - The user language.
  110. * @param {import('discord.js').Message} msg - The Discord message.
  111. * @param {String} title - The title of the special page.
  112. * @param {String} specialpage - The canonical name of the special page.
  113. * @param {import('discord.js').MessageEmbed} embed - The embed for the page.
  114. * @param {String} wiki - The wiki for the page.
  115. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  116. * @param {String} spoiler - If the response is in a spoiler.
  117. */
  118. function special_page(lang, msg, title, specialpage, embed, wiki, reaction, spoiler) {
  119. if ( specialpage in overwrites ) {
  120. var args = title.split('/').slice(1,3);
  121. overwrites[specialpage](this, lang, msg, wiki, reaction, spoiler, args, embed);
  122. return;
  123. }
  124. if ( specialpage === 'recentchanges' && msg.isAdmin() && msg.guild.id in patreons ) {
  125. embed.addField( lang.get('rcscript.title'), lang.get('rcscript.ad', ( patreons[msg?.guild?.id] || process.env.prefix ), '[RcGcDw](https://gitlab.com/piotrex43/RcGcDw)') );
  126. }
  127. got.get( wiki + 'api.php?action=query&meta=siteinfo|allmessages&siprop=general&amenableparser=true&amtitle=' + encodeURIComponent( title ) + '&ammessages=' + ( specialpage in descriptions ? descriptions[specialpage] : encodeURIComponent( specialpage ) + '-summary' ) + ( specialpage in querypages ? querypages[specialpage][0] : '' ) + '&format=json', {
  128. responseType: 'json'
  129. } ).then( response => {
  130. var body = response.body;
  131. if ( body && body.warnings ) log_warn(body.warnings);
  132. if ( response.statusCode !== 200 || !body ) {
  133. console.log( '- ' + response.statusCode + ': Error while getting the special page: ' + ( body && body.error && body.error.info ) );
  134. }
  135. else {
  136. if ( body.query.allmessages[0]['*'] ) {
  137. var description = body.query.allmessages[0]['*'].toPlaintext();
  138. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  139. embed.setDescription( description );
  140. }
  141. if ( msg.channel.type === 'text' && msg.guild.id in patreons && specialpage in querypages ) {
  142. var text = Util.splitMessage( querypages[specialpage][1](body.query, wiki), {maxLength:1000} )[0];
  143. embed.addField( lang.get('search.special'), ( text || lang.get('search.empty') ) );
  144. }
  145. }
  146. }, error => {
  147. console.log( '- Error while getting the special page: ' + error );
  148. } ).finally( () => {
  149. msg.sendChannel( spoiler + '<' + embed.url + '>' + spoiler, {embed} );
  150. if ( reaction ) reaction.removeEmoji();
  151. } );
  152. }
  153. module.exports = special_page;