diff.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. const htmlparser = require('htmlparser2');
  2. const {MessageEmbed} = require('discord.js');
  3. const {timeoptions} = require('../../../util/default.json');
  4. const {toFormatting} = require('../../../util/functions.js');
  5. /**
  6. * Processes a Fandom edit.
  7. * @param {import('../../../util/i18n.js')} lang - The user language.
  8. * @param {import('discord.js').Message} msg - The Discord message.
  9. * @param {String[]} args - The command arguments.
  10. * @param {import('../../../util/wiki.js')} wiki - The wiki for the edit.
  11. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  12. * @param {String} spoiler - If the response is in a spoiler.
  13. * @param {MessageEmbed} [embed] - The embed for the page.
  14. */
  15. function fandom_diff(lang, msg, args, wiki, reaction, spoiler, embed) {
  16. if ( args[0] ) {
  17. var error = false;
  18. var title = '';
  19. var revision = 0;
  20. var diff = 'prev';
  21. if ( /^\d+$/.test(args[0]) ) {
  22. revision = args[0];
  23. if ( args[1] ) {
  24. if ( /^\d+$/.test(args[1]) ) {
  25. diff = args[1];
  26. }
  27. else if ( args[1] === 'prev' || args[1] === 'next' ) {
  28. diff = args[1];
  29. }
  30. else error = true;
  31. }
  32. }
  33. else if ( args[0] === 'prev' || args[0] === 'next' ) {
  34. diff = args[0];
  35. if ( args[1] ) {
  36. if ( /^\d+$/.test(args[1]) ) {
  37. revision = args[1];
  38. }
  39. else error = true;
  40. }
  41. else error = true;
  42. }
  43. else title = args.join(' ');
  44. if ( error ) msg.reactEmoji('error');
  45. else if ( /^\d+$/.test(diff) ) {
  46. var argids = [];
  47. if ( parseInt(revision, 10) > parseInt(diff, 10) ) argids = [revision, diff];
  48. else if ( parseInt(revision, 10) === parseInt(diff, 10) ) argids = [revision];
  49. else argids = [diff, revision];
  50. fandom_diff_send(lang, msg, argids, wiki, reaction, spoiler);
  51. }
  52. else {
  53. got.get( wiki + 'api.php?action=query&prop=revisions&rvprop=' + ( title ? '&titles=' + encodeURIComponent( title ) : '&revids=' + revision ) + '&rvdiffto=' + diff + '&format=json' ).then( response => {
  54. var body = response.body;
  55. if ( body && body.warnings ) log_warn(body.warnings);
  56. if ( response.statusCode !== 200 || !body || !body.query ) {
  57. if ( wiki.noWiki(response.url) || response.statusCode === 410 ) {
  58. console.log( '- This wiki doesn\'t exist!' );
  59. msg.reactEmoji('nowiki');
  60. }
  61. else {
  62. console.log( '- ' + response.statusCode + ': Error while getting the search results: ' + ( body && body.error && body.error.info ) );
  63. msg.sendChannelError( spoiler + '<' + wiki.toLink(title, ( title ? {diff} : {diff,oldid:revision} )) + '>' + spoiler );
  64. }
  65. if ( reaction ) reaction.removeEmoji();
  66. }
  67. else {
  68. if ( body.query.badrevids ) {
  69. msg.replyMsg( lang.get('diff.badrev') );
  70. if ( reaction ) reaction.removeEmoji();
  71. } else if ( body.query.pages && !body.query.pages[-1] ) {
  72. var revisions = Object.values(body.query.pages)[0].revisions[0];
  73. if ( revisions.texthidden === undefined ) {
  74. var argids = [];
  75. var ids = revisions.diff;
  76. if ( !ids.from ) argids = [ids.to];
  77. else {
  78. argids = [ids.to, ids.from];
  79. var compare = ['', ''];
  80. if ( ids['*'] !== undefined ) {
  81. var more = '\n__' + lang.get('diff.info.more') + '__';
  82. var current_tag = '';
  83. var small_prev_ins = '';
  84. var small_prev_del = '';
  85. var ins_length = more.length;
  86. var del_length = more.length;
  87. var added = false;
  88. var parser = new htmlparser.Parser( {
  89. onopentag: (tagname, attribs) => {
  90. if ( tagname === 'ins' || tagname == 'del' ) {
  91. current_tag = tagname;
  92. }
  93. if ( tagname === 'td' && attribs.class === 'diff-addedline' ) {
  94. current_tag = tagname+'a';
  95. }
  96. if ( tagname === 'td' && attribs.class === 'diff-deletedline' ) {
  97. current_tag = tagname+"d";
  98. }
  99. if ( tagname === 'td' && attribs.class === 'diff-marker' ) {
  100. added = true;
  101. }
  102. },
  103. ontext: (htmltext) => {
  104. if ( current_tag === 'ins' && ins_length <= 1000 ) {
  105. ins_length += ( '**' + htmltext.escapeFormatting() + '**' ).length;
  106. if ( ins_length <= 1000 ) small_prev_ins += '**' + htmltext.escapeFormatting() + '**';
  107. else small_prev_ins += more;
  108. }
  109. if ( current_tag === 'del' && del_length <= 1000 ) {
  110. del_length += ( '~~' + htmltext.escapeFormatting() + '~~' ).length;
  111. if ( del_length <= 1000 ) small_prev_del += '~~' + htmltext.escapeFormatting() + '~~';
  112. else small_prev_del += more;
  113. }
  114. if ( ( current_tag === 'afterins' || current_tag === 'tda') && ins_length <= 1000 ) {
  115. ins_length += htmltext.escapeFormatting().length;
  116. if ( ins_length <= 1000 ) small_prev_ins += htmltext.escapeFormatting();
  117. else small_prev_ins += more;
  118. }
  119. if ( ( current_tag === 'afterdel' || current_tag === 'tdd') && del_length <= 1000 ) {
  120. del_length += htmltext.escapeFormatting().length;
  121. if ( del_length <= 1000 ) small_prev_del += htmltext.escapeFormatting();
  122. else small_prev_del += more;
  123. }
  124. if ( added ) {
  125. if ( htmltext === '+' && ins_length <= 1000 ) {
  126. ins_length++;
  127. if ( ins_length <= 1000 ) small_prev_ins += '\n';
  128. else small_prev_ins += more;
  129. }
  130. if ( htmltext === '−' && del_length <= 1000 ) {
  131. del_length++;
  132. if ( del_length <= 1000 ) small_prev_del += '\n';
  133. else small_prev_del += more;
  134. }
  135. added = false;
  136. }
  137. },
  138. onclosetag: (tagname) => {
  139. if ( tagname === 'ins' ) {
  140. current_tag = 'afterins';
  141. } else if ( tagname === 'del' ) {
  142. current_tag = 'afterdel';
  143. } else {
  144. current_tag = '';
  145. }
  146. }
  147. }, {decodeEntities:true} );
  148. parser.write( ids['*'] );
  149. parser.end();
  150. if ( small_prev_del.length ) {
  151. if ( small_prev_del.replace( /\~\~/g, '' ).trim().length ) {
  152. compare[0] = small_prev_del.replace( /\~\~\~\~/g, '' );
  153. } else compare[0] = '__' + lang.get('diff.info.whitespace') + '__';
  154. }
  155. if ( small_prev_ins.length ) {
  156. if ( small_prev_ins.replace( /\*\*/g, '' ).trim().length ) {
  157. compare[1] = small_prev_ins.replace( /\*\*\*\*/g, '' );
  158. } else compare[1] = '__' + lang.get('diff.info.whitespace') + '__';
  159. }
  160. }
  161. }
  162. fandom_diff_send(lang, msg, argids, wiki, reaction, spoiler, compare);
  163. } else {
  164. msg.replyMsg( lang.get('diff.badrev') );
  165. if ( reaction ) reaction.removeEmoji();
  166. }
  167. } else {
  168. if ( body.query.pages && body.query.pages[-1] ) msg.replyMsg( lang.get('diff.badrev') );
  169. else msg.reactEmoji('error');
  170. if ( reaction ) reaction.removeEmoji();
  171. }
  172. }
  173. }, error => {
  174. if ( wiki.noWiki(error.message) ) {
  175. console.log( '- This wiki doesn\'t exist!' );
  176. msg.reactEmoji('nowiki');
  177. }
  178. else {
  179. console.log( '- Error while getting the search results: ' + error );
  180. msg.sendChannelError( spoiler + '<' + wiki.toLink(title, ( title ? {diff} : {diff,oldid:revision} )) + '>' + spoiler );
  181. }
  182. if ( reaction ) reaction.removeEmoji();
  183. } );
  184. }
  185. }
  186. else {
  187. if ( embed ) msg.sendChannel( spoiler + '<' + embed.url + '>' + spoiler, {embed} );
  188. else msg.reactEmoji('error');
  189. if ( reaction ) reaction.removeEmoji();
  190. }
  191. }
  192. /**
  193. * Sends a Fandom edit.
  194. * @param {import('../../../util/i18n.js')} lang - The user language.
  195. * @param {import('discord.js').Message} msg - The Discord message.
  196. * @param {String[]} args - The command arguments.
  197. * @param {import('../../../util/wiki.js')} wiki - The wiki for the edit.
  198. * @param {import('discord.js').MessageReaction} reaction - The reaction on the message.
  199. * @param {String} spoiler - If the response is in a spoiler.
  200. * @param {String[]} [compare] - The edit difference.
  201. */
  202. function fandom_diff_send(lang, msg, args, wiki, reaction, spoiler, compare) {
  203. got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general&list=tags&tglimit=500&tgprop=displayname&prop=revisions&rvprop=ids|timestamp|flags|user|size|comment|tags' + ( args.length === 1 || args[0] === args[1] ? '|content' : '' ) + '&revids=' + args.join('|') + '&format=json' ).then( response => {
  204. var body = response.body;
  205. if ( body && body.warnings ) log_warn(body.warnings);
  206. if ( response.statusCode !== 200 || !body || !body.query ) {
  207. if ( wiki.noWiki(response.url) || response.statusCode === 410 ) {
  208. console.log( '- This wiki doesn\'t exist!' );
  209. msg.reactEmoji('nowiki');
  210. }
  211. else {
  212. console.log( '- ' + response.statusCode + ': Error while getting the search results: ' + ( body && body.error && body.error.info ) );
  213. msg.sendChannelError( spoiler + '<' + wiki.toLink('Special:Diff/' + ( args[1] ? args[1] + '/' : '' ) + args[0]) + '>' + spoiler );
  214. }
  215. if ( reaction ) reaction.removeEmoji();
  216. }
  217. else if ( body.query.badrevids ) {
  218. msg.replyMsg( lang.get('diff.badrev') );
  219. if ( reaction ) reaction.removeEmoji();
  220. }
  221. else if ( body.query.pages && !body.query.pages['-1'] ) {
  222. wiki.updateWiki(body.query.general);
  223. var pages = Object.values(body.query.pages);
  224. if ( pages.length !== 1 ) {
  225. msg.sendChannel( spoiler + '<' + wiki.toLink('Special:Diff/' + ( args[1] ? args[1] + '/' : '' ) + args[0]) + '>' + spoiler );
  226. if ( reaction ) reaction.removeEmoji();
  227. }
  228. else {
  229. var title = pages[0].title;
  230. var revisions = pages[0].revisions.sort( (first, second) => Date.parse(second.timestamp) - Date.parse(first.timestamp) );
  231. var diff = revisions[0].revid;
  232. var oldid = ( revisions[1] ? revisions[1].revid : 0 );
  233. var editor = [lang.get('diff.info.editor'), ( revisions[0].userhidden !== undefined ? lang.get('diff.hidden') : revisions[0].user )];
  234. var timestamp = [lang.get('diff.info.timestamp'), new Date(revisions[0].timestamp).toLocaleString(lang.get('dateformat'), timeoptions)];
  235. var difference = revisions[0].size - ( revisions[1] ? revisions[1].size : 0 );
  236. var size = [lang.get('diff.info.size'), lang.get('diff.info.bytes', ( difference > 0 ? '+' : '' ) + difference)];
  237. var comment = [lang.get('diff.info.comment'), ( revisions[0].commenthidden !== undefined ? lang.get('diff.hidden') : ( revisions[0].comment ? toFormatting(revisions[0].comment, msg.showEmbed(), wiki, title) : lang.get('diff.nocomment') ) )];
  238. if ( revisions[0].tags.length ) var tags = [lang.get('diff.info.tags'), body.query.tags.filter( tag => revisions[0].tags.includes( tag.name ) ).map( tag => tag.displayname ).join(', ')];
  239. var pagelink = wiki.toLink(title, {diff,oldid});
  240. if ( msg.showEmbed() ) {
  241. var text = '<' + pagelink + '>';
  242. var editorlink = '[' + editor[1] + '](' + wiki.toLink('User:' + editor[1], '', '', true) + ')';
  243. if ( revisions[0].anon !== undefined ) {
  244. editorlink = '[' + editor[1] + '](' + wiki.toLink('Special:Contributions/' + editor[1], '', '', true) + ')';
  245. }
  246. if ( editor[1] === lang.get('diff.hidden') ) editorlink = editor[1];
  247. var embed = new MessageEmbed().setAuthor( body.query.general.sitename ).setTitle( ( title + '?diff=' + diff + '&oldid=' + oldid ).escapeFormatting() ).setURL( pagelink ).addField( editor[0], editorlink, true ).addField( size[0], size[1], true ).addField( comment[0], comment[1] ).setFooter( timestamp[1] );
  248. if ( tags ) {
  249. var taglink = '';
  250. var tagtext = '';
  251. var tagparser = new htmlparser.Parser( {
  252. onopentag: (tagname, attribs) => {
  253. if ( tagname === 'a' ) taglink = attribs.href;
  254. },
  255. ontext: (htmltext) => {
  256. if ( taglink ) tagtext += '[' + htmltext.escapeFormatting() + '](' + taglink + ')'
  257. else tagtext += htmltext.escapeFormatting();
  258. },
  259. onclosetag: (tagname) => {
  260. if ( tagname === 'a' ) taglink = '';
  261. }
  262. }, {decodeEntities:true} );
  263. tagparser.write( tags[1] );
  264. tagparser.end();
  265. embed.addField( tags[0], tagtext );
  266. }
  267. var more = '\n__' + lang.get('diff.info.more') + '__';
  268. if ( !compare && oldid ) got.get( wiki + 'api.php?action=query&prop=revisions&rvprop=&revids=' + oldid + '&rvdiffto=' + diff + '&format=json' ).then( cpresponse => {
  269. var cpbody = cpresponse.body;
  270. if ( cpbody && cpbody.warnings ) log_warn(cpbody.warnings);
  271. if ( cpresponse.statusCode !== 200 || !cpbody || !cpbody.query || cpbody.query.badrevids || !cpbody.query.pages && cpbody.query.pages[-1] ) {
  272. console.log( '- ' + cpresponse.statusCode + ': Error while getting the diff: ' + ( cpbody && cpbody.error && cpbody.error.info ) );
  273. }
  274. else {
  275. var revision = Object.values(cpbody.query.pages)[0].revisions[0];
  276. if ( revision.texthidden === undefined && revision.diff && revision.diff['*'] !== undefined ) {
  277. var current_tag = '';
  278. var small_prev_ins = '';
  279. var small_prev_del = '';
  280. var ins_length = more.length;
  281. var del_length = more.length;
  282. var added = false;
  283. var parser = new htmlparser.Parser( {
  284. onopentag: (tagname, attribs) => {
  285. if ( tagname === 'ins' || tagname == 'del' ) {
  286. current_tag = tagname;
  287. }
  288. if ( tagname === 'td' && attribs.class === 'diff-addedline' ) {
  289. current_tag = tagname+'a';
  290. }
  291. if ( tagname === 'td' && attribs.class === 'diff-deletedline' ) {
  292. current_tag = tagname+"d";
  293. }
  294. if ( tagname === 'td' && attribs.class === 'diff-marker' ) {
  295. added = true;
  296. }
  297. },
  298. ontext: (htmltext) => {
  299. if ( current_tag === 'ins' && ins_length <= 1000 ) {
  300. ins_length += ( '**' + htmltext.escapeFormatting() + '**' ).length;
  301. if ( ins_length <= 1000 ) small_prev_ins += '**' + htmltext.escapeFormatting() + '**';
  302. else small_prev_ins += more;
  303. }
  304. if ( current_tag === 'del' && del_length <= 1000 ) {
  305. del_length += ( '~~' + htmltext.escapeFormatting() + '~~' ).length;
  306. if ( del_length <= 1000 ) small_prev_del += '~~' + htmltext.escapeFormatting() + '~~';
  307. else small_prev_del += more;
  308. }
  309. if ( ( current_tag === 'afterins' || current_tag === 'tda') && ins_length <= 1000 ) {
  310. ins_length += htmltext.escapeFormatting().length;
  311. if ( ins_length <= 1000 ) small_prev_ins += htmltext.escapeFormatting();
  312. else small_prev_ins += more;
  313. }
  314. if ( ( current_tag === 'afterdel' || current_tag === 'tdd') && del_length <= 1000 ) {
  315. del_length += htmltext.escapeFormatting().length;
  316. if ( del_length <= 1000 ) small_prev_del += htmltext.escapeFormatting();
  317. else small_prev_del += more;
  318. }
  319. if ( added ) {
  320. if ( htmltext === '+' && ins_length <= 1000 ) {
  321. ins_length++;
  322. if ( ins_length <= 1000 ) small_prev_ins += '\n';
  323. else small_prev_ins += more;
  324. }
  325. if ( htmltext === '−' && del_length <= 1000 ) {
  326. del_length++;
  327. if ( del_length <= 1000 ) small_prev_del += '\n';
  328. else small_prev_del += more;
  329. }
  330. added = false;
  331. }
  332. },
  333. onclosetag: (tagname) => {
  334. if ( tagname === 'ins' ) {
  335. current_tag = 'afterins';
  336. } else if ( tagname === 'del' ) {
  337. current_tag = 'afterdel';
  338. } else {
  339. current_tag = '';
  340. }
  341. }
  342. }, {decodeEntities:true} );
  343. parser.write( revision.diff['*'] );
  344. parser.end();
  345. if ( small_prev_del.length ) {
  346. if ( small_prev_del.replace( /\~\~/g, '' ).trim().length ) {
  347. embed.addField( lang.get('diff.info.removed'), small_prev_del.replace( /\~\~\~\~/g, '' ), true );
  348. } else embed.addField( lang.get('diff.info.removed'), '__' + lang.get('diff.info.whitespace') + '__', true );
  349. }
  350. if ( small_prev_ins.length ) {
  351. if ( small_prev_ins.replace( /\*\*/g, '' ).trim().length ) {
  352. embed.addField( lang.get('diff.info.added'), small_prev_ins.replace( /\*\*\*\*/g, '' ), true );
  353. } else embed.addField( lang.get('diff.info.added'), '__' + lang.get('diff.info.whitespace') + '__', true );
  354. }
  355. }
  356. else if ( revision.texthidden !== undefined ) {
  357. embed.addField( lang.get('diff.info.added'), '__' + lang.get('diff.hidden') + '__', true );
  358. }
  359. else if ( revision.diff && revision.diff['*'] === undefined ) {
  360. embed.addField( lang.get('diff.info.removed'), '__' + lang.get('diff.hidden') + '__', true );
  361. }
  362. }
  363. }, error => {
  364. console.log( '- Error while getting the diff: ' + error );
  365. } ).finally( () => {
  366. msg.sendChannel( spoiler + text + spoiler, {embed} );
  367. if ( reaction ) reaction.removeEmoji();
  368. } );
  369. else {
  370. if ( compare ) {
  371. if ( compare[0].length ) embed.addField( lang.get('diff.info.removed'), compare[0], true );
  372. if ( compare[1].length ) embed.addField( lang.get('diff.info.added'), compare[1], true );
  373. }
  374. else if ( revisions[0]['*'] ) {
  375. var content = revisions[0]['*'].escapeFormatting();
  376. if ( content.trim().length ) {
  377. if ( content.length <= 1000 ) content = '**' + content + '**';
  378. else {
  379. content = content.substring(0, 1000 - more.length);
  380. content = '**' + content.substring(0, content.lastIndexOf('\n')) + '**' + more;
  381. }
  382. embed.addField( lang.get('diff.info.added'), content, true );
  383. } else embed.addField( lang.get('diff.info.added'), '__' + lang.get('diff.info.whitespace') + '__', true );
  384. }
  385. msg.sendChannel( spoiler + text + spoiler, {embed} );
  386. if ( reaction ) reaction.removeEmoji();
  387. }
  388. }
  389. else {
  390. var embed = {};
  391. var text = '<' + pagelink + '>\n\n' + editor.join(' ') + '\n' + timestamp.join(' ') + '\n' + size.join(' ') + '\n' + comment.join(' ');
  392. if ( tags ) text += htmlToPlain( '\n' + tags.join(' ') );
  393. msg.sendChannel( spoiler + text + spoiler, {embed} );
  394. if ( reaction ) reaction.removeEmoji();
  395. }
  396. }
  397. }
  398. else {
  399. msg.reactEmoji('error');
  400. if ( reaction ) reaction.removeEmoji();
  401. }
  402. }, error => {
  403. if ( wiki.noWiki(error.message) ) {
  404. console.log( '- This wiki doesn\'t exist!' );
  405. msg.reactEmoji('nowiki');
  406. }
  407. else {
  408. console.log( '- Error while getting the search results: ' + error );
  409. msg.sendChannelError( spoiler + '<' + wiki.toLink('Special:Diff/' + ( args[1] ? args[1] + '/' : '' ) + args[0]) + '>' + spoiler );
  410. }
  411. if ( reaction ) reaction.removeEmoji();
  412. } );
  413. }
  414. /**
  415. * Change HTML text to plain text.
  416. * @param {String} html - The text in HTML.
  417. * @returns {String}
  418. */
  419. function htmlToPlain(html) {
  420. var text = '';
  421. var parser = new htmlparser.Parser( {
  422. ontext: (htmltext) => {
  423. text += htmltext.escapeFormatting();
  424. }
  425. }, {decodeEntities:true} );
  426. parser.write( html );
  427. parser.end();
  428. return text;
  429. };
  430. module.exports = {
  431. name: 'diff',
  432. run: fandom_diff
  433. };