Conditions | 63 |
Paths | > 20000 |
Total Lines | 304 |
Code Lines | 167 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
90 | private function inline_stuff($article, &$doc, $xpath) { |
||
91 | |||
92 | $entries = $xpath->query('(//a[@href]|//img[@src])'); |
||
93 | $img_entries = $xpath->query("(//img[@src])"); |
||
94 | |||
95 | $found = false; |
||
96 | //$debug = 1; |
||
97 | |||
98 | foreach ($entries as $entry) { |
||
99 | if ($entry->hasAttribute("href") && strpos($entry->getAttribute("href"), "reddit.com") === false) { |
||
100 | |||
101 | Debug::log("processing href: ".$entry->getAttribute("href"), Debug::$LOG_VERBOSE); |
||
102 | |||
103 | $matches = array(); |
||
104 | |||
105 | if (!$found && preg_match("/^https?:\/\/twitter.com\/(.*?)\/status\/(.*)/", $entry->getAttribute("href"), $matches)) { |
||
106 | Debug::log("handling as twitter: ".$matches[1]." ".$matches[2], Debug::$LOG_VERBOSE); |
||
107 | |||
108 | $oembed_result = fetch_file_contents("https://publish.twitter.com/oembed?url=".urlencode($entry->getAttribute("href"))); |
||
109 | |||
110 | if ($oembed_result) { |
||
111 | $oembed_result = json_decode($oembed_result, true); |
||
112 | |||
113 | if ($oembed_result && isset($oembed_result["html"])) { |
||
114 | |||
115 | $tmp = new DOMDocument(); |
||
116 | if ($tmp->loadHTML('<?xml encoding="utf-8" ?>'.$oembed_result["html"])) { |
||
117 | $p = $doc->createElement("p"); |
||
118 | |||
119 | $p->appendChild($doc->importNode( |
||
120 | $tmp->getElementsByTagName("blockquote")->item(0), true)); |
||
121 | |||
122 | $br = $doc->createElement('br'); |
||
123 | $entry->parentNode->insertBefore($p, $entry); |
||
124 | $entry->parentNode->insertBefore($br, $entry); |
||
125 | |||
126 | $found = 1; |
||
127 | } |
||
128 | } |
||
129 | } |
||
130 | } |
||
131 | |||
132 | if (!$found && preg_match("/\.gfycat.com\/([a-z]+)?(\.[a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
||
133 | $entry->setAttribute("href", "http://www.gfycat.com/".$matches[1]); |
||
134 | } |
||
135 | |||
136 | if (!$found && preg_match("/https?:\/\/(www\.)?gfycat.com\/([a-z]+)$/i", $entry->getAttribute("href"), $matches)) { |
||
137 | |||
138 | Debug::log("Handling as Gfycat", Debug::$LOG_VERBOSE); |
||
139 | |||
140 | $source_stream = 'https://giant.gfycat.com/'.$matches[2].'.mp4'; |
||
141 | $poster_url = 'https://thumbs.gfycat.com/'.$matches[2].'-mobile.jpg'; |
||
142 | |||
143 | $content_type = $this->get_content_type($source_stream); |
||
144 | |||
145 | if (strpos($content_type, "video/") !== false) { |
||
146 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
||
147 | $found = 1; |
||
148 | } |
||
149 | } |
||
150 | |||
151 | if (!$found && preg_match("/https?:\/\/v\.redd\.it\/(.*)$/i", $entry->getAttribute("href"), $matches)) { |
||
152 | |||
153 | Debug::log("Handling as reddit inline video", Debug::$LOG_VERBOSE); |
||
154 | |||
155 | $img = $img_entries->item(0); |
||
156 | |||
157 | if ($img) { |
||
158 | $poster_url = $img->getAttribute("src"); |
||
159 | } else { |
||
160 | $poster_url = false; |
||
161 | } |
||
162 | |||
163 | // Get original article URL from v.redd.it redirects |
||
164 | $source_article_url = $this->get_location($matches[0]); |
||
165 | Debug::log("Resolved ".$matches[0]." to ".$source_article_url, Debug::$LOG_VERBOSE); |
||
166 | |||
167 | $source_stream = false; |
||
168 | |||
169 | if ($source_article_url) { |
||
170 | $j = json_decode(fetch_file_contents($source_article_url.".json"), true); |
||
171 | |||
172 | if ($j) { |
||
173 | foreach ($j as $listing) { |
||
174 | foreach ($listing["data"]["children"] as $child) { |
||
175 | if ($child["data"]["url"] == $matches[0]) { |
||
176 | try { |
||
177 | $source_stream = $child["data"]["media"]["reddit_video"]["fallback_url"]; |
||
178 | } catch (Exception $e) { |
||
179 | } |
||
180 | break 2; |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | } |
||
185 | } |
||
186 | |||
187 | if (!$source_stream) { |
||
188 | $source_stream = "https://v.redd.it/".$matches[1]."/DASH_600_K"; |
||
189 | } |
||
190 | |||
191 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
||
192 | $found = 1; |
||
193 | } |
||
194 | |||
195 | if (!$found && preg_match("/https?:\/\/(www\.)?streamable.com\//i", $entry->getAttribute("href"))) { |
||
196 | |||
197 | Debug::log("Handling as Streamable", Debug::$LOG_VERBOSE); |
||
198 | |||
199 | $tmp = fetch_file_contents($entry->getAttribute("href")); |
||
200 | |||
201 | if ($tmp) { |
||
202 | $tmpdoc = new DOMDocument(); |
||
203 | |||
204 | if (@$tmpdoc->loadHTML($tmp)) { |
||
205 | $tmpxpath = new DOMXPath($tmpdoc); |
||
206 | |||
207 | $source_node = $tmpxpath->query("//video[contains(@class,'video-player-tag')]//source[contains(@src, '.mp4')]")->item(0); |
||
208 | $poster_node = $tmpxpath->query("//video[contains(@class,'video-player-tag') and @poster]")->item(0); |
||
209 | |||
210 | if ($source_node && $poster_node) { |
||
211 | $source_stream = $source_node->getAttribute("src"); |
||
212 | $poster_url = $poster_node->getAttribute("poster"); |
||
213 | |||
214 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
||
215 | $found = 1; |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | |||
221 | // imgur .gif -> .gifv |
||
222 | if (!$found && preg_match("/i\.imgur\.com\/(.*?)\.gif$/i", $entry->getAttribute("href"))) { |
||
223 | Debug::log("Handling as imgur gif (->gifv)", Debug::$LOG_VERBOSE); |
||
224 | |||
225 | $entry->setAttribute("href", |
||
226 | str_replace(".gif", ".gifv", $entry->getAttribute("href"))); |
||
227 | } |
||
228 | |||
229 | if (!$found && preg_match("/\.(gifv|mp4)$/i", $entry->getAttribute("href"))) { |
||
230 | Debug::log("Handling as imgur gifv", Debug::$LOG_VERBOSE); |
||
231 | |||
232 | $source_stream = str_replace(".gifv", ".mp4", $entry->getAttribute("href")); |
||
233 | |||
234 | if (strpos($source_stream, "imgur.com") !== false) { |
||
235 | $poster_url = str_replace(".mp4", "h.jpg", $source_stream); |
||
236 | } |
||
237 | |||
238 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
||
239 | |||
240 | $found = true; |
||
241 | } |
||
242 | |||
243 | $matches = array(); |
||
244 | if (!$found && preg_match("/youtube\.com\/v\/([\w-]+)/", $entry->getAttribute("href"), $matches) || |
||
245 | preg_match("/youtube\.com\/.*?[\&\?]v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
||
246 | preg_match("/youtube\.com\/watch\?v=([\w-]+)/", $entry->getAttribute("href"), $matches) || |
||
247 | preg_match("/\/\/youtu.be\/([\w-]+)/", $entry->getAttribute("href"), $matches)) { |
||
248 | |||
249 | $vid_id = $matches[1]; |
||
250 | |||
251 | Debug::log("Handling as youtube: $vid_id", Debug::$LOG_VERBOSE); |
||
252 | |||
253 | $iframe = $doc->createElement("iframe"); |
||
254 | $iframe->setAttribute("class", "youtube-player"); |
||
255 | $iframe->setAttribute("type", "text/html"); |
||
256 | $iframe->setAttribute("width", "640"); |
||
257 | $iframe->setAttribute("height", "385"); |
||
258 | $iframe->setAttribute("src", "https://www.youtube.com/embed/$vid_id"); |
||
259 | $iframe->setAttribute("allowfullscreen", "1"); |
||
260 | $iframe->setAttribute("frameborder", "0"); |
||
261 | |||
262 | $br = $doc->createElement('br'); |
||
263 | $entry->parentNode->insertBefore($iframe, $entry); |
||
264 | $entry->parentNode->insertBefore($br, $entry); |
||
265 | |||
266 | $found = true; |
||
267 | } |
||
268 | |||
269 | if (!$found && preg_match("/\.(jpg|jpeg|gif|png)(\?[0-9][0-9]*)?$/i", $entry->getAttribute("href")) || |
||
270 | mb_strpos($entry->getAttribute("href"), "i.reddituploads.com") !== false || |
||
271 | mb_strpos($this->get_content_type($entry->getAttribute("href")), "image/") !== false) { |
||
272 | |||
273 | Debug::log("Handling as a picture", Debug::$LOG_VERBOSE); |
||
274 | |||
275 | $img = $doc->createElement('img'); |
||
276 | $img->setAttribute("src", $entry->getAttribute("href")); |
||
277 | |||
278 | $br = $doc->createElement('br'); |
||
279 | $entry->parentNode->insertBefore($img, $entry); |
||
280 | $entry->parentNode->insertBefore($br, $entry); |
||
281 | |||
282 | $found = true; |
||
283 | } |
||
284 | |||
285 | // imgur via link rel="image_src" href="..." |
||
286 | if (!$found && preg_match("/imgur/", $entry->getAttribute("href"))) { |
||
287 | |||
288 | Debug::log("handling as imgur page/whatever", Debug::$LOG_VERBOSE); |
||
289 | |||
290 | $content = fetch_file_contents(["url" => $entry->getAttribute("href"), |
||
291 | "http_accept" => "text/*"]); |
||
292 | |||
293 | if ($content) { |
||
294 | $cdoc = new DOMDocument(); |
||
295 | |||
296 | if (@$cdoc->loadHTML($content)) { |
||
297 | $cxpath = new DOMXPath($cdoc); |
||
298 | |||
299 | $rel_image = $cxpath->query("//link[@rel='image_src']")->item(0); |
||
300 | |||
301 | if ($rel_image) { |
||
302 | |||
303 | $img = $doc->createElement('img'); |
||
304 | $img->setAttribute("src", $rel_image->getAttribute("href")); |
||
305 | |||
306 | $br = $doc->createElement('br'); |
||
307 | $entry->parentNode->insertBefore($img, $entry); |
||
308 | $entry->parentNode->insertBefore($br, $entry); |
||
309 | |||
310 | $found = true; |
||
311 | } |
||
312 | } |
||
313 | } |
||
314 | } |
||
315 | |||
316 | // wtf is this even |
||
317 | if (!$found && preg_match("/^https?:\/\/gyazo\.com\/([^\.\/]+$)/", $entry->getAttribute("href"), $matches)) { |
||
318 | $img_id = $matches[1]; |
||
319 | |||
320 | Debug::log("handling as gyazo: $img_id", Debug::$LOG_VERBOSE); |
||
321 | |||
322 | $img = $doc->createElement('img'); |
||
323 | $img->setAttribute("src", "https://i.gyazo.com/$img_id.jpg"); |
||
324 | |||
325 | $br = $doc->createElement('br'); |
||
326 | $entry->parentNode->insertBefore($img, $entry); |
||
327 | $entry->parentNode->insertBefore($br, $entry); |
||
328 | |||
329 | $found = true; |
||
330 | } |
||
331 | |||
332 | // let's try meta properties |
||
333 | if (!$found) { |
||
334 | Debug::log("looking for meta og:image", Debug::$LOG_VERBOSE); |
||
335 | |||
336 | $content = fetch_file_contents(["url" => $entry->getAttribute("href"), |
||
337 | "http_accept" => "text/*"]); |
||
338 | |||
339 | if ($content) { |
||
340 | $cdoc = new DOMDocument(); |
||
341 | |||
342 | if (@$cdoc->loadHTML($content)) { |
||
343 | $cxpath = new DOMXPath($cdoc); |
||
344 | |||
345 | $og_image = $cxpath->query("//meta[@property='og:image']")->item(0); |
||
346 | $og_video = $cxpath->query("//meta[@property='og:video']")->item(0); |
||
347 | |||
348 | if ($og_video) { |
||
349 | |||
350 | $source_stream = $og_video->getAttribute("content"); |
||
351 | |||
352 | if ($source_stream) { |
||
353 | |||
354 | if ($og_image) { |
||
355 | $poster_url = $og_image->getAttribute("content"); |
||
356 | } else { |
||
357 | $poster_url = false; |
||
358 | } |
||
359 | |||
360 | $this->handle_as_video($doc, $entry, $source_stream, $poster_url); |
||
361 | $found = true; |
||
362 | } |
||
363 | |||
364 | } else if ($og_image) { |
||
365 | |||
366 | $og_src = $og_image->getAttribute("content"); |
||
367 | |||
368 | if ($og_src) { |
||
369 | $img = $doc->createElement('img'); |
||
370 | $img->setAttribute("src", $og_src); |
||
371 | |||
372 | $br = $doc->createElement('br'); |
||
373 | $entry->parentNode->insertBefore($img, $entry); |
||
374 | $entry->parentNode->insertBefore($br, $entry); |
||
375 | |||
376 | $found = true; |
||
377 | } |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | } |
||
382 | |||
383 | } |
||
384 | |||
385 | // remove tiny thumbnails |
||
386 | if ($entry->hasAttribute("src")) { |
||
387 | if ($entry->parentNode && $entry->parentNode->parentNode) { |
||
388 | $entry->parentNode->parentNode->removeChild($entry->parentNode); |
||
389 | } |
||
390 | } |
||
391 | } |
||
392 | |||
393 | return $found; |
||
394 | } |
||
576 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.