r47759 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r47758‎ | r47759 | r47760 >
Date:19:48, 24 February 2009
Author:daniel
Status:deferred
Tags:
Comment:
first working prototype
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/php/wikiword.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwutils.php (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/php/wwutils.php
@@ -1,22 +1,30 @@
22 <?php
33
4 -class WWUtil {
5 - static function connect($server, $user, $password, $database) {
 4+class WWUtils {
 5+ var $debug = false;
 6+ var $db = NULL;
 7+
 8+ function connect($server, $user, $password, $database) {
69 $db = mysql_connect($server, $user, $password) or die("Connection Failure to Database: " . mysql_error());
710 mysql_select_db($database, $db) or die ("Database not found: " . mysql_error());
811 mysql_query("SET NAMES UTF8;", $db) or die ("Database not found: " . mysql_error());
912
10 - if (isset($this)) $this->db = $db;
 13+ $this->db = $db;
 14+
1115 return $db;
1216 }
1317
14 - static function query($sql, $db = NULL) {
15 - if ($db == NULL && isset($this)) $db = $this->$db;
 18+ function query($sql, $db = NULL) {
 19+ if ($db == NULL && isset($this)) $db = $this->db;
1620
17 - if ($debug) {
 21+ if ($this->debug) {
1822 print htmlspecialchars($sql);
1923 }
2024
 25+ if (!$db) {
 26+ throw new Exception("not connected!");
 27+ }
 28+
2129 $result = mysql_query($sql, $db);
2230
2331 if(!$result) {
@@ -28,12 +36,18 @@
2937 return $result;
3038 }
3139
 40+ function close() {
 41+ if ($this->db) mysql_close($this->db);
 42+ $this->db = NULL;
 43+ }
 44+
3245 function queryConceptsForTerm($lang, $term) {
3346 global $wwTablePrefix;
3447
3548 $term = trim($term);
3649
37 - $sql = "SELECT * FROM {$wwTablePrefix}_{$lang}_meaning "
 50+ $sql = "SELECT M.*, definition FROM {$wwTablePrefix}_{$lang}_meaning as M"
 51+ . " JOIN {$wwTablePrefix}_{$lang}_definition as D ON M.concept = D.concept "
3852 . " WHERE term_text = \"" . mysql_real_escape_string($term) . "\""
3953 . " ORDER BY freq DESC "
4054 . " LIMIT 100";
@@ -47,6 +61,7 @@
4862 $term = trim($term);
4963
5064 $sql = "SELECT * FROM {$wwTablePrefix}_{$lang}_concept_info "
 65+ . " JOIN {$wwTablePrefix}_{$lang}_definition as D ON M.concept = D.concept "
5166 . " WHERE concept = $id ";
5267
5368 return $this->query($sql);
Index: trunk/WikiWord/WikiWord/src/main/php/wikiword.php
@@ -9,6 +9,8 @@
1010 $term = @$_REQUEST['term'];
1111 $lang = @$_REQUEST['lang'];
1212
 13+if (!isset($wwSelf)) $wwSelf = @$_SERVER["PHP_SELF"];
 14+
1315 $error = NULL;
1416
1517 if ($lang && !isset($wwLanguages[$lang])) {
@@ -19,23 +21,29 @@
2022 $utils = new WWUtils();
2123 $utils->connect($wwDBServer, $wwDBUser, $wwDBPassword, $wwDBDatabase);
2224
 25+if (@$_REQUEST['debug']) $utils->debug = true;
 26+
 27+$result = NULL;
 28+
2329 if (!$error) {
2430 try {
25 - if ($id) {
 31+ if ($concept) {
2632 if ($lang) {
27 - $result = $utils->queryLocalConceptInfo($lang, $id);
 33+ $result = $utils->queryLocalConceptInfo($lang, $concept);
2834 } else {
29 - $result = $utils->queryGlobalConceptInfo($id);
 35+ $result = $utils->queryGlobalConceptInfo($concept);
3036 }
3137 } else if ($lang) {
32 - $result = $utils->queryConceptsForTerm($lang, $id);
 38+ $result = $utils->queryConceptsForTerm($lang, $term);
3339 }
3440 } catch (Exception $e) {
3541 $error = $e->getMessage();
3642 }
3743 }
3844
39 -function printLocalConcept($lang, $row) {
 45+function printLocalConcept($lang, $row, $pos = 0) {
 46+ global $wwSelf;
 47+
4048 extract($row);
4149
4250 if (!isset($weight) && isset($freq)) $weight = $freq;
@@ -43,12 +51,26 @@
4452 $wu = "http://$lang.wikipedia.org/wiki/" . urlencode($concept_name);
4553 $cu = "$wwSelf?id=" . urlencode($concept) . "&lang=" . urlencode($lang);
4654
47 - print "\t\t<li>";
48 - if (isset($weight) && !empty($weight)) print "<b>" . htmlspecialchars($weight) . "</b> ";
49 - print "<big><b><a href=\"".htmlspecialchars($wu)."\">".htmlspecialchars($concept_name)."</a></b></big> ";
50 - print " (<a href=\"".htmlspecialchars($cu)."\">#".htmlspecialchars($concept)."</a>) ";
51 - if (isset($definition) && !empty($definition)) print "<br/><small>" . htmlspecialchars($definition) . "</small>";
52 - print "</li>\n";
 55+ if (!$weight) $wclass = "x";
 56+ else if ($weight>1000) $wclass = "huge";
 57+ else if ($weight>100) $wclass = "big";
 58+ else if ($weight>10) $wclass = "normal";
 59+ else if ($weight>2) $wclass = "some";
 60+ else $wclass = "little";
 61+
 62+ ?>
 63+ <tr class="row_item">
 64+ <td class="col_weight <?php print "weight_$wclass"; ?>"><?php print htmlspecialchars($weight); ?></td>
 65+ <td class="col_name <?php print "weight_$wclass"; ?>"><a href="<?php print htmlspecialchars($wu); ?>"><?php print htmlspecialchars($concept_name); ?></a></td>
 66+ <td class="col_concept <?php print "weight_$wclass"; ?>">(#<a href="<?php print htmlspecialchars($cu); ?>"><?php print htmlspecialchars($concept); ?></a>)</td>
 67+ </tr>
 68+ <tr class="row_def">
 69+ <td></td>
 70+ <td colspan="2"><?php print htmlspecialchars($definition); ?></td>
 71+ </tr>
 72+ <?php
 73+ if ($weight && $weight<2 && $pos>=3) return false;
 74+ else return true;
5375 }
5476
5577 ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -56,18 +78,30 @@
5779 <head>
5880 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
5981 <title>WikiWord Navigator</title>
 82+
 83+ <style type="text/css">
 84+ .error { color: red; font-weight: bold; }
 85+ .weight_huge { font-size: 140%; font-weight:bold; }
 86+ .weight_big { font-size: 120%; font-weight:bold; }
 87+ .weight_normal { font-size: 110%; font-weight:bold; }
 88+ .weight_some { font-size: 100%; font-weight:normal; }
 89+ .weight_little { font-size: 90%; font-weight:normal; }
 90+ .row_def td { font-size: 80%; }
 91+ .col_weight td { text-align: right; }
 92+ </style>
6093 </head>
6194 <body>
6295 <h1>WikiWord Navigator</h1>
6396 <p>Experimental proof of concept <a href="http://brightbyte.de/page/WikiWord">WikiWord</a> navigator.</p>
6497
65 - <form name="search">
 98+ <form name="search" action="<?php print $wwSelf; ?>">
6699 <p>
67 - <lable for="term">Term: </label><input type="text" name="term" id="term" length="24" value="<?php print htmlspecialchars($term); ?>"/>
68 - <lable for="term">Language: </label>
 100+ <label for="term">Term: </label><input type="text" name="term" id="term" size="24" value="<?php print htmlspecialchars($term); ?>"/>
 101+ <label for="term">Language: </label>
69102 <?php WWUtils::printSelector("lang", $wwLanguages, $lang) ?>
70103 <input type="submit" value="go"/>
71104 </p>
 105+ <p>Note: this is a thesaurus lookup, not a full text search. Only exact matches are considered, matching is case-sensitive.</p>
72106 </form>
73107 <?php
74108 if ($error) {
@@ -78,18 +112,20 @@
79113 <?php
80114 if ($result) {
81115 ?>
82 - <ul>
 116+ <table border="0" class="results">
83117 <?php
84118 $count = 0;
85119 while ($row = mysql_fetch_assoc($result)) {
86 - if ($lang) printLocalConcept($lang, $row);
87 - else printGlobalConcept($lang, $row);
88120 $count += 1;
 121+ if ($lang) $continue= printLocalConcept($lang, $row, $count);
 122+ else $continue= printGlobalConcept($lang, $row, $count);
 123+
 124+ if (!$continue) break;
89125 }
90126
91127 mysql_free_result($result);
92128 ?>
93 - </ul>
 129+ </table>
94130
95131 <p>Found <?php print $count; ?> items.</p>
96132
@@ -97,7 +133,7 @@
98134 }
99135 ?>
100136 </body>
101 -
 137+</html>
102138 <?php
103 -mysql_close($db);
 139+$utils->close();
104140 ?>
\ No newline at end of file

Status & tagging log