r58402 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58401‎ | r58402 | r58403 >
Date:14:12, 1 November 2009
Author:daniel
Status:deferred
Tags:
Comment:
wikiword thesaurus api
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/php/api.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwclient.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwutils.php (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/php/api.php
@@ -12,8 +12,12 @@
1313 $format = @$_REQUEST['format'];
1414 if ( !$format ) $format = 'phps';
1515
 16+ if ($lang) {
 17+ $lang = preg_replace('[^\\w\\d_]', '', $lang);
 18+ }
 19+
1620 $result = array( 'query' => $query );
17 - $start = microtime();
 21+ $start = microtime(true);
1822
1923 try {
2024 $thesaurus = new WWThesaurus();
@@ -26,12 +30,12 @@
2731 $page = @$_REQUEST['page'];
2832
2933 if ( $lang === null ) $result['error'] = array('code' => 150, 'message' => "missing parameter lang");
30 - else if ( $term !=== null ) {
 34+ else if ( $term !== null ) {
3135 $result['concepts'] = $thesaurus->getConceptsForTerm($lang, $term);
3236 if ( $result['concepts'] === false || $result['concepts'] === null ) {
3337 $result['error'] = array('code' => 210, 'message' => "failed to retrieve concepts for term $langt:$term");
3438 }
35 - } else if ( $page !=== null ) {
 39+ } else if ( $page !== null ) {
3640 $result['concepts'] = $thesaurus->getConceptsForPage($lang, $page);
3741 if ( $result['concepts'] === false || $result['concepts'] === null ) {
3842 $result['error'] = array('code' => 250, 'message' => "failed to retrieve concepts for page $langt:$page");
@@ -39,7 +43,7 @@
4044 } else {
4145 $result['error'] = array('code' => 110, 'message' => "missing parameter term");
4246 }
43 - } if ($query == 'properties') else {
 47+ } else if ($query == 'properties') {
4448 $gcid = @$_REQUEST['gcid'];
4549 $props = @$_REQUEST['props'];
4650
@@ -49,7 +53,7 @@
5054 $props = preg_split('![\\s,;|/:]\\s*!', $props);
5155
5256 foreach ( $props as $p ) {
53 - $m = "get" . ucfist($p) . "ForConcept";
 57+ $m = "get" . ucfirst($p) . "ForConcept";
5458 if ( !method_exists($thesaurus, $m) ) {
5559 $result['error'] = array('code' => 190, 'message' => "unknown property: $p");
5660 break;
@@ -66,11 +70,11 @@
6771 } else {
6872 $result['error'] = array('code' => 10, 'message' => "bad query: $query");
6973 }
70 - } catch (Exception e) {
 74+ } catch (Exception $e) {
7175 $result['error'] = array('code' => 1000, 'message' => "unexpected exception: " . $e->getMessage());
7276 }
7377
74 - $result['time'] = (microtime() - $start) . "ms";
 78+ $result['time'] = (microtime(true) - $start) . " sec";
7579
7680 if ( isset($result['error']) ) {
7781 #TODO: HTTP error codce would be nice, but causes file_get_contents to swallow the data.
@@ -84,13 +88,13 @@
8589 $data = serialize($result);
8690 echo $data;
8791 } else if ($format == 'php') {
88 - header("Content-Type: text/php");
 92+ header("Content-Type: text/php; charset=UTF-8");
8993 var_export($result);
9094 } else if ($format == 'text') {
91 - header("Content-Type: text/plain");
 95+ header("Content-Type: text/plain; charset=UTF-8");
9296 print_r($result);
9397 } else {
94 - header("Content-Type: text/plain");
 98+ header("Content-Type: text/plain; charset=UTF-8");
9599 header("Status: 400 Bad Request", true, 400);
96100 echo "Bad format: $format";
97101 }
@@ -98,6 +102,6 @@
99103 exit();
100104 }
101105
102 -header("Content-Type: text/plain");
 106+header("Content-Type: text/plain; charset=UTF-8");
103107 ?>
104108 WikiWord REST API
\ No newline at end of file
Index: trunk/WikiWord/WikiWord/src/main/php/wwclient.php
@@ -28,13 +28,52 @@
2929 return $data;
3030 }
3131
32 - function getWikiPages( $id ) {
33 - $p = $this->getConceptProperties( $id, 'pages' );
34 -
 32+ function getPagesForConcept( $id, $lang = null ) {
 33+ $p = $this->getConceptProperties( $id, 'pages', $lang );
3534 return $p['pages'];
3635 }
3736
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 ) {
3978 $param = array(
4079 'query' => 'properties',
4180 'props' => ( is_array($props) ? join('|', $props) : $props ),
@@ -60,4 +99,16 @@
61100 return $rs['concepts'];
62101 }
63102
 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+
64115 }
Index: trunk/WikiWord/WikiWord/src/main/php/wwutils.php
@@ -56,17 +56,53 @@
5757 $this->wikidbs = array();
5858 }
5959
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) {
6175 if (is_string($rs)) $rs = $this->query($rs);
6276
6377 $list = array();
6478 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+ }
6686 }
6787
6888 return $list;
6989 }
7090
 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+
71107 static function slurpAssoc($rs, $keyField, $valueField) {
72108 if (is_string($rs)) $rs = $this->query($rs);
73109
Index: trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php
@@ -3,21 +3,49 @@
44
55 class WWThesaurus extends WWUTils {
66
7 - function queryConceptsForTerm($lang, $term) {
 7+ function queryConceptsForTerm($lang, $term, $limit = 100) {
88 global $wwTablePrefix, $wwThesaurusDataset;
99
1010 $term = trim($term);
1111
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"
1313 . " LEFT JOIN {$wwTablePrefix}_{$lang}_definition as D ON M.concept = D.concept "
1414 . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND M.concept = O.local_concept "
1515 . " WHERE term_text = \"" . mysql_real_escape_string($term) . "\""
1616 . " ORDER BY freq DESC "
17 - . " LIMIT 100";
 17+ . " LIMIT $limit";
1818
1919 return $this->query($sql);
2020 }
2121
 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+
2250 function queryLocalConcepts($id) {
2351 global $wwTablePrefix, $wwThesaurusDataset;
2452 $sql = "SELECT O.lang, O.local_concept_name from {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ";
@@ -119,4 +147,121 @@
120148 return $names;
121149 }
122150
 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+
123268 }

Status & tagging log