Index: trunk/WikiWord/WikiWord/src/main/php/api.php |
— | — | @@ -0,0 +1,103 @@ |
| 2 | +<?php |
| 3 | + |
| 4 | +$IP = dirname(__FILE__); |
| 5 | + |
| 6 | +require_once("$IP/config.php"); |
| 7 | +require_once("$IP/wwthesaurus.php"); |
| 8 | + |
| 9 | +$query = @$_REQUEST['query']; |
| 10 | + |
| 11 | +if ( $query ) { |
| 12 | + $lang = @$_REQUEST['lang']; |
| 13 | + $format = @$_REQUEST['format']; |
| 14 | + if ( !$format ) $format = 'phps'; |
| 15 | + |
| 16 | + $result = array( 'query' => $query ); |
| 17 | + $start = microtime(); |
| 18 | + |
| 19 | + try { |
| 20 | + $thesaurus = new WWThesaurus(); |
| 21 | + $db = $thesaurus->connect($wwDBServer, $wwDBUser, $wwDBPassword, $wwDBDatabase); |
| 22 | + |
| 23 | + if ( !$db ) { |
| 24 | + $result['error'] = array('code' => 1010, 'message' => "failed to connect to thesaurus database"); |
| 25 | + } else if ($query == 'concepts') { |
| 26 | + $term = @$_REQUEST['term']; |
| 27 | + $page = @$_REQUEST['page']; |
| 28 | + |
| 29 | + if ( $lang === null ) $result['error'] = array('code' => 150, 'message' => "missing parameter lang"); |
| 30 | + else if ( $term !=== null ) { |
| 31 | + $result['concepts'] = $thesaurus->getConceptsForTerm($lang, $term); |
| 32 | + if ( $result['concepts'] === false || $result['concepts'] === null ) { |
| 33 | + $result['error'] = array('code' => 210, 'message' => "failed to retrieve concepts for term $langt:$term"); |
| 34 | + } |
| 35 | + } else if ( $page !=== null ) { |
| 36 | + $result['concepts'] = $thesaurus->getConceptsForPage($lang, $page); |
| 37 | + if ( $result['concepts'] === false || $result['concepts'] === null ) { |
| 38 | + $result['error'] = array('code' => 250, 'message' => "failed to retrieve concepts for page $langt:$page"); |
| 39 | + } |
| 40 | + } else { |
| 41 | + $result['error'] = array('code' => 110, 'message' => "missing parameter term"); |
| 42 | + } |
| 43 | + } if ($query == 'properties') else { |
| 44 | + $gcid = @$_REQUEST['gcid']; |
| 45 | + $props = @$_REQUEST['props']; |
| 46 | + |
| 47 | + if ( $gcid === null ) $result['error'] = array('code' => 120, 'message' => "missing parameter gcid"); |
| 48 | + else if ( $props === null ) $result['error'] = array('code' => 130, 'message' => "missing parameter props"); |
| 49 | + else { |
| 50 | + $props = preg_split('![\\s,;|/:]\\s*!', $props); |
| 51 | + |
| 52 | + foreach ( $props as $p ) { |
| 53 | + $m = "get" . ucfist($p) . "ForConcept"; |
| 54 | + if ( !method_exists($thesaurus, $m) ) { |
| 55 | + $result['error'] = array('code' => 190, 'message' => "unknown property: $p"); |
| 56 | + break; |
| 57 | + } |
| 58 | + |
| 59 | + $result[$p] = $thesaurus->$m($gcid, $lang); |
| 60 | + |
| 61 | + if ( $result[$p] === false || $result[$p] === null ) { |
| 62 | + $result['error'] = array('code' => 220, 'message' => "failed to retrieve property $p for concept $gcid"); |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + } else { |
| 68 | + $result['error'] = array('code' => 10, 'message' => "bad query: $query"); |
| 69 | + } |
| 70 | + } catch (Exception e) { |
| 71 | + $result['error'] = array('code' => 1000, 'message' => "unexpected exception: " . $e->getMessage()); |
| 72 | + } |
| 73 | + |
| 74 | + $result['time'] = (microtime() - $start) . "ms"; |
| 75 | + |
| 76 | + if ( isset($result['error']) ) { |
| 77 | + #TODO: HTTP error codce would be nice, but causes file_get_contents to swallow the data. |
| 78 | + # need to use CURL in client! |
| 79 | + #header("Status: 400 Bad Request", true, 400); |
| 80 | + } |
| 81 | + |
| 82 | + #TODO: JSON (+YAML?), WDDX |
| 83 | + if ($format == 'phps') { |
| 84 | + header("Content-Type: application/vnd.php.serialized"); #as proposed by http://www.alvestrand.no/pipermail/ietf-types/2004-August/001259.html |
| 85 | + $data = serialize($result); |
| 86 | + echo $data; |
| 87 | + } else if ($format == 'php') { |
| 88 | + header("Content-Type: text/php"); |
| 89 | + var_export($result); |
| 90 | + } else if ($format == 'text') { |
| 91 | + header("Content-Type: text/plain"); |
| 92 | + print_r($result); |
| 93 | + } else { |
| 94 | + header("Content-Type: text/plain"); |
| 95 | + header("Status: 400 Bad Request", true, 400); |
| 96 | + echo "Bad format: $format"; |
| 97 | + } |
| 98 | + |
| 99 | + exit(); |
| 100 | +} |
| 101 | + |
| 102 | +header("Content-Type: text/plain"); |
| 103 | +?> |
| 104 | +WikiWord REST API |
\ No newline at end of file |
Property changes on: trunk/WikiWord/WikiWord/src/main/php/api.php |
___________________________________________________________________ |
Name: svn:mergeinfo |
1 | 105 | + |
Index: trunk/WikiWord/WikiWord/src/main/php/wwclient.php |
— | — | @@ -0,0 +1,63 @@ |
| 2 | +<?php |
| 3 | +require_once( dirname( __FILE__ ) . "/wwutils.php" ); |
| 4 | + |
| 5 | +class WWClient { |
| 6 | + var $api; |
| 7 | + |
| 8 | + function __construct( $api ) { |
| 9 | + $this->api = $api; |
| 10 | + } |
| 11 | + |
| 12 | + function query( $param ) { |
| 13 | + $url = $this->api . '?format=phps'; |
| 14 | + |
| 15 | + for ( $params as $k => $v ) { |
| 16 | + $url .= '&'; |
| 17 | + $url .= urlencode( $k ); |
| 18 | + $url .= '='; |
| 19 | + $url .= urlencode( $v ); |
| 20 | + } |
| 21 | + |
| 22 | + $data = file_get_contents( $url ); //TODO: CURL |
| 23 | + if ( !$data ) throw new Exception("failed to fetch data from $url"); |
| 24 | + |
| 25 | + $data = unserialize($data); |
| 26 | + if ( !$data ) throw new Exception("failed to unserialize data from $url"); |
| 27 | + |
| 28 | + if ( $data['error'] ) throw new Exception("API returned error ".$data['error']['code'].": ".$data['error']['message']); |
| 29 | + return $data; |
| 30 | + } |
| 31 | + |
| 32 | + function getWikiPages( $id ) { |
| 33 | + $p = $this->getConceptProperties( $id, 'pages' ); |
| 34 | + |
| 35 | + return $p['pages']; |
| 36 | + } |
| 37 | + |
| 38 | + function getConceptProperties( $id, $props, $lang = NUL L) { |
| 39 | + $param = array( |
| 40 | + 'query' => 'properties', |
| 41 | + 'props' => ( is_array($props) ? join('|', $props) : $props ), |
| 42 | + 'gcid' => $id, |
| 43 | + ); |
| 44 | + |
| 45 | + if ( $lang ) $param['lang'] = $lang; |
| 46 | + |
| 47 | + $rs = $this->query( $param ); |
| 48 | + |
| 49 | + return $rs; |
| 50 | + } |
| 51 | + |
| 52 | + function getConceptsForTerm( $lang, $term ) { |
| 53 | + $param = array( |
| 54 | + 'query' => 'concepts', |
| 55 | + 'lang' => $lang, |
| 56 | + 'term' => $term, |
| 57 | + ); |
| 58 | + |
| 59 | + $rs = $this->query( $param ); |
| 60 | + |
| 61 | + return $rs['concepts']; |
| 62 | + } |
| 63 | + |
| 64 | +} |
Property changes on: trunk/WikiWord/WikiWord/src/main/php/wwclient.php |
___________________________________________________________________ |
Name: svn:mergeinfo |
1 | 65 | + |
Index: trunk/WikiWord/WikiWord/src/main/php/wwutils.php |
— | — | @@ -1,86 +1,8 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | | -if (!defined('NS_IMAGE')) |
5 | | - define('NS_IMAGE', 6); |
6 | | - |
7 | | -if (!defined('NS_TEMPLATE')) |
8 | | - define('NS_TEMPLATE', 10); |
9 | | - |
10 | | -class ImageCollection { |
11 | | - |
12 | | - function __construct() { |
13 | | - $this->images = array(); |
14 | | - } |
15 | | - |
16 | | - static function compareRecords($a, $b) { |
17 | | - $d = (float)$b['score'] - (float)$a['score']; //NOTE: descending |
18 | | - |
19 | | - if ( $d > 0 ) return 1; |
20 | | - else if ( $d < 0 ) return -1; |
21 | | - else return 0; |
22 | | - } |
23 | | - |
24 | | - function size() { |
25 | | - return count($this->images); |
26 | | - } |
27 | | - |
28 | | - function listImages($max) { |
29 | | - uasort($this->images, "Imagecollection::compareRecords"); |
30 | | - |
31 | | - if ($max) return array_slice($this->images, 0, $max); |
32 | | - else return $this->images; |
33 | | - } |
34 | | - |
35 | | - function addImage($image, $key, $usage = "page", $weight = 1) { |
36 | | - if (!isset($this->images[$image])) { |
37 | | - $rec = array( |
38 | | - "name" => $image, |
39 | | - "score" => 0, |
40 | | - ); |
41 | | - } else { |
42 | | - $rec = $this->images[$image]; |
43 | | - } |
44 | | - |
45 | | - if (!isset($rec[$usage])) $rec[$usage] = array(); |
46 | | - $rec[$usage][] = $key; |
47 | | - $rec["score"] += $weight; |
48 | | - |
49 | | - $this->images[$image] = $rec; |
50 | | - return $rec['score']; |
51 | | - } |
52 | | - |
53 | | - function addImages($images, $key, $usage = "page", $weight = 1) { |
54 | | - foreach ($images as $image) { |
55 | | - $this->addImage($image, $key, $usage, $weight); |
56 | | - } |
57 | | - } |
58 | | - |
59 | | - function addTags($image, $tags, $prefix = "") { |
60 | | - global $wwTagScores; |
61 | | - |
62 | | - if (isset($this->images[$image])) { |
63 | | - foreach ($tags as $tag => $weight) { |
64 | | - if (is_int($tag)) { |
65 | | - $tag = $prefix.$weight; |
66 | | - |
67 | | - if (isset($wwTagScores[$tag])) $weight = $wwTagScores[$tag]; |
68 | | - else continue; |
69 | | - } else { |
70 | | - $tag = $prefix.$tag; |
71 | | - } |
72 | | - |
73 | | - $this->images[$image]['score'] += $weight; |
74 | | - $this->images[$image]['tags'][] = $tag; |
75 | | - } |
76 | | - } |
77 | | - } |
78 | | - |
79 | | -} |
80 | | - |
81 | 4 | class WWUtils { |
82 | 5 | var $debug = false; |
83 | 6 | var $db = NULL; |
84 | | - var $wikidbs = array(); |
85 | 7 | |
86 | 8 | var $dbuser; |
87 | 9 | var $dbpassword; |
— | — | @@ -119,66 +41,10 @@ |
120 | 42 | return $result; |
121 | 43 | } |
122 | 44 | |
123 | | - function getWikiTableName($lang, $table) { |
124 | | - global $wwWikitableNamePattern; |
125 | | - |
126 | | - if ($wwWikitableNamePattern) { |
127 | | - return str_replace(array('{lang}', '{name}'), array($lang, $table), $wwWikitableNamePattern); |
128 | | - } |
129 | | - |
130 | | - return $table; |
131 | | - } |
132 | | - |
133 | 45 | function quote($s) { |
134 | 46 | return '"' . mysql_real_escape_string($s) . '"'; |
135 | 47 | } |
136 | 48 | |
137 | | - function getWikiInfo($lang) { |
138 | | - global $wwWikiInfoTable, $wwWikiDbName, $wwWikiServerName, $wwCommonsServerName; |
139 | | - |
140 | | - $db = str_replace('{lang}', $lang, $wwWikiDbName); |
141 | | - |
142 | | - $dbname = "{$lang}wiki_p"; |
143 | | - $sql = "select * from $wwWikiInfoTable "; |
144 | | - $sql .= " where dbname = " . $this->quote("$db"); |
145 | | - |
146 | | - $rs = $this->query($sql); |
147 | | - $info = mysql_fetch_assoc($rs); |
148 | | - mysql_free_result($rs); |
149 | | - |
150 | | - if (!$info) $info = false; |
151 | | - else $info['server'] = str_replace('{num}', $info['server'], $wwWikiServerName); |
152 | | - |
153 | | - if ($lang == "commons" && $wwCommonsServerName) $info['server'] = $wwCommonsServerName; |
154 | | - |
155 | | - return $info; |
156 | | - } |
157 | | - |
158 | | - function getWikiConnection($lang) { |
159 | | - if (isset($this->wikidbs[$lang])) return $this->wikidbs[$lang]; |
160 | | - |
161 | | - $info = $this->getWikiInfo($lang); |
162 | | - |
163 | | - if (!$info) { |
164 | | - $db = false; |
165 | | - } else { |
166 | | - $db = mysql_connect($info['server'], $this->dbuser, $this->dbpassword); |
167 | | - if (!$db) throw new Exception("Connection Failure to Database: " . mysql_error()); |
168 | | - if (!mysql_select_db($info['dbname'], $db)) throw new Exception ("Database not found: " . mysql_error()); |
169 | | - if (!mysql_query("SET NAMES Latin1;", $db)) throw new Exception ("Database not found: " . mysql_error()); |
170 | | - } |
171 | | - |
172 | | - $this->wikidbs[$lang] = $db; |
173 | | - return $db; |
174 | | - } |
175 | | - |
176 | | - function queryWiki($lang, $sql) { |
177 | | - $db = $this->getWikiConnection($lang); |
178 | | - if (!$db) throw new Exception ("Wiki not found: $lang"); |
179 | | - |
180 | | - return $this->query($sql, $db); |
181 | | - } |
182 | | - |
183 | 49 | function close() { |
184 | 50 | if ($this->db) mysql_close($this->db); |
185 | 51 | $this->db = NULL; |
— | — | @@ -214,60 +80,6 @@ |
215 | 81 | return $list; |
216 | 82 | } |
217 | 83 | |
218 | | - function queryConceptsForTerm($lang, $term) { |
219 | | - global $wwTablePrefix, $wwThesaurusDataset; |
220 | | - |
221 | | - $term = trim($term); |
222 | | - |
223 | | - $sql = "SELECT M.*, O.*, definition FROM {$wwTablePrefix}_{$lang}_meaning as M" |
224 | | - . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON M.concept = D.concept " |
225 | | - . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND M.concept = O.local_concept " |
226 | | - . " WHERE term_text = \"" . mysql_real_escape_string($term) . "\"" |
227 | | - . " ORDER BY freq DESC " |
228 | | - . " LIMIT 100"; |
229 | | - |
230 | | - return $this->query($sql); |
231 | | - } |
232 | | - |
233 | | - function queryLocalConcepts($id) { |
234 | | - global $wwTablePrefix, $wwThesaurusDataset; |
235 | | - $sql = "SELECT O.lang, O.local_concept_name from {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O "; |
236 | | - $sql .= " WHERE O.global_concept = " . (int)$id; |
237 | | - |
238 | | - return $this->query($sql); |
239 | | - } |
240 | | - |
241 | | - function getLocalConcepts($id) { |
242 | | - $rs = $this->queryLocalConcepts($id); |
243 | | - $list = WWUtils::slurpAssoc($rs, "lang", "local_concept_name"); |
244 | | - mysql_free_result($rs); |
245 | | - return $list; |
246 | | - } |
247 | | - |
248 | | - function queryLocalConceptInfo($lang, $id) { |
249 | | - global $wwTablePrefix, $wwThesaurusDataset; |
250 | | - |
251 | | - $sql = "SELECT O.*, C.*, F.*, definition FROM {$wwTablePrefix}_{$lang}_concept_info as F " |
252 | | - . " JOIN {$wwTablePrefix}_{$lang}_concept as C ON F.concept = C.id " |
253 | | - . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND F.concept = O.local_concept " |
254 | | - . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON F.concept = D.concept " |
255 | | - . " WHERE O.local_concept = $id "; |
256 | | - |
257 | | - return $this->query($sql); |
258 | | - } |
259 | | - |
260 | | - function queryConceptInfo($id, $lang) { |
261 | | - global $wwTablePrefix, $wwThesaurusDataset; |
262 | | - |
263 | | - $sql = "SELECT O.*, C.*, F.*, definition FROM {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O " |
264 | | - . " LEFT JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_concept_info as F ON O.global_concept = F.concept " |
265 | | - . " LEFT JOIN {$wwTablePrefix}_{$lang}_concept as C ON O.local_concept = C.id " |
266 | | - . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON O.local_concept = D.concept " |
267 | | - . " WHERE O.global_concept = $id AND O.lang = \"" . mysql_real_escape_string($lang) . "\" "; |
268 | | - |
269 | | - return $this->query($sql); |
270 | | - } |
271 | | - |
272 | 84 | static function authFailed($realm) { |
273 | 85 | header("Status: 401 Unauthorized", true, 401); |
274 | 86 | header('WWW-Authenticate: Basic realm="'.$realm.'"'); |
— | — | @@ -302,339 +114,4 @@ |
303 | 115 | |
304 | 116 | print "</select>"; |
305 | 117 | } |
306 | | - |
307 | | - function unpickle($s, $lang, $hasId=true, $hasName=true, $hasConf=true) { |
308 | | - $ss = explode("\x1E", $s); |
309 | | - $items = array(); |
310 | | - |
311 | | - $fetchNames = false; |
312 | | - |
313 | | - foreach ($ss as $i) { |
314 | | - $r = explode("\x1F", $i); |
315 | | - $offs = -1; |
316 | | - |
317 | | - if ($hasId) $r['id'] = @$r[$offs += 1]; |
318 | | - if ($hasName) $r['name'] = @$r[$offs += 1]; |
319 | | - if ($hasConf) $r['conf'] = @$r[$offs += 1]; |
320 | | - |
321 | | - if ($hasId && !isset($r['name'])) |
322 | | - $fetchNames = true; |
323 | | - |
324 | | - if ($hasId) $items[ $r['id'] ] = $r; |
325 | | - else $items[] = $r; |
326 | | - } |
327 | | - |
328 | | - if ($fetchNames) { |
329 | | - $names = $this->fetchNames(array_keys($items), $lang); |
330 | | - |
331 | | - $keys = array_keys($items); |
332 | | - foreach ($keys as $k) { |
333 | | - $id = $items[$k]['id']; |
334 | | - $items[$k]['name'] = $names[$id]; |
335 | | - } |
336 | | - } |
337 | | - |
338 | | - return $items; |
339 | | - } |
340 | | - |
341 | | - function fetchNames($ids, $lang) { |
342 | | - global $wwTablePrefix, $wwThesaurusDataset; |
343 | | - |
344 | | - $names = array(); |
345 | | - if (!$ids) return $names; |
346 | | - |
347 | | - $set = NULL; |
348 | | - foreach ($ids as $id) { |
349 | | - if ($set===NULL) $set = ""; |
350 | | - else $set .= ", "; |
351 | | - $set .= $id; |
352 | | - } |
353 | | - |
354 | | - $sql = "select global_concept as id, local_concept_name as name from {$wwTablePrefix}_{$wwThesaurusDataset}_origin "; |
355 | | - $sql .= "where global_concept in ($set) and lang = \"" . mysql_real_escape_string($lang) . "\" "; |
356 | | - |
357 | | - $res = $this->query($sql); |
358 | | - |
359 | | - while ($row = mysql_fetch_assoc($res)) { |
360 | | - $id = $row['id']; |
361 | | - $names[$id] = $row['name']; |
362 | | - } |
363 | | - |
364 | | - mysql_free_result($res); |
365 | | - |
366 | | - return $names; |
367 | | - } |
368 | | - |
369 | | - function queryImagesOnPage($lang, $ns, $title, $commonsOnly = false) { |
370 | | - global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
371 | | - |
372 | | - if ($lang == "commons") $commonsOnly = false; |
373 | | - |
374 | | - $imagelinks_table = $this->getWikiTableName($lang, "imagelinks"); |
375 | | - $page_table = $this->getWikiTableName($lang, "page"); |
376 | | - $image_table = $this->getWikiTableName($lang, "image"); |
377 | | - |
378 | | - $sql = "/* queryImagesOnPage(" . $this->quote($lang) . ", " . (int)$ns . ", " . $this->quote($title) . ", " . (int)$commonsOnly . ") */ "; |
379 | | - |
380 | | - $sql .= " SELECT I.il_to as name FROM $imagelinks_table as I "; |
381 | | - $sql .= " JOIN $page_table as P on P.page_id = I.il_from "; |
382 | | - if ($commonsOnly) $sql .= " LEFT JOIN $image_table as R on R.img_name = I.il_to "; |
383 | | - if ($commonsOnly) $sql .= " JOIN {$wwCommonsTablePrefix}image as C on C.img_name = I.il_to "; |
384 | | - |
385 | | - $sql .= " WHERE P.page_namespace = " . (int)$ns; |
386 | | - $sql .= " AND P.page_title = " . $this->quote($title); |
387 | | - if ($commonsOnly) $sql .= " AND R.img_name IS NULL"; |
388 | | - |
389 | | - return $this->queryWiki($lang, $sql); |
390 | | - } |
391 | | - |
392 | | - function getImagesOnPage($lang, $ns, $title, $commonsOnly = false) { |
393 | | - $rs = $this->queryImagesOnPage($lang, $ns, $title, $commonsOnly); |
394 | | - |
395 | | - $list = WWUtils::slurpList($rs, "name"); |
396 | | - mysql_free_result($rs); |
397 | | - |
398 | | - return $list; |
399 | | - } |
400 | | - |
401 | | - function queryImagesOnPageTemplates($lang, $ns, $title, $commonsOnly = false) { |
402 | | - global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
403 | | - |
404 | | - if ($lang == "commons") $commonsOnly = false; |
405 | | - |
406 | | - $imagelinks_table = $this->getWikiTableName($lang, "imagelinks"); |
407 | | - $page_table = $this->getWikiTableName($lang, "page"); |
408 | | - $image_table = $this->getWikiTableName($lang, "image"); |
409 | | - $templatelinks_table = $this->getWikiTableName($lang, "templatelinks"); |
410 | | - |
411 | | - $sql = "/* queryImagesOnPageTemplates(" . $this->quote($lang) . ", " . (int)$ns . ", " . $this->quote($title) . ", " . (int)$commonsOnly . ") */ "; |
412 | | - |
413 | | - $sql .= " SELECT I.il_to as name FROM $imagelinks_table as I "; |
414 | | - $sql .= " JOIN $page_table as TP on TP.page_id = I.il_from "; |
415 | | - $sql .= " JOIN $templatelinks_table as T on T.tl_namespace = TP.page_namespace AND T.tl_title = TP.page_title "; |
416 | | - $sql .= " JOIN $page_table as P on P.page_id = T.tl_from "; |
417 | | - if ($commonsOnly) $sql .= " LEFT JOIN $image_table as R on R.img_name = I.il_to "; |
418 | | - if ($commonsOnly) $sql .= " JOIN {$wwCommonsTablePrefix}image as C on C.img_name = I.il_to "; |
419 | | - |
420 | | - $sql .= " WHERE P.page_namespace = " . (int)$ns; |
421 | | - $sql .= " AND P.page_title = " . $this->quote($title); |
422 | | - if ($commonsOnly) $sql .= " AND R.img_name IS NULL"; |
423 | | - |
424 | | - return $this->queryWiki($lang, $sql); |
425 | | - } |
426 | | - |
427 | | - function getImagesOnPageTemplates($lang, $ns, $title, $commonsOnly = false) { |
428 | | - $rs = $this->queryImagesOnPageTemplates($lang, $ns, $title, $commonsOnly); |
429 | | - $list = WWUtils::slurpList($rs, "name"); |
430 | | - mysql_free_result($rs); |
431 | | - return $list; |
432 | | - } |
433 | | - |
434 | | - function queryImagesInCategory($lang, $title) { |
435 | | - global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
436 | | - $categorylinks_table = $this->getWikiTableName($lang, "categorylinks"); |
437 | | - $page_table = $this->getWikiTableName($lang, "page"); |
438 | | - |
439 | | - $sql = "/* queryImagesInCategory(" . $this->quote($lang) . ", " . $this->quote($title) . ") */ "; |
440 | | - |
441 | | - $sql .= " SELECT P.page_title as name FROM $page_table as P "; |
442 | | - $sql .= " JOIN $categorylinks_table as C on C.cl_from = P.page_id "; |
443 | | - |
444 | | - $sql .= " WHERE C.cl_to = " . $this->quote($title); |
445 | | - $sql .= " AND P.page_namespace = " . NS_IMAGE; |
446 | | - |
447 | | - return $this->queryWiki($lang, $sql); |
448 | | - } |
449 | | - |
450 | | - function getImagesInCategory($lang, $title) { |
451 | | - $rs = $this->queryImagesInCategory($lang, $title); |
452 | | - $list = WWUtils::slurpList($rs, "name"); |
453 | | - mysql_free_result($rs); |
454 | | - return $list; |
455 | | - } |
456 | | - |
457 | | - function queryTemplatesOnImagePage($lang, $image) { |
458 | | - global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
459 | | - $page_table = $this->getWikiTableName($lang, "page"); |
460 | | - $templatelinks_table = $this->getWikiTableName($lang, "templatelinks"); |
461 | | - |
462 | | - $sql = "/* queryTemplatesOnImagePage(" . $this->quote($lang) . ", " . $this->quote($image) . ") */ "; |
463 | | - |
464 | | - $sql .= " SELECT tl_title as template FROM $templatelinks_table as T "; |
465 | | - $sql .= " JOIN $page_table as P on P.page_id = T.tl_from AND T.tl_namespace = " . NS_TEMPLATE . " "; |
466 | | - |
467 | | - $sql .= " WHERE P.page_title = " . $this->quote($image); |
468 | | - $sql .= " AND P.page_namespace = " . NS_IMAGE; |
469 | | - |
470 | | - return $this->queryWiki($lang, $sql); |
471 | | - } |
472 | | - |
473 | | - function getTemplatesOnImagePage($lang, $image) { |
474 | | - $rs = $this->queryTemplatesOnImagePage($lang, $image); |
475 | | - $list = WWUtils::slurpList($rs, "template"); |
476 | | - mysql_free_result($rs); |
477 | | - return $list; |
478 | | - } |
479 | | - |
480 | | - function queryCategoriesOfImagePage($lang, $image) { |
481 | | - global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
482 | | - $page_table = $this->getWikiTableName($lang, "page"); |
483 | | - $categorylinks_table = $this->getWikiTableName($lang, "categorylinks"); |
484 | | - |
485 | | - $sql = "/* queryCategoriesOfImagePage(" . $this->quote($lang) . ", " . $this->quote($image) . ") */ "; |
486 | | - |
487 | | - $sql .= " SELECT cl_to as category FROM $categorylinks_table as C "; |
488 | | - $sql .= " JOIN $page_table as P on P.page_id = C.cl_from "; |
489 | | - |
490 | | - $sql .= " WHERE P.page_title = " . $this->quote($image); |
491 | | - $sql .= " AND P.page_namespace = " . NS_IMAGE; |
492 | | - |
493 | | - return $this->queryWiki($lang, $sql); |
494 | | - } |
495 | | - |
496 | | - function getCategoriesOfImagePage($lang, $image) { |
497 | | - $rs = $this->queryCategoriesOfImagePage($lang, $image); |
498 | | - $list = WWUtils::slurpList($rs, "category"); |
499 | | - mysql_free_result($rs); |
500 | | - return $list; |
501 | | - } |
502 | | - |
503 | | - function getTemplateScores($templates, $values = NULL) { |
504 | | - global $wwWikiServerName; |
505 | | - if ($values === NULL) $values = $wwTemplateScores; |
506 | | - |
507 | | - if (!$values) return 0; |
508 | | - |
509 | | - $score = 0; |
510 | | - foreach ($templates as $t) { |
511 | | - $v = @$values[$t]; |
512 | | - if ($v) $score += $v; |
513 | | - } |
514 | | - |
515 | | - return $score; |
516 | | - } |
517 | | - |
518 | | - function getRelevantImagesOnPage($lang, $ns, $title, $commonsOnly = false) { |
519 | | - $img = $this->getImagesOnPage($lang, 0, $title, true); |
520 | | - $timg = $this->getImagesOnPageTemplates($lang, 0, $title, true); |
521 | | - $img = array_diff($img, $timg); |
522 | | - return $img; |
523 | | - } |
524 | | - |
525 | | - function getImagesAbout($id, $max = 0) { |
526 | | - global $wwFakeCommonsConcepts, $wwFakeCommonsPlural, $wwLanguages; |
527 | | - |
528 | | - $concepts = $this->getLocalConcepts($id); |
529 | | - |
530 | | - if ($wwFakeCommonsConcepts && isset($concepts['en'])) { |
531 | | - $concepts['commons'] = @$concepts['en']; |
532 | | - } |
533 | | - |
534 | | - $images = new ImageCollection(); |
535 | | - |
536 | | - foreach ($concepts as $lang => $title) { |
537 | | - if ($lang == "commons") continue; |
538 | | - if (!isset($wwLanguages[$lang])) continue; |
539 | | - |
540 | | - $img = $this->getRelevantImagesOnPage($lang, 0, $title, true); //FIXME: resource mapping |
541 | | - $images->addImages($img, $lang . ":" . $title, "article", 1); |
542 | | - } |
543 | | - |
544 | | - if ($max && $images->size()>$max) { |
545 | | - $this->addImageTags($images); |
546 | | - return $images->listImages($max); |
547 | | - } |
548 | | - |
549 | | - if (isset($concepts['commons'])) { |
550 | | - $title = $concepts['commons']; |
551 | | - |
552 | | - $img = $this->getRelevantImagesOnPage("commons", 0, $title, false); //FIXME: resource mapping |
553 | | - $images->addImages($img, "commons:" . $title, "gallery", 0.8); |
554 | | - |
555 | | - if ($max && $images->size()>$max) { |
556 | | - $this->addImageTags($images); |
557 | | - return $images->listImages($max); |
558 | | - } |
559 | | - |
560 | | - $img = $this->getImagesInCategory("commons", $title); //FIXME: resource mapping |
561 | | - if ($img) $images->addImages($img, "commons:category:" . $title, "category", 0.5); |
562 | | - else if ($wwFakeCommonsConcepts && $wwFakeCommonsPlural && !preg_match('/s$/', $title)) { |
563 | | - $cname = $title."s"; |
564 | | - |
565 | | - $img = $this->getImagesInCategory("commons", $cname); //FIXME: resource mapping |
566 | | - $images->addImages($img, "commons:category:" . $cname, "category(pl)", 0.5); |
567 | | - } |
568 | | - } |
569 | | - |
570 | | - $this->addImageTags($images); |
571 | | - return $images->listImages($max); |
572 | | - } |
573 | | - |
574 | | - function addImageTags($images) { |
575 | | - foreach ($images->images as $image) { |
576 | | - $image = $image['name']; |
577 | | - |
578 | | - $tags = $this->getTemplatesOnImagePage('commons', $image); |
579 | | - $images->addTags($image, $tags, "Template:"); |
580 | | - |
581 | | - $cats = $this->getCategoriesOfImagePage('commons', $image); |
582 | | - $images->addTags($image, $cats, "Category:"); |
583 | | - } |
584 | | - } |
585 | | - |
586 | | - function getThumbnailURL($image, $width = 120, $height = NULL) { |
587 | | - global $wwThumbnailURL; |
588 | | - |
589 | | - if (is_array($image)) $image = $image['name']; |
590 | | - |
591 | | - if (!$height) $height = $width; |
592 | | - |
593 | | - $u = $wwThumbnailURL; |
594 | | - $u = str_replace("{name}", urlencode($image), $u); |
595 | | - $u = str_replace("{width}", !$width ? "" : urlencode($width), $u); |
596 | | - $u = str_replace("{height}", !$height ? "" : urlencode($height), $u); |
597 | | - |
598 | | - return $u; |
599 | | - } |
600 | | - |
601 | | - function getImagePageURL($image) { |
602 | | - global $wwImagePageURL; |
603 | | - |
604 | | - if (is_array($image)) $image = $image['name']; |
605 | | - |
606 | | - $u = $wwImagePageURL; |
607 | | - $u = str_replace("{name}", urlencode($image), $u); |
608 | | - |
609 | | - return $u; |
610 | | - } |
611 | | - |
612 | | - function getThumbnailHTML($image, $w = 120, $h = NULL) { |
613 | | - $thumb = $this->getThumbnailURL($image, $w, $h); |
614 | | - $page = $this->getImagePageURL($image); |
615 | | - |
616 | | - if (is_array($image)) { |
617 | | - $title = @$image['title']; |
618 | | - $name = @$image['name']; |
619 | | - } else { |
620 | | - $name = $image; |
621 | | - } |
622 | | - |
623 | | - if (!@$title) $title = $name; |
624 | | - |
625 | | - $tags = ""; |
626 | | - if (isset($image['tags'])) { |
627 | | - foreach ($image['tags'] as $tag) { |
628 | | - $tags .= "tag-" . str_replace(":", "-", $tag) . " "; |
629 | | - } |
630 | | - } |
631 | | - |
632 | | - $html= "<img src=\"" . htmlspecialchars($thumb) . "\" alt=\"" . htmlspecialchars($title) . "\" border=\"0\"/>"; |
633 | | - $html= "<a href=\"" . htmlspecialchars($page) . "\" title=\"" . htmlspecialchars($title) . " (score " . htmlspecialchars($image['score']) . ")\" class=\"thumb-link $tags\">$html</a>"; |
634 | | - |
635 | | - if (is_array($image)) { |
636 | | - $html .= "<!-- " . str_replace("--", "~~", var_export( $image, true ) ) . " -->"; |
637 | | - } |
638 | | - |
639 | | - return $html; |
640 | | - } |
641 | 118 | } |
Index: trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php |
— | — | @@ -0,0 +1,122 @@ |
| 2 | +<?php |
| 3 | +require_once(dirname(__FILE__)."/wwutils.php"); |
| 4 | + |
| 5 | +class WWThesaurus extends WWUTils { |
| 6 | + |
| 7 | + function queryConceptsForTerm($lang, $term) { |
| 8 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 9 | + |
| 10 | + $term = trim($term); |
| 11 | + |
| 12 | + $sql = "SELECT M.*, O.*, definition FROM {$wwTablePrefix}_{$lang}_meaning as M" |
| 13 | + . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON M.concept = D.concept " |
| 14 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND M.concept = O.local_concept " |
| 15 | + . " WHERE term_text = \"" . mysql_real_escape_string($term) . "\"" |
| 16 | + . " ORDER BY freq DESC " |
| 17 | + . " LIMIT 100"; |
| 18 | + |
| 19 | + return $this->query($sql); |
| 20 | + } |
| 21 | + |
| 22 | + function queryLocalConcepts($id) { |
| 23 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 24 | + $sql = "SELECT O.lang, O.local_concept_name from {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O "; |
| 25 | + $sql .= " WHERE O.global_concept = " . (int)$id; |
| 26 | + |
| 27 | + return $this->query($sql); |
| 28 | + } |
| 29 | + |
| 30 | + function getLocalConcepts($id) { |
| 31 | + $rs = $this->queryLocalConcepts($id); |
| 32 | + $list = WWUtils::slurpAssoc($rs, "lang", "local_concept_name"); |
| 33 | + mysql_free_result($rs); |
| 34 | + return $list; |
| 35 | + } |
| 36 | + |
| 37 | + function queryLocalConceptInfo($lang, $id) { |
| 38 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 39 | + |
| 40 | + $sql = "SELECT O.*, C.*, F.*, definition FROM {$wwTablePrefix}_{$lang}_concept_info as F " |
| 41 | + . " JOIN {$wwTablePrefix}_{$lang}_concept as C ON F.concept = C.id " |
| 42 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND F.concept = O.local_concept " |
| 43 | + . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON F.concept = D.concept " |
| 44 | + . " WHERE O.local_concept = $id "; |
| 45 | + |
| 46 | + return $this->query($sql); |
| 47 | + } |
| 48 | + |
| 49 | + function queryConceptInfo($id, $lang) { |
| 50 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 51 | + |
| 52 | + $sql = "SELECT O.*, C.*, F.*, definition FROM {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O " |
| 53 | + . " LEFT JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_concept_info as F ON O.global_concept = F.concept " |
| 54 | + . " LEFT JOIN {$wwTablePrefix}_{$lang}_concept as C ON O.local_concept = C.id " |
| 55 | + . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON O.local_concept = D.concept " |
| 56 | + . " WHERE O.global_concept = $id AND O.lang = \"" . mysql_real_escape_string($lang) . "\" "; |
| 57 | + |
| 58 | + return $this->query($sql); |
| 59 | + } |
| 60 | + |
| 61 | + function unpickle($s, $lang, $hasId=true, $hasName=true, $hasConf=true) { |
| 62 | + $ss = explode("\x1E", $s); |
| 63 | + $items = array(); |
| 64 | + |
| 65 | + $fetchNames = false; |
| 66 | + |
| 67 | + foreach ($ss as $i) { |
| 68 | + $r = explode("\x1F", $i); |
| 69 | + $offs = -1; |
| 70 | + |
| 71 | + if ($hasId) $r['id'] = @$r[$offs += 1]; |
| 72 | + if ($hasName) $r['name'] = @$r[$offs += 1]; |
| 73 | + if ($hasConf) $r['conf'] = @$r[$offs += 1]; |
| 74 | + |
| 75 | + if ($hasId && !isset($r['name'])) |
| 76 | + $fetchNames = true; |
| 77 | + |
| 78 | + if ($hasId) $items[ $r['id'] ] = $r; |
| 79 | + else $items[] = $r; |
| 80 | + } |
| 81 | + |
| 82 | + if ($fetchNames) { |
| 83 | + $names = $this->fetchNames(array_keys($items), $lang); |
| 84 | + |
| 85 | + $keys = array_keys($items); |
| 86 | + foreach ($keys as $k) { |
| 87 | + $id = $items[$k]['id']; |
| 88 | + $items[$k]['name'] = $names[$id]; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return $items; |
| 93 | + } |
| 94 | + |
| 95 | + function fetchNames($ids, $lang) { |
| 96 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 97 | + |
| 98 | + $names = array(); |
| 99 | + if (!$ids) return $names; |
| 100 | + |
| 101 | + $set = NULL; |
| 102 | + foreach ($ids as $id) { |
| 103 | + if ($set===NULL) $set = ""; |
| 104 | + else $set .= ", "; |
| 105 | + $set .= $id; |
| 106 | + } |
| 107 | + |
| 108 | + $sql = "select global_concept as id, local_concept_name as name from {$wwTablePrefix}_{$wwThesaurusDataset}_origin "; |
| 109 | + $sql .= "where global_concept in ($set) and lang = \"" . mysql_real_escape_string($lang) . "\" "; |
| 110 | + |
| 111 | + $res = $this->query($sql); |
| 112 | + |
| 113 | + while ($row = mysql_fetch_assoc($res)) { |
| 114 | + $id = $row['id']; |
| 115 | + $names[$id] = $row['name']; |
| 116 | + } |
| 117 | + |
| 118 | + mysql_free_result($res); |
| 119 | + |
| 120 | + return $names; |
| 121 | + } |
| 122 | + |
| 123 | +} |
Property changes on: trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php |
___________________________________________________________________ |
Name: svn:mergeinfo |
1 | 124 | + |
Index: trunk/WikiWord/WikiWord/src/main/php/wwimages.php |
— | — | @@ -0,0 +1,354 @@ |
| 2 | +<?php |
| 3 | +require_once(dirname(__FILE__)."/wwwikis.php"); |
| 4 | + |
| 5 | +class ImageCollection { |
| 6 | + |
| 7 | + function __construct() { |
| 8 | + $this->images = array(); |
| 9 | + } |
| 10 | + |
| 11 | + static function compareRecords($a, $b) { |
| 12 | + $d = (float)$b['score'] - (float)$a['score']; //NOTE: descending |
| 13 | + |
| 14 | + if ( $d > 0 ) return 1; |
| 15 | + else if ( $d < 0 ) return -1; |
| 16 | + else return 0; |
| 17 | + } |
| 18 | + |
| 19 | + function size() { |
| 20 | + return count($this->images); |
| 21 | + } |
| 22 | + |
| 23 | + function listImages($max) { |
| 24 | + uasort($this->images, "Imagecollection::compareRecords"); |
| 25 | + |
| 26 | + if ($max) return array_slice($this->images, 0, $max); |
| 27 | + else return $this->images; |
| 28 | + } |
| 29 | + |
| 30 | + function addImage($image, $key, $usage = "page", $weight = 1) { |
| 31 | + if (!isset($this->images[$image])) { |
| 32 | + $rec = array( |
| 33 | + "name" => $image, |
| 34 | + "score" => 0, |
| 35 | + ); |
| 36 | + } else { |
| 37 | + $rec = $this->images[$image]; |
| 38 | + } |
| 39 | + |
| 40 | + if (!isset($rec[$usage])) $rec[$usage] = array(); |
| 41 | + $rec[$usage][] = $key; |
| 42 | + $rec["score"] += $weight; |
| 43 | + |
| 44 | + $this->images[$image] = $rec; |
| 45 | + return $rec['score']; |
| 46 | + } |
| 47 | + |
| 48 | + function addImages($images, $key, $usage = "page", $weight = 1) { |
| 49 | + foreach ($images as $image) { |
| 50 | + $this->addImage($image, $key, $usage, $weight); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + function addTags($image, $tags, $prefix = "") { |
| 55 | + global $wwTagScores; |
| 56 | + |
| 57 | + if (isset($this->images[$image])) { |
| 58 | + foreach ($tags as $tag => $weight) { |
| 59 | + if (is_int($tag)) { |
| 60 | + $tag = $prefix.$weight; |
| 61 | + |
| 62 | + if (isset($wwTagScores[$tag])) $weight = $wwTagScores[$tag]; |
| 63 | + else continue; |
| 64 | + } else { |
| 65 | + $tag = $prefix.$tag; |
| 66 | + } |
| 67 | + |
| 68 | + $this->images[$image]['score'] += $weight; |
| 69 | + $this->images[$image]['tags'][] = $tag; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +class WWImages extends WWUtils { |
| 77 | + var $thesaurus; |
| 78 | + |
| 79 | + function __construct($thesaurus) { |
| 80 | + $this->thesaurus = $thesaurus; |
| 81 | + } |
| 82 | + |
| 83 | + function queryImagesOnPage($lang, $ns, $title, $commonsOnly = false) { |
| 84 | + global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
| 85 | + |
| 86 | + if ($lang == "commons") $commonsOnly = false; |
| 87 | + |
| 88 | + $imagelinks_table = $this->getWikiTableName($lang, "imagelinks"); |
| 89 | + $page_table = $this->getWikiTableName($lang, "page"); |
| 90 | + $image_table = $this->getWikiTableName($lang, "image"); |
| 91 | + |
| 92 | + $sql = "/* queryImagesOnPage(" . $this->quote($lang) . ", " . (int)$ns . ", " . $this->quote($title) . ", " . (int)$commonsOnly . ") */ "; |
| 93 | + |
| 94 | + $sql .= " SELECT I.il_to as name FROM $imagelinks_table as I "; |
| 95 | + $sql .= " JOIN $page_table as P on P.page_id = I.il_from "; |
| 96 | + if ($commonsOnly) $sql .= " LEFT JOIN $image_table as R on R.img_name = I.il_to "; |
| 97 | + if ($commonsOnly) $sql .= " JOIN {$wwCommonsTablePrefix}image as C on C.img_name = I.il_to "; |
| 98 | + |
| 99 | + $sql .= " WHERE P.page_namespace = " . (int)$ns; |
| 100 | + $sql .= " AND P.page_title = " . $this->quote($title); |
| 101 | + if ($commonsOnly) $sql .= " AND R.img_name IS NULL"; |
| 102 | + |
| 103 | + return $this->queryWiki($lang, $sql); |
| 104 | + } |
| 105 | + |
| 106 | + function getImagesOnPage($lang, $ns, $title, $commonsOnly = false) { |
| 107 | + $rs = $this->queryImagesOnPage($lang, $ns, $title, $commonsOnly); |
| 108 | + |
| 109 | + $list = WWUtils::slurpList($rs, "name"); |
| 110 | + mysql_free_result($rs); |
| 111 | + |
| 112 | + return $list; |
| 113 | + } |
| 114 | + |
| 115 | + function queryImagesOnPageTemplates($lang, $ns, $title, $commonsOnly = false) { |
| 116 | + global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
| 117 | + |
| 118 | + if ($lang == "commons") $commonsOnly = false; |
| 119 | + |
| 120 | + $imagelinks_table = $this->getWikiTableName($lang, "imagelinks"); |
| 121 | + $page_table = $this->getWikiTableName($lang, "page"); |
| 122 | + $image_table = $this->getWikiTableName($lang, "image"); |
| 123 | + $templatelinks_table = $this->getWikiTableName($lang, "templatelinks"); |
| 124 | + |
| 125 | + $sql = "/* queryImagesOnPageTemplates(" . $this->quote($lang) . ", " . (int)$ns . ", " . $this->quote($title) . ", " . (int)$commonsOnly . ") */ "; |
| 126 | + |
| 127 | + $sql .= " SELECT I.il_to as name FROM $imagelinks_table as I "; |
| 128 | + $sql .= " JOIN $page_table as TP on TP.page_id = I.il_from "; |
| 129 | + $sql .= " JOIN $templatelinks_table as T on T.tl_namespace = TP.page_namespace AND T.tl_title = TP.page_title "; |
| 130 | + $sql .= " JOIN $page_table as P on P.page_id = T.tl_from "; |
| 131 | + if ($commonsOnly) $sql .= " LEFT JOIN $image_table as R on R.img_name = I.il_to "; |
| 132 | + if ($commonsOnly) $sql .= " JOIN {$wwCommonsTablePrefix}image as C on C.img_name = I.il_to "; |
| 133 | + |
| 134 | + $sql .= " WHERE P.page_namespace = " . (int)$ns; |
| 135 | + $sql .= " AND P.page_title = " . $this->quote($title); |
| 136 | + if ($commonsOnly) $sql .= " AND R.img_name IS NULL"; |
| 137 | + |
| 138 | + return $this->queryWiki($lang, $sql); |
| 139 | + } |
| 140 | + |
| 141 | + function getImagesOnPageTemplates($lang, $ns, $title, $commonsOnly = false) { |
| 142 | + $rs = $this->queryImagesOnPageTemplates($lang, $ns, $title, $commonsOnly); |
| 143 | + $list = WWUtils::slurpList($rs, "name"); |
| 144 | + mysql_free_result($rs); |
| 145 | + return $list; |
| 146 | + } |
| 147 | + |
| 148 | + function queryImagesInCategory($lang, $title) { |
| 149 | + global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
| 150 | + $categorylinks_table = $this->getWikiTableName($lang, "categorylinks"); |
| 151 | + $page_table = $this->getWikiTableName($lang, "page"); |
| 152 | + |
| 153 | + $sql = "/* queryImagesInCategory(" . $this->quote($lang) . ", " . $this->quote($title) . ") */ "; |
| 154 | + |
| 155 | + $sql .= " SELECT P.page_title as name FROM $page_table as P "; |
| 156 | + $sql .= " JOIN $categorylinks_table as C on C.cl_from = P.page_id "; |
| 157 | + |
| 158 | + $sql .= " WHERE C.cl_to = " . $this->quote($title); |
| 159 | + $sql .= " AND P.page_namespace = " . NS_IMAGE; |
| 160 | + |
| 161 | + return $this->queryWiki($lang, $sql); |
| 162 | + } |
| 163 | + |
| 164 | + function getImagesInCategory($lang, $title) { |
| 165 | + $rs = $this->queryImagesInCategory($lang, $title); |
| 166 | + $list = WWUtils::slurpList($rs, "name"); |
| 167 | + mysql_free_result($rs); |
| 168 | + return $list; |
| 169 | + } |
| 170 | + |
| 171 | + function queryTemplatesOnImagePage($lang, $image) { |
| 172 | + global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
| 173 | + $page_table = $this->getWikiTableName($lang, "page"); |
| 174 | + $templatelinks_table = $this->getWikiTableName($lang, "templatelinks"); |
| 175 | + |
| 176 | + $sql = "/* queryTemplatesOnImagePage(" . $this->quote($lang) . ", " . $this->quote($image) . ") */ "; |
| 177 | + |
| 178 | + $sql .= " SELECT tl_title as template FROM $templatelinks_table as T "; |
| 179 | + $sql .= " JOIN $page_table as P on P.page_id = T.tl_from AND T.tl_namespace = " . NS_TEMPLATE . " "; |
| 180 | + |
| 181 | + $sql .= " WHERE P.page_title = " . $this->quote($image); |
| 182 | + $sql .= " AND P.page_namespace = " . NS_IMAGE; |
| 183 | + |
| 184 | + return $this->queryWiki($lang, $sql); |
| 185 | + } |
| 186 | + |
| 187 | + function getTemplatesOnImagePage($lang, $image) { |
| 188 | + $rs = $this->queryTemplatesOnImagePage($lang, $image); |
| 189 | + $list = WWUtils::slurpList($rs, "template"); |
| 190 | + mysql_free_result($rs); |
| 191 | + return $list; |
| 192 | + } |
| 193 | + |
| 194 | + function queryCategoriesOfImagePage($lang, $image) { |
| 195 | + global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix; |
| 196 | + $page_table = $this->getWikiTableName($lang, "page"); |
| 197 | + $categorylinks_table = $this->getWikiTableName($lang, "categorylinks"); |
| 198 | + |
| 199 | + $sql = "/* queryCategoriesOfImagePage(" . $this->quote($lang) . ", " . $this->quote($image) . ") */ "; |
| 200 | + |
| 201 | + $sql .= " SELECT cl_to as category FROM $categorylinks_table as C "; |
| 202 | + $sql .= " JOIN $page_table as P on P.page_id = C.cl_from "; |
| 203 | + |
| 204 | + $sql .= " WHERE P.page_title = " . $this->quote($image); |
| 205 | + $sql .= " AND P.page_namespace = " . NS_IMAGE; |
| 206 | + |
| 207 | + return $this->queryWiki($lang, $sql); |
| 208 | + } |
| 209 | + |
| 210 | + function getCategoriesOfImagePage($lang, $image) { |
| 211 | + $rs = $this->queryCategoriesOfImagePage($lang, $image); |
| 212 | + $list = WWUtils::slurpList($rs, "category"); |
| 213 | + mysql_free_result($rs); |
| 214 | + return $list; |
| 215 | + } |
| 216 | + |
| 217 | + function getTemplateScores($templates, $values = NULL) { |
| 218 | + global $wwWikiServerName; |
| 219 | + if ($values === NULL) $values = $wwTemplateScores; |
| 220 | + |
| 221 | + if (!$values) return 0; |
| 222 | + |
| 223 | + $score = 0; |
| 224 | + foreach ($templates as $t) { |
| 225 | + $v = @$values[$t]; |
| 226 | + if ($v) $score += $v; |
| 227 | + } |
| 228 | + |
| 229 | + return $score; |
| 230 | + } |
| 231 | + |
| 232 | + function getRelevantImagesOnPage($lang, $ns, $title, $commonsOnly = false) { |
| 233 | + $img = $this->getImagesOnPage($lang, 0, $title, true); |
| 234 | + $timg = $this->getImagesOnPageTemplates($lang, 0, $title, true); |
| 235 | + $img = array_diff($img, $timg); |
| 236 | + return $img; |
| 237 | + } |
| 238 | + |
| 239 | + function getImagesAbout($id, $max = 0) { |
| 240 | + global $wwFakeCommonsConcepts, $wwFakeCommonsPlural, $wwLanguages; |
| 241 | + |
| 242 | + $concepts = $this->thesaurus->getLocalConcepts($id); |
| 243 | + |
| 244 | + if ($wwFakeCommonsConcepts && isset($concepts['en'])) { |
| 245 | + $concepts['commons'] = @$concepts['en']; |
| 246 | + } |
| 247 | + |
| 248 | + $images = new ImageCollection(); |
| 249 | + |
| 250 | + foreach ($concepts as $lang => $title) { |
| 251 | + if ($lang == "commons") continue; |
| 252 | + if (!isset($wwLanguages[$lang])) continue; |
| 253 | + |
| 254 | + $img = $this->getRelevantImagesOnPage($lang, 0, $title, true); //FIXME: resource mapping |
| 255 | + $images->addImages($img, $lang . ":" . $title, "article", 1); |
| 256 | + } |
| 257 | + |
| 258 | + if ($max && $images->size()>$max) { |
| 259 | + $this->addImageTags($images); |
| 260 | + return $images->listImages($max); |
| 261 | + } |
| 262 | + |
| 263 | + if (isset($concepts['commons'])) { |
| 264 | + $title = $concepts['commons']; |
| 265 | + |
| 266 | + $img = $this->getRelevantImagesOnPage("commons", 0, $title, false); //FIXME: resource mapping |
| 267 | + $images->addImages($img, "commons:" . $title, "gallery", 0.8); |
| 268 | + |
| 269 | + if ($max && $images->size()>$max) { |
| 270 | + $this->addImageTags($images); |
| 271 | + return $images->listImages($max); |
| 272 | + } |
| 273 | + |
| 274 | + $img = $this->getImagesInCategory("commons", $title); //FIXME: resource mapping |
| 275 | + if ($img) $images->addImages($img, "commons:category:" . $title, "category", 0.5); |
| 276 | + else if ($wwFakeCommonsConcepts && $wwFakeCommonsPlural && !preg_match('/s$/', $title)) { |
| 277 | + $cname = $title."s"; |
| 278 | + |
| 279 | + $img = $this->getImagesInCategory("commons", $cname); //FIXME: resource mapping |
| 280 | + $images->addImages($img, "commons:category:" . $cname, "category(pl)", 0.5); |
| 281 | + } |
| 282 | + } |
| 283 | + |
| 284 | + $this->addImageTags($images); |
| 285 | + return $images->listImages($max); |
| 286 | + } |
| 287 | + |
| 288 | + function addImageTags($images) { |
| 289 | + foreach ($images->images as $image) { |
| 290 | + $image = $image['name']; |
| 291 | + |
| 292 | + $tags = $this->getTemplatesOnImagePage('commons', $image); |
| 293 | + $images->addTags($image, $tags, "Template:"); |
| 294 | + |
| 295 | + $cats = $this->getCategoriesOfImagePage('commons', $image); |
| 296 | + $images->addTags($image, $cats, "Category:"); |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + function getThumbnailURL($image, $width = 120, $height = NULL) { |
| 301 | + global $wwThumbnailURL; |
| 302 | + |
| 303 | + if (is_array($image)) $image = $image['name']; |
| 304 | + |
| 305 | + if (!$height) $height = $width; |
| 306 | + |
| 307 | + $u = $wwThumbnailURL; |
| 308 | + $u = str_replace("{name}", urlencode($image), $u); |
| 309 | + $u = str_replace("{width}", !$width ? "" : urlencode($width), $u); |
| 310 | + $u = str_replace("{height}", !$height ? "" : urlencode($height), $u); |
| 311 | + |
| 312 | + return $u; |
| 313 | + } |
| 314 | + |
| 315 | + function getImagePageURL($image) { |
| 316 | + global $wwImagePageURL; |
| 317 | + |
| 318 | + if (is_array($image)) $image = $image['name']; |
| 319 | + |
| 320 | + $u = $wwImagePageURL; |
| 321 | + $u = str_replace("{name}", urlencode($image), $u); |
| 322 | + |
| 323 | + return $u; |
| 324 | + } |
| 325 | + |
| 326 | + function getThumbnailHTML($image, $w = 120, $h = NULL) { |
| 327 | + $thumb = $this->getThumbnailURL($image, $w, $h); |
| 328 | + $page = $this->getImagePageURL($image); |
| 329 | + |
| 330 | + if (is_array($image)) { |
| 331 | + $title = @$image['title']; |
| 332 | + $name = @$image['name']; |
| 333 | + } else { |
| 334 | + $name = $image; |
| 335 | + } |
| 336 | + |
| 337 | + if (!@$title) $title = $name; |
| 338 | + |
| 339 | + $tags = ""; |
| 340 | + if (isset($image['tags'])) { |
| 341 | + foreach ($image['tags'] as $tag) { |
| 342 | + $tags .= "tag-" . str_replace(":", "-", $tag) . " "; |
| 343 | + } |
| 344 | + } |
| 345 | + |
| 346 | + $html= "<img src=\"" . htmlspecialchars($thumb) . "\" alt=\"" . htmlspecialchars($title) . "\" border=\"0\"/>"; |
| 347 | + $html= "<a href=\"" . htmlspecialchars($page) . "\" title=\"" . htmlspecialchars($title) . " (score " . htmlspecialchars($image['score']) . ")\" class=\"thumb-link $tags\">$html</a>"; |
| 348 | + |
| 349 | + if (is_array($image)) { |
| 350 | + $html .= "<!-- " . str_replace("--", "~~", var_export( $image, true ) ) . " -->"; |
| 351 | + } |
| 352 | + |
| 353 | + return $html; |
| 354 | + } |
| 355 | +} |
Property changes on: trunk/WikiWord/WikiWord/src/main/php/wwimages.php |
___________________________________________________________________ |
Name: svn:mergeinfo |
1 | 356 | + |
Index: trunk/WikiWord/WikiWord/src/main/php/wwwikis.php |
— | — | @@ -0,0 +1,70 @@ |
| 2 | +<?php |
| 3 | +require_once(dirname(__FILE__)."/wwutils.php"); |
| 4 | + |
| 5 | +if (!defined('NS_IMAGE')) |
| 6 | + define('NS_IMAGE', 6); |
| 7 | + |
| 8 | +if (!defined('NS_TEMPLATE')) |
| 9 | + define('NS_TEMPLATE', 10); |
| 10 | + |
| 11 | + |
| 12 | +class WWWikis extends WWUtils { |
| 13 | + var $wikidbs = array(); |
| 14 | + |
| 15 | + function getWikiTableName($lang, $table) { |
| 16 | + global $wwWikitableNamePattern; |
| 17 | + |
| 18 | + if ($wwWikitableNamePattern) { |
| 19 | + return str_replace(array('{lang}', '{name}'), array($lang, $table), $wwWikitableNamePattern); |
| 20 | + } |
| 21 | + |
| 22 | + return $table; |
| 23 | + } |
| 24 | + |
| 25 | + function getWikiInfo($lang) { |
| 26 | + global $wwWikiInfoTable, $wwWikiDbName, $wwWikiServerName, $wwCommonsServerName; |
| 27 | + |
| 28 | + $db = str_replace('{lang}', $lang, $wwWikiDbName); |
| 29 | + |
| 30 | + $dbname = "{$lang}wiki_p"; |
| 31 | + $sql = "select * from $wwWikiInfoTable "; |
| 32 | + $sql .= " where dbname = " . $this->quote("$db"); |
| 33 | + |
| 34 | + $rs = $this->query($sql); |
| 35 | + $info = mysql_fetch_assoc($rs); |
| 36 | + mysql_free_result($rs); |
| 37 | + |
| 38 | + if (!$info) $info = false; |
| 39 | + else $info['server'] = str_replace('{num}', $info['server'], $wwWikiServerName); |
| 40 | + |
| 41 | + if ($lang == "commons" && $wwCommonsServerName) $info['server'] = $wwCommonsServerName; |
| 42 | + |
| 43 | + return $info; |
| 44 | + } |
| 45 | + |
| 46 | + function getWikiConnection($lang) { |
| 47 | + if (isset($this->wikidbs[$lang])) return $this->wikidbs[$lang]; |
| 48 | + |
| 49 | + $info = $this->getWikiInfo($lang); |
| 50 | + |
| 51 | + if (!$info) { |
| 52 | + $db = false; |
| 53 | + } else { |
| 54 | + $db = mysql_connect($info['server'], $this->dbuser, $this->dbpassword); |
| 55 | + if (!$db) throw new Exception("Connection Failure to Database: " . mysql_error()); |
| 56 | + if (!mysql_select_db($info['dbname'], $db)) throw new Exception ("Database not found: " . mysql_error()); |
| 57 | + if (!mysql_query("SET NAMES Latin1;", $db)) throw new Exception ("Database not found: " . mysql_error()); |
| 58 | + } |
| 59 | + |
| 60 | + $this->wikidbs[$lang] = $db; |
| 61 | + return $db; |
| 62 | + } |
| 63 | + |
| 64 | + function queryWiki($lang, $sql) { |
| 65 | + $db = $this->getWikiConnection($lang); |
| 66 | + if (!$db) throw new Exception ("Wiki not found: $lang"); |
| 67 | + |
| 68 | + return $this->query($sql, $db); |
| 69 | + } |
| 70 | + |
| 71 | +} |
Property changes on: trunk/WikiWord/WikiWord/src/main/php/wwwikis.php |
___________________________________________________________________ |
Name: svn:mergeinfo |
1 | 72 | + |