diff.js 19 KB

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