special_page.js 11 KB

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