diff.js 19 KB

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