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