special_page.js 10 KB

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