| author | cavaliet |
| Thu, 10 Nov 2011 12:40:22 +0100 | |
| changeset 32 | 38dcd2db04e4 |
| parent 22 | 99c15cfe420b |
| child 42 | 0e57c730bb18 |
| permissions | -rwxr-xr-x |
| 2 | 1 |
<?php |
2 |
||
3 |
namespace IRI\Bundle\WikiTagBundle\Utils; |
|
4 |
||
5 |
use IRI\Bundle\WikiTagBundle\Entity\Tag; |
|
6 |
||
7 |
class WikiTagUtils |
|
8 |
{ |
|
9 |
// Constants |
|
10 |
private static $WIKIPEDIA_API_URL = "http://fr.wikipedia.org/w/api.php"; |
|
11 |
private static $WIKIPEDIA_VERSION_PERMALINK_TEMPLATE = "http://fr.wikipedia.org/w/index.php?oldid=%s"; |
|
12 |
private static $DBPEDIA_URI_TEMPLATE = "http://dbpedia.org/resource/%s"; |
|
13 |
||
14 |
||
15 |
/** |
|
16 |
* Get or create tag. Returns an array(tag:WikiTagTag, revision_id=int, created:Boolean) |
|
17 |
*/ |
|
18 |
||
19 |
/** |
|
20 |
* |
|
21 |
* Enter description here ... |
|
22 |
* @param unknown_type $tag_label |
|
23 |
* @param unknown_type $doctrine |
|
24 |
* @return multitype:boolean Ambigous <NULL, \IRI\Bundle\WikiTagBundle\Entity\Tag> Ambigous <NULL, unknown, mixed, string> (array(\IRI\Bundle\WikiTagBundle\Model\TagInterface, revision_id=int, created:Boolean)) |
|
25 |
*/ |
|
26 |
public static function getOrCreateTag($tag_label, $doctrine) |
|
27 |
{ |
|
28 |
$tag_label_normalized = WikiTagUtils::normalizeTag($tag_label); |
|
29 |
// We get the wikipedia references for the tag_label |
|
30 |
// We get or create the tag object |
|
31 |
$tags = $doctrine->getRepository('WikiTagBundle:Tag')->findBy(array('normalizedLabel' => $tag_label_normalized)); |
|
32 |
$tag = null; |
|
33 |
foreach ($tags as $t){ |
|
34 |
if($tag==null || $t->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']){ |
|
35 |
$tag = $t; |
|
36 |
if($t->getUrlStatus()!=Tag::$TAG_URL_STATUS_DICT['null_result']){ |
|
37 |
break; |
|
38 |
} |
|
39 |
} |
|
40 |
} |
|
| 32 | 41 |
$wp_request_done = false; |
| 2 | 42 |
if($tag==null){ |
43 |
$tag = new Tag(); |
|
44 |
$tag->setLabel($tag_label_normalized); |
|
45 |
$tag->setOriginalLabel($tag_label); |
|
46 |
$tag->setNormalizedLabel($tag_label_normalized); |
|
47 |
$created = true; |
|
48 |
} |
|
49 |
else{ |
|
50 |
$created = false; |
|
| 32 | 51 |
$match_exists = false; |
52 |
// Even if a tag with the normalised label exists, IF this tag is not wikipedia semantised, |
|
53 |
// we search if a wikipedia semantised version exists in the base |
|
54 |
foreach ($tags as $t){ |
|
55 |
if($t->getUrlStatus()==Tag::$TAG_URL_STATUS_DICT['match']){ |
|
56 |
$tag = $t; |
|
57 |
$match_exists = true; |
|
58 |
break; |
|
59 |
} |
|
60 |
} |
|
61 |
if($match_exists==false){ |
|
62 |
$wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized); |
|
63 |
$status = $wp_response['status']; |
|
64 |
if($status==Tag::$TAG_URL_STATUS_DICT['match']){ |
|
65 |
$tag = new Tag(); |
|
66 |
$tag->setLabel($tag_label_normalized); |
|
67 |
$tag->setOriginalLabel($tag_label); |
|
68 |
$tag->setNormalizedLabel($tag_label_normalized); |
|
69 |
$created = true; |
|
70 |
$wp_request_done = true; |
|
71 |
} |
|
72 |
} |
|
| 2 | 73 |
} |
74 |
||
75 |
// We request Wikipedia if the tag is created |
|
76 |
if($created==true){ |
|
| 32 | 77 |
if($wp_request_done==false){ |
78 |
$wp_response = WikiTagUtils::getWikipediaInfo($tag_label_normalized); |
|
79 |
} |
|
| 2 | 80 |
$new_label = $wp_response['new_label']; |
81 |
$status = $wp_response['status']; |
|
82 |
$url = $wp_response['wikipedia_url']; |
|
83 |
$pageid = $wp_response['pageid']; |
|
84 |
$dbpedia_uri = $wp_response["dbpedia_uri"]; |
|
85 |
$wikipedia_revision_id = $wp_response['revision_id']; |
|
86 |
||
87 |
# We save the datas |
|
88 |
if($new_label!=null){ |
|
89 |
$tag->setLabel($new_label); |
|
90 |
} |
|
91 |
if($status!=null){ |
|
92 |
$tag->setUrlStatus($status); |
|
93 |
} |
|
94 |
$tag->setWikipediaUrl($url); |
|
95 |
$tag->setWikipediaPageId($pageid); |
|
96 |
$tag->setDbpediaUri($dbpedia_uri); |
|
97 |
||
98 |
// Save datas. |
|
99 |
$em = $doctrine->getEntityManager(); |
|
100 |
$em->persist($tag); |
|
101 |
$em->flush(); |
|
102 |
||
103 |
} |
|
104 |
else if($tag!=null && $tag->getWikipediaPageId()!=null){ |
|
105 |
$wp_response = WikiTagUtils::getWikipediaInfo(null, $tag->getWikipediaPageId()); |
|
106 |
$wikipedia_revision_id = $wp_response['revision_id']; |
|
107 |
} |
|
108 |
else{ |
|
109 |
$wikipedia_revision_id = null; |
|
110 |
} |
|
111 |
||
112 |
return array($tag, $wikipedia_revision_id, $created);//, $wpReponse); |
|
113 |
} |
|
114 |
||
115 |
/** |
|
116 |
* Cleans the tag label |
|
117 |
*/ |
|
| 8 | 118 |
public static function normalizeTag($tag_label) |
| 2 | 119 |
{ |
120 |
if(strlen($tag_label)==0){ |
|
121 |
return $tag_label; |
|
122 |
} |
|
123 |
$tag_label = trim($tag_label);//tag.strip() |
|
124 |
$tag_label = str_replace("_", " ", $tag_label);//tag.replace("_", " ") |
|
|
9
cc32af725176
first step for tag list and add Pagerfanta for paginator
cavaliet
parents:
8
diff
changeset
|
125 |
$tag_label = str_replace("Œ", "oe", $tag_label); |
|
cc32af725176
first step for tag list and add Pagerfanta for paginator
cavaliet
parents:
8
diff
changeset
|
126 |
$tag_label = str_replace("œ", "oe", $tag_label); |
| 2 | 127 |
$tag_label = preg_replace('/\s+/', ' ', $tag_label);//" ".join(tag.split()) |
128 |
$tag_label = ucfirst($tag_label);//tag[0].upper() + tag[1:] |
|
129 |
return $tag_label; |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* |
|
134 |
* TODO: Enter description here ... |
|
135 |
* @param unknown_type $tag_label_normalized |
|
136 |
* @param unknown_type $page_id |
|
137 |
* @return multitype:NULL unknown |multitype:Ambigous <NULL, unknown> multitype:number mixed Ambigous <NULL, string> Ambigous <unknown, mixed> |
|
138 |
*/ |
|
|
22
99c15cfe420b
Add ModifyTag from tag list. Add Reset Wikipedia info from tag list. Enable boolean from addJavascript controller/template to switch from list or document context.
cavaliet
parents:
9
diff
changeset
|
139 |
public static function getWikipediaInfo($tag_label_normalized, $page_id=null) |
| 2 | 140 |
{ |
141 |
$params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json'); |
|
142 |
if($tag_label_normalized!=null){ |
|
143 |
$params['titles'] = urlencode($tag_label_normalized); |
|
144 |
} |
|
145 |
else if($page_id!=null){ |
|
146 |
$params['pageids'] = $page_id; |
|
147 |
} |
|
148 |
else{ |
|
149 |
return WikiTagUtils::returnNullResult(null); |
|
150 |
} |
|
151 |
||
152 |
$ar = WikiTagUtils::requestWikipedia($params); |
|
153 |
$res = $ar[0]; |
|
154 |
$original_response = $res; |
|
155 |
$pages = $ar[1]; |
|
156 |
// If there 0 or more than 1 result, the query has failed |
|
157 |
if(count($pages)>1 || count($pages)==0){ |
|
158 |
return WikiTagUtils::returnNullResult($res); |
|
159 |
} |
|
160 |
// get first result |
|
161 |
$page = reset($pages); |
|
162 |
// Unknow entry ? |
|
163 |
if(array_key_exists('missing', $page) || array_key_exists('invalid', $page)){ |
|
164 |
return WikiTagUtils::returnNullResult($res); |
|
165 |
} |
|
166 |
// The entry exists, we get the datas. |
|
167 |
$url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null; |
|
168 |
$pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null; |
|
169 |
$new_label = array_key_exists('title', $page) ? $page['title'] : null; |
|
170 |
// We test the status (redirect first because a redirect has no categories key) |
|
171 |
if(array_key_exists('redirect', $page)){ |
|
172 |
//return " REDIRECT"; |
|
173 |
$status = Tag::$TAG_URL_STATUS_DICT["redirection"]; |
|
174 |
} |
|
175 |
else if(WikiTagUtils::isHomonymy($page)){ |
|
176 |
//return " HOMONYMY"; |
|
177 |
$status = Tag::$TAG_URL_STATUS_DICT["homonyme"]; |
|
178 |
} |
|
179 |
else{ |
|
180 |
//return " MATCH"; |
|
181 |
$status = Tag::$TAG_URL_STATUS_DICT["match"]; |
|
182 |
} |
|
183 |
// In redirection, we have to get more datas by adding redirects=true to the params |
|
184 |
if($status==Tag::$TAG_URL_STATUS_DICT["redirection"]){ |
|
185 |
$params['redirects'] = "true"; |
|
186 |
$ar = WikiTagUtils::requestWikipedia($params); |
|
187 |
$res = $ar[0]; |
|
188 |
$pages = $ar[1]; |
|
189 |
#we know that we have at least one answer |
|
190 |
if(count($pages)>1 || count($pages)==0){ |
|
191 |
return WikiTagUtils::returnNullResult($res); |
|
192 |
} |
|
193 |
// get first result |
|
194 |
$page = reset($pages); |
|
195 |
} |
|
196 |
||
197 |
$revision_id = $page['lastrevid']; |
|
198 |
||
199 |
// process language to extract the english label |
|
200 |
$english_label = null; |
|
201 |
if($status==Tag::$TAG_URL_STATUS_DICT["match"] || $status==Tag::$TAG_URL_STATUS_DICT["redirection"]){ |
|
202 |
if(array_key_exists("langlinks", $page)){ |
|
203 |
foreach ($page["langlinks"] as $ar) { |
|
204 |
if($ar["lang"]=="en"){ |
|
205 |
$english_label = $ar["*"]; |
|
206 |
break; |
|
207 |
} |
|
208 |
} |
|
209 |
} |
|
210 |
} |
|
211 |
// We create the dbpedia uri. |
|
212 |
$dbpedia_uri = null; |
|
213 |
if($english_label!=null && strpos($english_label, '#')===false){ |
|
214 |
$dbpedia_uri = WikiTagUtils::getDbpediaUri($english_label); |
|
215 |
} |
|
216 |
||
217 |
$wp_response = array('new_label'=>$new_label, 'status'=>$status, 'wikipedia_url'=>$url, 'pageid'=>$pageid, 'dbpedia_uri'=>$dbpedia_uri, 'revision_id'=>$revision_id, 'response'=>$original_response); |
|
218 |
//return $url." <br/>RES = ".$res/*." <br/>DUMP = ".var_dump($pages)*/." <br/>COUNT = ".count($pages)." <br/>page = ".var_dump($page); |
|
219 |
return $wp_response; |
|
220 |
} |
|
221 |
||
222 |
||
223 |
/** |
|
224 |
* |
|
225 |
* TODO : Enter description here ... |
|
226 |
* @param unknown_type $params |
|
227 |
* @return multitype:unknown mixed |
|
228 |
*/ |
|
229 |
private static function requestWikipedia($params) |
|
230 |
{ |
|
231 |
$params_str = ''; |
|
232 |
foreach ($params as $key => $value) { |
|
233 |
if ($params_str==''){ |
|
234 |
$params_str = $key.'='.$value; |
|
235 |
} |
|
236 |
else{ |
|
237 |
$params_str .= '&'.$key.'='.$value; |
|
238 |
} |
|
239 |
} |
|
240 |
||
241 |
$url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str; |
|
242 |
||
243 |
$ch = curl_init(); |
|
244 |
curl_setopt($ch, CURLOPT_URL, $url); |
|
245 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
246 |
curl_setopt($ch, CURLOPT_USERAGENT, 'http://www.iri.centrepompidou.fr'); |
|
247 |
$res = curl_exec($ch); |
|
248 |
curl_close($ch); |
|
249 |
||
250 |
$val = json_decode($res, true); |
|
251 |
$pages = $val["query"]["pages"]; |
|
252 |
return array($res, $pages); |
|
253 |
} |
|
254 |
||
255 |
/** |
|
256 |
* Returns tag with a null result, usually used after a failed request on Wikipedia |
|
257 |
*/ |
|
258 |
private static function returnNullResult($response) |
|
259 |
{ |
|
260 |
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); |
|
261 |
} |
|
262 |
||
263 |
/** |
|
264 |
* Returns tag with a null result, usually used after a failed request on Wikipedia |
|
265 |
*/ |
|
266 |
private static function isHomonymy($page) |
|
267 |
{ |
|
268 |
//$s = ""; |
|
269 |
foreach ($page["categories"] as $ar) { |
|
270 |
//$s .= ", b : ".$ar." - title = ".$ar["title"].", strpos = ".strpos($ar["title"], 'Catégorie:Homonymie'); |
|
271 |
// Strict test because false can be seen as "O". |
|
272 |
if(strpos($ar["title"], 'Catégorie:Homonymie')!==false || strpos($ar["title"], 'Category:Disambiguation')!==false){ |
|
273 |
//$s .= "TRUE"; |
|
274 |
return true; |
|
275 |
} |
|
276 |
} |
|
277 |
return false; |
|
278 |
} |
|
279 |
||
280 |
/** |
|
281 |
* Builds DbPedia URI |
|
282 |
*/ |
|
283 |
private static function getDbpediaUri($english_label) |
|
284 |
{ |
|
285 |
return sprintf(WikiTagUtils::$DBPEDIA_URI_TEMPLATE, WikiTagUtils::urlize_for_wikipedia($english_label)); |
|
286 |
} |
|
287 |
||
288 |
/** |
|
289 |
* URLencode label for wikipedia |
|
290 |
*/ |
|
291 |
private static function urlize_for_wikipedia($label){ |
|
292 |
return urlencode(str_replace(" ", "_", $label)); |
|
293 |
} |
|
294 |
} |