| author | ymh <ymh.work@gmail.com> |
| Fri, 25 Nov 2011 18:55:42 +0100 | |
| changeset 42 | 0e57c730bb18 |
| parent 32 | 38dcd2db04e4 |
| child 43 | 54f204bceb28 |
| 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 |
* Cleans the tag label |
|
17 |
*/ |
|
| 8 | 18 |
public static function normalizeTag($tag_label) |
| 2 | 19 |
{ |
20 |
if(strlen($tag_label)==0){ |
|
21 |
return $tag_label; |
|
22 |
} |
|
23 |
$tag_label = trim($tag_label);//tag.strip() |
|
24 |
$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
|
25 |
$tag_label = str_replace("Œ", "oe", $tag_label); |
|
cc32af725176
first step for tag list and add Pagerfanta for paginator
cavaliet
parents:
8
diff
changeset
|
26 |
$tag_label = str_replace("œ", "oe", $tag_label); |
| 2 | 27 |
$tag_label = preg_replace('/\s+/', ' ', $tag_label);//" ".join(tag.split()) |
28 |
$tag_label = ucfirst($tag_label);//tag[0].upper() + tag[1:] |
|
29 |
return $tag_label; |
|
30 |
} |
|
31 |
||
32 |
/** |
|
33 |
* |
|
34 |
* TODO: Enter description here ... |
|
35 |
* @param unknown_type $tag_label_normalized |
|
36 |
* @param unknown_type $page_id |
|
37 |
* @return multitype:NULL unknown |multitype:Ambigous <NULL, unknown> multitype:number mixed Ambigous <NULL, string> Ambigous <unknown, mixed> |
|
38 |
*/ |
|
|
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
|
39 |
public static function getWikipediaInfo($tag_label_normalized, $page_id=null) |
| 2 | 40 |
{ |
41 |
$params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json'); |
|
42 |
if($tag_label_normalized!=null){ |
|
43 |
$params['titles'] = urlencode($tag_label_normalized); |
|
44 |
} |
|
45 |
else if($page_id!=null){ |
|
46 |
$params['pageids'] = $page_id; |
|
47 |
} |
|
48 |
else{ |
|
49 |
return WikiTagUtils::returnNullResult(null); |
|
50 |
} |
|
51 |
||
52 |
$ar = WikiTagUtils::requestWikipedia($params); |
|
53 |
$res = $ar[0]; |
|
54 |
$original_response = $res; |
|
55 |
$pages = $ar[1]; |
|
56 |
// If there 0 or more than 1 result, the query has failed |
|
57 |
if(count($pages)>1 || count($pages)==0){ |
|
58 |
return WikiTagUtils::returnNullResult($res); |
|
59 |
} |
|
60 |
// get first result |
|
61 |
$page = reset($pages); |
|
62 |
// Unknow entry ? |
|
63 |
if(array_key_exists('missing', $page) || array_key_exists('invalid', $page)){ |
|
64 |
return WikiTagUtils::returnNullResult($res); |
|
65 |
} |
|
66 |
// The entry exists, we get the datas. |
|
67 |
$url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null; |
|
68 |
$pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null; |
|
69 |
$new_label = array_key_exists('title', $page) ? $page['title'] : null; |
|
70 |
// We test the status (redirect first because a redirect has no categories key) |
|
71 |
if(array_key_exists('redirect', $page)){ |
|
72 |
//return " REDIRECT"; |
|
73 |
$status = Tag::$TAG_URL_STATUS_DICT["redirection"]; |
|
74 |
} |
|
75 |
else if(WikiTagUtils::isHomonymy($page)){ |
|
76 |
//return " HOMONYMY"; |
|
77 |
$status = Tag::$TAG_URL_STATUS_DICT["homonyme"]; |
|
78 |
} |
|
79 |
else{ |
|
80 |
//return " MATCH"; |
|
81 |
$status = Tag::$TAG_URL_STATUS_DICT["match"]; |
|
82 |
} |
|
83 |
// 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
|
84 |
$alternative_label = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
85 |
$alternative_url = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
86 |
$alternative_pageid = null; |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
87 |
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
|
88 |
{ |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
89 |
//TODO: add alternative label |
| 2 | 90 |
$params['redirects'] = "true"; |
91 |
$ar = WikiTagUtils::requestWikipedia($params); |
|
92 |
$res = $ar[0]; |
|
93 |
$pages = $ar[1]; |
|
94 |
#we know that we have at least one answer |
|
95 |
if(count($pages)>1 || count($pages)==0){ |
|
96 |
return WikiTagUtils::returnNullResult($res); |
|
97 |
} |
|
98 |
// get first result |
|
99 |
$page = reset($pages); |
|
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
100 |
$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
|
101 |
$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
|
102 |
$alternative_pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null; |
| 2 | 103 |
} |
104 |
||
105 |
$revision_id = $page['lastrevid']; |
|
106 |
||
107 |
// process language to extract the english label |
|
108 |
$english_label = null; |
|
109 |
if($status==Tag::$TAG_URL_STATUS_DICT["match"] || $status==Tag::$TAG_URL_STATUS_DICT["redirection"]){ |
|
110 |
if(array_key_exists("langlinks", $page)){ |
|
111 |
foreach ($page["langlinks"] as $ar) { |
|
112 |
if($ar["lang"]=="en"){ |
|
113 |
$english_label = $ar["*"]; |
|
114 |
break; |
|
115 |
} |
|
116 |
} |
|
117 |
} |
|
118 |
} |
|
119 |
// We create the dbpedia uri. |
|
120 |
$dbpedia_uri = null; |
|
121 |
if($english_label!=null && strpos($english_label, '#')===false){ |
|
122 |
$dbpedia_uri = WikiTagUtils::getDbpediaUri($english_label); |
|
123 |
} |
|
124 |
||
|
42
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
125 |
$wp_response = array( |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
126 |
'new_label'=>$new_label, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
127 |
'alternative_label'=>$alternative_label, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
128 |
'status'=>$status, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
129 |
'wikipedia_url'=>$url, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
130 |
'wikipedia_alternative_url'=>$alternative_url, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
131 |
'pageid'=>$pageid, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
132 |
'alternative_pageid'=>$alternative_pageid, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
133 |
'dbpedia_uri'=>$dbpedia_uri, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
134 |
'revision_id'=>$revision_id, |
|
0e57c730bb18
Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents:
32
diff
changeset
|
135 |
'response'=>$original_response); |
| 2 | 136 |
//return $url." <br/>RES = ".$res/*." <br/>DUMP = ".var_dump($pages)*/." <br/>COUNT = ".count($pages)." <br/>page = ".var_dump($page); |
137 |
return $wp_response; |
|
138 |
} |
|
139 |
||
140 |
||
141 |
/** |
|
142 |
* |
|
143 |
* TODO : Enter description here ... |
|
144 |
* @param unknown_type $params |
|
145 |
* @return multitype:unknown mixed |
|
146 |
*/ |
|
147 |
private static function requestWikipedia($params) |
|
148 |
{ |
|
149 |
$params_str = ''; |
|
150 |
foreach ($params as $key => $value) { |
|
151 |
if ($params_str==''){ |
|
152 |
$params_str = $key.'='.$value; |
|
153 |
} |
|
154 |
else{ |
|
155 |
$params_str .= '&'.$key.'='.$value; |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
$url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str; |
|
160 |
||
161 |
$ch = curl_init(); |
|
162 |
curl_setopt($ch, CURLOPT_URL, $url); |
|
163 |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
164 |
curl_setopt($ch, CURLOPT_USERAGENT, 'http://www.iri.centrepompidou.fr'); |
|
165 |
$res = curl_exec($ch); |
|
166 |
curl_close($ch); |
|
167 |
||
168 |
$val = json_decode($res, true); |
|
169 |
$pages = $val["query"]["pages"]; |
|
170 |
return array($res, $pages); |
|
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* Returns tag with a null result, usually used after a failed request on Wikipedia |
|
175 |
*/ |
|
176 |
private static function returnNullResult($response) |
|
177 |
{ |
|
178 |
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); |
|
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Returns tag with a null result, usually used after a failed request on Wikipedia |
|
183 |
*/ |
|
184 |
private static function isHomonymy($page) |
|
185 |
{ |
|
186 |
//$s = ""; |
|
187 |
foreach ($page["categories"] as $ar) { |
|
188 |
//$s .= ", b : ".$ar." - title = ".$ar["title"].", strpos = ".strpos($ar["title"], 'Catégorie:Homonymie'); |
|
189 |
// Strict test because false can be seen as "O". |
|
190 |
if(strpos($ar["title"], 'Catégorie:Homonymie')!==false || strpos($ar["title"], 'Category:Disambiguation')!==false){ |
|
191 |
//$s .= "TRUE"; |
|
192 |
return true; |
|
193 |
} |
|
194 |
} |
|
195 |
return false; |
|
196 |
} |
|
197 |
||
198 |
/** |
|
199 |
* Builds DbPedia URI |
|
200 |
*/ |
|
201 |
private static function getDbpediaUri($english_label) |
|
202 |
{ |
|
203 |
return sprintf(WikiTagUtils::$DBPEDIA_URI_TEMPLATE, WikiTagUtils::urlize_for_wikipedia($english_label)); |
|
204 |
} |
|
205 |
||
206 |
/** |
|
207 |
* URLencode label for wikipedia |
|
208 |
*/ |
|
209 |
private static function urlize_for_wikipedia($label){ |
|
210 |
return urlencode(str_replace(" ", "_", $label)); |
|
211 |
} |
|
212 |
} |