Stephan Bösch-Plepelits
7 years ago
10 changed files with 303 additions and 4 deletions
-
1lang/de.json
-
1lang/en.json
-
1modulekit.php
-
121src/image.js
-
1src/index.js
-
37src/wikidata.js
-
28src/wikimedia.php
-
108src/wikipedia.js
-
2src/wikipedia.php
-
7style.css
@ -0,0 +1,121 @@ |
|||
var wikidata = require('./wikidata') |
|||
|
|||
function showImage (url, dom) { |
|||
var div = document.createElement('div') |
|||
div.innerHTML = '<a target="_blank" href="' + url +'"><img src="' + url + '"></a>' |
|||
|
|||
dom.appendChild(div) |
|||
} |
|||
|
|||
function showWikimediaImage (value, dom) { |
|||
var url = 'https://commons.wikimedia.org/w/thumb.php?f=' + encodeURIComponent(value) + '&w=' + 235 //imgSize
|
|||
|
|||
var div = document.createElement('div') |
|||
div.innerHTML = '<a target="_blank" href="https://commons.wikimedia.org/wiki/File:' + encodeURIComponent(value) + '"><img src="' + url + '"/></a>' |
|||
|
|||
dom.appendChild(div) |
|||
} |
|||
|
|||
register_hook('show-details', function (data, category, dom, callback) { |
|||
var found = 0 |
|||
var img |
|||
var foundImages = [] |
|||
var div = document.createElement('div') |
|||
div.className = 'images loading' |
|||
var callbackCount = 1 |
|||
|
|||
var l = document.createElement('div') |
|||
l.innerHTML = '<i class="fa fa-spinner fa-pulse fa-fw"></i><span class="sr-only">Loading...</span>' |
|||
l.className = 'loadingIndicator' |
|||
div.appendChild(l) |
|||
|
|||
if (data.object.tags.image) { |
|||
img = data.object.tags.image |
|||
|
|||
if (img.indexOf('File:') === 0) { |
|||
showWikimediaImage(img.substr(5), div) |
|||
foundImages.push(img.substr(5)) |
|||
found++ |
|||
} else if (img.indexOf('http://commons.wikimedia.org/wiki/File:') === 0) { |
|||
showWikimediaImage(decodeURIComponent(img.substr(39)), div) |
|||
foundImages.push(img.substr(39)) |
|||
found++ |
|||
} else if (img.indexOf('https://commons.wikimedia.org/wiki/File:') === 0) { |
|||
showWikimediaImage(decodeURIComponent(img.substr(40)), div) |
|||
foundImages.push(img.substr(40)) |
|||
found++ |
|||
} else { |
|||
foundImages.push(img) |
|||
showImage(img, div) |
|||
found++ |
|||
} |
|||
} |
|||
|
|||
if (data.object.tags.wikidata) { |
|||
found++ |
|||
|
|||
wikidata.load(data.object.tags.wikidata, function (err, result) { |
|||
if (result.claims && result.claims.P18) { |
|||
result.claims.P18.forEach(function (d) { |
|||
img = d.mainsnak.datavalue.value |
|||
|
|||
if (foundImages.indexOf(img) === -1) { |
|||
showWikimediaImage(img, div) |
|||
foundImages.push(img) |
|||
} |
|||
}) |
|||
} |
|||
|
|||
checkCallback() |
|||
}) |
|||
|
|||
callbackCount++ |
|||
} |
|||
|
|||
if (!data.object.tags.wikidata && data.object.tags.wikimedia_commons) { |
|||
var value = data.object.tags.wikimedia_commons |
|||
|
|||
if (value.substr(0, 9) === 'Category:') { |
|||
found++ |
|||
ajax('wikimedia', { page: value }, function (result) { |
|||
if (result.images) { |
|||
result.images.forEach(function (d) { |
|||
if (foundImages.indexOf(d) === -1) { |
|||
showWikimediaImage(d, div) |
|||
foundImages.push(d) |
|||
} |
|||
}) |
|||
} |
|||
|
|||
checkCallback() |
|||
}) |
|||
|
|||
callbackCount++ |
|||
} else if (value.substr(0, 5) === 'File:') { |
|||
found++ |
|||
if (foundImages.indexOf(value.substr(5)) === -1) { |
|||
showWikimediaImage(value.substr(5), div) |
|||
foundImages.push(d) |
|||
} |
|||
} |
|||
} |
|||
|
|||
if (found) { |
|||
h = document.createElement('h3') |
|||
h.appendChild(document.createTextNode(lang('images'))) |
|||
dom.appendChild(h) |
|||
|
|||
dom.appendChild(div) |
|||
} |
|||
|
|||
checkCallback() |
|||
|
|||
function checkCallback () { |
|||
callbackCount-- |
|||
|
|||
if (callbackCount === 0) { |
|||
div.classList.remove('loading') |
|||
callback(null) |
|||
} |
|||
} |
|||
}) |
@ -0,0 +1,37 @@ |
|||
var httpGet = require('./httpGet') |
|||
var loadClash = {} |
|||
var cache = {} |
|||
|
|||
function wikidataLoad (id, callback) { |
|||
if (id in cache) { |
|||
return callback(null, cache[id]) |
|||
} |
|||
|
|||
if (id in loadClash) { |
|||
loadClash[id].push(callback) |
|||
return |
|||
} |
|||
loadClash[id] = [] |
|||
|
|||
httpGet('https://www.wikidata.org/wiki/Special:EntityData/' + id + '.json', function (err, result) { |
|||
result = JSON.parse(result.body) |
|||
|
|||
if (!result.entities || !result.entities[id]) { |
|||
console.log('invalid result', result) |
|||
return callback(err, null) |
|||
} |
|||
|
|||
cache[id] = result.entities[id] |
|||
|
|||
callback(null, result.entities[id]) |
|||
|
|||
loadClash[id].forEach(function (d) { |
|||
d(null, result.entities[id]) |
|||
}) |
|||
delete loadClash[id] |
|||
}) |
|||
} |
|||
|
|||
module.exports = { |
|||
load: wikidataLoad |
|||
} |
@ -0,0 +1,28 @@ |
|||
<?php |
|||
function ajax_wikimedia ($param) { |
|||
$ret = array(); |
|||
|
|||
$wm_url = "https://commons.wikimedia.org/wiki/" . urlencode(strtr($param['page'], array(" " => "_"))); |
|||
|
|||
$content = file_get_contents($wm_url); |
|||
|
|||
$dom = new DOMDocument(); |
|||
$dom->loadHTML($content); |
|||
|
|||
$uls = $dom->getElementsByTagName('ul');//interlanguage-link interwiki-bar');
|
|||
for ($i = 0; $i < $uls->length; $i++) { |
|||
$ul = $uls->item($i); |
|||
|
|||
if ($ul->getAttribute('class') === 'gallery mw-gallery-traditional') { |
|||
$imgs = $ul->getElementsByTagName('img'); |
|||
|
|||
for ($j = 0; $j < $imgs->length; $j++) { |
|||
$ret[] = $imgs->item($j)->getAttribute('alt'); |
|||
} |
|||
} |
|||
} |
|||
|
|||
return array( |
|||
'images' => $ret, |
|||
); |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue