r57715 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57714‎ | r57715 | r57716 >
Date:20:06, 14 October 2009
Author:daniel
Status:deferred
Tags:
Comment:
fixed image ranking
Modified paths:
  • /trunk/WikiWord/WikiWord/src/main/php/config.sample.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwtest.php (modified) (history)
  • /trunk/WikiWord/WikiWord/src/main/php/wwutils.php (modified) (history)

Diff [purge]

Index: trunk/WikiWord/WikiWord/src/main/php/wwtest.php
@@ -13,7 +13,8 @@
1414 if (!isset($argv[1])) die("usage: wwtest <id>\n");
1515
1616 $id = $argv[1];
 17+$max = @$argv[2];
1718
18 -$images = $utils->getImagesAbout($id);
 19+$images = $utils->getImagesAbout($id, $max);
1920
2021 print_r($images);
\ No newline at end of file
Index: trunk/WikiWord/WikiWord/src/main/php/wwutils.php
@@ -3,6 +3,9 @@
44 if (!defined('NS_IMAGE'))
55 define('NS_IMAGE', 6);
66
 7+if (!defined('NS_TEMPLATE'))
 8+ define('NS_TEMPLATE', 10);
 9+
710 class ImageCollection {
811
912 function __construct() {
@@ -10,7 +13,11 @@
1114 }
1215
1316 static function compareRecords($a, $b) {
14 - return $b['score'] - $a['score']; //NOTE: descending
 17+ $d = (float)$b['score'] - (float)$a['score']; //NOTE: descending
 18+
 19+ if ( $d > 0 ) return 1;
 20+ else if ( $d < 0 ) return -1;
 21+ else return 0;
1522 }
1623
1724 function size() {
@@ -28,7 +35,7 @@
2936 if (!isset($this->images[$image])) {
3037 $rec = array(
3138 "name" => $image,
32 - "score" => 0
 39+ "score" => 0,
3340 );
3441 } else {
3542 $rec = $this->images[$image];
@@ -48,6 +55,26 @@
4956 }
5057 }
5158
 59+ function addTags($image, $tags, $prefix = "") {
 60+ global $wwTagScores;
 61+
 62+ if (isset($this->images[$image])) {
 63+ foreach ($tags as $tag => $weight) {
 64+ if (is_int($tag)) {
 65+ $tag = $prefix.$weight;
 66+
 67+ if (isset($wwTagScores[$tag])) $weight = $wwTagScores[$tag];
 68+ else continue;
 69+ } else {
 70+ $tag = $prefix.$tag;
 71+ }
 72+
 73+ $this->images[$image]['score'] += $weight;
 74+ $this->images[$image]['tags'][] = $tag;
 75+ }
 76+ }
 77+ }
 78+
5279 }
5380
5481 class WWUtils {
@@ -107,7 +134,7 @@
108135 }
109136
110137 function getWikiInfo($lang) {
111 - global $wwWikiInfoTable, $wwWikiDbName, $wwWikiServerName;
 138+ global $wwWikiInfoTable, $wwWikiDbName, $wwWikiServerName, $wwCommonsServerName;
112139
113140 $db = str_replace('{lang}', $lang, $wwWikiDbName);
114141
@@ -122,6 +149,8 @@
123150 if (!$info) $info = false;
124151 else $info['server'] = str_replace('{num}', $info['server'], $wwWikiServerName);
125152
 153+ if ($lang == "commons" && $wwCommonsServerName) $info['server'] = $wwCommonsServerName;
 154+
126155 return $info;
127156 }
128157
@@ -447,6 +476,29 @@
448477 return $list;
449478 }
450479
 480+ function queryCategoriesOfImagePage($lang, $image) {
 481+ global $wwTablePrefix, $wwThesaurusDataset, $wwCommonsTablePrefix;
 482+ $page_table = $this->getWikiTableName($lang, "page");
 483+ $categorylinks_table = $this->getWikiTableName($lang, "categorylinks");
 484+
 485+ $sql = "/* queryCategoriesOfImagePage(" . $this->quote($lang) . ", " . $this->quote($image) . ") */ ";
 486+
 487+ $sql .= " SELECT cl_to as category FROM $categorylinks_table as C ";
 488+ $sql .= " JOIN $page_table as P on P.page_id = C.cl_from ";
 489+
 490+ $sql .= " WHERE P.page_title = " . $this->quote($image);
 491+ $sql .= " AND P.page_namespace = " . NS_IMAGE;
 492+
 493+ return $this->queryWiki($lang, $sql);
 494+ }
 495+
 496+ function getCategoriesOfImagePage($lang, $image) {
 497+ $rs = $this->queryCategoriesOfImagePage($lang, $image);
 498+ $list = WWUtils::slurpList($rs, "category");
 499+ mysql_free_result($rs);
 500+ return $list;
 501+ }
 502+
451503 function getTemplateScores($templates, $values = NULL) {
452504 global $wwWikiServerName;
453505 if ($values === NULL) $values = $wwTemplateScores;
@@ -470,7 +522,7 @@
471523 }
472524
473525 function getImagesAbout($id, $max = 0) {
474 - global $wwFakeCommonsConcepts, $wwFakeCommonsPlural;
 526+ global $wwFakeCommonsConcepts, $wwFakeCommonsPlural, $wwLanguages;
475527
476528 $concepts = $this->getLocalConcepts($id);
477529
@@ -482,13 +534,16 @@
483535
484536 foreach ($concepts as $lang => $title) {
485537 if ($lang == "commons") continue;
 538+ if (!isset($wwLanguages[$lang])) continue;
486539
487540 $img = $this->getRelevantImagesOnPage($lang, 0, $title, true); //FIXME: resource mapping
488541 $images->addImages($img, $lang . ":" . $title, "article", 1);
489542 }
490543
491 - if ($max && $images->size()>$max)
 544+ if ($max && $images->size()>$max) {
 545+ $this->addImageTags($images);
492546 return $images->listImages($max);
 547+ }
493548
494549 if (isset($concepts['commons'])) {
495550 $title = $concepts['commons'];
@@ -496,21 +551,37 @@
497552 $img = $this->getRelevantImagesOnPage("commons", 0, $title, false); //FIXME: resource mapping
498553 $images->addImages($img, "commons:" . $title, "gallery", 0.8);
499554
500 - if ($max && $images->size()>$max)
 555+ if ($max && $images->size()>$max) {
 556+ $this->addImageTags($images);
501557 return $images->listImages($max);
 558+ }
502559
503560 $img = $this->getImagesInCategory("commons", $title); //FIXME: resource mapping
504 - $images->addImages($img, "commons:category:" . $title, "category", 0.5);
 561+ if ($img) $images->addImages($img, "commons:category:" . $title, "category", 0.5);
 562+ else if ($wwFakeCommonsConcepts && $wwFakeCommonsPlural && !preg_match('/s$/', $title)) {
 563+ $cname = $title."s";
505564
506 - if ($wwFakeCommonsConcepts && $wwFakeCommonsPlural) {
507 - $img = $this->getImagesInCategory("commons", $title."s"); //FIXME: resource mapping
508 - $images->addImages($img, "commons:category:" . $title."s", "category(pl)", 0.5);
 565+ $img = $this->getImagesInCategory("commons", $cname); //FIXME: resource mapping
 566+ $images->addImages($img, "commons:category:" . $cname, "category(pl)", 0.5);
509567 }
510568 }
511569
 570+ $this->addImageTags($images);
512571 return $images->listImages($max);
513572 }
514573
 574+ function addImageTags($images) {
 575+ foreach ($images->images as $image) {
 576+ $image = $image['name'];
 577+
 578+ $tags = $this->getTemplatesOnImagePage('commons', $image);
 579+ $images->addTags($image, $tags, "Template:");
 580+
 581+ $cats = $this->getCategoriesOfImagePage('commons', $image);
 582+ $images->addTags($image, $cats, "Category:");
 583+ }
 584+ }
 585+
515586 function getThumbnailURL($image, $width = 120, $height = NULL) {
516587 global $wwThumbnailURL;
517588
@@ -550,11 +621,18 @@
551622
552623 if (!@$title) $title = $name;
553624
 625+ $tags = "";
 626+ if (isset($image['tags'])) {
 627+ foreach ($image['tags'] as $tag) {
 628+ $tags .= "tag-" . str_replace(":", "-", $tag) . " ";
 629+ }
 630+ }
 631+
554632 $html= "<img src=\"" . htmlspecialchars($thumb) . "\" alt=\"" . htmlspecialchars($title) . "\" border=\"0\"/>";
555 - $html= "<a href=\"" . htmlspecialchars($page) . "\" title=\"" . htmlspecialchars($title) . "\">$html</a>";
 633+ $html= "<a href=\"" . htmlspecialchars($page) . "\" title=\"" . htmlspecialchars($title) . " (score " . htmlspecialchars($image['score']) . ")\" class=\"thumb-link $tags\">$html</a>";
556634
557635 if (is_array($image)) {
558 - $html .= "<!-- " . htmlspecialchars( str_replace("--", "~~", var_export( $image, true ) ) ) . " -->";
 636+ $html .= "<!-- " . str_replace("--", "~~", var_export( $image, true ) ) . " -->";
559637 }
560638
561639 return $html;
Index: trunk/WikiWord/WikiWord/src/main/php/config.sample.php
@@ -11,7 +11,7 @@
1212
1313 #error_reporting(E_ALL);
1414
15 -$wwMaxPreviewImages = 5;
 15+$wwMaxPreviewImages = 8;
1616 $wwThumbSize = 120;
1717 $wwThumbnailURL = "http://toolserver.org/tsthumb/tsthumb?f={name}&domain=commons.wikimedia.org&w={width}&h={height}";
1818 $wwImagePageURL = "http://commons.wikimedia.org/wiki/File:{name}";
@@ -22,4 +22,22 @@
2323
2424 $wwWikiInfoTable = "toolserver.wiki";
2525 $wwWikiDbName = "{lang}wiki_p";
26 -$wwWikiServerName = "sql-s{num}";
\ No newline at end of file
 26+$wwWikiServerName = "sql-s{num}";
 27+
 28+$wwCommonsServerName = null;
 29+
 30+$wwTagScores = array(
 31+ 'Category:Featured_pictures_on_Wikimedia_Commons' => 3,
 32+ 'Category:Featured_pictures_on_Wikipedia,_German' => 3,
 33+
 34+ 'Template:Former_featured_picture' => 2.8,
 35+
 36+ 'Template:Media_of_the_day' => 2.5,
 37+ 'Template:Picture_of_the_day' => 2.5,
 38+
 39+ 'Category:Quality_images' => 2.0,
 40+
 41+ 'Category:Valued_image' => 1.4,
 42+ 'Category:Former_valued_images' => 1.3,
 43+ 'Category:Images_used_in_valued_image_sets' => 1.2,
 44+);
\ No newline at end of file

Status & tagging log