| author | cavaliet |
| Fri, 24 Jan 2014 17:43:38 +0100 | |
| changeset 112 | 14653baf4f6b |
| parent 77 | 021131fbe2c5 |
| child 115 | 085ea4dbfeee |
| permissions | -rwxr-xr-x |
| 2 | 1 |
<?php |
|
74
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
2 |
/* |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
3 |
* This file is part of the WikiTagBundle package. |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
4 |
* |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
5 |
* (c) IRI <http://www.iri.centrepompidou.fr/> |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
6 |
* |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
7 |
* For the full copyright and license information, please view the LICENSE |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
8 |
* file that was distributed with this source code. |
|
901463f9b11c
add headers for public repository release
ymh <ymh.work@gmail.com>
parents:
68
diff
changeset
|
9 |
*/ |
| 2 | 10 |
|
11 |
namespace IRI\Bundle\WikiTagBundle\Utils; |
|
12 |
||
13 |
use IRI\Bundle\WikiTagBundle\Entity\Tag; |
|
14 |
||
15 |
class WikiTagUtils |
|
|
112
14653baf4f6b
first change for wikipedia and dbpedia lang configuration
cavaliet
parents:
77
diff
changeset
|
16 |
{ |
| 2 | 17 |
/** |
18 |
* Cleans the tag label |
|
19 |
*/ |
|
| 8 | 20 |
public static function normalizeTag($tag_label) |
| 2 | 21 |
{ |
22 |
if(strlen($tag_label)==0){ |
|
23 |
return $tag_label; |
|
24 |
} |
|
25 |
$tag_label = trim($tag_label);//tag.strip() |
|
26 |
$tag_label = str_replace("_", " ", $tag_label);//tag.replace("_", " ") |
|
| 43 | 27 |
$tag_label = preg_replace('/\s+/u', ' ', $tag_label);//" ".join(tag.split()) |
| 2 | 28 |
$tag_label = ucfirst($tag_label);//tag[0].upper() + tag[1:] |
29 |
return $tag_label; |
|
30 |
} |
|
31 |
||
32 |
/** |
|
| 43 | 33 |
* Query wikipedia with a normalized label or a pageid |
34 |
* return an array with the form |
|
35 |
* array( |
|
36 |
* 'new_label'=>$new_label, |
|
37 |
* 'alternative_label'=>$alternative_label, |
|
38 |
* 'status'=>$status, |
|
39 |
* 'wikipedia_url'=>$url, |
|
40 |
* 'wikipedia_alternative_url'=>$alternative_url, |
|
41 |
* 'pageid'=>$pageid, |
|
42 |
* 'alternative_pageid'=>$alternative_pageid, |
|
43 |
* 'dbpedia_uri'=>$dbpedia_uri, |
|
44 |
* 'revision_id'=> , |
|
45 |
* 'response'=> the original wikipedia json response |
|
46 |
* ) |
|
| 2 | 47 |
* |
| 43 | 48 |
* @param string $tag_label_normalized |
49 |
* @param bigint $page_id |
|
50 |
* @return array |
|
| 2 | 51 |
*/ |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
52 |
public static function getWikipediaInfo($tag_label_normalized, $page_id=null, $ignore_wikipedia_error=false, $logger = null) |
| 2 | 53 |
{ |
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
54 |
|
| 2 | 55 |
$params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json'); |
56 |
if($tag_label_normalized!=null){ |
|
57 |
$params['titles'] = urlencode($tag_label_normalized); |
|
58 |
} |
|
59 |
else if($page_id!=null){ |
|
60 |
$params['pageids'] = $page_id; |
|
61 |
} |
|
62 |
else{ |
|
63 |
return WikiTagUtils::returnNullResult(null); |
|
64 |
} |
|
65 |
||
| 63 | 66 |
try { |
67 |
$ar = WikiTagUtils::requestWikipedia($params); |
|
68 |
} |
|
69 |
catch(\Exception $e) { |
|
70 |
if($ignore_wikipedia_error) { |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
71 |
if(!is_null($logger)) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
72 |
$logger->err("Error when querying wikipedia : ".$e->getMessage()." with trace : ".$e->getTraceAsString()); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
73 |
} |
| 63 | 74 |
return WikiTagUtils::returnNullResult(null); |
75 |
} |
|
76 |
else { |
|
77 |
throw $e; |
|
78 |
} |
|
79 |
} |
|
| 60 | 80 |
|
| 2 | 81 |
$res = $ar[0]; |
82 |
$original_response = $res; |
|
83 |
$pages = $ar[1]; |
|
84 |
// If there 0 or more than 1 result, the query has failed |
|
85 |
if(count($pages)>1 || count($pages)==0){ |
|
86 |
return WikiTagUtils::returnNullResult($res); |
|
87 |
} |
|
88 |
// get first result |
|
89 |
$page = reset($pages); |
|
90 |
// Unknow entry ? |
|
91 |
if(array_key_exists('missing', $page) || array_key_exists('invalid', $page)){ |
|
92 |
return WikiTagUtils::returnNullResult($res); |
|
93 |
} |
|
94 |
// The entry exists, we get the datas. |
|
95 |
$url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null; |
|
96 |
$pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null; |
|
97 |
$new_label = array_key_exists('title', $page) ? $page['title'] : null; |
|
98 |
// We test the status (redirect first because a redirect has no categories key) |
|
99 |
if(array_key_exists('redirect', $page)){ |
|
100 |
//return " REDIRECT"; |
|
101 |
$status = Tag::$TAG_URL_STATUS_DICT["redirection"]; |
|
102 |
} |
|
103 |
else if(WikiTagUtils::isHomonymy($page)){ |
|
104 |
//return " HOMONYMY"; |
|
105 |
$status = Tag::$TAG_URL_STATUS_DICT["homonyme"]; |
|
106 |
} |
|
107 |
else{ |
|
108 |
//return " MATCH"; |
|
109 |
$status = Tag::$TAG_URL_STATUS_DICT["match"]; |
|
110 |
} |
|
111 |
// In redirection, we have to get more datas by adding redirects=true to the params |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
112 |
$alternative_label = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
113 |
$alternative_url = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
114 |
$alternative_pageid = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
115 |
if($status==Tag::$TAG_URL_STATUS_DICT["redirection"]) |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
116 |
{ |
| 2 | 117 |
$params['redirects'] = "true"; |
| 63 | 118 |
try { |
119 |
$ar = WikiTagUtils::requestWikipedia($params); |
|
120 |
} |
|
121 |
catch(\Exception $e) { |
|
122 |
if($ignore_wikipedia_error) { |
|
|
68
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
123 |
if(!is_null($logger)) { |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
124 |
$logger->error("Error when querying wikipedia for redirection : ".$e->getMessage()." with trace : ".$e->getTraceAsString()); |
|
e7384fb35f7a
improve search test and documentation
ymh <ymh.work@gmail.com>
parents:
67
diff
changeset
|
125 |
} |
| 63 | 126 |
return WikiTagUtils::returnNullResult(null); |
127 |
} |
|
128 |
else { |
|
129 |
throw $e; |
|
130 |
} |
|
131 |
} |
|
132 |
||
| 2 | 133 |
$res = $ar[0]; |
134 |
$pages = $ar[1]; |
|
135 |
#we know that we have at least one answer |
|
136 |
if(count($pages)>1 || count($pages)==0){ |
|
137 |
return WikiTagUtils::returnNullResult($res); |
|
138 |
} |
|
139 |
// get first result |
|
140 |
$page = reset($pages); |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
141 |
$alternative_label = array_key_exists('title', $page) ? $page['title'] : null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
142 |
$alternative_url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
143 |
$alternative_pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null; |
| 2 | 144 |
} |
145 |
||
146 |
$revision_id = $page['lastrevid']; |
|
147 |
||
148 |
// process language to extract the english label |
|
149 |
$english_label = null; |
|
150 |
if($status==Tag::$TAG_URL_STATUS_DICT["match"] || $status==Tag::$TAG_URL_STATUS_DICT["redirection"]){ |
|
151 |
if(array_key_exists("langlinks", $page)){ |
|
152 |
foreach ($page["langlinks"] as $ar) { |
|
153 |
if($ar["lang"]=="en"){ |
|
154 |
$english_label = $ar["*"]; |
|
155 |
break; |
|
156 |
} |
|
157 |
} |
|
158 |
} |
|
159 |
} |
|
160 |
// We create the dbpedia uri. |
|
161 |
$dbpedia_uri = null; |
|
162 |
if($english_label!=null && strpos($english_label, '#')===false){ |
|
163 |
$dbpedia_uri = WikiTagUtils::getDbpediaUri($english_label); |
|
164 |
} |
|
165 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
166 |
$wp_response = array( |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
167 |
'new_label'=>$new_label, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
168 |
'alternative_label'=>$alternative_label, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
169 |
'status'=>$status, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
170 |
'wikipedia_url'=>$url, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
171 |
'wikipedia_alternative_url'=>$alternative_url, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
172 |
'pageid'=>$pageid, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
173 |
'alternative_pageid'=>$alternative_pageid, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
174 |
'dbpedia_uri'=>$dbpedia_uri, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
175 |
'revision_id'=>$revision_id, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
176 |
'response'=>$original_response); |
| 63 | 177 |
|
| 2 | 178 |
return $wp_response; |
179 |
} |
|
180 |
||
181 |
||
182 |
/** |
|
| 43 | 183 |
* build and do the request to Wikipedia. |
| 2 | 184 |
* |
| 43 | 185 |
* @param array $params |
186 |
* @return array |
|
| 2 | 187 |
*/ |
188 |
private static function requestWikipedia($params) |
|
189 |
{ |
|
190 |
$params_str = ''; |
|
191 |
foreach ($params as $key => $value) { |
|
192 |
if ($params_str==''){ |
|
193 |
$params_str = $key.'='.$value; |
|
194 |
} |
|
195 |
else{ |
|
196 |
$params_str .= '&'.$key.'='.$value; |
|
197 |
} |
|
198 |
} |
|
199 |
||
|
112
14653baf4f6b
first change for wikipedia and dbpedia lang configuration
cavaliet
parents:
77
diff
changeset
|
200 |
//$url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str; |
|
14653baf4f6b
first change for wikipedia and dbpedia lang configuration
cavaliet
parents:
77
diff
changeset
|
201 |
//throw new \Exception($GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates"), 1, null); |
|
14653baf4f6b
first change for wikipedia and dbpedia lang configuration
cavaliet
parents:
77
diff
changeset
|
202 |
$url = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["wikipedia_api"].'?'.$params_str; |
| 2 | 203 |
|
204 |
$ch = curl_init(); |
|
205 |
curl_setopt($ch, CURLOPT_URL, $url); |
|
206 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
|
76
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
207 |
// default values |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
208 |
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.1) Gecko/20100101 Firefox/10.0.1'); |
|
50
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
43
diff
changeset
|
209 |
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); |
|
76
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
210 |
// Set options if they are set in the config.yml file, typically for proxy configuration. |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
211 |
// Thanks to the configuration file, it will execute commands like "curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);" or "curl_setopt($ch, CURLOPT_PROXY, "xxx.yyy.zzz:PORT");" |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
212 |
$curl_options = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.curl_options"); |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
213 |
foreach ($curl_options as $key => $value) { |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
214 |
if(strtoupper($value)=='TRUE'){ |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
215 |
$value = TRUE; |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
216 |
} |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
217 |
else if (strtoupper($value)=='FALSE'){ |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
218 |
$value = FALSE; |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
219 |
} |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
220 |
else if (defined($value)){ |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
221 |
$value = constant($value); |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
222 |
} |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
223 |
curl_setopt($ch, constant($key), $value); |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
224 |
} |
|
bb7808e180c3
Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents:
68
diff
changeset
|
225 |
// end of treatment |
| 2 | 226 |
$res = curl_exec($ch); |
|
50
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
43
diff
changeset
|
227 |
$curl_errno = curl_errno($ch); |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
43
diff
changeset
|
228 |
$curl_error = curl_error($ch); |
| 2 | 229 |
curl_close($ch); |
230 |
||
|
50
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
43
diff
changeset
|
231 |
if ($curl_errno > 0) { |
| 63 | 232 |
throw new \Exception("Wikipedia request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null); |
|
50
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
43
diff
changeset
|
233 |
} |
|
e967654e90cb
First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents:
43
diff
changeset
|
234 |
|
| 2 | 235 |
$val = json_decode($res, true); |
236 |
$pages = $val["query"]["pages"]; |
|
237 |
return array($res, $pages); |
|
238 |
} |
|
239 |
||
240 |
/** |
|
241 |
* Returns tag with a null result, usually used after a failed request on Wikipedia |
|
242 |
*/ |
|
243 |
private static function returnNullResult($response) |
|
244 |
{ |
|
245 |
return array('new_label'=>null, 'status'=>Tag::$TAG_URL_STATUS_DICT['null_result'], 'wikipedia_url'=>null, 'pageid'=>null, 'dbpedia_uri'=>null, 'revision_id'=>null, 'response'=>$response); |
|
246 |
} |
|
247 |
||
248 |
/** |
|
249 |
* Returns tag with a null result, usually used after a failed request on Wikipedia |
|
250 |
*/ |
|
251 |
private static function isHomonymy($page) |
|
252 |
{ |
|
253 |
//$s = ""; |
|
254 |
foreach ($page["categories"] as $ar) { |
|
255 |
//$s .= ", b : ".$ar." - title = ".$ar["title"].", strpos = ".strpos($ar["title"], 'Catégorie:Homonymie'); |
|
256 |
// Strict test because false can be seen as "O". |
|
257 |
if(strpos($ar["title"], 'Catégorie:Homonymie')!==false || strpos($ar["title"], 'Category:Disambiguation')!==false){ |
|
258 |
//$s .= "TRUE"; |
|
259 |
return true; |
|
260 |
} |
|
261 |
} |
|
262 |
return false; |
|
263 |
} |
|
264 |
||
265 |
/** |
|
266 |
* Builds DbPedia URI |
|
267 |
*/ |
|
268 |
private static function getDbpediaUri($english_label) |
|
269 |
{ |
|
|
112
14653baf4f6b
first change for wikipedia and dbpedia lang configuration
cavaliet
parents:
77
diff
changeset
|
270 |
return sprintf($GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.url_templates")["dbpedia"], WikiTagUtils::urlize_for_wikipedia($english_label)); |
| 2 | 271 |
} |
272 |
||
273 |
/** |
|
274 |
* URLencode label for wikipedia |
|
275 |
*/ |
|
276 |
private static function urlize_for_wikipedia($label){ |
|
277 |
return urlencode(str_replace(" ", "_", $label)); |
|
278 |
} |
|
279 |
} |