Pārlūkot izejas kodu

remove allSites

Markus-Rost 4 gadi atpakaļ
vecāks
revīzija
bc5a601463
4 mainītis faili ar 24 papildinājumiem un 114 dzēšanām
  1. 5 36
      cmds/wiki/overview.js
  2. 13 21
      cmds/wiki/user.js
  3. 0 47
      util/allSites.js
  4. 6 10
      util/wiki.js

+ 5 - 36
cmds/wiki/overview.js

@@ -3,10 +3,6 @@ const logging = require('../../util/logging.js');
 const {timeoptions} = require('../../util/default.json');
 const {toFormatting, toPlaintext} = require('../../util/functions.js');
 
-var allSites = [];
-const getAllSites = require('../../util/allSites.js');
-getAllSites.then( sites => allSites = sites );
-
 /**
  * Sends a Gamepedia wiki overview.
  * @param {import('../../util/i18n.js')} lang - The user language.
@@ -16,8 +12,7 @@ getAllSites.then( sites => allSites = sites );
  * @param {String} spoiler - If the response is in a spoiler.
  */
 function gamepedia_overview(lang, msg, wiki, reaction, spoiler) {
-	if ( !allSites.length ) getAllSites.update();
-	got.get( wiki + 'api.php?action=query&meta=allmessages|siteinfo&ammessages=custom-Wiki_Manager|custom-GamepediaNotice|custom-FandomMergeNotice&amenableparser=true&siprop=general|statistics|languages|rightsinfo' + ( wiki.isFandom() ? '|variables' : '' ) + '&siinlanguagecode=' + lang.lang + '&list=logevents&ledir=newer&lelimit=1&leprop=timestamp&titles=Special:Statistics&format=json' ).then( response => {
+	got.get( wiki + 'api.php?action=query&meta=allmessages|siteinfo&ammessages=custom-GamepediaNotice&amenableparser=true&siprop=general|statistics|languages|rightsinfo' + ( wiki.isFandom() ? '|variables' : '' ) + '&siinlanguagecode=' + lang.lang + '&list=logevents&ledir=newer&lelimit=1&leprop=timestamp&titles=Special:Statistics&format=json' ).then( response => {
 		var body = response.body;
 		if ( body && body.warnings ) log_warn(body.warnings);
 		if ( response.statusCode !== 200 || !body || body.batchcomplete === undefined || !body.query || !body.query.pages ) {
@@ -109,36 +104,14 @@ function gamepedia_overview(lang, msg, wiki, reaction, spoiler) {
 			var posts = [lang.get('overview.posts')];
 			var walls = [lang.get('overview.walls')];
 			var comments = [lang.get('overview.comments')];
-			var manager = [lang.get('overview.manager'), body.query.allmessages[0]['*']];
+			var manager = [lang.get('overview.manager'), ''];
 			var founder = [lang.get('overview.founder')];
 			var crossover = [lang.get('overview.crossover')];
-			if ( body.query.allmessages[1]['*'] ) {
-				crossover[1] = '<https://' + body.query.allmessages[1]['*'] + '.gamepedia.com/>';
-			}
-			else if ( body.query.allmessages[2]['*'] ) {
-				let merge = body.query.allmessages[2]['*'].split('/');
-				crossover[1] = '<https://' + merge[0] + '.fandom.com/' + ( merge[1] ? merge[1] + '/' : '' ) + '>';
+			if ( body.query.allmessages[0]['*'] ) {
+				crossover[1] = '<https://' + body.query.allmessages[0]['*'] + '.gamepedia.com/>';
 			}
 			var description = [lang.get('overview.description')];
 			var image = [lang.get('overview.image')];
-			if ( allSites.some( site => site.wiki_domain === wiki.hostname ) ) {
-				let site = allSites.find( site => site.wiki_domain === wiki.hostname );
-				
-				manager[1] = ( site.wiki_managers[0] || lang.get('overview.none') );
-				official[1] = lang.get('overview.' + ( site.official_wiki ? 'yes' : 'no' ));
-				if ( site.created && creation_date > new Date(parseInt(site.created + '000', 10)) ) {
-					creation_date = new Date(parseInt(site.created + '000', 10));
-					created[1] = dateformat.format(creation_date);
-				}
-				if ( site.wiki_crossover ) crossover[1] = '<https://' + site.wiki_crossover + '/>';
-				if ( site.wiki_description ) {
-					description[1] = site.wiki_description.escapeFormatting();
-					if ( description[1].length > 1000 ) {
-						description[1] = description[1].substring(0, 1000) + '\u2026';
-					}
-				}
-				if ( site.wiki_image ) image[1] = new URL(site.wiki_image, wiki).href;
-			}
 			return got.get( 'https://community.fandom.com/api/v1/Wikis/Details?ids=' + wikiid + '&format=json&cache=' + Date.now() ).then( ovresponse => {
 				var ovbody = ovresponse.body;
 				if ( ovresponse.statusCode !== 200 || !ovbody || ovbody.exception || !ovbody.items || !ovbody.items[wikiid] ) {
@@ -217,11 +190,7 @@ function gamepedia_overview(lang, msg, wiki, reaction, spoiler) {
 					embed.addField( users[0], users[1], true ).addField( admins[0], admins[1], true );
 					if ( manager[1] ) embed.addField( manager[0], '[' + manager[1] + '](' + wiki.toLink('User:' + manager[1], '', '', true) + ') ([' + lang.get('overview.talk') + '](' + wiki.toLink('User talk:' + manager[1], '', '', true) + '))', true );
 					if ( founder[1] ) embed.addField( founder[0], founder[1], true );
-					if ( crossover[1] ) {
-						let crossoverSite = allSites.find( site => '<https://' + site.wiki_domain + '/>' === crossover[1] );
-						if ( crossoverSite ) embed.addField( crossover[0], '[' + crossoverSite.wiki_display_name + '](' + crossover[1] + ')', true );
-						else embed.addField( crossover[0], crossover[1], true );
-					}
+					if ( crossover[1] ) embed.addField( crossover[0], crossover[1], true );
 					embed.addField( license[0], license[1], true ).addField( misermode[0], misermode[1], true ).setFooter( lang.get('overview.inaccurate') + ( wikiid ? ' • ' + lang.get('overview.wikiid') + ' ' + wikiid : '' ) );
 					if ( description[1] ) embed.addField( description[0], description[1] );
 					if ( image[1] ) embed.addField( image[0], image[1] ).setImage( image[1] );

+ 13 - 21
cmds/wiki/user.js

@@ -6,10 +6,6 @@ const extract_desc = require('../../util/extract_desc.js');
 const {timeoptions, usergroups} = require('../../util/default.json');
 const {toMarkdown, toPlaintext, htmlToDiscord} = require('../../util/functions.js');
 
-var allSites = [];
-const getAllSites = require('../../util/allSites.js');
-getAllSites.then( sites => allSites = sites );
-
 /**
  * Processes a Gamepedia user.
  * @param {import('../../util/i18n.js')} lang - The user language.
@@ -25,7 +21,6 @@ getAllSites.then( sites => allSites = sites );
  * @param {String} spoiler - If the response is in a spoiler.
  */
 function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragment, querypage, contribs, reaction, spoiler) {
-	if ( !allSites.length ) getAllSites.update();
 	if ( /^(?:(?:\d{1,3}\.){3}\d{1,3}(?:\/\d{2})?|(?:[\dA-F]{1,4}:){7}[\dA-F]{1,4}(?:\/\d{2,3})?)$/.test(username) ) return got.get( wiki + 'api.php?action=query&meta=siteinfo&siprop=general&list=blocks&bkprop=user|by|timestamp|expiry|reason&bkip=' + encodeURIComponent( username ) + '&format=json' ).then( response => {
 		logging(wiki, msg.guild?.id, 'user', 'ip');
 		var body = response.body;
@@ -193,7 +188,7 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 	} );
 
 	logging(wiki, msg.guild?.id, 'user');
-	got.get( wiki + 'api.php?action=query&meta=allmessages|siteinfo' + ( wiki.hasCentralAuth() ? '|globaluserinfo&guiprop=groups|editcount|merged&guiuser=' + encodeURIComponent( username ) + '&' : '' ) + '&ammessages=custom-Wiki_Manager&amenableparser=true&siprop=general&prop=revisions&rvprop=content|user&rvslots=main&titles=User:' + encodeURIComponent( username ) + '/Discord&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ) + '&format=json' ).then( response => {
+	got.get( wiki + 'api.php?action=query&meta=siteinfo' + ( wiki.hasCentralAuth() ? '|globaluserinfo&guiprop=groups|editcount|merged&guiuser=' + encodeURIComponent( username ) + '&' : '' ) + '&siprop=general&prop=revisions&rvprop=content|user&rvslots=main&titles=User:' + encodeURIComponent( username ) + '/Discord&list=users&usprop=blockinfo|groups|editcount|registration|gender&ususers=' + encodeURIComponent( username ) + '&format=json' ).then( response => {
 		var body = response.body;
 		if ( body && body.warnings ) log_warn(body.warnings);
 		if ( response.statusCode !== 200 || !body || body.batchcomplete === undefined || !body.query || !body.query.users || !body.query.users[0] ) {
@@ -318,16 +313,7 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 						} ));
 					}
 					else if ( globalgroups.includes( usergroup ) ) {
-						let thisSite = allSites.find( site => site.wiki_domain === wiki.hostname );
-						if ( usergroup === 'wiki_manager' && thisSite && thisSite.wiki_managers.includes( username ) ) {
-							globalgroup.push('**' + groupnames[globalgroups.indexOf(usergroup) + groups.length] + '**');
-						}
-						else if ( usergroup === 'wiki-manager' && ( body.query.allmessages[0]['*'] === username || thisSite && thisSite.wiki_managers.includes( username ) ) ) {
-							globalgroup.push('**' + groupnames[globalgroups.indexOf(usergroup) + groups.length] + '**');
-						}
-						else {
-							globalgroup.push(groupnames[globalgroups.indexOf(usergroup) + groups.length]);
-						}
+						globalgroup.push(groupnames[globalgroups.indexOf(usergroup) + groups.length]);
 					}
 				}
 			}
@@ -450,11 +436,17 @@ function gamepedia_user(lang, msg, namespace, username, wiki, querystring, fragm
 						else text += '\n' + discordname.join(' ');
 					}
 					if ( cpbody.profile['favwiki'] ) {
-						var favwiki = [lang.get('user.info.favwiki'),allSites.find( site => site.md5_key === cpbody.profile['favwiki'] )];
-						if ( favwiki[1] ) {
-							if ( msg.showEmbed() ) embed.addField( favwiki[0], '[' + favwiki[1].wiki_display_name + '](<https://' + favwiki[1].wiki_domain + '/>)', true );
-							else text += '\n' + favwiki[0] + ' <https://' + favwiki[1].wiki_domain + '/>';
-						}
+						return got.get( wiki + 'api.php?action=profile&do=getWiki&hash=' + encodeURIComponent( cpbody.profile['favwiki'] ) + '&format=json' ).then( favresponse => {
+							var favbody = favresponse.body;
+							if ( favresponse.statusCode !== 200 || !favbody?.result === 'success' || !favbody.data ) {
+								console.log( '- ' + favresponse.statusCode + ': Error while getting the favorite wiki: ' + ( favbody && ( favbody.error && favbody.error.info || favbody.errormsg ) ) );
+								return;
+							}
+							if ( msg.showEmbed() ) embed.addField( lang.get('user.info.favwiki'), '[' + favbody.data.wiki_name_display + '](<' + favbody.data.wiki_url + '>)', true );
+							else text += '\n' + lang.get('user.info.favwiki') + ' <' + favbody.data.wiki_url + '>';
+						}, error => {
+							console.log( '- Error while getting the favorite wiki: ' + error );
+						} );
 					}
 				}, error => {
 					console.log( '- Error while getting the curse profile: ' + error );

+ 0 - 47
util/allSites.js

@@ -1,47 +0,0 @@
-var allSites = getAllSites();
-
-/**
- * Get all Gamepedia sites.
- * @returns {Promise<Object[]>}
- */
-function getAllSites() {
-	if ( typeof got === 'undefined' ) return Promise.resolve([]);
-	return got.get( 'https://commons.gamepedia.com/api.php?action=allsites&formatversion=2&do=getSiteStats&filter=wikis|md5_key,wiki_domain,wiki_display_name,wiki_image,wiki_description,wiki_managers,official_wiki,wiki_crossover,created&format=json' ).then( response => {
-		var body = response.body;
-		if ( response.statusCode !== 200 || !body || body.status !== 'okay' || !body.data || !body.data.wikis ) {
-			console.log( '- ' + shardId + ': ' + response.statusCode + ': Error while gettings all sites: ' + ( body && body.error && body.error.info ) );
-			return [];
-		}
-		else {
-			console.log( '- ' + shardId + ': Sites successfully loaded.' );
-			var sites = JSON.parse(JSON.stringify(body.data.wikis.filter( site => /^[a-z\d-]{1,50}\.gamepedia\.com$/.test(site.wiki_domain) )));
-			sites.filter( site => site.wiki_crossover ).forEach( site => site.wiki_crossover = site.wiki_crossover.replace( /^(?:https?:)?\/\/(([a-z\d-]{1,50})\.(?:fandom\.com|wikia\.org)(?:(?!\/wiki\/)\/([a-z-]{1,8}))?).*/, '$1' ) );
-			return sites;
-		}
-	}, error => {
-		console.log( '- ' + shardId + ': Error while gettings all sites: ' + error );
-		return [];
-	} );
-}
-
-/**
- * Update the list of all sites.
- * @returns {Promise<Object[]>}
- */
-function updateAllSites() {
-	return new Promise( function(resolve, reject) {
-		getAllSites().then( newSites => {
-			if ( newSites.length ) allSites.then( sites => {
-				sites.splice(0, sites.length);
-				sites.push(...newSites);
-				resolve(sites);
-			} );
-			else resolve(newSites);
-		} );
-	} );
-}
-
-module.exports = {
-	update: updateAllSites,
-	then: (callback) => allSites.then(callback)
-};

+ 6 - 10
util/wiki.js

@@ -1,10 +1,6 @@
 const util = require('util');
 const {defaultSettings, wikiProjects} = require('./default.json');
 
-var allSites = [];
-const getAllSites = require('../util/allSites.js');
-getAllSites.then( sites => allSites = sites );
-
 /**
  * A wiki.
  * @class Wiki
@@ -21,6 +17,7 @@ class Wiki extends URL {
 		this.protocol = 'https';
 		let articlepath = '/index.php?title=$1';
 		if ( this.isFandom() ) articlepath = this.pathname + 'wiki/$1';
+		this.gamepedia = this.hostname.endsWith( '.gamepedia.com' );
 		if ( this.isGamepedia() ) articlepath = '/$1';
 		let project = wikiProjects.find( project => this.hostname.endsWith( project.name ) );
 		if ( project ) {
@@ -62,15 +59,17 @@ class Wiki extends URL {
 	 * @param {String} siteinfo.mainpage - Main page of the wiki.
 	 * @param {String} siteinfo.centralidlookupprovider - Central auth of the wiki.
 	 * @param {String} siteinfo.logo - Logo of the wiki.
+	 * @param {String} [siteinfo.gamepedia] - If the wiki is a Gamepedia wiki.
 	 * @returns {Wiki}
 	 */
-	updateWiki({servername, scriptpath, articlepath, mainpage, centralidlookupprovider, logo}) {
+	updateWiki({servername, scriptpath, articlepath, mainpage, centralidlookupprovider, logo, gamepedia = 'false'}) {
 		this.hostname = servername;
 		this.pathname = scriptpath + '/';
 		this.articlepath = articlepath;
 		this.mainpage = mainpage;
 		this.centralauth = centralidlookupprovider;
 		this.miraheze = /^(?:https?:)?\/\/static\.miraheze\.org\//.test(logo);
+		this.gamepedia = ( gamepedia === 'true' ? true : this.hostname.endsWith( '.gamepedia.com' ) );
 		return this;
 	}
 
@@ -81,7 +80,7 @@ class Wiki extends URL {
 	 */
 	isFandom(includeGP = true) {
 		return ( this.hostname.endsWith( '.fandom.com' ) || this.hostname.endsWith( '.wikia.org' )
-		|| ( includeGP && this.hostname.endsWith( '.gamepedia.com' ) ) );
+		|| ( includeGP && this.isGamepedia() ) );
 	}
 
 	/**
@@ -89,7 +88,7 @@ class Wiki extends URL {
 	 * @returns {Boolean}
 	 */
 	isGamepedia() {
-		return this.hostname.endsWith( '.gamepedia.com' );
+		return this.gamepedia;
 	}
 
 	/**
@@ -215,9 +214,6 @@ class Wiki extends URL {
 			regex = input.match( new RegExp( project.regex + `(?:${project.articlePath}|${project.scriptPath}|/?$)` ) );
 			if ( regex ) return new this('https://' + regex[1] + project.scriptPath);
 		}
-		if ( allSites.some( site => site.wiki_domain === input + '.gamepedia.com' ) ) {
-			return new this('https://' + input + '.gamepedia.com/');
-		}
 		if ( /^(?:[a-z-]{2,12}\.)?[a-z\d-]{1,50}$/.test(input) ) {
 			if ( !input.includes( '.' ) ) return new this('https://' + input + '.fandom.com/');
 			else return new this('https://' + input.split('.')[1] + '.fandom.com/' + input.split('.')[0] + '/');