summaryrefslogtreecommitdiff
path: root/plugins/af_comics/filters/af_comics_dilbert.php
blob: a1c59b94c352c024ce39f2ecdb03dc2e533b1c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

class Af_Comics_Dilbert extends Af_ComicFilter {

	function supported() {
		return array("Dilbert");
	}

	function process(&$article) {
		if (strpos($article["link"], "dilbert.com") !== false ||
			strpos($article["link"], "/DilbertDailyStrip") !== false) {

				$res = UrlHelper::fetch($article["link"], false, false, false,
					 false, false, 0,
					 "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0");

				if (!$res && UrlHelper::$fetch_last_error_content)
					$res = UrlHelper::$fetch_last_error_content;

				$doc = new DOMDocument();

				if ($res && $doc->loadHTML($res)) {
					$xpath = new DOMXPath($doc);

					/** @var DOMElement|null $basenode (image container) */
					$basenode = $xpath->query('(//div[@class="img-comic-container"]/a[@class="img-comic-link"])')->item(0);

					// Get the comic title
					$comic_title = $xpath->query('(//span[@class="comic-title-name"])')->item(0)->textContent;

					// Get tags from the article
					$matches = $xpath->query('(//p[contains(@class, "comic-tags")][1]//a)');
					$tags = array();

					foreach ($matches as $tag) {
						// Only strings starting with a number sign are considered tags
						if ( substr($tag->textContent, 0, 1) == '#' ) {
							$tags[] = mb_strtolower(substr($tag->textContent, 1), 'utf-8');
						}
					}

					// Get the current comics transcript and set it
					// as the title so it will be visible on mousover
					$transcript = $xpath->query('(//div[starts-with(@id, "js-toggle-transcript-")]//p)')->item(0);
					if ($transcript) {
						$basenode->setAttribute("title", $transcript->textContent);
					}

					if ($basenode) {
						$article["content"] = $doc->saveHTML($basenode);
					}

					// Add comic title to article type if not empty (mostly Sunday strips)
					if ($comic_title) {
						$article["title"] = $article["title"] . " - " . $comic_title;
					}

					if (!empty($tags)) {
						// Ignore existing tags and just replace them all
						$article["tags"] = array_unique($tags);
					}

				}

			return true;
		}

		return false;
	}
}
?>