special_page.js 11 KB

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