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