r58550 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58549‎ | r58550 | r58551 >
Date:16:52, 4 November 2009
Author:daniel
Status:deferred
Tags:
Comment:
improving image search using remote api
Modified paths:
  • /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/wwimages.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/php/wikiword.php
@@ -47,6 +47,8 @@
4848 function printLocalConceptLink($lang, $row) {
4949 global $wwSelf, $images;
5050
 51+ $row = normalizeConceptRow($lang, $row);
 52+
5153 extract($row);
5254
5355 if (!isset($weight) && isset($freq)) $weight = $freq;
@@ -117,17 +119,17 @@
118120
119121 if (!$row) return $row;
120122
121 - $row['lang'] = $lang;
122 -
 123+ if (!isset($row['lang'])) $row['lang'] = $lang;
123124 if (!isset($row['weight']) && isset($row['freq'])) $row['weight'] = $row['freq'];
124125 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'];
 126+ if (!empty($row['local_concept_name'])) $row['concept_name'] = $row['local_concept_name'];
126127 if (!isset($row['concept_name']) && isset($row['name'])) $row['concept_name'] = $row['name'];
127128 if (!isset($row['concept_name']) && isset($row['global_concept_name'])) $row['concept_name'] = $row['global_concept_name'];
128129 if (!isset($row['reference_id']) && isset($row['global_id'])) $row['reference_id'] = $row['global_id'];
129130 if (!isset($row['reference_id']) && isset($row['global_concept'])) $row['reference_id'] = $row['global_concept'];
130131 if (!isset($row['reference_id']) && isset($row['concept'])) $row['reference_id'] = $row['concept'];
131132 if (!isset($row['reference_id']) && isset($row['id'])) $row['reference_id'] = $row['id'];
 133+ if (!empty($row['definition']) && is_array($row['definition'])) $row['definition'] = $row['definition'][$lang];
132134
133135 #print "<pre>";
134136 #print_r($row);
@@ -151,8 +153,9 @@
152154 return $row;
153155 }
154156
155 -function printLocalConcept($a_lang, $a_row, $b_lang, $b_row, $pos = 0, $images) {
156 - global $wwSelf;
 157+function printLocalConcept($a_lang, $a_row, $b_lang, $b_row, $pos = 0, $terse = true) {
 158+ global $wwSelf, $images, $utils;
 159+ global $wwMaxPreviewImages, $wwMaxGalleryImages;
157160
158161 $a_row = normalizeConceptRow($a_lang, $a_row);
159162 $b_row = normalizeConceptRow($b_lang, $b_row);
@@ -164,13 +167,17 @@
165168 <tr class="row_item">
166169 <td class="cell_weight <?php print "weight_$a_wclass"; ?>"><?php print htmlspecialchars($a_weight); ?></td>
167170 <td colspan="3" class="cell_name <?php print "weight_$a_wclass"; ?>">
 171+ <h3>
168172 <a href="<?php print htmlspecialchars($a_cu); ?>"><?php print htmlspecialchars($a_concept_name); ?></a>
169173 <span class="conceptref">(<a href="<?php print htmlspecialchars($a_wu); ?>" title="<?php print htmlspecialchars($a_concept_name); ?>">wiki page</a>)</span>
 174+ </h3>
170175 </td>
171176 <?php if ($b_row) { ?>
172177 <td colspan="3" class="cell_name <?php print "weight_$b_wclass"; ?>">
 178+ <h3>
173179 <a href="<?php print htmlspecialchars($b_cu); ?>"><?php print htmlspecialchars($b_concept_name); ?></a>
174180 <span class="conceptref">(<a href="<?php print htmlspecialchars($b_wu); ?>" title="<?php print htmlspecialchars($b_concept_name); ?>">wiki page</a>)</span>
 181+ </h3>
175182 </td>
176183 <?php } ?>
177184 </tr>
@@ -199,6 +206,19 @@
200207 </tr>
201208 <?php } ?>
202209
 210+ <?php if ($images) { ?>
 211+ <tr class="row_details row_images">
 212+ <td></td>
 213+ <td class="cell_label">Images:</td>
 214+ <td class="cell_broader" colspan="<?php $b_row ? 5 : 2 ?>">
 215+ <?php
 216+ $gallery = $utils->getImagesAbout($a_reference_id, $terse ? $wwMaxPreviewImages : $wwMaxGalleryImages );
 217+ $c = printConceptImageList( $gallery, $terse ? "terselist" : "gallery" );
 218+ ?>
 219+ </td>
 220+ </tr>
 221+ <?php } ?>
 222+
203223 <?php if (isset($a_similar) && !empty($a_similar)) { ?>
204224 <tr class="row_details row_similar">
205225 <td></td>
@@ -247,15 +267,6 @@
248268 </tr>
249269 <?php } ?>
250270
251 - <?php if ($images) { ?>
252 - <tr class="row_details row_images">
253 - <td></td>
254 - <td class="cell_label">Images:</td>
255 - <td class="cell_broader" colspan="<?php $b_row ? 5 : 2 ?>">
256 - <?php printConceptImageList($a_reference_id); ?>
257 - </td>
258 - </tr>
259 - <?php } ?>
260271 <?php
261272 if (isset($a_weight) && $a_weight && $a_weight<2 && $pos>=3) return false;
262273 else return true;
@@ -401,36 +412,9 @@
402413 else if (@$row['concept']) $id = $row['concept'];
403414 else $id = "concept";
404415
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 -
411416 ?>
412417 <div id="<?php print htmlspecialchars("concept-$id")?>">
413 - <h2><?php print htmlspecialchars($title); ?></h2>
414418
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 -
435419 <table border="0" class="results">
436420 <?php
437421 if ($lang) {
@@ -462,14 +446,14 @@
463447 if ($tolang && isset($row['global_concept'])) {
464448 $toresult = $utils->queryConceptInfo($row['global_concept'], $tolang);
465449 while ($torow = mysql_fetch_assoc($toresult)) {
466 - $continue= printLocalConcept($lang, $row, $tolang, $torow, $count, $images);
 450+ $continue= printLocalConcept($lang, $row, $tolang, $torow, $count, true);
467451 $show_single = false;
468452 }
469453 mysql_free_result($toresult);
470454 }
471455
472456 if ($show_single) {
473 - $continue= printLocalConcept($lang, $row, NULL, NULL, $count, $images);
 457+ $continue= printLocalConcept($lang, $row, NULL, NULL, $count, true);
474458 }
475459 }
476460 //else $continue= printGlobalConcept($lang, $row, $count);
Index: trunk/WikiWord/WikiWord/src/main/php/wwthesaurus.php
@@ -180,10 +180,14 @@
181181 function getRelatedForConcept( $id, $lang = null, $limit = 100 ) {
182182 global $wwTablePrefix, $wwThesaurusDataset;
183183
184 - $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
185 - . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_relation as R ON R.concept2 = C.id "
186 - . " WHERE R.concept1 = ".(int)$id
 184+ $sql = "SELECT " . ($lang ? " C.*, O.* " : " C.* " ) . " FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
 185+ . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_relation as R ON R.concept2 = C.id ";
 186+
 187+ if ( $lang ) $sql .= " LEFT JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND C.id = O.global_concept ";
 188+
 189+ $sql .= " WHERE R.concept1 = ".(int)$id
187190 . " AND ( R.bilink > 0 OR R.langref > 0 OR R.langmatch > 0 )"
 191+ . " GROUP BY C.id "
188192 . " LIMIT " . (int)$limit;
189193
190194 return $this->getRows($sql);
@@ -192,9 +196,13 @@
193197 function getBroaderForConcept( $id, $lang = null, $limit = 100 ) {
194198 global $wwTablePrefix, $wwThesaurusDataset;
195199
196 - $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
197 - . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_broader as R ON R.broad = C.id "
198 - . " WHERE R.narrow = ".(int)$id
 200+ $sql = "SELECT " . ($lang ? " C.*, O.* " : " C.* " ) . " FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
 201+ . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_broader as R ON R.broad = C.id ";
 202+
 203+ if ( $lang ) $sql .= " LEFT JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND C.id = O.global_concept ";
 204+
 205+ $sql .= " WHERE R.narrow = ".(int)$id
 206+ . " GROUP BY C.id "
199207 . " LIMIT " . (int)$limit;
200208
201209 return $this->getRows($sql);
@@ -203,15 +211,19 @@
204212 function getNarrowerForConcept( $id, $lang = null, $limit = 100 ) {
205213 global $wwTablePrefix, $wwThesaurusDataset;
206214
207 - $sql = "SELECT C.* FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
208 - . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_broader as R ON R.narrow = C.id "
209 - . " WHERE R.broad = ".(int)$id
 215+ $sql = "SELECT " . ($lang ? " C.*, O.* " : " C.* " ) . " FROM {$wwTablePrefix}_{$wwThesaurusDataset}_concept as C "
 216+ . " JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_broader as R ON R.narrow = C.id ";
 217+
 218+ if ( $lang ) $sql .= " LEFT JOIN {$wwTablePrefix}_{$wwThesaurusDataset}_origin as O ON O.lang = \"" . mysql_real_escape_string($lang) . "\" AND C.id = O.global_concept ";
 219+
 220+ $sql .= " WHERE R.broad = ".(int)$id
 221+ . " GROUP BY C.id "
210222 . " LIMIT " . (int)$limit;
211223
212224 return $this->getRows($sql);
213225 }
214226
215 - function getPagesForConcept( $id, $lang, $limit = 100 ) {
 227+ function getPagesForConcept( $id, $lang = null, $limit = 100 ) {
216228 global $wwTablePrefix, $wwThesaurusDataset, $wwLanguages;
217229
218230 if ( !$lang ) $lang = array_keys( $wwLanguages );
Index: trunk/WikiWord/WikiWord/src/main/php/wwimages.php
@@ -252,6 +252,7 @@
253253 if (!isset($wwLanguages[$lang])) continue;
254254
255255 foreach ($pages as $page) {
 256+ //FIXME: detect namespace prefix?!
256257 $img = $this->getRelevantImagesOnPage($lang, 0, $page, true);
257258 $images->addImages($img, $lang . ":" . $page, "article", 1);
258259 }
@@ -266,21 +267,27 @@
267268 $pages = $concepts['commons'];
268269
269270 foreach ($pages as $page) {
270 - $img = $this->getRelevantImagesOnPage("commons", 0, $page, false);
271 - $images->addImages($img, "commons:" . $page, "gallery", 0.8);
 271+ if ( $wwFakeCommonsConcepts || !preg_match('/^Category:(.*)$/', $page) ) {
 272+ $img = $this->getRelevantImagesOnPage("commons", 0, $page, false);
 273+ $images->addImages($img, "commons:" . $page, "gallery", 0.8);
 274+ }
272275
273276 if ($max && $images->size()>$max) {
274277 $this->addImageTags($images);
275278 return $images->listImages($max);
276279 }
277280
278 - $img = $this->getImagesInCategory("commons", $page); //FIXME: resource mapping
279 - if ($img) $images->addImages($img, "commons:category:" . $page, "category", 0.5);
280 - else if ($wwFakeCommonsConcepts && $wwFakeCommonsPlural && !preg_match('/s$/', $page)) {
281 - $cname = $page."s";
 281+ if ( $wwFakeCommonsConcepts || preg_match('/^Category:(.*)$/', $page, $m) ) {
 282+ if ( @$m[1] ) $page = $m[1]; //hack
 283+ $img = $this->getImagesInCategory("commons", $page);
282284
283 - $img = $this->getImagesInCategory("commons", $cname); //FIXME: resource mapping
284 - $images->addImages($img, "commons:category:" . $cname, "category(pl)", 0.5);
 285+ if ($img) $images->addImages($img, "commons:category:" . $page, "category", 0.5);
 286+ else if ($wwFakeCommonsConcepts && $wwFakeCommonsPlural && !preg_match('/s$/', $page)) {
 287+ $cname = $page."s";
 288+
 289+ $img = $this->getImagesInCategory("commons", $cname);
 290+ $images->addImages($img, "commons:category:" . $cname, "category(pl)", 0.5);
 291+ }
285292 }
286293 }
287294 }
Index: trunk/WikiWord/WikiWord/src/main/php/config.sample.php
@@ -17,6 +17,7 @@
1818 #error_reporting(E_ALL);
1919
2020 $wwMaxPreviewImages = 8;
 21+$wwMaxGalleryImages = 200;
2122 $wwThumbSize = 120;
2223 $wwThumbnailURL = "http://toolserver.org/tsthumb/tsthumb?f={name}&domain=commons.wikimedia.org&w={width}&h={height}";
2324 $wwImagePageURL = "http://commons.wikimedia.org/wiki/File:{name}";

Status & tagging log