list=allpages 不传递所有页面
list=allpages does not deliver all pages
我有问题,我想用我的 wiki 中所有页面的名称填写一个列表。我的脚本:
$TitleList = [];
$nsList = [];
$nsURL= 'wiki/api.php?action=query&meta=siteinfo& siprop=namespaces|namespacealiases&format=json';
$nsJson = file_get_contents($nsURL);
$nsJsonD = json_decode($nsJson, true);
foreach ($nsJsonD['query']['namespaces'] as $ns)
{
if ( $ns['id'] >= 0 )
array_push ($nsList, $ns['id']);
}
# populate the list of all pages in each namespace
foreach ($nsList as $n)
{
$urlGET = 'wiki/api.php?action=query&list=allpages&apnamespace='.$n.'&format=json';
$json = file_get_contents($urlGET);
$json_b = json_decode( $json ,true);
foreach ($json_b['query']['allpages'] as $page)
{
echo("\n".$page['title']);
array_push($TitleList, $page["title"]);
}
}
但是仍然有 35% 的页面丢失,我可以在我的 wiki 上访问这些页面(用 "random site" 测试)。有谁知道,为什么会发生这种情况?
MediaWiki API 不会 return 一次获得所有结果,而是分批进行。
默认批次只有 10 页;您可以指定 aplimit
来更改它(用户最多 500 个,机器人最多 5,000 个)。
获取下一批,需要指定continue=
参数;在每个批次中,您还会在 returned 数据中得到一个 continue
属性,您可以使用它来请求下一批次。要获取所有页面,只要存在 continue 元素就必须循环。
例如,在英语维基百科上,这将是第一个 API 调用:
https://en.wikipedia.org/w/api.php?action=query&list=allpages&apnamespace=0&format=json&aplimit=500&continue=
...continue
对象将是这样的:
"continue":{
"apcontinue":"\"Cigar\"_Daisey",
"continue":"-||"
}
(根据 OP 的评论更新,带有示例代码)
您现在想要将 continue
数组展平为 url 参数,例如使用 `
在此处查看更完整的解释:
https://www.mediawiki.org/wiki/API:Query#Continuing_queries
您的代码的工作版本应该是(使用略有不同的代码使用 Wikipedia 进行测试):
# populate the list of all pages in each namespace
$baseUrl = 'wiki/api.php?action=query&list=allpages&apnamespace='.$n.'&format=json&limit=500&'; // Increase limit if you are using a bot, up to 5,000
foreach ($nsList as $n) {
$next = '';
while ( isset( $next ) ) {
$urlGET = $baseUrl . $next;
$json = file_get_contents($urlGET);
$json_b = json_decode($json, true);
foreach ($json_b['query']['allpages'] as $page)
{
echo("\n".$page['title']);
array_push($TitleList, $page["title"]);
}
if (isset($json_b['continue'])) {
$next = http_build_query($json_b['continue']);
}
}
}
我有问题,我想用我的 wiki 中所有页面的名称填写一个列表。我的脚本:
$TitleList = [];
$nsList = [];
$nsURL= 'wiki/api.php?action=query&meta=siteinfo& siprop=namespaces|namespacealiases&format=json';
$nsJson = file_get_contents($nsURL);
$nsJsonD = json_decode($nsJson, true);
foreach ($nsJsonD['query']['namespaces'] as $ns)
{
if ( $ns['id'] >= 0 )
array_push ($nsList, $ns['id']);
}
# populate the list of all pages in each namespace
foreach ($nsList as $n)
{
$urlGET = 'wiki/api.php?action=query&list=allpages&apnamespace='.$n.'&format=json';
$json = file_get_contents($urlGET);
$json_b = json_decode( $json ,true);
foreach ($json_b['query']['allpages'] as $page)
{
echo("\n".$page['title']);
array_push($TitleList, $page["title"]);
}
}
但是仍然有 35% 的页面丢失,我可以在我的 wiki 上访问这些页面(用 "random site" 测试)。有谁知道,为什么会发生这种情况?
MediaWiki API 不会 return 一次获得所有结果,而是分批进行。
默认批次只有 10 页;您可以指定 aplimit
来更改它(用户最多 500 个,机器人最多 5,000 个)。
获取下一批,需要指定continue=
参数;在每个批次中,您还会在 returned 数据中得到一个 continue
属性,您可以使用它来请求下一批次。要获取所有页面,只要存在 continue 元素就必须循环。
例如,在英语维基百科上,这将是第一个 API 调用:
https://en.wikipedia.org/w/api.php?action=query&list=allpages&apnamespace=0&format=json&aplimit=500&continue=
...continue
对象将是这样的:
"continue":{
"apcontinue":"\"Cigar\"_Daisey",
"continue":"-||"
}
(根据 OP 的评论更新,带有示例代码)
您现在想要将 continue
数组展平为 url 参数,例如使用 `
在此处查看更完整的解释: https://www.mediawiki.org/wiki/API:Query#Continuing_queries
您的代码的工作版本应该是(使用略有不同的代码使用 Wikipedia 进行测试):
# populate the list of all pages in each namespace
$baseUrl = 'wiki/api.php?action=query&list=allpages&apnamespace='.$n.'&format=json&limit=500&'; // Increase limit if you are using a bot, up to 5,000
foreach ($nsList as $n) {
$next = '';
while ( isset( $next ) ) {
$urlGET = $baseUrl . $next;
$json = file_get_contents($urlGET);
$json_b = json_decode($json, true);
foreach ($json_b['query']['allpages'] as $page)
{
echo("\n".$page['title']);
array_push($TitleList, $page["title"]);
}
if (isset($json_b['continue'])) {
$next = http_build_query($json_b['continue']);
}
}
}