special_page.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. var 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. disambiguations: ['&list=querypage&qplimit=10&qppage=Disambiguations', queryfunctions.title],
  53. doubleredirects: ['&list=querypage&qplimit=10&qppage=DoubleRedirects', queryfunctions.doubleredirect],
  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. mostlinkedcategories: ['&list=querypage&qplimit=10&qppage=Mostlinkedcategories', queryfunctions.times],
  63. mostlinkedtemplates: ['&list=querypage&qplimit=10&qppage=Mostlinkedtemplates', queryfunctions.times],
  64. mostlinked: ['&list=querypage&qplimit=10&qppage=Mostlinked', queryfunctions.times],
  65. mostrevisions: ['&list=querypage&qplimit=10&qppage=Mostrevisions', queryfunctions.times],
  66. fewestrevisions: ['&list=querypage&qplimit=10&qppage=Fewestrevisions', 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. wantedcategories: ['&list=querypage&qplimit=10&qppage=Wantedcategories', queryfunctions.times],
  75. wantedfiles: ['&list=querypage&qplimit=10&qppage=Wantedfiles', queryfunctions.times],
  76. wantedpages: ['&list=querypage&qplimit=10&qppage=Wantedpages', queryfunctions.times],
  77. wantedtemplates: ['&list=querypage&qplimit=10&qppage=Wantedtemplates', queryfunctions.times],
  78. unwatchedpages: ['&list=querypage&qplimit=10&qppage=Unwatchedpages', queryfunctions.title],
  79. unusedtemplates: ['&list=querypage&qplimit=10&qppage=Unusedtemplates', queryfunctions.title],
  80. withoutinterwiki: ['&list=querypage&qplimit=10&qppage=Withoutinterwiki', queryfunctions.title],
  81. mostpopularcategories: ['&list=querypage&qplimit=10&qppage=Mostpopularcategories', queryfunctions.category],
  82. mostimagesincontent: ['&list=querypage&qplimit=10&qppage=MostLinkedFilesInContent', queryfunctions.times],
  83. unusedvideos: ['&list=querypage&qplimit=10&qppage=UnusedVideos', queryfunctions.title],
  84. withoutimages: ['&list=querypage&qplimit=10&qppage=Withoutimages', queryfunctions.title],
  85. nonportableinfoboxes: ['&list=querypage&qplimit=10&qppage=Nonportableinfoboxes', queryfunctions.title],
  86. popularpages: ['&list=querypage&qplimit=10&qppage=Popularpages', queryfunctions.title],
  87. pageswithoutinfobox: ['&list=querypage&qplimit=10&qppage=Pageswithoutinfobox', queryfunctions.title],
  88. templateswithouttype: ['&list=querypage&qplimit=10&qppage=Templateswithouttype', queryfunctions.title],
  89. allinfoboxes: ['&list=querypage&qplimit=10&qppage=AllInfoboxes', queryfunctions.title],
  90. gadgetusage: ['&list=querypage&qplimit=10&qppage=GadgetUsage', queryfunctions.gadget],
  91. recentchanges: ['&list=recentchanges&rctype=edit|new|log&rclimit=10', queryfunctions.recentchanges]
  92. }
  93. function special_page(lang, msg, title, specialpage, embed, wiki, reaction, spoiler) {
  94. if ( specialpage in overwrites ) {
  95. var args = title.split('/').slice(1,3);
  96. overwrites[specialpage](this, lang, msg, wiki, reaction, spoiler, args, embed);
  97. return;
  98. }
  99. got.get( wiki + 'api.php?action=query&meta=siteinfo|allmessages&siprop=general&amenableparser=true&amtitle=' + encodeURIComponent( title ) + '&ammessages=' + encodeURIComponent( specialpage ) + '-summary' + ( specialpage in querypages ? querypages[specialpage][0] : '' ) + '&format=json', {
  100. responseType: 'json'
  101. } ).then( response => {
  102. var body = response.body;
  103. if ( body && body.warnings ) log_warn(body.warnings);
  104. if ( response.statusCode !== 200 || !body ) {
  105. console.log( '- ' + response.statusCode + ': Error while getting the special page: ' + ( body && body.error && body.error.info ) );
  106. }
  107. else {
  108. if ( body.query.allmessages[0]['*'] ) {
  109. var description = body.query.allmessages[0]['*'].toPlaintext();
  110. if ( description.length > 2000 ) description = description.substring(0, 2000) + '\u2026';
  111. embed.setDescription( description );
  112. }
  113. if ( msg.channel.type === 'text' && msg.guild.id in patreons && specialpage in querypages ) {
  114. var text = Util.splitMessage( querypages[specialpage][1](body.query, wiki), {maxLength:1000} )[0];
  115. embed.addField( lang.get('search.special'), ( text || lang.get('search.empty') ) );
  116. }
  117. }
  118. }, error => {
  119. console.log( '- Error while getting the special page: ' + error );
  120. } ).finally( () => {
  121. msg.sendChannel( spoiler + '<' + embed.url + '>' + spoiler, {embed} );
  122. if ( reaction ) reaction.removeEmoji();
  123. } );
  124. }
  125. module.exports = special_page;