Index: trunk/WikiWord/WikiWord/src/main/php/api.php |
— | — | @@ -12,8 +12,12 @@ |
13 | 13 | $format = @$_REQUEST['format']; |
14 | 14 | if ( !$format ) $format = 'phps'; |
15 | 15 | |
| 16 | + if ($lang) { |
| 17 | + $lang = preg_replace('[^\\w\\d_]', '', $lang); |
| 18 | + } |
| 19 | + |
16 | 20 | $result = array( 'query' => $query ); |
17 | | - $start = microtime(); |
| 21 | + $start = microtime(true); |
18 | 22 | |
19 | 23 | try { |
20 | 24 | $thesaurus = new WWThesaurus(); |
— | — | @@ -26,12 +30,12 @@ |
27 | 31 | $page = @$_REQUEST['page']; |
28 | 32 | |
29 | 33 | if ( $lang === null ) $result['error'] = array('code' => 150, 'message' => "missing parameter lang"); |
30 | | - else if ( $term !=== null ) { |
| 34 | + else if ( $term !== null ) { |
31 | 35 | $result['concepts'] = $thesaurus->getConceptsForTerm($lang, $term); |
32 | 36 | if ( $result['concepts'] === false || $result['concepts'] === null ) { |
33 | 37 | $result['error'] = array('code' => 210, 'message' => "failed to retrieve concepts for term $langt:$term"); |
34 | 38 | } |
35 | | - } else if ( $page !=== null ) { |
| 39 | + } else if ( $page !== null ) { |
36 | 40 | $result['concepts'] = $thesaurus->getConceptsForPage($lang, $page); |
37 | 41 | if ( $result['concepts'] === false || $result['concepts'] === null ) { |
38 | 42 | $result['error'] = array('code' => 250, 'message' => "failed to retrieve concepts for page $langt:$page"); |
— | — | @@ -39,7 +43,7 @@ |
40 | 44 | } else { |
41 | 45 | $result['error'] = array('code' => 110, 'message' => "missing parameter term"); |
42 | 46 | } |
43 | | - } if ($query == 'properties') else { |
| 47 | + } else if ($query == 'properties') { |
44 | 48 | $gcid = @$_REQUEST['gcid']; |
45 | 49 | $props = @$_REQUEST['props']; |
46 | 50 | |
— | — | @@ -49,7 +53,7 @@ |
50 | 54 | $props = preg_split('![\\s,;|/:]\\s*!', $props); |
51 | 55 | |
52 | 56 | foreach ( $props as $p ) { |
53 | | - $m = "get" . ucfist($p) . "ForConcept"; |
| 57 | + $m = "get" . ucfirst($p) . "ForConcept"; |
54 | 58 | if ( !method_exists($thesaurus, $m) ) { |
55 | 59 | $result['error'] = array('code' => 190, 'message' => "unknown property: $p"); |
56 | 60 | break; |
— | — | @@ -66,11 +70,11 @@ |
67 | 71 | } else { |
68 | 72 | $result['error'] = array('code' => 10, 'message' => "bad query: $query"); |
69 | 73 | } |
70 | | - } catch (Exception e) { |
| 74 | + } catch (Exception $e) { |
71 | 75 | $result['error'] = array('code' => 1000, 'message' => "unexpected exception: " . $e->getMessage()); |
72 | 76 | } |
73 | 77 | |
74 | | - $result['time'] = (microtime() - $start) . "ms"; |
| 78 | + $result['time'] = (microtime(true) - $start) . " sec"; |
75 | 79 | |
76 | 80 | if ( isset($result['error']) ) { |
77 | 81 | #TODO: HTTP error codce would be nice, but causes file_get_contents to swallow the data. |
— | — | @@ -84,13 +88,13 @@ |
85 | 89 | $data = serialize($result); |
86 | 90 | echo $data; |
87 | 91 | } else if ($format == 'php') { |
88 | | - header("Content-Type: text/php"); |
| 92 | + header("Content-Type: text/php; charset=UTF-8"); |
89 | 93 | var_export($result); |
90 | 94 | } else if ($format == 'text') { |
91 | | - header("Content-Type: text/plain"); |
| 95 | + header("Content-Type: text/plain; charset=UTF-8"); |
92 | 96 | print_r($result); |
93 | 97 | } else { |
94 | | - header("Content-Type: text/plain"); |
| 98 | + header("Content-Type: text/plain; charset=UTF-8"); |
95 | 99 | header("Status: 400 Bad Request", true, 400); |
96 | 100 | echo "Bad format: $format"; |
97 | 101 | } |
— | — | @@ -98,6 +102,6 @@ |
99 | 103 | exit(); |
100 | 104 | } |
101 | 105 | |
102 | | -header("Content-Type: text/plain"); |
| 106 | +header("Content-Type: text/plain; charset=UTF-8"); |
103 | 107 | ?> |
104 | 108 | WikiWord REST API |
\ No newline at end of file |
Index: trunk/WikiWord/WikiWord/src/main/php/wwclient.php |
— | — | @@ -28,13 +28,52 @@ |
29 | 29 | return $data; |
30 | 30 | } |
31 | 31 | |
32 | | - function getWikiPages( $id ) { |
33 | | - $p = $this->getConceptProperties( $id, 'pages' ); |
34 | | - |
| 32 | + function getPagesForConcept( $id, $lang = null ) { |
| 33 | + $p = $this->getConceptProperties( $id, 'pages', $lang ); |
35 | 34 | return $p['pages']; |
36 | 35 | } |
37 | 36 | |
38 | | - function getConceptProperties( $id, $props, $lang = NUL L) { |
| 37 | + function getRelatedForConcept( $id, $lang = null ) { |
| 38 | + $p = $this->getConceptProperties( $id, 'related', $lang ); |
| 39 | + return $p['related']; |
| 40 | + } |
| 41 | + |
| 42 | + function getBroaderForConcept( $id, $lang = null ) { |
| 43 | + $p = $this->getConceptProperties( $id, 'broader', $lang ); |
| 44 | + return $p['broader']; |
| 45 | + } |
| 46 | + |
| 47 | + function getNarrowerForConcept( $id, $lang = null ) { |
| 48 | + $p = $this->getConceptProperties( $id, 'narrower', $lang ); |
| 49 | + return $p['narrower']; |
| 50 | + } |
| 51 | + |
| 52 | + function getTermsForConcept( $id, $lang = null ) { |
| 53 | + $p = $this->getConceptProperties( $id, 'terms', $lang ); |
| 54 | + return $p['terms']; |
| 55 | + } |
| 56 | + |
| 57 | + function getDefinitionForConcept( $id, $lang = null ) { |
| 58 | + $p = $this->getConceptProperties( $id, 'definition', $lang ); |
| 59 | + return $p['definition']; |
| 60 | + } |
| 61 | + |
| 62 | + function getReferencesForConcept( $id, $lang = null ) { |
| 63 | + $p = $this->getConceptProperties( $id, 'links', $lang ); |
| 64 | + return $p['references']; |
| 65 | + } |
| 66 | + |
| 67 | + function getLinksForConcept( $id, $lang = null ) { |
| 68 | + $p = $this->getConceptProperties( $id, 'links', $lang ); |
| 69 | + return $p['links']; |
| 70 | + } |
| 71 | + |
| 72 | + function getScoresForConcept( $id, $lang = null ) { |
| 73 | + $p = $this->getConceptProperties( $id, 'scores', $lang ); |
| 74 | + return $p['scores']; |
| 75 | + } |
| 76 | + |
| 77 | + function getConceptProperties( $id, $props, $lang = null ) { |
39 | 78 | $param = array( |
40 | 79 | 'query' => 'properties', |
41 | 80 | 'props' => ( is_array($props) ? join('|', $props) : $props ), |
— | — | @@ -60,4 +99,16 @@ |
61 | 100 | return $rs['concepts']; |
62 | 101 | } |
63 | 102 | |
| 103 | + function getConceptsForPage( $lang, $page ) { |
| 104 | + $param = array( |
| 105 | + 'query' => 'concepts', |
| 106 | + 'lang' => $lang, |
| 107 | + 'page' => $page, |
| 108 | + ); |
| 109 | + |
| 110 | + $rs = $this->query( $param ); |
| 111 | + |
| 112 | + return $rs['concepts']; |
| 113 | + } |
| 114 | + |
64 | 115 | } |
Index: trunk/WikiWord/WikiWord/src/main/php/wwutils.php |
— | — | @@ -56,17 +56,53 @@ |
57 | 57 | $this->wikidbs = array(); |
58 | 58 | } |
59 | 59 | |
60 | | - static function slurpList($rs, $field) { |
| 60 | + function getRows($sql, $key = NULL) { |
| 61 | + $rs = $this->query($sql); |
| 62 | + $list = WWUtils::slurpRows($rs, $key); |
| 63 | + mysql_free_result($rs); |
| 64 | + return $list; |
| 65 | + } |
| 66 | + |
| 67 | + function getList($sql, $valueField, $key = NULL) { |
| 68 | + $rs = $this->query($sql); |
| 69 | + $list = WWUtils::slurpList($rs, $valueField, $key); |
| 70 | + mysql_free_result($rs); |
| 71 | + return $list; |
| 72 | + } |
| 73 | + |
| 74 | + static function slurpList($rs, $field, $key = null) { |
61 | 75 | if (is_string($rs)) $rs = $this->query($rs); |
62 | 76 | |
63 | 77 | $list = array(); |
64 | 78 | while ($row = mysql_fetch_assoc($rs)) { |
65 | | - $list[] = $row[$field]; |
| 79 | + $v = $row[$field]; |
| 80 | + if ($key) { |
| 81 | + $k = $row[$key]; |
| 82 | + $list[$k] = $v; |
| 83 | + } else { |
| 84 | + $list[] = $v; |
| 85 | + } |
66 | 86 | } |
67 | 87 | |
68 | 88 | return $list; |
69 | 89 | } |
70 | 90 | |
| 91 | + static function slurpRows($rs, $key = null) { |
| 92 | + if (is_string($rs)) $rs = $this->query($rs); |
| 93 | + |
| 94 | + $list = array(); |
| 95 | + while ($row = mysql_fetch_assoc($rs)) { |
| 96 | + if ($key) { |
| 97 | + $k = $row[$key]; |
| 98 | + $list[$k] = $row; |
| 99 | + } else { |
| 100 | + $list[] = $row; |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return $list; |
| 105 | + } |
| 106 | + |
71 | 107 | static function slurpAssoc($rs, $keyField, $valueField) { |
72 | 108 | if (is_string($rs)) $rs = $this->query($rs); |
73 | 109 | |
Index: trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php |
— | — | @@ -3,21 +3,49 @@ |
4 | 4 | |
5 | 5 | class WWThesaurus extends WWUTils { |
6 | 6 | |
7 | | - function queryConceptsForTerm($lang, $term) { |
| 7 | + function queryConceptsForTerm($lang, $term, $limit = 100) { |
8 | 8 | global $wwTablePrefix, $wwThesaurusDataset; |
9 | 9 | |
10 | 10 | $term = trim($term); |
11 | 11 | |
12 | | - $sql = "SELECT M.*, O.*, definition FROM {$wwTablePrefix}_{$lang}_meaning as M" |
| 12 | + $sql = "SELECT O.global_concept as id, M.*, O.*, definition FROM {$wwTablePrefix}_{$lang}_meaning as M" |
13 | 13 | . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON M.concept = D.concept " |
14 | 14 | . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND M.concept = O.local_concept " |
15 | 15 | . " WHERE term_text = \"" . mysql_real_escape_string($term) . "\"" |
16 | 16 | . " ORDER BY freq DESC " |
17 | | - . " LIMIT 100"; |
| 17 | + . " LIMIT $limit"; |
18 | 18 | |
19 | 19 | return $this->query($sql); |
20 | 20 | } |
21 | 21 | |
| 22 | + function getConceptsForTerm($lang, $term, $limit = 100) { |
| 23 | + $rs = $this->queryConceptsForTerm($lang, $term); |
| 24 | + $list = WWUtils::slurpRows($rs); |
| 25 | + mysql_free_result($rs); |
| 26 | + return $list; |
| 27 | + } |
| 28 | + |
| 29 | + function queryConceptsForPage($lang, $page, $limit = 100) { |
| 30 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 31 | + |
| 32 | + $page = trim($page); |
| 33 | + |
| 34 | + $sql = "SELECT O.global_concept as id, O.* FROM {$wwTablePrefix}_{$lang}_resource as R " |
| 35 | + . " JOIN {$wwTablePrefix}_{$lang}_about as A ON A.resource = R.id " |
| 36 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND A.concept = O.local_concept " |
| 37 | + . " WHERE R.name = \"" . mysql_real_escape_string($page) . "\"" |
| 38 | + . " LIMIT $limit"; |
| 39 | + |
| 40 | + return $this->query($sql); |
| 41 | + } |
| 42 | + |
| 43 | + function getConceptsForPage($lang, $page, $limit = 100) { |
| 44 | + $rs = $this->queryConceptsForPage($lang, $page); |
| 45 | + $list = WWUtils::slurpRows($rs); |
| 46 | + mysql_free_result($rs); |
| 47 | + return $list; |
| 48 | + } |
| 49 | + |
22 | 50 | function queryLocalConcepts($id) { |
23 | 51 | global $wwTablePrefix, $wwThesaurusDataset; |
24 | 52 | $sql = "SELECT O.lang, O.local_concept_name from {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O "; |
— | — | @@ -119,4 +147,121 @@ |
120 | 148 | return $names; |
121 | 149 | } |
122 | 150 | |
| 151 | + ///////////////////////////////////////////////////////// |
| 152 | + function getPagesForConcept( $id, $lang, $limit = 100 ) { |
| 153 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 154 | + |
| 155 | + #FIXME: if $lang is not given, collect *all* languages! |
| 156 | + $sql = "SELECT R.name FROM {$wwTablePrefix}_{$lang}_resource as R " |
| 157 | + . " JOIN {$wwTablePrefix}_{$lang}_about as A ON A.resource = R.id " |
| 158 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND A.concept = O.local_concept " |
| 159 | + . " WHERE O.global_concept = " . (int)$id |
| 160 | + . " LIMIT $limit"; |
| 161 | + |
| 162 | + $pages = $this->getList($sql, "name"); |
| 163 | + if ( $pages === false || $pages === null ) return false; |
| 164 | + |
| 165 | + return array( $lang => $pages ); |
| 166 | + } |
| 167 | + |
| 168 | + function getRelatedForConcept( $id, $lang = null, $limit = 100 ) { |
| 169 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 170 | + |
| 171 | + $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C " |
| 172 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_relation as R ON R.concept2 = C.id " |
| 173 | + . " WHERE R.concept1 = ".(int)$id |
| 174 | + . " AND ( R.bilink > 0 OR R.langref > 0 OR R.langmatch > 0 )" |
| 175 | + . " LIMIT $limit"; |
| 176 | + |
| 177 | + return $this->getRows($sql); |
| 178 | + } |
| 179 | + |
| 180 | + function getBroaderForConcept( $id, $lang = null, $limit = 100 ) { |
| 181 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 182 | + |
| 183 | + $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C " |
| 184 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_broader as R ON R.broad = C.id " |
| 185 | + . " WHERE R.narrow = ".(int)$id |
| 186 | + . " LIMIT $limit"; |
| 187 | + |
| 188 | + return $this->getRows($sql); |
| 189 | + } |
| 190 | + |
| 191 | + function getNarrowerForConcept( $id, $lang = null, $limit = 100 ) { |
| 192 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 193 | + |
| 194 | + $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C " |
| 195 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_broader as R ON R.narrow = C.id " |
| 196 | + . " WHERE R.broad = ".(int)$id |
| 197 | + . " LIMIT $limit"; |
| 198 | + |
| 199 | + return $this->getRows($sql); |
| 200 | + } |
| 201 | + |
| 202 | + function getTermsForConcept( $id, $lang , $limit = 100 ) { |
| 203 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 204 | + |
| 205 | + #FIXME: if $lang is not given, collect *all* languages! |
| 206 | + $sql = "SELECT M.term_text FROM {$wwTablePrefix}_{$lang}_meaning as M" |
| 207 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND M.concept = O.local_concept " |
| 208 | + . " WHERE O.global_concept = " . (int)$id |
| 209 | + . " ORDER BY freq DESC " |
| 210 | + . " LIMIT $limit"; |
| 211 | + |
| 212 | + $terms = $this->getList($sql, "term_text"); |
| 213 | + if ( $terms === false || $terms === null ) return false; |
| 214 | + |
| 215 | + return array( $lang => $terms ); |
| 216 | + } |
| 217 | + |
| 218 | + function getDefinitionForConcept( $id, $lang, $limit = 100 ) { |
| 219 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 220 | + |
| 221 | + #FIXME: if $lang is not given, collect *all* languages! |
| 222 | + $sql = "SELECT D.definition FROM {$wwTablePrefix}_{$lang}_definition as D" |
| 223 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND D.concept = O.local_concept " |
| 224 | + . " WHERE O.global_concept = " . (int)$id |
| 225 | + . " LIMIT $limit"; |
| 226 | + |
| 227 | + $definitions = $this->getList($sql, "definition"); |
| 228 | + if ( $definitions === false || $definitions === null ) return false; |
| 229 | + |
| 230 | + return array( $lang => $definitions ); |
| 231 | + } |
| 232 | + |
| 233 | + function getLinksForConcept( $id, $lang = null, $limit = 100 ) { |
| 234 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 235 | + |
| 236 | + $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C " |
| 237 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_link as L ON L.target = C.id " |
| 238 | + . " WHERE L.anchor = ".(int)$id |
| 239 | + . " LIMIT $limit"; |
| 240 | + |
| 241 | + return $this->getRows($sql); |
| 242 | + } |
| 243 | + |
| 244 | + function getReferencesForConcept( $id, $lang = null, $limit = 100 ) { |
| 245 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 246 | + |
| 247 | + $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C " |
| 248 | + . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_link as L ON L.anchor = C.id " |
| 249 | + . " WHERE L.target = ".(int)$id |
| 250 | + . " LIMIT $limit"; |
| 251 | + |
| 252 | + return $this->getRows($sql); |
| 253 | + } |
| 254 | + |
| 255 | + function getScoresForConcept( $id, $lang = null ) { |
| 256 | + global $wwTablePrefix, $wwThesaurusDataset; |
| 257 | + |
| 258 | + $sql = "SELECT S.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept_stats as S " |
| 259 | + . " WHERE S.concept = ".(int)$id |
| 260 | + ; |
| 261 | + |
| 262 | + $r = $this->getRows($sql); |
| 263 | + if ( !$r ) return false; |
| 264 | + |
| 265 | + return $r; |
| 266 | + } |
| 267 | + |
123 | 268 | } |