<commit_before><?php
use MediaWiki\Session\SessionInfo;
use MediaWiki\Session\UserInfo;
use MediaWiki\Session\SessionBackend;

class ProductOpenerSessionProvider extends MediaWiki\Session\SessionProvider {

	/**
	 * Provide session info for a request
	 *
	 * If no session exists for the request, return null. Otherwise return an
	 * SessionInfo object identifying the session.
	 *
	 * If multiple SessionProviders provide sessions, the one with highest
	 * priority wins. In case of a tie, an exception is thrown.
	 * SessionProviders are encouraged to make priorities user-configurable
	 * unless only max-priority makes sense.
	 *
	 * @warning This will be called early in the MediaWiki setup process,
	 *  before $wgUser, $wgLang, $wgOut, $wgParser, $wgTitle, and corresponding
	 *  pieces of the main RequestContext are set up! If you try to use these,
	 *  things *will* break.
	 * @note The SessionProvider must not attempt to auto-create users.
	 *  MediaWiki will do this later (when it's safe) if the chosen session has
	 *  a user with a valid name but no ID.
	 * @protected For use by \MediaWiki\Session\SessionManager only
	 * @param WebRequest $request
	 * @return SessionInfo|null
	 */
	public function provideSessionInfo( WebRequest $request ) {
		$cookie = $request->getCookie( 'session' );
		if ( $cookie === null || $cookie === '' || $cookie === 'deleted') {
			$this->logger->notice('No session cookie found for request.');
			return null;
		}

		$this->logger->debug('Session cookie found for request: {cookie}.',
		[
			'cookie' => $cookie
		]);

		$chunks = array_chunk(preg_split('/&/', $cookie), 2);
		$data = array_combine(array_column($chunks, 0), array_column($chunks, 1));

		try {
			$response = Http::post( 'https://world.openfoodfacts.org/cgi/sso.pl',  [ "postData" => $data ] );
			if ($response === false) {
				$this->logger->notice('SSO response for cookie {cookie} was {response}.',
				[
					'cookie' => $cookie,
					'response' => $response,
				]);
				return null;
			}

			$this->logger->debug('SSO response for cookie {cookie} was {response}.',
			[
				'cookie' => $cookie,
				'response' => $response,
			]);
			$obj = json_decode($respone);

			$user = User::newFromName( $obj->{'user_id'} );
			if ( $user === false ) {
				$this->logger->info('User::newFromName for {userId} returned false.',
				[
					'cookie' => $cookie,
					'response' => $response,
					'userId' => $obj->{'user_id'},
					'user' => $user,
				]);
				return null;
			}

			if ( $user->getID() == 0 ) {
				$this->logger->notice('User not found (id == 0), trying to create new user.',
				[
					'cookie' => $cookie,
					'response' => $response,
					'userId' => $obj->{'user_id'},
					'user' => $user,
				]);
				$am = AuthManager::singleton();
				$user->setRealName($obj->{'name'});
				$user->setEmail($obj->{'email'});
				AuthManager::singleton()->autoCreateUser($user, self::AUTOCREATE_SOURCE_SESSION, false);
			}

			$info['userInfo'] = UserInfo::newFromUser( $user, true );
			return new SessionInfo( $this->priority, $info );
		} catch (HttpException $ex) {
			$this->logger->error(
				'Could not retrieve user information for session cookie.',
				[
					'cookie' => $cookie
				]
			);
		}
	}

	/**
	 * Validate a loaded SessionInfo and refresh provider metadata
	 *
	 * This is similar in purpose to the 'SessionCheckInfo' hook, and also
	 * allows for updating the provider metadata. On failure, the provider is
	 * expected to write an appropriate message to its logger.
	 *
	 * @protected For use by \MediaWiki\Session\SessionManager only
	 * @param SessionInfo $info Any changes by mergeMetadata() will already be reflected here.
	 * @param WebRequest $request
	 * @param array|null &$metadata Provider metadata, may be altered.
	 * @return bool Return false to reject the SessionInfo after all.
	 */
	public function refreshSessionInfo( SessionInfo $info, WebRequest $request, &$metadata ) {
		return true;<commit_msg>chg: use cookie from $_COOKIE['session'] instead of the mediawiki method fix: typo "respone" => "response" fix: add 'provider' to session info<commit_after><?php
use MediaWiki\Session\SessionInfo;
use MediaWiki\Session\UserInfo;
use MediaWiki\Session\SessionBackend;

class ProductOpenerSessionProvider extends MediaWiki\Session\SessionProvider {

	/**
	 * Provide session info for a request
	 *
	 * If no session exists for the request, return null. Otherwise return an
	 * SessionInfo object identifying the session.
	 *
	 * If multiple SessionProviders provide sessions, the one with highest
	 * priority wins. In case of a tie, an exception is thrown.
	 * SessionProviders are encouraged to make priorities user-configurable
	 * unless only max-priority makes sense.
	 *
	 * @warning This will be called early in the MediaWiki setup process,
	 *  before $wgUser, $wgLang, $wgOut, $wgParser, $wgTitle, and corresponding
	 *  pieces of the main RequestContext are set up! If you try to use these,
	 *  things *will* break.
	 * @note The SessionProvider must not attempt to auto-create users.
	 *  MediaWiki will do this later (when it's safe) if the chosen session has
	 *  a user with a valid name but no ID.
	 * @protected For use by \MediaWiki\Session\SessionManager only
	 * @param WebRequest $request
	 * @return SessionInfo|null
	 */
	public function provideSessionInfo( WebRequest $request ) {
		$cookie = $_COOKIE['session'];
		if ( $cookie === null || $cookie === '' || $cookie === 'deleted') {
			$this->logger->notice('No session cookie found for request.');
			return null;
		}

		$this->logger->debug('Session cookie found for request: {cookie}.',
		[
			'cookie' => $cookie
		]);

		$chunks = array_chunk(preg_split('/&/', $cookie), 2);
		$data = array_combine(array_column($chunks, 0), array_column($chunks, 1));

		try {
			$response = Http::post( 'https://world.openfoodfacts.org/cgi/sso.pl',  [ "postData" => $data ] );
			if ($response === false) {
				$this->logger->notice('SSO response for cookie {cookie} was {response}.',
				[
					'cookie' => $cookie,
					'response' => $response,
				]);
				return null;
			}

			$this->logger->debug('SSO response for cookie {cookie} was {response}.',
			[
				'cookie' => $cookie,
				'response' => $response,
			]);
			$obj = json_decode($response);

			$user = User::newFromName( $obj->{'user_id'} );
			if ( $user === false ) {
				$this->logger->info('User::newFromName for {userId} returned false.',
				[
					'cookie' => $cookie,
					'response' => $response,
					'userId' => $obj->{'user_id'},
					'user' => $user,
				]);
				return null;
			}

			if ( $user->getID() == 0 ) {
				$this->logger->notice('User not found (id == 0), trying to create new user.',
				[
					'cookie' => $cookie,
					'response' => $response,
					'userId' => $obj->{'user_id'},
					'user' => $user,
				]);
				$am = AuthManager::singleton();
				$user->setRealName($obj->{'name'});
				$user->setEmail($obj->{'email'});
				AuthManager::singleton()->autoCreateUser($user, self::AUTOCREATE_SOURCE_SESSION, false);
			}

			$info['userInfo'] = UserInfo::newFromUser( $user, true );
			$info['provider'] = $this;
			return new SessionInfo( $this->priority, $info );
		} catch (HttpException $ex) {
			$this->logger->error(
				'Could not retrieve user information for session cookie.',
				[
					'cookie' => $cookie
				]
			);
		}
	}

	/**
	 * Validate a loaded SessionInfo and refresh provider metadata
	 *
	 * This is similar in purpose to the 'SessionCheckInfo' hook, and also
	 * allows for updating the provider metadata. On failure, the provider is
	 * expected to write an appropriate message to its logger.
	 *
	 * @protected For use by \MediaWiki\Session\SessionManager only
	 * @param SessionInfo $info Any changes by mergeMetadata() will already be reflected here.
	 * @param WebRequest $request
	 * @param array|null &$metadata Provider metadata, may be altered.
	 * @return bool Return false to reject the SessionInfo after all.
	 */
	public function refreshSessionInfo( SessionInfo $info, WebRequest $request, &$metadata ) {
		return true;
{'fraction_non_alphanumeric': 0.14195466369379414, 'fraction_numerical': 0.0012386968908708038, 'mean_word_length': 3.5979498861047836, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 2, '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': '419877', 'n_tokens_mistral': 2776, 'n_tokens_neox': 2375, 'n_words': 775}