PMi*_*int 1 javascript mediawiki mediawiki-api
是否有任何api或其他方式,使用Javascript,搜索任何mediawiki并打印找到的页面(如果没有找到打印).
我更喜欢这样的东西:
function searchWiki(wikipage, search) {
//the function here
document.write(foundPage);
}
//run it
searchWiki('https://en.wikipedia.org', 'banana');
//it would print 'https://en.wikipedia.org/wiki/Banana'
Run Code Online (Sandbox Code Playgroud)
这是我实现的这样一个功能.它通过JSONP 使用MediaWiki API,非常灵活.我想jQuery解决方案很好.我创造了一个小小提琴.
searchWiki(网站,搜索,[回调],[选项])
function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required URLs
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var apiUrl = siteUrl + (opts.apiBase || '/w/') + 'api.php';
var queryUrl = apiUrl + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
// Issue the JSONP request
$.ajax(queryUrl + '&callback=?', {
dataType: 'jsonp',
// This prevents warnings about the unrecognized parameter "_"
cache: true,
success: function(data) {
// Get all returned pages
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
}
});
}
Run Code Online (Sandbox Code Playgroud)
示例1:单个维基百科搜索
searchWiki('en.wikipedia.org', 'banana fruit', {
ssl: true,
success: function(title, link) {
// link is now "https://en.wikipedia.org/wiki/Banana"
if(title === null) {
$('#search-msg').text('Not found');
} else {
var anchor = $('<a>').text(title).attr('href', link);
$('#search-msg').append(anchor);
}
}
});
Run Code Online (Sandbox Code Playgroud)
此示例显示指向维基百科页面的链接以及相关标题.
示例2:多个结果
searchWiki('www.mediawiki.org', 'Release notes', {
ssl: true,
maxResults: 5,
success: function(titles, links) {
for(var i = 0; i < titles.length; i++) {
alert('MediaWiki ' + titles[i] + ' at ' + links[i]);
}
}
});
Run Code Online (Sandbox Code Playgroud)
此示例最多显示五个指向MediaWiki页面的链接,这些页面与查询"发行说明"匹配.
选项:
ssl:使用HTTPS而不是HTTPmaxResults:返回多个(最多n个)结果apiBase:目标站点上的API目录(默认为/w/)wikiBase:目标站点上的Wiki目录(默认为/wiki/)success:检索结果列表后调用的函数您可以将回调作为函数参数(在选项之前)或作为success选项传递.
更新:这是纯JS解决方案(不需要jQuery).而且还有另外一个小提琴,这次没有jQuery的.
function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required URLs
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var apiUrl = siteUrl + (opts.apiBase || '/w/') + 'api.php';
var queryUrl = apiUrl + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
var fnName = '_cb_' + Math.floor(Math.random() * 4294967296);
window[fnName] = function(data) {
// Clear references to this function
window[fnName] = null;
// Get all returned pages
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
}
// Issue the JSONP request
var scriptTag = document.createElement('script');
scriptTag.setAttribute('src', queryUrl + '&callback=' + fnName);
document.head.appendChild(scriptTag);
}
Run Code Online (Sandbox Code Playgroud)
更新2:最后一个node.js的解决方案.API仍然相同,但它提供了一些额外的选项:
error:错误回调(在基于浏览器的JS中这是不可能的)userAgent:文档中建议的自定义用户代理字符串port:目标端口(默认为80/443)encoding:响应编码(默认为utf8)我没有测试这么多,但是示例(见上文)应该仍然有效.
var http = require('http'),
https = require('https');
function searchWiki(site, search, callback, opts) {
if(typeof callback == 'object') {
opts = callback;
callback = null;
} else {
opts = opts || {};
}
// Build the required paths
var apiPath = (opts.apiBase || '/w/') + 'api.php';
var queryPath = apiPath + '?action=query&list=search&srsearch=' + encodeURIComponent(search) + '&srlimit=' + (opts.maxResults || 1) + '&format=json';
// Request options
var httpOpts = {
hostname: site,
port: (opts.port ? opts.port : (opts.ssl ? 443 : 80)),
method: 'GET',
path: queryPath,
agent: false
};
// Custom user agent
if(opts.userAgent) {
httpOpts.headers = {
'User-Agent': opts.userAgent
};
}
// Make the request
var req = (opts.ssl ? https : http).request(httpOpts, function(res) {
var msgBody = '';
res.setEncoding(opts.encoding || 'utf8');
res.on('data', function(chunk) {
msgBody += chunk;
});
res.on('end', function() {
// Parse response as JSON
var data;
try {
data = JSON.parse(msgBody);
} catch(err) {
(opts.error || function(){})(err);
return;
}
// Get all returned pages
var siteUrl = (opts.ssl ? 'https' : 'http') + '://' + site;
var titles = [], links = [];
for(var i = 0; i < data.query.search.length; i++) {
var title = data.query.search[i].title,
link = siteUrl + (opts.wikiBase || '/wiki/') + encodeURIComponent(title);
titles.push(title);
links.push(link);
}
if(!opts.maxResults) {
// Single result requested
if(data.query.search.length == 0) {
titles = links = null;
} else {
titles = titles[0];
links = links[0];
}
}
// Call the callback
(callback || opts.success || function(){})(titles, links);
});
});
req.on('error', function(err) {
(opts.error || function(){})(err);
});
req.end();
}
Run Code Online (Sandbox Code Playgroud)