<commit_before><?php
use MediaWiki\MediaWikiServices;

/**
 * Database factory class, this will determine whether to use the main database
 * or an external database defined in configuration file
 */
class MWEchoDbFactory {

	/**
	 * The cluster for the database
	 * @var string|bool
	 */
	private $cluster;

	private $shared;

	private $sharedCluster;

	/**
	 * @param string|bool $cluster
	 * @param string|bool $shared
	 * @param string|bool $sharedCluster
	 */
	public function __construct( $cluster = false, $shared = false, $sharedCluster = false ) {
		$this->cluster = $cluster;
		$this->shared = $shared;
		$this->sharedCluster = $sharedCluster;
	}

	/**
	 * Create a db factory instance from default Echo configuration
	 * A singleton is not necessary because it's actually handled
	 * inside core database object
	 *
	 * @return MWEchoDbFactory
	 */
	public static function newFromDefault() {
		global $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster;

		return new self( $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster );
	}

	/**
	 * Get the database load balancer
	 * @return LoadBalancer
	 */
	protected function getLB() {
		// Use the external db defined for Echo
		if ( $this->cluster ) {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->cluster );
		} else {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		}

		return $lb;
	}

	/**
	 * @return LoadBalancer
	 */
	protected function getSharedLB() {
		if ( $this->sharedCluster ) {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->sharedCluster );
		} else {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		}

		return $lb;
	}

	/**
	 * Get the database connection for Echo
	 * @param $db int Index of the connection to get
	 * @param $groups mixed Query groups.
	 * @return IDatabase
	 */
	public function getEchoDb( $db, $groups = [] ) {
		return $this->getLB()->getConnection( $db, $groups );
	}

	/**
	 * @param $db int Index of the connection to get
	 * @param array $groups Query groups
	 * @return bool|IDatabase false if no shared db is configured
	 */
	public function getSharedDb( $db, $groups = [] ) {
		if ( !$this->shared ) {
			return false;
		}

		return $this->getSharedLB()->getConnection( $db, $groups, $this->shared );
	}

	/**
	 * Wrapper function for wfGetDB, some extensions like MobileFrontend is
	 * using this to issue sql queries against Echo database directly.  This
	 * is totally not accepted and should be updated to use Echo database access
	 * objects
	 *
	 * @deprecated Use newFromDefault() instead to create a db factory
	 * @param $db int Index of the connection to get
	 * @param $groups mixed Query groups.
	 * @param $wiki string|bool The wiki ID, or false for the current wiki
	 * @return IDatabase
	 */
	public static function getDB( $db, $groups = [], $wiki = false ) {
		global $wgEchoCluster;

		// Use the external db defined for Echo
		if ( $wgEchoCluster ) {
			$lb = wfGetLBFactory()->getExternalLB( $wgEchoCluster, $wiki );
		} else {
			$lb = wfGetLB( $wiki );
		}

		return $lb->getConnection( $db, $groups, $wiki );
	}

	/**
	 * Wait for the slaves of the database
	 */
	public function waitForSlaves() {
		$this->waitFor( $this->getMasterPosition() );
	}

	/**
	 * Get the current master position for the wiki and echo
	 * db when they have at least one slave in their cluster.<commit_msg>Replace deprecated wfGetLBFactory() and wfGetLB() call<commit_after><?php
use MediaWiki\MediaWikiServices;

/**
 * Database factory class, this will determine whether to use the main database
 * or an external database defined in configuration file
 */
class MWEchoDbFactory {

	/**
	 * The cluster for the database
	 * @var string|bool
	 */
	private $cluster;

	private $shared;

	private $sharedCluster;

	/**
	 * @param string|bool $cluster
	 * @param string|bool $shared
	 * @param string|bool $sharedCluster
	 */
	public function __construct( $cluster = false, $shared = false, $sharedCluster = false ) {
		$this->cluster = $cluster;
		$this->shared = $shared;
		$this->sharedCluster = $sharedCluster;
	}

	/**
	 * Create a db factory instance from default Echo configuration
	 * A singleton is not necessary because it's actually handled
	 * inside core database object
	 *
	 * @return MWEchoDbFactory
	 */
	public static function newFromDefault() {
		global $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster;

		return new self( $wgEchoCluster, $wgEchoSharedTrackingDB, $wgEchoSharedTrackingCluster );
	}

	/**
	 * Get the database load balancer
	 * @return LoadBalancer
	 */
	protected function getLB() {
		// Use the external db defined for Echo
		if ( $this->cluster ) {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->cluster );
		} else {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		}

		return $lb;
	}

	/**
	 * @return LoadBalancer
	 */
	protected function getSharedLB() {
		if ( $this->sharedCluster ) {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancerFactory()->getExternalLB( $this->sharedCluster );
		} else {
			$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
		}

		return $lb;
	}

	/**
	 * Get the database connection for Echo
	 * @param $db int Index of the connection to get
	 * @param $groups mixed Query groups.
	 * @return IDatabase
	 */
	public function getEchoDb( $db, $groups = [] ) {
		return $this->getLB()->getConnection( $db, $groups );
	}

	/**
	 * @param $db int Index of the connection to get
	 * @param array $groups Query groups
	 * @return bool|IDatabase false if no shared db is configured
	 */
	public function getSharedDb( $db, $groups = [] ) {
		if ( !$this->shared ) {
			return false;
		}

		return $this->getSharedLB()->getConnection( $db, $groups, $this->shared );
	}

	/**
	 * Wrapper function for wfGetDB, some extensions like MobileFrontend is
	 * using this to issue sql queries against Echo database directly.  This
	 * is totally not accepted and should be updated to use Echo database access
	 * objects
	 *
	 * @deprecated Use newFromDefault() instead to create a db factory
	 * @param $db int Index of the connection to get
	 * @param $groups mixed Query groups.
	 * @param $wiki string|bool The wiki ID, or false for the current wiki
	 * @return IDatabase
	 */
	public static function getDB( $db, $groups = [], $wiki = false ) {
		global $wgEchoCluster;

		$services = MediaWikiServices::getInstance();

		// Use the external db defined for Echo
		if ( $wgEchoCluster ) {
			$lb = $services->getDBLoadBalancerFactory()->getExternalLB( $wgEchoCluster, $wiki );
		} else {
			if ( $wiki === false ) {
				$lb = $services->getDBLoadBalancer();
			} else {
				$lb = $services->getDBLoadBalancerFactory()->getMainLB( $wiki );
			}
		}

		return $lb->getConnection( $db, $groups, $wiki );
	}

	/**
	 * Wait for the slaves of the database
	 */
	public function waitForSlaves() {
		$this->waitFor( $this->getMasterPosition() );
	}

	/**
	 * Get the current master position for the wiki and echo
	 * db when they have at least one slave in their cluster.
{'fraction_non_alphanumeric': 0.11974965229485396, 'fraction_numerical': 0.0, 'mean_word_length': 3.755952380952381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '210250', 'n_tokens_mistral': 2359, 'n_tokens_neox': 2103, 'n_words': 704}