<commit_before><?php
namespace wcf\system\package;
use wcf\data\package\update\server\PackageUpdateServer;
use wcf\data\package\update\server\PackageUpdateServerEditor;
use wcf\data\package\update\version\PackageUpdateVersionEditor;
use wcf\data\package\update\PackageUpdate;
use wcf\data\package\update\PackageUpdateEditor;
use wcf\data\package\Package;
use wcf\system\cache\builder\PackageUpdateCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\HTTPUnauthorizedException;
use wcf\system\exception\SystemException;
use wcf\system\package\PackageUpdateUnauthorizedException;
use wcf\system\Regex;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
use wcf\util\HTTPRequest;
use wcf\util\XML;

/**
 * Provides functions to manage package updates.
 * 
 * @author	<NAME>
 * @copyright	2001-2014 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	com.woltlab.wcf
 * @subpackage	system.package
 * @category	Community Framework
 */
class PackageUpdateDispatcher extends SingletonFactory {
	/**
	 * Refreshes the package database.
	 * 
	 * @param	array<integer>		$packageUpdateServerIDs
	 * @param	boolean			$ignoreCache
	 */
	public function refreshPackageDatabase(array $packageUpdateServerIDs = array(), $ignoreCache = false) {
		// get update server data
		$updateServers = PackageUpdateServer::getActiveUpdateServers($packageUpdateServerIDs);
		
		// loop servers
		$refreshedPackageLists = false;
		foreach ($updateServers as $updateServer) {
			if ($ignoreCache || $updateServer->lastUpdateTime < TIME_NOW - 600) {
				$errorMessage = '';
				
				try {
					$this->getPackageUpdateXML($updateServer);
					$refreshedPackageLists = true;
				}
				catch (SystemException $e) {
					$errorMessage = $e->getMessage();
				}
				catch (PackageUpdateUnauthorizedException $e) {
					$reply = $e->getRequest()->getReply();
					$errorMessage = reset($reply['httpHeaders']);
				}
				
				if ($errorMessage) {
					// save error status
					$updateServerEditor = new PackageUpdateServerEditor($updateServer);
					$updateServerEditor->update(array(
						'status' => 'offline',
						'errorMessage' => $errorMessage
					));
				}
			}
		}
		
		if ($refreshedPackageLists) {
			PackageUpdateCacheBuilder::getInstance()->reset();
		}
	}
	
	/**
	 * Gets the package_update.xml from an update server.
	 * 
	 * @param	\wcf\data\package\update\server\PackageUpdateServer	$updateServer
	 */
	protected function getPackageUpdateXML(PackageUpdateServer $updateServer) {
		$settings = array();
		$authData = $updateServer->getAuthData();
		if ($authData) $settings['auth'] = $authData;
		<commit_msg>Fix handling of 401 when updating package list<commit_after><?php
namespace wcf\system\package;
use wcf\data\package\update\server\PackageUpdateServer;
use wcf\data\package\update\server\PackageUpdateServerEditor;
use wcf\data\package\update\version\PackageUpdateVersionEditor;
use wcf\data\package\update\PackageUpdate;
use wcf\data\package\update\PackageUpdateEditor;
use wcf\data\package\Package;
use wcf\system\cache\builder\PackageUpdateCacheBuilder;
use wcf\system\database\util\PreparedStatementConditionBuilder;
use wcf\system\exception\HTTPUnauthorizedException;
use wcf\system\exception\SystemException;
use wcf\system\package\PackageUpdateUnauthorizedException;
use wcf\system\Regex;
use wcf\system\SingletonFactory;
use wcf\system\WCF;
use wcf\util\HTTPRequest;
use wcf\util\XML;

/**
 * Provides functions to manage package updates.
 * 
 * @author	Alex<NAME>
 * @copyright	2001-2014 WoltLab GmbH
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 * @package	com.woltlab.wcf
 * @subpackage	system.package
 * @category	Community Framework
 */
class PackageUpdateDispatcher extends SingletonFactory {
	/**
	 * Refreshes the package database.
	 * 
	 * @param	array<integer>		$packageUpdateServerIDs
	 * @param	boolean			$ignoreCache
	 */
	public function refreshPackageDatabase(array $packageUpdateServerIDs = array(), $ignoreCache = false) {
		// get update server data
		$updateServers = PackageUpdateServer::getActiveUpdateServers($packageUpdateServerIDs);
		
		// loop servers
		$refreshedPackageLists = false;
		foreach ($updateServers as $updateServer) {
			if ($ignoreCache || $updateServer->lastUpdateTime < TIME_NOW - 600) {
				$errorMessage = '';
				
				try {
					$this->getPackageUpdateXML($updateServer);
					$refreshedPackageLists = true;
				}
				catch (SystemException $e) {
					$errorMessage = $e->getMessage();
				}
				catch (PackageUpdateUnauthorizedException $e) {
					$reply = $e->getRequest()->getReply();
					list($errorMessage) = reset($reply['httpHeaders']);
				}
				
				if ($errorMessage) {
					// save error status
					$updateServerEditor = new PackageUpdateServerEditor($updateServer);
					$updateServerEditor->update(array(
						'status' => 'offline',
						'errorMessage' => $errorMessage
					));
				}
			}
		}
		
		if ($refreshedPackageLists) {
			PackageUpdateCacheBuilder::getInstance()->reset();
		}
	}
	
	/**
	 * Gets the package_update.xml from an update server.
	 * 
	 * @param	\wcf\data\package\update\server\PackageUpdateServer	$updateServer
	 */
	protected function getPackageUpdateXML(PackageUpdateServer $updateServer) {
		$settings = array();
		$authData = $updateServer->getAuthData();
		if ($authData) $settings['auth'] = $authData;
		
{'fraction_non_alphanumeric': 0.11893870082342177, 'fraction_numerical': 0.004574565416285453, 'mean_word_length': 4.444223107569721, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '5834107', 'n_tokens_mistral': 1882, 'n_tokens_neox': 1603, 'n_words': 319}