Index: trunk/WikiWord/WikiWord/src/main/php/wwtest.php |
— | — | @@ -13,7 +13,8 @@ |
14 | 14 | if (!isset($argv[1])) die("usage: wwtest <id>\n"); |
15 | 15 | |
16 | 16 | $id = $argv[1]; |
| 17 | +$max = @$argv[2]; |
17 | 18 | |
18 | | -$images = $utils->getImagesAbout($id); |
| 19 | +$images = $utils->getImagesAbout($id, $max); |
19 | 20 | |
20 | 21 | print_r($images); |
\ No newline at end of file |
Index: trunk/WikiWord/WikiWord/src/main/php/wwutils.php |
— | — | @@ -3,6 +3,9 @@ |
4 | 4 | if (!defined('NS_IMAGE')) |
5 | 5 | define('NS_IMAGE', 6); |
6 | 6 | |
| 7 | +if (!defined('NS_TEMPLATE')) |
| 8 | + define('NS_TEMPLATE', 10); |
| 9 | + |
7 | 10 | class ImageCollection { |
8 | 11 | |
9 | 12 | function __construct() { |
— | — | @@ -10,7 +13,11 @@ |
11 | 14 | } |
12 | 15 | |
13 | 16 | 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; |
15 | 22 | } |
16 | 23 | |
17 | 24 | function size() { |
— | — | @@ -28,7 +35,7 @@ |
29 | 36 | if (!isset($this->images[$image])) { |
30 | 37 | $rec = array( |
31 | 38 | "name" => $image, |
32 | | - "score" => 0 |
| 39 | + "score" => 0, |
33 | 40 | ); |
34 | 41 | } else { |
35 | 42 | $rec = $this->images[$image]; |
— | — | @@ -48,6 +55,26 @@ |
49 | 56 | } |
50 | 57 | } |
51 | 58 | |
| 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 | + |
52 | 79 | } |
53 | 80 | |
54 | 81 | class WWUtils { |
— | — | @@ -107,7 +134,7 @@ |
108 | 135 | } |
109 | 136 | |
110 | 137 | function getWikiInfo($lang) { |
111 | | - global $wwWikiInfoTable, $wwWikiDbName, $wwWikiServerName; |
| 138 | + global $wwWikiInfoTable, $wwWikiDbName, $wwWikiServerName, $wwCommonsServerName; |
112 | 139 | |
113 | 140 | $db = str_replace('{lang}', $lang, $wwWikiDbName); |
114 | 141 | |
— | — | @@ -122,6 +149,8 @@ |
123 | 150 | if (!$info) $info = false; |
124 | 151 | else $info['server'] = str_replace('{num}', $info['server'], $wwWikiServerName); |
125 | 152 | |
| 153 | + if ($lang == "commons" && $wwCommonsServerName) $info['server'] = $wwCommonsServerName; |
| 154 | + |
126 | 155 | return $info; |
127 | 156 | } |
128 | 157 | |
— | — | @@ -447,6 +476,29 @@ |
448 | 477 | return $list; |
449 | 478 | } |
450 | 479 | |
| 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 | + |
451 | 503 | function getTemplateScores($templates, $values = NULL) { |
452 | 504 | global $wwWikiServerName; |
453 | 505 | if ($values === NULL) $values = $wwTemplateScores; |
— | — | @@ -470,7 +522,7 @@ |
471 | 523 | } |
472 | 524 | |
473 | 525 | function getImagesAbout($id, $max = 0) { |
474 | | - global $wwFakeCommonsConcepts, $wwFakeCommonsPlural; |
| 526 | + global $wwFakeCommonsConcepts, $wwFakeCommonsPlural, $wwLanguages; |
475 | 527 | |
476 | 528 | $concepts = $this->getLocalConcepts($id); |
477 | 529 | |
— | — | @@ -482,13 +534,16 @@ |
483 | 535 | |
484 | 536 | foreach ($concepts as $lang => $title) { |
485 | 537 | if ($lang == "commons") continue; |
| 538 | + if (!isset($wwLanguages[$lang])) continue; |
486 | 539 | |
487 | 540 | $img = $this->getRelevantImagesOnPage($lang, 0, $title, true); //FIXME: resource mapping |
488 | 541 | $images->addImages($img, $lang . ":" . $title, "article", 1); |
489 | 542 | } |
490 | 543 | |
491 | | - if ($max && $images->size()>$max) |
| 544 | + if ($max && $images->size()>$max) { |
| 545 | + $this->addImageTags($images); |
492 | 546 | return $images->listImages($max); |
| 547 | + } |
493 | 548 | |
494 | 549 | if (isset($concepts['commons'])) { |
495 | 550 | $title = $concepts['commons']; |
— | — | @@ -496,21 +551,37 @@ |
497 | 552 | $img = $this->getRelevantImagesOnPage("commons", 0, $title, false); //FIXME: resource mapping |
498 | 553 | $images->addImages($img, "commons:" . $title, "gallery", 0.8); |
499 | 554 | |
500 | | - if ($max && $images->size()>$max) |
| 555 | + if ($max && $images->size()>$max) { |
| 556 | + $this->addImageTags($images); |
501 | 557 | return $images->listImages($max); |
| 558 | + } |
502 | 559 | |
503 | 560 | $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"; |
505 | 564 | |
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); |
509 | 567 | } |
510 | 568 | } |
511 | 569 | |
| 570 | + $this->addImageTags($images); |
512 | 571 | return $images->listImages($max); |
513 | 572 | } |
514 | 573 | |
| 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 | + |
515 | 586 | function getThumbnailURL($image, $width = 120, $height = NULL) { |
516 | 587 | global $wwThumbnailURL; |
517 | 588 | |
— | — | @@ -550,11 +621,18 @@ |
551 | 622 | |
552 | 623 | if (!@$title) $title = $name; |
553 | 624 | |
| 625 | + $tags = ""; |
| 626 | + if (isset($image['tags'])) { |
| 627 | + foreach ($image['tags'] as $tag) { |
| 628 | + $tags .= "tag-" . str_replace(":", "-", $tag) . " "; |
| 629 | + } |
| 630 | + } |
| 631 | + |
554 | 632 | $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>"; |
556 | 634 | |
557 | 635 | if (is_array($image)) { |
558 | | - $html .= "<!-- " . htmlspecialchars( str_replace("--", "~~", var_export( $image, true ) ) ) . " -->"; |
| 636 | + $html .= "<!-- " . str_replace("--", "~~", var_export( $image, true ) ) . " -->"; |
559 | 637 | } |
560 | 638 | |
561 | 639 | return $html; |
Index: trunk/WikiWord/WikiWord/src/main/php/config.sample.php |
— | — | @@ -11,7 +11,7 @@ |
12 | 12 | |
13 | 13 | #error_reporting(E_ALL); |
14 | 14 | |
15 | | -$wwMaxPreviewImages = 5; |
| 15 | +$wwMaxPreviewImages = 8; |
16 | 16 | $wwThumbSize = 120; |
17 | 17 | $wwThumbnailURL = "http://toolserver.org/tsthumb/tsthumb?f={name}&domain=commons.wikimedia.org&w={width}&h={height}"; |
18 | 18 | $wwImagePageURL = "http://commons.wikimedia.org/wiki/File:{name}"; |
— | — | @@ -22,4 +22,22 @@ |
23 | 23 | |
24 | 24 | $wwWikiInfoTable = "toolserver.wiki"; |
25 | 25 | $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 |