r58538 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58537‎ | r58538 | r58539 >
Date:13:50, 4 November 2009
Author:daniel
Status:deferred
Tags:
Comment:
fixed image search using remote api
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/php/api.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/config.sample.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wikiword.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwclient.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/php/api.php
@@ -45,6 +45,8 @@
4646 }
4747 } else if ($query == 'properties') {
4848 $gcid = @$_REQUEST['gcid'];
 49+ if (!$gcid) $gcid = @$_REQUEST['id'];
 50+
4951 $props = @$_REQUEST['props'];
5052
5153 if ( $gcid === null ) $result['error'] = array('code' => 120, 'message' => "missing parameter gcid");
@@ -52,19 +54,29 @@
5355 else {
5456 $props = preg_split('![\\s,;|/:]\\s*!', $props);
5557
56 - foreach ( $props as $p ) {
57 - $m = "get" . ucfirst($p) . "ForConcept";
58 - if ( !method_exists($thesaurus, $m) ) {
59 - $result['error'] = array('code' => 190, 'message' => "unknown property: $p");
60 - break;
61 - }
 58+ $info = $thesaurus->getConcept($gcid, $lang);
6259
63 - $result[$p] = $thesaurus->$m($gcid, $lang);
 60+ if ( !$info ) $result['error'] = array('code' => 100, 'message' => "concept not found: $gcid");
 61+ else {
 62+ $result = array_merge($result, $info);
6463
65 - if ( $result[$p] === false || $result[$p] === null ) {
66 - $result['error'] = array('code' => 220, 'message' => "failed to retrieve property $p for concept $gcid");
67 - break;
 64+ foreach ( $props as $p ) {
 65+ $m = "get" . ucfirst($p) . "ForConcept";
 66+ if ( !method_exists($thesaurus, $m) ) {
 67+ $result['error'] = array('code' => 190, 'message' => "unknown property: $p");
 68+ break;
 69+ }
 70+
 71+ $result[$p] = $thesaurus->$m($gcid, $lang);
 72+
 73+ if ( $result[$p] === false || $result[$p] === null ) {
 74+ $result['error'] = array('code' => 220, 'message' => "failed to retrieve property $p for concept $gcid");
 75+ break;
 76+ }
6877 }
 78+
 79+ if (!isset($result['id'])) $result['id'] = $gcid;
 80+ if (!isset($result['lang'])) $result['lang'] = $lang;
6981 }
7082 }
7183 } else {
Index: trunk/WikiWord/WikiWord/src/main/php/wwclient.php
@@ -32,6 +32,11 @@
3333 return getPagesForConcept($id);
3434 }
3535
 36+ function getConcept( $id, $lang = null ) {
 37+ $p = $this->getConceptProperties( $id, '', $lang );
 38+ return $p['pages'];
 39+ }
 40+
3641 function getPagesForConcept( $id, $lang = null ) {
3742 $p = $this->getConceptProperties( $id, 'pages', $lang );
3843 return $p['pages'];
@@ -78,8 +83,8 @@
7984 }
8085
8186 function getConceptInfo( $id, $lang = null ) {
82 - if ( $lang ) return $this->getConceptProperties( "definition,broader,narrower,related" );
83 - else return $this->getConceptProperties( "broader,narrower,related" );
 87+ if ( $lang ) return $this->getConceptProperties( $id, "definition,broader,narrower,related", $lang );
 88+ else return $this->getConceptProperties( $id, "broader,narrower,related" );
8489 }
8590
8691 function getConceptProperties( $id, $props, $lang = null ) {
@@ -93,6 +98,9 @@
9499
95100 $rs = $this->query( $param );
96101
 102+ if (!isset($rs['id'])) $rs['id'] = $id;
 103+ if (!isset($rs['lang'])) $rs['lang'] = $lang;
 104+
97105 return $rs;
98106 }
99107
Index: trunk/WikiWord/WikiWord/src/main/php/wikiword.php
@@ -45,7 +45,7 @@
4646 }
4747
4848 function printLocalConceptLink($lang, $row) {
49 - global $wwSelf;
 49+ global $wwSelf, $images;
5050
5151 extract($row);
5252
@@ -56,16 +56,21 @@
5757 if (!isset($concept) && isset($id)) $concept = $id;
5858 if (!isset($concept)) $concept = NULL;
5959
60 - $wu = $concept_name ? "http://$lang.wikipedia.org/wiki/" . urlencode($concept_name) : NULL;
 60+ if ($lang == 'commons') $domain = 'commons.wikimedia.org';
 61+ else $domain = "$lang.wikipedia.org";
 62+
 63+ $wu = $concept_name ? "http://$domain/wiki/" . urlencode($concept_name) : NULL;
6164 $cu = "$wwSelf?id=" . urlencode($concept) . "&lang=" . urlencode($lang);
6265
 66+ if ($images) $cu .= "&images=1";
 67+
6368 ?>
6469 <li>
6570 <?php if ($concept_name) { ?>
66 - <a href="<?php print htmlspecialchars($wu); ?>"><?php print htmlspecialchars($concept_name); ?></a>
 71+ <a href="<?php print htmlspecialchars($cu); ?>"><?php print htmlspecialchars($concept_name); ?></a>
6772 <?php } ?>
6873 <?php if ($concept) { ?>
69 - (#<a href="<?php print htmlspecialchars($cu); ?>"><?php print htmlspecialchars($concept); ?></a>)
 74+ (<a href="<?php print htmlspecialchars($wu); ?>" title="<?php print htmlspecialchars($concept_name); ?>">wiki page</a>)
7075 <?php } ?>
7176 </li>
7277 <?php
@@ -116,7 +121,9 @@
117122
118123 if (!isset($row['weight']) && isset($row['freq'])) $row['weight'] = $row['freq'];
119124 if (!isset($row['weight']) && isset($row['conf'])) $row['weight'] = $row['conf'];
 125+ if (isset($row['local_concept_name'])) $row['concept_name'] = $row['local_concept_name'];
120126 if (!isset($row['concept_name']) && isset($row['name'])) $row['concept_name'] = $row['name'];
 127+ if (!isset($row['concept_name']) && isset($row['global_concept_name'])) $row['concept_name'] = $row['global_concept_name'];
121128 if (!isset($row['reference_id']) && isset($row['global_id'])) $row['reference_id'] = $row['global_id'];
122129 if (!isset($row['reference_id']) && isset($row['global_concept'])) $row['reference_id'] = $row['global_concept'];
123130 if (!isset($row['reference_id']) && isset($row['concept'])) $row['reference_id'] = $row['concept'];
@@ -144,8 +151,8 @@
145152 return $row;
146153 }
147154
148 -function printLocalConcept($a_lang, $a_row, $b_lang, $b_row, $pos = 0) {
149 - global $wwSelf, $images;
 155+function printLocalConcept($a_lang, $a_row, $b_lang, $b_row, $pos = 0, $images) {
 156+ global $wwSelf;
150157
151158 $a_row = normalizeConceptRow($a_lang, $a_row);
152159 $b_row = normalizeConceptRow($b_lang, $b_row);
@@ -157,15 +164,13 @@
158165 <tr class="row_item">
159166 <td class="cell_weight <?php print "weight_$a_wclass"; ?>"><?php print htmlspecialchars($a_weight); ?></td>
160167 <td colspan="3" class="cell_name <?php print "weight_$a_wclass"; ?>">
161 - <a href="<?php print htmlspecialchars($a_wu); ?>"><?php print htmlspecialchars($a_concept_name); ?></a>
162 - <span class="conceptref">(#<a href="<?php print htmlspecialchars($a_cu); ?>"><?php print htmlspecialchars($a_reference_id); ?></a>)</span>
163 - <span class="galleryref">(<a href="<?php print htmlspecialchars($a_gu); ?>"><?php print htmlspecialchars("gallery"); ?></a>)</span>
 168+ <a href="<?php print htmlspecialchars($a_cu); ?>"><?php print htmlspecialchars($a_concept_name); ?></a>
 169+ <span class="conceptref">(<a href="<?php print htmlspecialchars($a_wu); ?>" title="<?php print htmlspecialchars($a_concept_name); ?>">wiki page</a>)</span>
164170 </td>
165171 <?php if ($b_row) { ?>
166172 <td colspan="3" class="cell_name <?php print "weight_$b_wclass"; ?>">
167 - <a href="<?php print htmlspecialchars($b_wu); ?>"><?php print htmlspecialchars($b_concept_name); ?></a>
168 - <span class="conceptref">(#<a href="<?php print htmlspecialchars($b_cu); ?>"><?php print htmlspecialchars($b_reference_id); ?></a>)</span>
169 - <span class="galleryref">(<a href="<?php print htmlspecialchars($a_gu); ?>"><?php print htmlspecialchars("gallery"); ?></a>)</span>
 173+ <a href="<?php print htmlspecialchars($b_cu); ?>"><?php print htmlspecialchars($b_concept_name); ?></a>
 174+ <span class="conceptref">(<a href="<?php print htmlspecialchars($b_wu); ?>" title="<?php print htmlspecialchars($b_concept_name); ?>">wiki page</a>)</span>
170175 </td>
171176 <?php } ?>
172177 </tr>
@@ -260,7 +265,7 @@
261266 $term = @$_REQUEST['term'];
262267 $lang = @$_REQUEST['lang'];
263268 $tolang = @$_REQUEST['tolang'];
264 -$images = @$_REQUEST['images'] || $wwImageSearch;
 269+$images = (@$_REQUEST['images'] || $wwImageSearch === true ) && !($wwImageSearch === false);
265270
266271 if (!isset($_REQUEST['translate'])) $tolang = NULL;
267272 if ($lang == $tolang) $tolang = NULL;
@@ -288,7 +293,6 @@
289294 $limit = 20;
290295
291296 $result = NULL;
292 -$gallery = NULL;
293297
294298 if (!$error) {
295299 try {
@@ -297,9 +301,7 @@
298302 if ( $result ) $result = array( $result ); //hack
299303 } else if ($lang && $term) {
300304 $result = $thesaurus->getConceptsForTerm($lang, $term, $limit);
301 - } else if ($concept && $images) {
302 - $gallery = $utils->getImagesAbout($concept);
303 - }
 305+ }
304306 } catch (Exception $e) {
305307 $error = $e->getMessage();
306308 }
@@ -361,7 +363,7 @@
362364 <td>
363365 <input type="submit" name="go" value="go"/>
364366 </td>
365 - <?php if (!$wwImageSearch) { ?>
 367+ <?php if ($wwImageSearch === null) { ?>
366368 <td>
367369 <label for="images">Images: </label>
368370 <input type="checkbox" name="images" value="Images" <?php print $images ? " checked=\"checked\"" : ""?>/>
@@ -393,14 +395,63 @@
394396 ?>
395397
396398 <?php
397 -if ($result) {
398 - if ($concept) $title = "Concept #$concept";
399 - else if ($tolang) $title = "$lang: $term -> $tolang";
400 - else $title = "$lang: $term";
 399+if ($result && $concept) {
 400+ foreach ( $result as $row ) {
 401+ if (@$row['id']) $id = $row['id'];
 402+ else if (@$row['concept']) $id = $row['concept'];
 403+ else $id = "concept";
 404+
 405+ if (@$row['local_concept_name']) $title = $row['local_concept_name'];
 406+ else if (@$row['concept_name']) $title = $row['concept_name'];
 407+ else if (@$row['name']) $title = $row['name'];
 408+ else if ($id) $title = "Concept #$id";
 409+ else if ($concept) $title = "Concept #$concept";
 410+
401411 ?>
 412+ <div id="<?php print htmlspecialchars("concept-$id")?>">
402413 <h2><?php print htmlspecialchars($title); ?></h2>
 414+
 415+ <?php
 416+ if ($images) $gallery = $utils->getImagesAbout($id);
 417+ else $images = NULL;
 418+
 419+ if ($gallery) {
 420+ $title = "Gallery #$concept";
 421+ ?>
 422+ <h2><?php print htmlspecialchars($title); ?></h2>
 423+ <div border="0" class="gallery">
 424+ <?php
 425+ printConceptImageList($gallery, "gallery");
 426+ ?>
 427+ </div>
 428+
 429+ <p>Found <?php print count($gallery); ?> images.</p>
 430+
 431+ <?php
 432+ }
 433+ ?>
 434+
403435 <table border="0" class="results">
404436 <?php
 437+ if ($lang) {
 438+ $continue= printLocalConcept($lang, $row, NULL, NULL, 0, false);
 439+ }
 440+ //else $continue= printGlobalConcept($lang, $row, $count);
 441+
 442+ if (!$continue) break;
 443+ ?>
 444+ </table>
 445+ </div>
 446+
 447+<?php
 448+ } #concept loop
 449+} else if ($result && $term) {
 450+ if ($tolang) $title = "$lang: $term -> $tolang";
 451+ else if ($term) $title = "$lang: $term";
 452+?>
 453+ <h2><?php print htmlspecialchars($title); ?></h2>
 454+ <table border="0" class="results">
 455+ <?php
405456 $count = 0;
406457 foreach ( $result as $row ) {
407458 $count += 1;
@@ -411,14 +462,14 @@
412463 if ($tolang && isset($row['global_concept'])) {
413464 $toresult = $utils->queryConceptInfo($row['global_concept'], $tolang);
414465 while ($torow = mysql_fetch_assoc($toresult)) {
415 - $continue= printLocalConcept($lang, $row, $tolang, $torow, $count);
 466+ $continue= printLocalConcept($lang, $row, $tolang, $torow, $count, $images);
416467 $show_single = false;
417468 }
418469 mysql_free_result($toresult);
419470 }
420471
421472 if ($show_single) {
422 - $continue= printLocalConcept($lang, $row, NULL, NULL, $count);
 473+ $continue= printLocalConcept($lang, $row, NULL, NULL, $count, $images);
423474 }
424475 }
425476 //else $continue= printGlobalConcept($lang, $row, $count);
@@ -434,24 +485,6 @@
435486 }
436487 ?>
437488
438 -<?php
439 -if ($gallery) {
440 - $title = "Gallery #$concept";
441 -?>
442 - <h2><?php print htmlspecialchars($title); ?></h2>
443 - <div border="0" class="results">
444 - <?php
445 - printConceptImageList($gallery, "gallery");
446 - ?>
447 - </div>
448 -
449 - <p>Found <?php print count($gallery); ?> images.</p>
450 -
451 -<?php
452 -}
453 -?>
454 -
455 -
456489 <p class="footer">
457490 The WikiWord Navigator is part of the <a href="http://wikimedia.de">Wikimedia</a> project <a href="http://brightbyte.de/page/WikiWord">WikiWord</a>
458491 <p>
Index: trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php
@@ -84,7 +84,7 @@
8585 }*/
8686
8787 function getConceptInfo( $id, $lang = null ) {
88 - $result = array();
 88+ $result = $this->getConcept($id, $lang);
8989
9090 $result['broader'] = $this->getBroaderForConcept($id);
9191 $result['narrower'] = $this->getNarrowerForConcept($id);
@@ -161,6 +161,22 @@
162162 }
163163
164164 /////////////////////////////////////////////////////////
 165+ function getConcept( $id, $lang = null, $limit = 100 ) {
 166+ global $wwTablePrefix, $wwThesaurusDataset;
 167+
 168+ if ($lang) $sql = "SELECT C.*, O.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
 169+ . " LEFT JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O "
 170+ . " ON C.id = O.global_concept " . " AND O.lang = \"".mysql_real_escape_string($lang)."\""
 171+ . " WHERE C.id = ".(int)$id ;
 172+ else $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
 173+ . " WHERE C.id = ".(int)$id;
 174+
 175+ $r = $this->getRows($sql);
 176+
 177+ if ( !$r ) return false;
 178+ else return $r[0];
 179+ }
 180+
165181 function getRelatedForConcept( $id, $lang = null, $limit = 100 ) {
166182 global $wwTablePrefix, $wwThesaurusDataset;
167183
Index: trunk/WikiWord/WikiWord/src/main/php/config.sample.php
@@ -3,7 +3,7 @@
44 #$wwAPI = "http://wikiword.wikimedia.de/api.php";
55 $wwAPI = false;
66 $wwAllowTranslate = false;
7 -$wwImageSearch = false;
 7+$wwImageSearch = null; //NOTE: trinary: true forces image searche, false disables it. null makes it optional.
88
99 $wwDBServer = "localhost";
1010 $wwDBUser = "wikiword";

Status & tagging log