Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
common.php
1<?php
2
7
8class Common extends Parser
9{
10 const MIN_IMAGE_HEIGHT = 100;
11 const MIN_IMAGE_WIDTH = 100;
12
14 protected $imgElements = array();
15
22 public function handle(HtmlDocument $document)
23 {
24 if($document->getTitle() == '')
25 {
26 $document->setTitle($this->getTitle($document));
27 }
28
29 if($document->getDescription() == '')
30 {
31 $document->setDescription($document->getMetaContent('description'));
32 }
33
34 $this->imgElements = $document->extractElementAttributes('img');
35 if($document->getImage() == '')
36 {
37 $image = $this->getImage($document);
38 if($image <> '')
39 {
40 $document->setImage($image);
41 }
42 else
43 {
44 $imageCandidates = $this->getImageCandidates();
45 if(count($imageCandidates) === 1)
46 {
47 $document->setImage($imageCandidates[0]);
48 }
49 else if(count($imageCandidates) > 1)
50 {
51 $document->setExtraField('IMAGES', $imageCandidates);
52 }
53 }
54 }
55 if($document->getExtraField('VIDEO') == '')
56 {
57 preg_match_all("/<video.+?<\/video>/mis", $document->getHtml(), $videoTags);
58 foreach($videoTags[0] as $videoTag)
59 {
60 $videoInfo = $this->getVideoInfo($videoTag);
61 if(!empty($videoInfo))
62 {
63 $document->setExtraField('VIDEO', $videoInfo['src']);
64 $document->setExtraField('VIDEO_TYPE', $videoInfo['type']);
65 $document->setExtraField('VIDEO_WIDTH', $videoInfo['width']);
66 $document->setExtraField('VIDEO_HEIGHT', $videoInfo['height']);
67 }
68 }
69 }
70 }
71
76 protected function getTitle(HtmlDocument $document)
77 {
78 $title = $document->getMetaContent('title');
79 if($title <> '')
80 {
81 return $title;
82 }
83
84 preg_match('/<title>(.+?)<\/title>/mis', $document->getHtml(), $matches);
85 return ($matches[1] ?? null);
86 }
87
92 protected function getImage(HtmlDocument $document)
93 {
94 $result = $document->getLinkHref('image_src');
95 if($result <> '')
96 {
97 return $result;
98 }
99
100 foreach($this->imgElements as $imgElement)
101 {
102 if(isset($imgElement['rel']) && $imgElement['rel'] == 'image_src')
103 {
104 $result = $imgElement['src'];
105 return $result;
106 }
107 }
108
109 return null;
110 }
111
116 protected function getImageCandidates()
117 {
118 $result = array();
119 foreach ($this->imgElements as $imgElement)
120 {
121 $imageDimensions = $this->getImageDimensions($imgElement);
122 if($imageDimensions['width'] >= self::MIN_IMAGE_WIDTH && $imageDimensions['height'] >= self::MIN_IMAGE_HEIGHT)
123 {
124 $result[] = $imgElement['src'];
125 }
126 }
127 return $result;
128 }
129
135 protected function getImageDimensions(array $imageAttributes)
136 {
137 $result = array(
138 'width' => null,
139 'height' => null
140 );
141
142 foreach(array_keys($result) as $imageDimension)
143 {
144 if(isset($imageAttributes[$imageDimension]))
145 {
146 $result[$imageDimension] = $imageAttributes[$imageDimension];
147 }
148 else if(isset($imageAttributes['style']) && preg_match('/'.$imageDimension.':\s*(\d+?)px/', $imageAttributes['style'], $matches))
149 {
150 $result[$imageDimension] = $matches[1];
151 }
152 }
153 return $result;
154 }
155
162 protected function getVideoInfo($html = '')
163 {
164 $maxWeight = -1;
165 $result = array();
166 $uri = new Uri('/');
167 $document = new HtmlDocument($html, $uri);
168 $videoElements = $document->extractElementAttributes('video');
169 foreach ($videoElements as $videoElement)
170 {
171 if (!isset($videoElement['src']))
172 {
173 $sourceElements = $document->extractElementAttributes('source');
174 foreach ($sourceElements as $sourceElement)
175 {
176 if (
177 isset($sourceElement['type'])
178 && $this->isValidVideoMimeType($sourceElement['type'])
179 )
180 {
181 $videoElement['src'] = $sourceElement['src'];
182 $videoElement['type'] = $sourceElement['type'];
183 break;
184 }
185 }
186 }
187 if (isset($videoElement['src']))
188 {
189 if (
190 (isset($videoElement['width']) &&
191 isset($videoElement['height']) &&
192 (int)$videoElement['width'] * (int)$videoElement['height'] > $maxWeight) ||
193 (empty($result))
194 )
195 {
196 $result = $videoElement;
197 $maxWeight = 0;
198 if(isset($videoElement['width']) && isset($videoElement['height']))
199 {
200 $maxWeight = (int)$videoElement['width'] * (int)$videoElement['height'];
201 }
202 }
203 }
204 }
205
206 return $result;
207 }
208
215 protected function isValidVideoMimeType($type = '')
216 {
217 if(empty($type))
218 {
219 return false;
220 }
221
222 static $validTypes = array(
223 'video/mp4', 'video/x-flv', 'video/webm', 'video/ogg', 'video/quicktime'
224 );
225
226 return in_array($type, $validTypes);
227 }
228}
setExtraField($fieldName, $fieldValue)
getImage(HtmlDocument $document)
Definition common.php:92
handle(HtmlDocument $document)
Definition common.php:22
getTitle(HtmlDocument $document)
Definition common.php:76
getImageDimensions(array $imageAttributes)
Definition common.php:135