-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsdlExtractor.class.php
189 lines (150 loc) · 3.65 KB
/
CsdlExtractor.class.php
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* User: Guillem LLuch
*/
namespace glluchcom\csdlExtractor;
require 'Validate.class.php';
/**
* Class Extractor Extracts information from a CSDL searches.
* To download the webpages, use another program as
* [jsCSDLscraping](https://github.com/guillem72/jsCSDLscraping), for example
* @package glluchcom\csdlScraping
*/
class Extractor extends \DOMDocument
{
/**
* @var string Directory where the html files are saved.
*/
protected $sourceDir = "./html/";
/**
* @var string Xpath expression to get the information need. In this case
* is the titles of the articles.
*/
protected $pattern = "//div[@class]/h5/../../span/text()";
/**
* @var string saves the term currently searched. The term is the basename
* of the file. For "array.html" it is only array.
*/
protected $query;
/**
* @var \Logger The logger for debug, info , warm, etc messages
*/
protected $titles = array();
/**
* @var string The name of the file where the extracting will act.
*/
protected $filename;
/**
* Scraper constructor.
* @param $q The basename of the file, which it is the term searched.
*/
public function __construct($q)
{
$this->query = $q;
$this->filename = $this->query . ".html";
}
//https://www.computer.org/web/search?cs_search_action=advancedsearch&search-options=dl&searchOperation=exact&searchText=swot
/**
* Convert the html to a well-formed xml and extract the titles.
* @return array The titles of the articles found in the html.
*/
public function getTitles()
{
Validate::file2Xml($this->sourceDir . $this->filename);
//echo $this->sourceDir . $this->filename.PHP_EOL;
$this->load($this->sourceDir . $this->filename);
$this->extract();
return $this->titles;
}
/**
* Extract the titles using xpath.
*
*/
protected function extract()
{
$xpath = new \DOMXPath($this);
//var_dump($xpath);
$items = $xpath->query($this->pattern);
if (!$items) {
echo("The search for '" . $this->query . "' doesn't
produce any result");
return false;
} else {
//var_dump($items);
foreach ($items as $item) {
$this->titles[] = trim($item->nodeValue);
}
}
}
//Getter and setters
/**
* @return string
*/
public function getFilename()
{
return $this->filename;
}
/**
* @param string $filename
*/
public function setFilename($filename)
{
$this->filename = $filename;
}
/**
* @return mixed
*/
public function getUrlBase()
{
return $this->urlBase;
}
/**
* @param mixed $urlBase
*/
public function setUrlBase($urlBase)
{
$this->urlBase = $urlBase;
}
/**
* @return mixed
*/
public function getSourceDir()
{
return $this->sourceDir;
}
/**
* @param mixed $sourceDir
*/
public function setSourceDir($sourceDir)
{
$this->sourceDir = $sourceDir;
}
/**
* @return string
*/
public function getPattern()
{
return $this->pattern;
}
/**
* @param string $pattern
*/
public function setPattern($pattern)
{
$this->pattern = $pattern;
}
/**
* @return mixed
*/
public function getQuery()
{
return $this->query;
}
/**
* @param mixed $query
*/
public function setQuery($query)
{
$this->query = $query;
}
}