mirror of
https://github.com/Metastem/Wikiless.git
synced 2025-06-28 21:19:51 +00:00
add support for maps.wikimedia.org map images, fixes #1
This commit is contained in:
parent
9dfe1e76d2
commit
a89453e967
5 changed files with 114 additions and 9 deletions
1
media/.gitignore
vendored
1
media/.gitignore
vendored
|
@ -2,3 +2,4 @@
|
||||||
*
|
*
|
||||||
# Except this file
|
# Except this file
|
||||||
!.gitignore
|
!.gitignore
|
||||||
|
!map.png
|
||||||
|
|
BIN
media/map.png
Normal file
BIN
media/map.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 10 KiB |
|
@ -30,7 +30,14 @@ module.exports = (app, utils) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(req.url.startsWith('/media')) {
|
if(req.url.startsWith('/media')) {
|
||||||
let media = await proxyMedia(req)
|
let media
|
||||||
|
|
||||||
|
if(req.url.startsWith('/media/maps_wikimedia_org/')) {
|
||||||
|
media = await proxyMedia(req, 'maps.wikimedia.org')
|
||||||
|
} else {
|
||||||
|
media = await proxyMedia(req)
|
||||||
|
}
|
||||||
|
|
||||||
if(media.success === true) {
|
if(media.success === true) {
|
||||||
return res.sendFile(media.path)
|
return res.sendFile(media.path)
|
||||||
} else {
|
} else {
|
||||||
|
@ -53,6 +60,10 @@ module.exports = (app, utils) => {
|
||||||
return handleWikiPage(req, res, '/w/')
|
return handleWikiPage(req, res, '/w/')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
app.get('/wiki/Special:Map/*', (req, res, next) => {
|
||||||
|
return handleWikiPage(req, res, '/wiki/Map')
|
||||||
|
})
|
||||||
|
|
||||||
app.get('/', (req, res, next) => {
|
app.get('/', (req, res, next) => {
|
||||||
return handleWikiPage(req, res, '/')
|
return handleWikiPage(req, res, '/')
|
||||||
})
|
})
|
||||||
|
|
44
src/utils.js
44
src/utils.js
|
@ -140,6 +140,10 @@ module.exports = function(redis) {
|
||||||
const upload_wikimedia_regx = /((https:|http:|)\/\/?upload.wikimedia.org)/gm
|
const upload_wikimedia_regx = /((https:|http:|)\/\/?upload.wikimedia.org)/gm
|
||||||
data.html = data.html.replace(upload_wikimedia_regx, '/media')
|
data.html = data.html.replace(upload_wikimedia_regx, '/media')
|
||||||
|
|
||||||
|
// replace maps.wikimedia.org with /media
|
||||||
|
const maps_wikimedia_regx = /((https:|http:|)\/\/?maps.wikimedia.org)/gm
|
||||||
|
data.html = data.html.replace(maps_wikimedia_regx, '/media/maps_wikimedia_org')
|
||||||
|
|
||||||
// replace wiki links
|
// replace wiki links
|
||||||
const wiki_href_regx = /(href=\"(https:|http:|)\/\/([A-z.]+\.)?(wikipedia.org|wikimedia.org|wikidata.org|mediawiki.org))/gm
|
const wiki_href_regx = /(href=\"(https:|http:|)\/\/([A-z.]+\.)?(wikipedia.org|wikimedia.org|wikidata.org|mediawiki.org))/gm
|
||||||
data.html = data.html.replace(wiki_href_regx, `href="${protocol}${config.domain}`)
|
data.html = data.html.replace(wiki_href_regx, `href="${protocol}${config.domain}`)
|
||||||
|
@ -159,7 +163,7 @@ module.exports = function(redis) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
this.proxyMedia = (req) => {
|
this.proxyMedia = (req, wiki_domain='') => {
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
let params = new URLSearchParams(req.query).toString() || ''
|
let params = new URLSearchParams(req.query).toString() || ''
|
||||||
|
|
||||||
|
@ -167,9 +171,20 @@ module.exports = function(redis) {
|
||||||
params = '?' + params
|
params = '?' + params
|
||||||
}
|
}
|
||||||
|
|
||||||
let path = req.url.split('/media')[1]
|
let path = ''
|
||||||
let wikimedia_path = path + params
|
let domain = 'upload.wikimedia.org'
|
||||||
let url = new URL(`https://upload.wikimedia.org${wikimedia_path}`)
|
let wikimedia_path = ''
|
||||||
|
|
||||||
|
if(wiki_domain === 'maps.wikimedia.org') {
|
||||||
|
path = req.url.split('/media/maps_wikimedia_org')[1]
|
||||||
|
domain = 'maps.wikimedia.org'
|
||||||
|
wikimedia_path = path
|
||||||
|
} else {
|
||||||
|
path = req.url.split('/media')[1]
|
||||||
|
wikimedia_path = path + params
|
||||||
|
}
|
||||||
|
|
||||||
|
url = new URL(`https://${domain}${wikimedia_path}`)
|
||||||
|
|
||||||
let file = await saveFile(url, path)
|
let file = await saveFile(url, path)
|
||||||
|
|
||||||
|
@ -183,7 +198,13 @@ module.exports = function(redis) {
|
||||||
|
|
||||||
this.saveFile = (url, file_path) => {
|
this.saveFile = (url, file_path) => {
|
||||||
return new Promise(async resolve => {
|
return new Promise(async resolve => {
|
||||||
const media_path = path.join(__dirname, '../media')
|
let media_path = ''
|
||||||
|
if(url.href.startsWith('https://maps.wikimedia.org/')) {
|
||||||
|
media_path = path.join(__dirname, '../media/maps_wikimedia_org')
|
||||||
|
} else {
|
||||||
|
media_path = path.join(__dirname, '../media')
|
||||||
|
}
|
||||||
|
|
||||||
const path_with_filename = `${media_path}${file_path}`
|
const path_with_filename = `${media_path}${file_path}`
|
||||||
let path_without_filename = path_with_filename.split('/')
|
let path_without_filename = path_with_filename.split('/')
|
||||||
path_without_filename.pop()
|
path_without_filename.pop()
|
||||||
|
@ -202,7 +223,7 @@ module.exports = function(redis) {
|
||||||
resolve({ success: true, path: path_with_filename })
|
resolve({ success: true, path: path_with_filename })
|
||||||
})
|
})
|
||||||
}).on('error', (err) => {
|
}).on('error', (err) => {
|
||||||
console.log('Error while fetching data...', err)
|
console.log('Error while fetching data. Details:', err)
|
||||||
resolve({ success: false, reason: 'SERVER_ERROR' })
|
resolve({ success: false, reason: 'SERVER_ERROR' })
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -252,6 +273,12 @@ module.exports = function(redis) {
|
||||||
url = `https://${lang}.wikipedia.org/w/${file}`
|
url = `https://${lang}.wikipedia.org/w/${file}`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(prefix === '/wiki/Map') {
|
||||||
|
let page = 'Special:Map'
|
||||||
|
let sub_page = req.params['0'] || ''
|
||||||
|
url = `https://${lang}.wikipedia.org/wiki/${page}/${sub_page}`
|
||||||
|
}
|
||||||
|
|
||||||
if(prefix === '/') {
|
if(prefix === '/') {
|
||||||
url = `https://${lang}.wikipedia.org/?lang=${lang}`
|
url = `https://${lang}.wikipedia.org/?lang=${lang}`
|
||||||
}
|
}
|
||||||
|
@ -345,9 +372,9 @@ module.exports = function(redis) {
|
||||||
|
|
||||||
this.preferencesPage = (req, res) => {
|
this.preferencesPage = (req, res) => {
|
||||||
let user_preferences = req.cookies
|
let user_preferences = req.cookies
|
||||||
|
|
||||||
let lang_select = '<select id="default_lang" name="default_lang">'
|
let lang_select = '<select id="default_lang" name="default_lang">'
|
||||||
let valid_langs = validLang('', true)
|
let valid_langs = validLang('', true)
|
||||||
|
|
||||||
for(var i = 0; i < valid_langs.length; i++) {
|
for(var i = 0; i < valid_langs.length; i++) {
|
||||||
let selected = ''
|
let selected = ''
|
||||||
if(valid_langs[i] == user_preferences.default_lang) {
|
if(valid_langs[i] == user_preferences.default_lang) {
|
||||||
|
@ -355,6 +382,7 @@ module.exports = function(redis) {
|
||||||
}
|
}
|
||||||
lang_select += `<option value="${valid_langs[i]}" ${selected}>${valid_langs[i]}</option>`
|
lang_select += `<option value="${valid_langs[i]}" ${selected}>${valid_langs[i]}</option>`
|
||||||
}
|
}
|
||||||
|
|
||||||
lang_select += '</select>'
|
lang_select += '</select>'
|
||||||
|
|
||||||
let html = `
|
let html = `
|
||||||
|
@ -364,7 +392,7 @@ module.exports = function(redis) {
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
<link rel="stylesheet" href="/styles.css"></head>
|
<link rel="stylesheet" href="/styles.css"></head>
|
||||||
<title>preferences - wikiless</title>
|
<title>Preferences - Wikiless</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<div id="preferences">
|
<div id="preferences">
|
||||||
|
|
|
@ -74,6 +74,11 @@ details[open] #preferences {
|
||||||
}
|
}
|
||||||
/* END PREFERENCES CSS */
|
/* END PREFERENCES CSS */
|
||||||
|
|
||||||
|
/* WIKILESS OVERRIDES */
|
||||||
|
#mw-specialMap-map {
|
||||||
|
background-image: url(/media/map.png) !important;
|
||||||
|
}
|
||||||
|
/* END WIKILESS OVERRIDES */
|
||||||
|
|
||||||
/* START WIKIPEDIA CSS */
|
/* START WIKIPEDIA CSS */
|
||||||
#p-lang .uls-settings-trigger {
|
#p-lang .uls-settings-trigger {
|
||||||
|
@ -8692,4 +8697,64 @@ div.after-portlet-lang:after {
|
||||||
display:block
|
display:block
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#mw-specialMap-container {
|
||||||
|
position:relative
|
||||||
|
}
|
||||||
|
#mw-specialMap-container .thumbinner {
|
||||||
|
display:inline-block;
|
||||||
|
position:relative;
|
||||||
|
width:256px
|
||||||
|
}
|
||||||
|
#mw-specialMap-inner {
|
||||||
|
height:256px;
|
||||||
|
position:relative
|
||||||
|
}
|
||||||
|
#mw-specialMap-map {
|
||||||
|
position:absolute;
|
||||||
|
width:256px;
|
||||||
|
height:256px;
|
||||||
|
background-position:center;
|
||||||
|
background-repeat:no-repeat;
|
||||||
|
background-size:256px 256px
|
||||||
|
}
|
||||||
|
|
||||||
|
#mw-specialMap-marker {
|
||||||
|
position:absolute;
|
||||||
|
background-color:#7b4e10;
|
||||||
|
height:10px;
|
||||||
|
width:10px;
|
||||||
|
margin-top:-5px;
|
||||||
|
margin-left:-5px;
|
||||||
|
border-radius:5px
|
||||||
|
}
|
||||||
|
#mw-specialMap-caption {
|
||||||
|
display:block;
|
||||||
|
padding-top:0.5em
|
||||||
|
}
|
||||||
|
#mw-specialMap-icon {
|
||||||
|
display:inline-block;
|
||||||
|
margin:0 12px 0 0;
|
||||||
|
background-color:#7b4e10;
|
||||||
|
height:10px;
|
||||||
|
width:10px;
|
||||||
|
margin-top:-5px;
|
||||||
|
margin-left:0.5em;
|
||||||
|
border-radius:5px
|
||||||
|
}
|
||||||
|
#mw-specialMap-attributions {
|
||||||
|
position:absolute;
|
||||||
|
bottom:0;
|
||||||
|
font-size:9px;
|
||||||
|
right:0;
|
||||||
|
text-align:right
|
||||||
|
}
|
||||||
|
|
||||||
|
#p-lang .uls-settings-trigger::-moz-focus-inner {
|
||||||
|
border:0
|
||||||
|
}
|
||||||
|
#p-lang .uls-settings-trigger:focus {
|
||||||
|
outline:1px solid #36c
|
||||||
|
}
|
||||||
|
|
||||||
/* END WIKIPEDIA CSS */
|
/* END WIKIPEDIA CSS */
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue