<?php
if ( defined( 'WS_TOC' ) ) {
$info['description'] = "Finds images for a given article, using interwiki links";
return;
}
define( 'WS_WEB', true );
require_once('common/WebInit.php');
require_once('WikiQuery.php');
require_once( "WikiSelector.php" );
function getImageTemplates(&$wiki, $name) {
$sql = 'SELECT tl_namespace, tl_title
FROM templatelinks
JOIN page on tl_from = page_id
WHERE page_namespace = ' . NS_IMAGE .' AND page_title = ' . $wiki->wikiDB->addQuotes($name);
$res = $wiki->wikiDB->query($sql, "getUsedImages");
if (!$res) return false;
$templates = array();
while ( $row = $wiki->wikiDB->fetchRow($res) ) {
$n = $row['tl_title'];
if ($row['tl_namespace']==NS_MAIN) $n = ':' . $n;
else if ($row['tl_namespace']!=NS_TEMPLATE) $n = $wiki->getNsText($row['tl_namespace']) . ':' . $n;
$row['namespace'] = $row['tl_namespace'];
$row['title'] = $row['tl_title'];
$row['fulltitle'] = $n;
$row['class'] = strtolower($wiki->classifyTag($n));
$row['url'] = $wiki->makePageLink($row['tl_title'], $row['tl_namespace']);
$templates[] = $row;
}
return $templates;
}
function getUsedImages(&$wiki, $title, $ns = NULL) {
global $commonsWiki;
$commonsDB = $commonsWiki->wikiDB->getProperty('mDBname');
if (is_null($ns)) {
$e = $wiki->makeLinkEntry($title);
$ns = $e['namespacenum'];
$title = $e['link'];
}
#print "\n+++ ".$wiki->domain.": $ns:$title";
#print "\n";
$sql= 'SELECT il_to as img_name,
if (L.img_name is NULL, 1, 0) as from_commons,
if (L.img_name is NULL, C.img_width, L.img_width) as img_width,
if (L.img_name is NULL, C.img_height, L.img_height) as img_height,
(select count(*) from imagelinks as X where X.il_to = IL.il_to) as weight
FROM imagelinks as IL
JOIN page on page_id = IL.il_from
LEFT JOIN image as L on IL.il_to = L.img_name
LEFT JOIN '.$commonsDB.'.image as C on IL.il_to = C.img_name
WHERE (L.img_name is not null OR C.img_name is not null)
AND page_namespace = ' . (int)$ns . ' AND page_title = ' . $wiki->wikiDB->addQuotes($wiki->asDBKey($title));
#print "\n-------------------------------\n$sql\n----------------------------\n";
$res = $wiki->wikiDB->query($sql, "getUsedImages");
if (!$res) return false;
$images = array();
while ( $row = $wiki->wikiDB->fetchObject($res) ) {
#print "\n--- ";
#print_r($row); print "\n";
$w = $row->from_commons ? $commonsWiki : $wiki;
$row->templates = getImageTemplates( $w, $row->img_name);
$images[] = $row;
}
return $images;
}
/*
function getUsedImageIds(&$wiki, $title, $ns = NULL) {
global $commonsDomain;
$images = getUsedImages($wiki, $title, $ns);
$imageIds = array();
foreach ($images as $img) {
$imgid = ( $img->from_commons ? $commonsDomain : $wiki->domain ) . '|' . $img->img_name;
$imageIds[] = $imgid;
}
return $imageIds;
}
*/
function addUsedImages(&$wiki, &$result, $page, $home = false) {
global $commonsWiki;
$title = $page['title'];
$ns = $page['namespace'];
$images = getUsedImages($wiki, $title, $ns);
foreach ($images as $img) {
$imgid = ( $img->from_commons ? $commonsWiki->domain : $wiki->domain ) . '|' . $img->img_name;
if (!isset($result[$imgid])) {
$result[$imgid] = array(
'home' => false,
'location' => $img->from_commons ? $commonsWiki->domain : $wiki->domain,
'name' => $img->img_name,
'width' => $img->img_width,
'height' => $img->img_height,
'weight' => $img->weight,
'usage' => array(),
'wikilang' => $img->from_commons ? NULL : $wiki->language,
'wikifamily' => $img->from_commons ? NULL : $wiki->family,
'templates' => $img->templates,
);
$result[$imgid]['thumbnailHTML'] = $img->from_commons ? $commonsWiki->getThumbnailHTML($result[$imgid])
: $wiki->getThumbnailHTML($result[$imgid]) ;
}
$result[$imgid]['usage'][] = $page;
if ($home) $result[$imgid]['home'] = true;
}
}
function findImages(&$wiki, $title, $ns = NULL) {
if (is_null($ns)) {
$e = $wiki->makeLinkEntry($title);
$ns = $e['namespacenum'];
$title = $e['link'];
}
#print "\n*** ".$wiki->domain.": $ns:$title";
#print "\n";
if (!is_int($ns)) $ns = $wiki->getNsIndex($ns);
$images = array();
$pages = array();
$page = array(
'lang' => $wiki->language,
'domain' => $wiki->domain,
'title' => $title,
'namespace' => $ns,
'fulltitle' => $ns ? $wiki->getNsText($ns) . ':' . $title : $title,
'url' => $wiki->makePageLink($title, $ns),
);
$pages[] = $page;
addUsedImages($wiki, $images, $page, true);
$sql= 'SELECT ll_lang, ll_title, iw_url
FROM langlinks
JOIN page on page_id = ll_from
JOIN interwiki on iw_prefix = ll_lang
WHERE page_namespace = ' . (int)$ns . ' AND page_title = ' . $wiki->wikiDB->addQuotes($wiki->asDBKey($title));
#print "\n-------------------------------\n$sql\n----------------------------\n";
$res = $wiki->wikiDB->query($sql, "findImages");
if (!$res) return false;
while ( $row = $wiki->wikiDB->fetchObject($res) ) {
#print "\n=== ";
#print_r($row); print "\n";
preg_match('!^\w+://([^/]+)/!', $row->iw_url, $m);
$domain = $m[1];
$rwiki = WikiAccess::newInstance( $domain );
if (!$rwiki) continue;
$e = $wiki->makeLinkEntry($row->ll_title);
$pns = $e['namespacenum'];
$ptitle = $e['link'];
$page = array(
'lang' => $rwiki->language,
'domain' => $rwiki->domain,
'title' => $ptitle,
'namespace' => $pns,
'fulltitle' => $pns ? $rwiki->getNsText($pns) . ':' . $ptitle : $ptitle,
'url' => $rwiki->makePageLink($ptitle, $pns),
);
$pages[] = $page;
addUsedImages($rwiki, $images, $page);
$rwiki->close();
}
return array($pages, $images);
}
function printPages($pages) {
foreach ($pages as $page) {
$cls = 'class="page"';
//if ($homeDomain == $page['domain']) $cls= 'class="domain-home"';
$s = '';
$s.= "<span $cls>";
$s.= ' <a href="';
$s.= escapeHtml($page['url']);
$s.= '">';
$s.= escapeHtml($page['lang']);
$s.= ':';
$s.= escapeHtml($page['fulltitle']);
$s.= '</a>';
$s.= '</span>';
print $s;
}
}
function printImage($info, $homeDomain) {
global $wsgThumbnailRedirector, $wsgFileNamespace;
$imageCell = $info['thumbnailHTML'];
$infoCell = '';
$infoCell.= '<div class="imagedata">';
if ($info['width'] && $info['height']) {
$infoCell.= '<small class="pixels">';
$infoCell.= $info['width'];
$infoCell.= 'x';
$infoCell.= $info['height'];
$infoCell.= '</small> ';
}
if ($info['weight']) {
$infoCell.= '<small class="weight">used ';
$infoCell.= $info['weight'];
$infoCell.= 'x</small> ';
}
$infoCell.= "<tt>[[$wsgFileNamespace:";
$infoCell.= escapeHtml($info['name']);
$infoCell.= ']]</tt> ';
$cls = 'class="location-bad"';
if ($homeDomain == $info['location']) $cls= 'class="location-home"';
else if ("commons.wikimedia.org" == $info['location']) $cls= 'class="location-commons"';
$needsCopy = ($info['location'] != $homeDomain && $info['location'] != "commons.wikimedia.org");
$infoCell.= ' <small>(';
$infoCell.= "<span $cls>";
$infoCell.= escapeHtml($info['location']);
$infoCell.= '</span>';
if ($needsCopy) {
if ($wsgThumbnailRedirector) $downloadURL = "$wsgThumbnailRedirector?f=" . urlencodeTitle($info['name']) . "&domain={$info['location']}";
else $downloadURL = "http://{$info['location']}/wiki/Special:Filepath/" . urlencodeTitle($info['name']);
$uploadURL = "http://commons.wikimedia.org/wiki/Special:Upload?wpDestFile=" . urlencodeTitle($info['name']);
$helperURL = "http://tools.wikimedia.de/~magnus/commonshelper.php?language=".$info['wikilang']."&image=".urlencodeTitle($info['name'])."&newname=&project=".$info['wikifamily']."&doit=Get+text";
$infoCell.= ' - <a href="'.escapeHtml($downloadURL).'">down</a>';
$infoCell.= '/<a href="'.escapeHtml($uploadURL).'">up</a>';
$infoCell.= '/<a href="'.escapeHtml($helperURL).'">helper</a>';
}
$infoCell.= '); ';
$infoCell.= '</div>';
//sort($info['usage']); //TODO: sort by lang id
$infoCell.= '<div class="usage">';
$infoCell.= 'Usage: ';
foreach ($info['usage'] as $page) {
$cls = 'class="domain"';
if ($homeDomain == $page['domain']) $cls= 'class="domain-home"';
$infoCell.= "<span $cls>";
$infoCell.= ' <a href="';
$infoCell.= escapeHtml($page['url']);
$infoCell.= '">';
$infoCell.= escapeHtml($page['lang']);
$infoCell.= ':';
$infoCell.= escapeHtml($page['fulltitle']);
$infoCell.= '</a>';
$infoCell.= '</span>';
}
$infoCell.= '</div>';
if ($info['templates']) {
$infoCell.= '<div class="tags">';
$infoCell.= 'Tags: ';
foreach ($info['templates'] as $template) {
$tcls = $template['class'];
if (!$tcls) $tcls = 'other';
$cls = 'class="template-'.$tcls.'"';
$infoCell.= "<span $cls>";
$infoCell.= ' <a href="';
$infoCell.= escapeHtml($template['url']);
$infoCell.= '">';
$infoCell.= escapeHtml($template['fulltitle']);
$infoCell.= '</a>';
$infoCell.= '</span>';
}
$infoCell.= '</div>';
}
$wcls= '';
if ($info['weight']>1000) $wcls = "weight-1000";
else if ($info['weight']>100) $wcls = "weight-100";
else if ($info['weight']>10) $wcls = "weight-10";
$cls = '';
if ($info['home']) $cls = 'class="used '.$wcls.'"';
else if ($info['location'] == 'commons.wikimedia.org') $cls = 'class="commons '.$wcls.'"';
else $cls = 'class="foreign '.$wcls.'"';
print "\t\t<tr $cls>";
print "<td class='cell-image'>$imageCell</td>";
print "<td class='cell-info'>$infoCell</td>";
print "</tr>\n";
}
function imageLocationValue($img) {
if ($img['home']) return 3;
else if ($img['location'] == "commons.wikimedia.org") return 1;
else return 2;
}
function imageCompare($a, $b) {
$x = imageLocationValue($a);
$y = imageLocationValue($b);
if ($x==$y) {
$x = $a['weight'];
$y = $b['weight'];
}
if ($x==$y) {
$x = $b['width'] * $b['height'];
$y = $a['width'] * $a['height'];
}
return $x - $y;
}
function printImages($homeDomain, $images) {
$images = array_values($images);
usort( $images, "imageCompare" );
foreach ($images as $info) {
printImage($info, $homeDomain);
}
}
$wiki = NULL;
$commonsWiki = NULL;
$wikiSelector= new WikiSelector();
$domain = $wikiSelector->domain;
$title = @$_GET['page'];
if ($domain && $title) {
$wiki = WikiAccess::newInstance( $domain );
$commonsWiki = WikiAccess::newInstance( 'commons.wikimedia.org' );
}
header('Content-Type: text/html; charset=utf-8');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>FindImages</title>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<link rel="stylesheet" href="wikisense.css" type="text/css">
<style type="text/css">
.location-bad { color:#440000; }
.location-commons { color:green; }
.location-home { color:green; }
.domain-home { color:green; }
td { vertical-align: top; }
td.cell-image { text-align: center; }
td.cell-info { text-align: left; }
tr.foreign td { background-color:#E0B060; }
tr.used td { background-color:#CCCCCC; }
tr.commons td { background-color:#D0F0D0; }
tr.weight-10 td { color:#333333; }
tr.weight-100 td { color:#666666; }
tr.weight-1000 td { color:#999999; }
tr.weight-10 .weight { color:#990000; }
tr.weight-100 .weight { color:#CC0000; }
tr.weight-1000 .weight { color:#FF0000; }
.template-license { background-color:#88FF88; }
.template-highlite-tag { background-color:#FFEEAA; }
.template-deprecated { background-color:#FFEEAA; }
.template-custom { background-color:#AAFFAA; }
.template-delete { font-weight: bold; background-color:#FF8888; }
.template-problem { background-color:#FFDD88; }
.template-meta { background-color:#AAAAFF; }
.template-magic { background-color:#AAAAAA; }
.template-unknown { background-color:#FFAAFF; }
.template-other { font-size:78%; }
.template-language { font-size:78%; }
.template-fatured { font-weight: bold; background-color:#00FF00; }
tr.weight-100 { display:none; }
tr.weight-1000 { display:none; }
</style>
</head>
<body>
<? wsfHeader(); ?>
<div id='content' class='centerbox'>
<h1>FindImages</h1>
<?
if (@$wiki) $replag= $wiki->getSecondsSinceLastEdit( true );
else $replag= wsfGetReplagHTML();
if ($replag) {
print "<div class='replag'>$replag</div>";
}
?>
<p>
This tool helps you to find images for a given wiki page.
It works by looking for images used on those pages on other wikis,
that are interlanguage-linked by the page you gave.
For example, to find images for "moon" (english), it will look for
images used on "Mond" (german), "lune" (french), "luna" (italian), etc.
If you get no or too few results, see if you can add more interlangauge-links
to the page (and beware the replication lag).
</p>
<div class="querybox">
<form action="FindImages.php">
<p>
Wiki: <? $wikiSelector->printSelector(false); ?>
</p>
<p>
Page: <input id="page" name="page" type="text" width="16" value="<?= escapeHtml($title)?>"/>
</p>
<p>
<input id="go" name="go" type="submit" value="find images"/>
</p>
</form>
</div>
<? if ($wiki && $title): ?>
<? list($pages, $images) = findImages($wiki, $title); ?>
<p>
Pages: <? printPages($pages); ?>
</p>
<table summary="images">
<tr>
<th>Image</th>
<th>Info</th>
</tr>
<?
list($pages, $images) = findImages($wiki, $title);
printImages($domain, $images);
?>
</table>
<? endif; ?>
</div>
<? wsfFooter(false); ?>
</body>
</html>
<?
if ($domain && $title) {
$wiki->close();
$commonsWiki->close();
}
?>
FindImages.php
application/x-php, 14056 bytes (load raw)

