extract from pingbackcontent that has no type yet
[stapibas.git] / src / stapibas / Content / Extractor.php
1 <?php
2 namespace stapibas;
3
4 class Content_Extractor
5 {
6     public $db;
7     public $log;
8
9     public function __construct(Dependencies $deps)
10     {
11         $this->deps = $deps;
12         $this->db   = $deps->db;
13         $this->log  = $deps->log;
14     }
15
16     /**
17      * Extracts content from all pingbackcontent entries and puts it
18      * into rbookmarks/rcomments/rlinks.
19      */
20     public function updateAll()
21     {
22         $this->log->info('Extracting pingback content..');
23         $res = $this->db->query(
24             'SELECT * FROM pingbackcontent, pingbacks'
25             . ' WHERE p_id = pc_p_id' . $this->sqlNeedsUpdate()
26         );
27         $items = 0;
28         while ($contentRow = $res->fetch(\PDO::FETCH_OBJ)) {
29             ++$items;
30             $this->extractContent($contentRow);
31         }
32         $this->log->info('Finished extracting %d pingback contents.', $items);
33     }
34
35     protected function extractContent($contentRow)
36     {
37         $doc = new \DOMDocument();
38         $typeParts = explode(';', $contentRow->pc_mime_type);
39         $type = $typeParts[0];
40         if ($type == 'application/xhtml+xml'
41             || $type == 'application/xml'
42             || $type == 'text/xml'
43         ) {
44             $doc->loadXML($contentRow->pc_fulltext);
45         } else { 
46             $doc->loadHTML($contentRow->pc_fulltext);
47         }
48
49         //delete old content
50         $this->db->exec(
51             'DELETE FROM rbookmarks WHERE'
52             . ' rb_pc_id = ' . $this->db->quote($contentRow->pc_id)
53         );
54         $this->db->exec(
55             'DELETE FROM rcomments WHERE'
56             . ' rc_pc_id = ' . $this->db->quote($contentRow->pc_id)
57         );
58         $this->db->exec(
59             'DELETE FROM rlinks WHERE'
60             . ' rl_pc_id = ' . $this->db->quote($contentRow->pc_id)
61         );
62
63         $ce = new Content_Extractor_Comment($this->deps->log);
64         $data = $ce->extract($doc, $contentRow->p_source, $contentRow->p_target);
65         if ($data !== null) {
66             $this->log->info('Comment found');
67             var_dump($data);
68             $this->db->exec(
69                 'INSERT INTO rcomments SET'
70                 . '  rc_p_id = ' . $this->db->quote($contentRow->p_id)
71                 . ', rc_pc_id = ' . $this->db->quote($contentRow->pc_id)
72                 . ', rc_source = ' . $this->db->quote($contentRow->p_source)
73                 . ', rc_target = ' . $this->db->quote($contentRow->p_target)
74                 . ', rc_title = ' . $this->db->quote($data['title'])
75                 . ', rc_author_name = ' . $this->db->quote($data['author_name'])
76                 . ', rc_author_url = ' . $this->db->quote($data['author_url'])
77                 . ', rc_author_image = ' . $this->db->quote($data['author_image'])
78                 . ', rc_content = ' . $this->db->quote($data['content'])
79                 . ', rc_updated = NOW()'
80             );
81             $this->setDetectedType($contentRow, 'comment');
82             return;
83         }
84
85         //FIXME: bookmark
86
87         $ce = new Content_Extractor_Link($this->deps->log);
88         $data = $ce->extract($doc, $contentRow->p_source, $contentRow->p_target);
89         if ($data !== null) {
90             $this->log->info('Link found');
91             $this->db->exec(
92                 'INSERT INTO rlinks SET'
93                 . '  rl_p_id = ' . $this->db->quote($contentRow->p_id)
94                 . ', rl_pc_id = ' . $this->db->quote($contentRow->pc_id)
95                 . ', rl_source = ' . $this->db->quote($contentRow->p_source)
96                 . ', rl_target = ' . $this->db->quote($contentRow->p_target)
97                 . ', rl_title = ' . $this->db->quote($data['title'])
98                 . ', rl_author_name = ' . $this->db->quote($data['author_name'])
99                 . ', rl_author_url = ' . $this->db->quote($data['author_url'])
100                 . ', rl_author_image = ' . $this->db->quote($data['author_image'])
101                 . ', rl_updated = NOW()'
102             );
103             $this->setDetectedType($contentRow, 'link');
104             return;
105         }
106
107         $this->setDetectedType($contentRow, 'nothing');
108         $this->log->info('Nothing found');
109     }
110
111     protected function setDetectedType($contentRow, $type)
112     {
113         $this->db->exec(
114             'UPDATE pingbackcontent'
115             . ' SET pc_detected_type = ' . $this->db->quote($type)
116             . ' WHERE pc_id = ' . $this->db->quote($contentRow->pc_id)
117         );
118     }
119
120
121     protected function sqlNeedsUpdate()
122     {
123         if ($this->deps->options['force']) {
124             return '';
125         }
126         return ' AND pc_detected_type = ""';
127     }
128
129 }
130 ?>