47 * @param bigint $page_id |
47 * @param bigint $page_id |
48 * @return array |
48 * @return array |
49 */ |
49 */ |
50 public static function getWikipediaInfo($tag_label_normalized, $page_id=null) |
50 public static function getWikipediaInfo($tag_label_normalized, $page_id=null) |
51 { |
51 { |
|
52 // get ignore_wikipedia_error parameter |
|
53 $ignore_wikipedia_error = false; |
|
54 |
|
55 if(key_exists('kernel', $GLOBALS)) { |
|
56 $kernel = $GLOBALS['kernel']; |
|
57 $ignore_wikipedia_error = $kernel->getContainer()->getParameter('wiki_tag.ignore_wikipedia_error'); |
|
58 } |
|
59 |
|
60 |
52 $params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json'); |
61 $params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json'); |
53 if($tag_label_normalized!=null){ |
62 if($tag_label_normalized!=null){ |
54 $params['titles'] = urlencode($tag_label_normalized); |
63 $params['titles'] = urlencode($tag_label_normalized); |
55 } |
64 } |
56 else if($page_id!=null){ |
65 else if($page_id!=null){ |
58 } |
67 } |
59 else{ |
68 else{ |
60 return WikiTagUtils::returnNullResult(null); |
69 return WikiTagUtils::returnNullResult(null); |
61 } |
70 } |
62 |
71 |
63 $ar = WikiTagUtils::requestWikipedia($params); |
72 try { |
|
73 $ar = WikiTagUtils::requestWikipedia($params); |
|
74 } |
|
75 catch(\Exception $e) { |
|
76 if($ignore_wikipedia_error) { |
|
77 return WikiTagUtils::returnNullResult(null); |
|
78 } |
|
79 else { |
|
80 throw $e; |
|
81 } |
|
82 } |
64 |
83 |
65 $res = $ar[0]; |
84 $res = $ar[0]; |
66 $original_response = $res; |
85 $original_response = $res; |
67 $pages = $ar[1]; |
86 $pages = $ar[1]; |
68 // If there 0 or more than 1 result, the query has failed |
87 // If there 0 or more than 1 result, the query has failed |
97 $alternative_url = null; |
116 $alternative_url = null; |
98 $alternative_pageid = null; |
117 $alternative_pageid = null; |
99 if($status==Tag::$TAG_URL_STATUS_DICT["redirection"]) |
118 if($status==Tag::$TAG_URL_STATUS_DICT["redirection"]) |
100 { |
119 { |
101 $params['redirects'] = "true"; |
120 $params['redirects'] = "true"; |
102 $ar = WikiTagUtils::requestWikipedia($params); |
121 try { |
|
122 $ar = WikiTagUtils::requestWikipedia($params); |
|
123 } |
|
124 catch(\Exception $e) { |
|
125 if($ignore_wikipedia_error) { |
|
126 return WikiTagUtils::returnNullResult(null); |
|
127 } |
|
128 else { |
|
129 throw $e; |
|
130 } |
|
131 } |
|
132 |
103 $res = $ar[0]; |
133 $res = $ar[0]; |
104 $pages = $ar[1]; |
134 $pages = $ar[1]; |
105 #we know that we have at least one answer |
135 #we know that we have at least one answer |
106 if(count($pages)>1 || count($pages)==0){ |
136 if(count($pages)>1 || count($pages)==0){ |
107 return WikiTagUtils::returnNullResult($res); |
137 return WikiTagUtils::returnNullResult($res); |
142 'pageid'=>$pageid, |
172 'pageid'=>$pageid, |
143 'alternative_pageid'=>$alternative_pageid, |
173 'alternative_pageid'=>$alternative_pageid, |
144 'dbpedia_uri'=>$dbpedia_uri, |
174 'dbpedia_uri'=>$dbpedia_uri, |
145 'revision_id'=>$revision_id, |
175 'revision_id'=>$revision_id, |
146 'response'=>$original_response); |
176 'response'=>$original_response); |
147 //return $url." <br/>RES = ".$res/*." <br/>DUMP = ".var_dump($pages)*/." <br/>COUNT = ".count($pages)." <br/>page = ".var_dump($page); |
177 |
148 return $wp_response; |
178 return $wp_response; |
149 } |
179 } |
150 |
180 |
151 |
181 |
152 /** |
182 /** |
170 $url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str; |
200 $url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str; |
171 |
201 |
172 $ch = curl_init(); |
202 $ch = curl_init(); |
173 curl_setopt($ch, CURLOPT_URL, $url); |
203 curl_setopt($ch, CURLOPT_URL, $url); |
174 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
204 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
|
205 //TODO: change |
175 curl_setopt($ch, CURLOPT_USERAGENT, 'http://www.iri.centrepompidou.fr'); |
206 curl_setopt($ch, CURLOPT_USERAGENT, 'http://www.iri.centrepompidou.fr'); |
176 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); |
207 curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000); |
177 $res = curl_exec($ch); |
208 $res = curl_exec($ch); |
178 $curl_errno = curl_errno($ch); |
209 $curl_errno = curl_errno($ch); |
179 $curl_error = curl_error($ch); |
210 $curl_error = curl_error($ch); |
180 curl_close($ch); |
211 curl_close($ch); |
181 |
212 |
182 if ($curl_errno > 0) { |
213 if ($curl_errno > 0) { |
183 throw new \Exception("Wikipedia request failed. cURLError #$curl_errno: $curl_error\n"); |
214 throw new \Exception("Wikipedia request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null); |
184 } |
215 } |
185 |
216 |
186 $val = json_decode($res, true); |
217 $val = json_decode($res, true); |
187 $pages = $val["query"]["pages"]; |
218 $pages = $val["query"]["pages"]; |
188 return array($res, $pages); |
219 return array($res, $pages); |