<commit_before><?php

/**
 *	@author <NAME> <<EMAIL>>
 *	@author Laughingwithu <<EMAIL>>
 *	@license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause)
 */

namespace Essence\Provider;

use Essence\Exception;
use Essence\Media;
use Essence\Provider;
use Essence\Dom\Parser as DomParser;
use Essence\Http\Client as HttpClient;
use Essence\Utility\Hash;



/**
 *	Base class for an OpenGraph provider.
 *	This kind of provider extracts embed informations from OpenGraph meta tags.
 *
 *	@package fg.Essence.Provider
 */

class OpenGraph extends Provider {

	/**
	 *	Internal HTTP client.
	 *
	 *	@var Essence\Http\Client
	 */

	protected $_Http = null;



	/**
	 *	Internal DOM parser.
	 *
	 *	@var Essence\Dom\Parser
	 */

	protected $_Dom = null;



	/**
	 *	Constructor.
	 *
	 *	@param Essence\Http\Client $Http Http client.
	 *	@param Essence\Dom\Parser $Cache Dom parser.
	 */

	public function __construct( HttpClient $Http, DomParser $Dom ) {

		$this->_Http = $Http;
		$this->_Dom = $Dom;
	}



	/**
	 *	{@inheritDoc}
	 */

	protected function _embed( $url, $options ) {

		$og = $this->_extractInformations( $url );

		if ( empty( $og )) {
			throw new Exception(
				'Unable to extract OpenGraph data.'
			);
		}

		return new Media(
			Hash::reindex(
				$og,
				array(
					'og:type' => 'type',
					'og:title' => 'title',
					'og:description' => 'description',
					'og:site_name' => 'providerName',
					'og:image' => 'thumbnailUrl',
					'og:image:url' => 'thumbnailUrl',
					'og:image:width' => 'width',
					'og:image:height' => 'height',
					'og:video:width' => 'width',
					'og:video:height' => 'height',
					'og:url' => 'url'
				)
			)
		);
	}



	/**
	 *	Extracts OpenGraph informations from the given URL.
	 *
	 *	@param string $url URL to fetch informations from.
	 *	@return array Extracted informations.
	 */

	protected function _extractInformations( $url ) {

		$attributes = $this->_Dom->extractAttributes(
			$this->_Http->get( $url ),
			array(
				'meta' => array(
					'property' => '#^og:.+#i',
					'content'
				)
			)
		);

		$og = array( );

		foreach ( $attributes['meta'] as $meta ) {
			if ( !isset( $og[ $meta['property']])) {
				$og[ $meta['property']] = trim( $meta['content']);
			}
		}

		if ( empty( $og['html'])) {
			if ( empty( $og['og:url'])) {
				$og['og:url'] = $url;
			}

			$og['html'] = $this->_buildHtml( $og );
		}

		return $og;
	}



	/**
	 *	Builds an HTML code from OpenGraph properties.
	 *
	 *	@param array $og OpenGraph properties.
	 *	@return string Generated HTML.
	 */
<commit_msg>Added a test to avoid a possible bug<commit_after><?php

/**
 *	@author <NAME> <<EMAIL>>
 *	@author Laughingwithu <<EMAIL>>
 *	@license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause)
 */

namespace Essence\Provider;

use Essence\Exception;
use Essence\Media;
use Essence\Provider;
use Essence\Dom\Parser as DomParser;
use Essence\Http\Client as HttpClient;
use Essence\Utility\Hash;



/**
 *	Base class for an OpenGraph provider.
 *	This kind of provider extracts embed informations from OpenGraph meta tags.
 *
 *	@package fg.Essence.Provider
 */

class OpenGraph extends Provider {

	/**
	 *	Internal HTTP client.
	 *
	 *	@var Essence\Http\Client
	 */

	protected $_Http = null;



	/**
	 *	Internal DOM parser.
	 *
	 *	@var Essence\Dom\Parser
	 */

	protected $_Dom = null;



	/**
	 *	Constructor.
	 *
	 *	@param Essence\Http\Client $Http Http client.
	 *	@param Essence\Dom\Parser $Cache Dom parser.
	 */

	public function __construct( HttpClient $Http, DomParser $Dom ) {

		$this->_Http = $Http;
		$this->_Dom = $Dom;
	}



	/**
	 *	{@inheritDoc}
	 */

	protected function _embed( $url, $options ) {

		$og = $this->_extractInformations( $url );

		if ( empty( $og )) {
			throw new Exception(
				'Unable to extract OpenGraph data.'
			);
		}

		return new Media(
			Hash::reindex(
				$og,
				array(
					'og:type' => 'type',
					'og:title' => 'title',
					'og:description' => 'description',
					'og:site_name' => 'providerName',
					'og:image' => 'thumbnailUrl',
					'og:image:url' => 'thumbnailUrl',
					'og:image:width' => 'width',
					'og:image:height' => 'height',
					'og:video:width' => 'width',
					'og:video:height' => 'height',
					'og:url' => 'url'
				)
			)
		);
	}



	/**
	 *	Extracts OpenGraph informations from the given URL.
	 *
	 *	@param string $url URL to fetch informations from.
	 *	@return array Extracted informations.
	 */

	protected function _extractInformations( $url ) {

		$attributes = $this->_Dom->extractAttributes(
			$this->_Http->get( $url ),
			array(
				'meta' => array(
					'property' => '#^og:.+#i',
					'content'
				)
			)
		);

		$og = array( );

		if ( !empty( $attributes['meta'])) {
			foreach ( $attributes['meta'] as $meta ) {
				if ( !isset( $og[ $meta['property']])) {
					$og[ $meta['property']] = trim( $meta['content']);
				}
			}

			if ( empty( $og['html'])) {
				if ( empty( $og['og:url'])) {
					$og['og:url'] = $url;
				}

				$og['html'] = $this->_buildHtml( $og );
			}
		}

		return $og;
	}



	/**
	 *	Builds an HTML code from OpenGraph properties.
	 *
	 *	@param array $og OpenGraph properties.
	 *	@return string Generated HTML.
	 */

{'fraction_non_alphanumeric': 0.17463058913836116, 'fraction_numerical': 0.00038380349261178274, 'mean_word_length': 2.8808637379002233, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 55, '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': '5430561', 'n_tokens_mistral': 2165, 'n_tokens_neox': 1791, 'n_words': 438}