r64877 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64876‎ | r64877 | r64878 >
Date:13:53, 10 April 2010
Author:daniel
Status:deferred
Tags:
Comment:
image meta-data, tags, etc
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/php/common/wwclient.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/common/wwimages.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/config.sample.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/search/wikipics.php (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/php/search/wikipics.php
@@ -42,7 +42,7 @@
4343 return $pics;
4444 }
4545
46 -function printConceptImageList($concept, $class = "tersetable", $columns = 5, $limit = false ) {
 46+function printConceptImageList($concept, $terse = false, $columns = 5, $limit = false ) {
4747 global $utils, $wwThumbSize;
4848
4949 if (!$concept) return false;
@@ -52,17 +52,24 @@
5353
5454 if (!$images) return;
5555
 56+ $class = $terse ? "terseImageTable" : "";
 57+
5658 $imgList = array_values($images);
5759
 60+ $cw = $wwThumbSize + 32; //FIXME: magic number, use config!
 61+
5862 ?>
59 - <table class="imageTable <?php print $class; ?>" summary="images">
 63+ <table class="imageTable <?php print $class; ?>" summary="images" width="<?php print $columns*$cw; ?>">
 64+ <colgroup span="<?php print $columns; ?>" width="<?php print $cw; ?>">
 65+ </colgroup>
 66+
6067 <?php
6168 $i = 0;
6269 $c = count($images);
6370 if (!$limit || $limit > $c) $limit = $c;
6471
6572 while ($i < $limit) {
66 - $i = printConceptImageRow($imgList, $i, $limit, $columns);
 73+ $i = printConceptImageRow($imgList, $i, $terse, $columns, $limit);
6774 }
6875 ?>
6976 </table>
@@ -71,10 +78,10 @@
7279 return $i < $c;
7380 }
7481
75 -function printConceptImageRow($images, $from, $limit, $columns = 5) {
 82+function printConceptImageRow($images, $from, $terse, $columns = 5, $limit = false) {
7683 global $wwThumbSize, $utils;
7784
78 - $cw = $wwThumbSize + 20;
 85+ $cw = $wwThumbSize + 32; //FIXME: magic number, use config!
7986 $cwcss = $cw . "px";
8087
8188 $to = $from + $columns;
@@ -91,25 +98,82 @@
9299
93100 print "\n\t</tr>\n";
94101
95 - print "\t<tr class=\"imageMetaRow\">\n";
96 -
97 - for ($i = $from; $i<$to; $i += 1) {
98 - $img = $images[$i];
 102+ if (!$terse) {
 103+ print "\t<tr class=\"imageMetaRow\">\n";
 104+
 105+ for ($i = $from; $i<$to; $i += 1) {
 106+ $img = $images[$i];
99107
100 - $title = $img['name'];
101 - $title = str_replace("_", " ", $title);
102 - $title = preg_replace("/\\.[^.]+$/", "", $title);
 108+ $title = $img['name'];
 109+ $title = str_replace("_", " ", $title);
 110+ $title = preg_replace("/\\.[^.]+$/", "", $title);
103111
104 - print "\t\t<td class=\"imageMetaCell\" width=\"$cw\" align=\"left\" valign=\"top\" style=\"width: $cwcss\">";
105 - print "<div class=\"imageTitle\">" . htmlspecialchars( $title ) . "</div>";
106 - print "</td>\n";
 112+ $info = getImageInfo($img);
 113+ $labels = getImageLabels($img);
 114+
 115+ print "\t\t<td class=\"imageMetaCell\" width=\"$cw\" align=\"left\" valign=\"top\" style=\"width: $cwcss\">";
 116+ print "<div class=\"imageTitle\">" . htmlspecialchars( $title ) . "</div>";
 117+
 118+ if ($info) {
 119+ print "<div class=\"imageInfo\">";
 120+ printList($info, false, "terselist");
 121+ print "</div>";
 122+ }
 123+
 124+ if ($labels) {
 125+ print "<div class=\"imageLabels\">";
 126+ printList($labels, false, "terselist");
 127+ print "</div>";
 128+ }
 129+
 130+ print "</td>\n";
 131+ }
 132+
 133+ print "\n\t</tr>\n";
107134 }
108 -
109 - print "\n\t</tr>\n";
110135
111136 return $to;
112137 }
113138
 139+function getImageInfo($img) {
 140+ if (empty($img['meta'])) return false;
 141+
 142+ $info = array();
 143+ extract($img['meta']);
 144+
 145+ $info[] = htmlspecialchars("$img_minor_mime");
 146+
 147+ if ( $img_media_type == "BITMAP" ) {
 148+ $info[] = htmlspecialchars("{$img_width}x{$img_height}");
 149+ }
 150+
 151+ if ( $img_size > 1024*1024 ) $info[] = htmlspecialchars(sprintf("%1.0fM", $img_size / 1024.0*1024.0));
 152+ else if ( $img_size > 1024 ) $info[] = htmlspecialchars(sprintf("%1.0fK", $img_size / 1024.0));
 153+ else $info[] = htmlspecialchars(sprintf("%dB", $img_size));
 154+
 155+ return $info;
 156+}
 157+
 158+function getImageLabels($img) {
 159+ global $wwLabelPatterns;
 160+
 161+ if (!$wwLabelPatterns || empty($img['tags'])) return false;
 162+
 163+ $labels = array();
 164+
 165+ foreach ( $img['tags'] as $tag ) {
 166+ foreach ( $wwLabelPatterns as $pattern => $label ) {
 167+ if ( preg_match($pattern, $tag) ) {
 168+ $labels[$label] = 1;
 169+ break;
 170+ }
 171+ }
 172+ }
 173+
 174+ $labels = array_keys($labels);
 175+ return $labels;
 176+}
 177+
114178 function getConceptDetailsURL($langs, $concept) {
115179 global $wwSelf;
116180
@@ -184,7 +248,7 @@
185249 <?php
186250 foreach ($items as $item) {
187251 if ( $escape ) $item = htmlspecialchars($item);
188 - print "\t\t<li>" . $item . "</li>\n";
 252+ print "<li>" . trim($item) . "</li>";
189253 }
190254 ?>
191255 </ul>
@@ -232,10 +296,11 @@
233297 $related = array();
234298 if ( @$concept['similar'] ) $related += $concept['similar'];
235299 if ( @$concept['related'] ) $related += $concept['related'];
236 - if ( @$concept['narrower'] ) $related += $concept['narrower'];
237300
238301 if (isset($concept['broader'])) $related = array_key_diff($related, $concept['broader']);
 302+ if (isset($concept['narrower'])) $related = array_key_diff($related, $concept['narrower']);
239303
 304+ sortConceptList($related);
240305 return $related;
241306 }
242307
@@ -267,12 +332,55 @@
268333 else return "little";
269334 }
270335
 336+function stripSections(&$concepts) {
 337+ foreach ($concepts as $k => $c) {
 338+ foreach ($c['name'] as $l => $n) {
 339+ if (preg_match('/#/', $n)) {
 340+ unset($concepts[$k]);
 341+ break;
 342+ }
 343+ }
 344+ }
 345+}
 346+
 347+function compareConceptScoreAndName($a, $b) {
 348+ if (isset($a['score']) && isset($b['score']) && $a['score'] != $b['score']) {
 349+ if ( $a['score'] > $b['score'] ) return 1;
 350+ else return -1;
 351+ } else {
 352+ if ( $a['name'] > $b['name'] ) return 1; //XXX: unicode collation??
 353+ else return -1;
 354+ }
 355+
 356+ return 0;
 357+}
 358+
 359+function sortConceptList(&$concepts) {
 360+ usort($concepts, 'compareConceptScoreAndName');
 361+}
 362+
 363+function mangleConcept(&$concept) {
 364+ stripSections($concept['narrower']);
 365+
 366+ sortConceptList($concept['narrower']);
 367+ sortConceptList($concept['related']);
 368+ sortConceptList($concept['similar']);
 369+ sortConceptList($concept['broader']);
 370+}
 371+
271372 function printConcept($concept, $langs, $terse = true) {
272 - global $utils, $wwMaxPreviewImages, $wwMaxGalleryImages, $wwMaxPreviewLinks, $wwGalleryColumns;
 373+ global $utils, $wwMaxPreviewImages, $wwMaxGalleryImages, $wwMaxPreviewLinks, $wwMaxDetailLinks, $wwGalleryColumns;
273374
274375 extract( $concept );
275 - $wclass = getWeightClass($score);
276 - $lclass = $terse ? "terselist" : "list";
 376+ if (@$score) $wclass = getWeightClass($score);
 377+ else $wclass = "";
 378+
 379+ #$lclass = $terse ? "terselist" : "list";
 380+ $lclass = "terselist";
 381+
 382+ $name = $utils->pickLocal($concept['name'], $langs);
 383+ $name = str_replace("_", " ", $name);
 384+
277385 $gallery = getImagesAbout($concept, $terse ? $wwMaxPreviewImages*2 : $wwMaxGalleryImages+1 );
278386
279387 if (is_array($definition)) $definition = $utils->pickLocal($definition, $langs);
@@ -289,14 +397,29 @@
290398 <tr class="row_images">
291399 <td class="cell_images" colspan="2">
292400 <?php
293 - $more = printConceptImageList( $gallery, $terse ? "tersegallery" : "gallery", $wwGalleryColumns, $terse ? $wwMaxPreviewImages : $wwMaxGalleryImages );
 401+ if (!$gallery) print "<p class=\"notice\">No images found for concept <em>".htmlspecialchars($name)."</em>.</p>";
 402+ else $more = printConceptImageList( $gallery, $terse, $wwGalleryColumns, $terse ? $wwMaxPreviewImages : $wwMaxGalleryImages );
294403 ?>
295404 </td>
 405+ <?php if ($gallery) { ?>
296406 <td class="cell_more_images" colspan="1" width="100%" style="vertical-align:bottom; padding: 1ex; font-size:normal;">
297 - <?php if ($terse && $more) print " <div><strong class=\"more\">[" . getConceptDetailsLink($langs, $concept, "more...") . "]</strong></div>"; ?>
 407+ <?php if ($terse) print " <div><strong class=\"more\">[" . getConceptDetailsLink($langs, $concept, "more/details...") . "]</strong></div>"; ?>
298408 </td>
 409+ <?php } ?>
299410 </tr>
300411
 412+ <?php if (@$concept['narrower']) { ?>
 413+ <tr class="row_narrower">
 414+ <td class="cell_related" colspan="3">
 415+ <strong class="label">Narrower:</strong>
 416+ <?php
 417+ $more = printConceptList( $langs, $concept['narrower'], $lclass, $terse ? $wwMaxPreviewLinks : $wwMaxDetailLinks );
 418+ ?>
 419+ <?php if ($terse && $more) print " <strong class=\"more\">[" . getConceptDetailsLink($langs, $concept, "more...") . "]</strong>"; ?>
 420+ </td>
 421+ </tr>
 422+ <?php } ?>
 423+
301424 <?php
302425 $related = getRelatedConceptList($concept);
303426 if ($related) {
@@ -339,13 +462,23 @@
340463 $term = @$_REQUEST['term'];
341464 $lang = @$_REQUEST['lang'];
342465
 466+if ($term!==NULL && preg_match('/^\s*#(\d+)\s*$/', $term, $m)) {
 467+ $conceptId = $m[1];
 468+ $term = NULL;
 469+}
 470+
343471 if (!isset($wwSelf)) $wwSelf = @$_SERVER["PHP_SELF"];
344472
345473 $error = NULL;
346474
347 -if ($lang && !isset($wwLanguages[$lang])) {
348 - $lang = NULL;
349 - $error = "bad language code: $lang";
 475+if ($lang) {
 476+ $ll = preg_split('![,;/|+]!', $lang);
 477+ foreach ($ll as $l) {
 478+ if (!isset($wwLanguages[$l]) && $l != "commons") {
 479+ $error = "bad language code: $l";
 480+ $lang = NULL;
 481+ }
 482+ }
350483 }
351484
352485 if ($wwAPI) $thesaurus = new WWClient($wwAPI);
@@ -365,6 +498,7 @@
366499 $limit = 20;
367500 $norm = 1;
368501
 502+$mode = NULL;
369503 $result = NULL;
370504
371505 $languages = array( $lang, "en", "commons" ); #TODO: make the user define this list
@@ -376,9 +510,11 @@
377511 $t = microtime(true);
378512 try {
379513 if ($lang && $conceptId) {
 514+ $mode = "concept";
380515 $result = $thesaurus->getConceptInfo($conceptId, $lang);
381516 if ( $result ) $result = array( $result ); //hack
382517 } else if ($lang && $term) {
 518+ $mode = "term";
383519 $result = $thesaurus->getConceptsForTerm($lang, $term, $languages, $norm, $limit);
384520 }
385521 } catch (Exception $e) {
@@ -433,8 +569,7 @@
434570
435571 .row_images td { vertical-align: bottom; }
436572
437 - .row_related td { font-size: small; background-color: #F0F7F9; }
438 - .row_category td { font-size: small; background-color: #F0F7F9; }
 573+ .row_related td, .row_category, .row_narrower { font-size: small; background-color: #F0F7F9; }
439574 .row_blank td { font-size: small; }
440575
441576 /*
@@ -467,14 +602,21 @@
468603
469604 .imageTable td.imageCell {
470605 vertical-align: bottom;
471 - padding-top: 1em;
 606+ padding-top: 0.5em;
 607+ padding-right: 1em;
 608+ font-size:33%;
472609 }
473 - .imageTable td.imageCell img { border: 1px solid; }
474 - .imageTable td.imageCell, .imageTable td.imageMetaCell {
 610+
 611+ .imageTable td.imageMetaCell {
 612+ vertical-align: top;
 613+ padding-bottom: 0.5em;
 614+ padding-right: 1em;
475615 font-size:small;
476 - border-spacing: 1em 0;
477 - margin: 1em;
478616 }
 617+
 618+ .imageTable td.imageCell img { border: 1px solid; }
 619+
 620+ .imageInfo { color: #676767 }
479621 </style>
480622 </head>
481623 <body>
@@ -524,12 +666,17 @@
525667 if ($error) {
526668 print "<p class=\"error\">".htmlspecialchars($error)."</p>";
527669 }
 670+
 671+if (!$result && $mode) {
 672+ if ($mode=="concept") print "<p class=\"error\">".htmlspecialchars($error)."</p>";
 673+ else if ($mode=="term") print "<p class=\"notice\">No meanings found for term <em>".htmlspecialchars($term)."</em>.</p>";
 674+}
528675 ?>
529676
530677 <?php
531 -if ($result) {
532 - if ( $conceptId ) $terse = false;
533 - else $terse = true;
 678+if ($result && $mode) {
 679+ if ( $mode == 'concept' ) $terse = false;
 680+ else if ( $mode == 'term' ) $terse = true;
534681 ?>
535682 <table border="0" class="results" cellspacing="0" summary="search results">
536683 <?php
@@ -540,6 +687,7 @@
541688
542689 ?>
543690 <?php
 691+ mangleConcept($row);
544692 $continue= printConcept($row, $languages, $terse);
545693
546694 if (!$continue) break;
Index: trunk/WikiWord/WikiWord/src/main/php/common/wwclient.php
@@ -103,6 +103,11 @@
104104
105105 $rs = $this->query( $param );
106106
 107+ if (!$rs) return $rs;
 108+ if (!isset($rs['concept'])) throw new Exception("bad response, missing concept");
 109+
 110+ $rs = $rs['concept'];
 111+
107112 if (!isset($rs['id'])) $rs['id'] = $id;
108113 if (!isset($rs['lang'])) $rs['lang'] = $lang;
109114
Index: trunk/WikiWord/WikiWord/src/main/php/common/wwimages.php
@@ -74,6 +74,12 @@
7575 }
7676 }
7777
 78+ function addMeta($image, $meta) {
 79+ if (isset($this->images[$image])) {
 80+ $this->images[$image]['meta'] = $meta;
 81+ }
 82+ }
 83+
7884 }
7985
8086 class WWImages extends WWWikis {
@@ -191,6 +197,29 @@
192198 return $list;
193199 }
194200
 201+ function queryMetaForImages($lang, $images) {
 202+ if (!$images) return false;
 203+
 204+ $image_table = $this->getWikiTableName($lang, "image");
 205+
 206+ $sql = "/* queryMetaForImages(" . $this->quote($lang) . ", " . $this->quoteSet($images) . ") */ ";
 207+
 208+ $sql .= " SELECT img_name, img_size, img_width, img_height, img_media_type, img_major_mime, img_minor_mime, img_timestamp, img_sha1 ";
 209+ $sql .= " FROM $image_table as T ";
 210+ $sql .= " WHERE T.img_name IN " . $this->quoteSet($images);
 211+
 212+ return $this->queryWiki($lang, $sql);
 213+ }
 214+
 215+ function getMetaForImages($lang, $images) {
 216+ if (!$images) return array();
 217+
 218+ $rs = $this->queryMetaForImages($lang, $images);
 219+ $list = WWUtils::slurpRows($rs, "img_name");
 220+ mysql_free_result($rs);
 221+ return $list;
 222+ }
 223+
195224 function queryTemplatesOnImagePage($lang, $image) {
196225 $page_table = $this->getWikiTableName($lang, "page");
197226 $templatelinks_table = $this->getWikiTableName($lang, "templatelinks");
@@ -394,6 +423,7 @@
395424 }
396425
397426 if ($max && $images->size()>$max) { //short-cirquit, if we already reached the max
 427+ $this->addImageMeta($images);
398428 $this->addImageTags($images);
399429 return $images->listImages($max);
400430 }
@@ -411,6 +441,7 @@
412442 }
413443 }
414444
 445+ $this->addImageMeta($images);
415446 $this->addImageTags($images);
416447 return $images->listImages($max);
417448 }
@@ -445,6 +476,18 @@
446477 }
447478 }
448479
 480+ function addImageMeta($images) {
 481+ $img = array();
 482+ foreach ($images->images as $image) {
 483+ $img[] = $image['name'];
 484+ }
 485+
 486+ $infoMap = $this->getMetaForImages('commons', $img);
 487+ foreach ($infoMap as $image => $meta) {
 488+ $images->addMeta($image, $meta, "");
 489+ }
 490+ }
 491+
449492 function getThumbnailURL($image, $width = 120, $height = NULL) {
450493 global $wwThumbnailURL;
451494
@@ -484,6 +527,8 @@
485528
486529 if (!@$title) $title = $name;
487530
 531+ $alt = str_replace('_', ' ', $name);
 532+
488533 $tags = "";
489534 if (isset($image['tags'])) {
490535 foreach ($image['tags'] as $tag) {
@@ -491,7 +536,7 @@
492537 }
493538 }
494539
495 - $html= "<img src=\"" . htmlspecialchars($thumb) . "\" alt=\"" . htmlspecialchars($title) . "\" border=\"0\"/>";
 540+ $html= "<img src=\"" . htmlspecialchars($thumb) . "\" alt=\"" . htmlspecialchars($alt) . "\" border=\"0\"/>";
496541 $html= "<a href=\"" . htmlspecialchars($page) . "\" title=\"" . htmlspecialchars($title) . " (score " . htmlspecialchars($image['score']) . ")\" class=\"thumb-link $tags\">$html</a>";
497542
498543 if (is_array($image)) {
@@ -560,15 +605,39 @@
561606 }
562607
563608 function getTagsForImages($lang, $images, $tagTable) {
564 - return array("Quux", "Xyzzy");
 609+ $r = array();
 610+ foreach ($images as $img) {
 611+ $r[$img] = array("assessment:Featured_picture", "license:Cc-by-sa");
 612+ }
 613+
 614+ return $r;
565615 }
566616
 617+ function getMetaForImages($lang, $images) {
 618+ $r = array();
 619+ foreach ($images as $img) {
 620+ $r[$img] = array(
 621+ 'img_name' => $img,
 622+ 'img_size' => 123456,
 623+ 'img_width' => 300,
 624+ 'img_height' => 200,
 625+ 'img_media_type' => 'BITMAP',
 626+ 'img_major_mime' => 'image',
 627+ 'img_minor_mime' => 'jpeg',
 628+ 'img_timestamp' => '20090808132422',
 629+ );
 630+ }
 631+
 632+ return $r;
 633+ }
 634+
 635+
567636 function queryTemplatesOnImagePage($lang, $image) {
568637 throw new Exception( __METHOD__ . " not implemented" );
569638 }
570639
571640 function getTemplatesOnImagePage($lang, $image) {
572 - return array("Quux", "Xyzzy");
 641+ return array("Cc-by-sa", "Featured_picture");
573642 }
574643
575644 function queryCategoriesOfImagePage($lang, $image) {
Index: trunk/WikiWord/WikiWord/src/main/php/config.sample.php
@@ -60,4 +60,15 @@
6161 'assessment:Picture_of_the_day' => 2.5,
6262 'assessment:Quality_image' => 2.0,
6363 'assessment:Featured_picture' => 3.0,
 64+);
 65+
 66+$wwLabelPatterns = array(
 67+ '/^assessment:.*_of_the_day([-_].*|$)/' => "PotD",
 68+ '/^assessment:(Featured|Former_featured_picture).*/' => 'FP',
 69+ '/^assessment:Quiality.*/' => 'QI',
 70+ '/^assessment:(Valued|Former_valued_images|Images_used_in_valued_image_sets).*/' => 'VI',
 71+ '/^license:PD([-_].*|$)/' => 'PD',
 72+ '/^license:GFDL([-_].*|$)/' => 'GFDL',
 73+ '/^license:CC-BY-SA([-_].*|$)/i' => 'CC-BY-SA',
 74+ '/^license:CC-BY([-_].*|$)/i' => 'BY',
6475 );
\ No newline at end of file

Status & tagging log