嘿!如何使用Wiki扩展(WPMW)内的Wiki Media API工具将内容写入/添加到页面
这里是现场:
我在WordPress的网站,应该有它的用户整合到媒体维基设置工作。通过使用名为WPMW的Media Wiki扩展,一切都很好。当Wordpress用户登录后,他们会自动添加到Media Wiki用户数据库中,并且他们也成为Wiki的一员。 迄今为止成功。
这是我需要做的:
我需要,在WordPress的+媒体维基用户登录(通过WPMW扩展名),找到一个方法来更新基于一个特定的媒体Wiki页面我从过程中的当前用户收集的信息。例如:当WordPress的用户称为奥拉夫(编辑作用),连接到媒体的Wiki,我想简单地写(附加)到一个特殊的维基页面(称为编辑)名称奥拉夫,这将与其他现存的名字一起。
因此,我研究了关于Media Wiki API,并在遵循一些示例之后,我能够创建一个PHP文件,在服务器上访问后,它定位特定的代码并向文件添加文本(即使使用之前建立的媒体维基模板)。 再次成功,但仅限于独立使用...我需要去example.com/my-script.php使其工作。
这里的问题是:
我试图插入WPMW扩展在我的API代码,但显而易见的是,事情并没有这么简单,并且一切都停止了工作。所以,我想知道,在Media Wiki扩展中,的正确方法是什么?调用Media Wiki API,并给出指令以将文本附加到Media Wiki页面上,基于目前存在的PHP变量的WPMW登录执行?
例如:当Olaf(Editor)在Wordpress上登录并进入Wiki时,WPMW Extension得到激活并建立桥梁 - 通过自动注册Olaf到Wiki数据库(如果这是他的第一次) - 或者只需将Olaf登录到Media Wiki网站即可。
在这一点上,我能够从WordPress的获得,作为变量,我有关的所有数据,将用户名和用户角色。但是,如何使用它们来追加的奥拉夫名称为Wiki页面称为编辑?
这里是我的API代码,基于例子,我发现:
<?php
$settings['wikiroot'] = "http://example.com/wiki";
$settings['user'] = "shangchi";
$settings['pass'] = "my-password";
$settings['domain'] = 'Windows';
$settings['cookiefile'] = "cookies.tmp";
// Test: use a Wiki template to append to page
$prepend = "{{test}}\n\n";
function httpRequest($url, $post="") {
global $settings;
$ch = curl_init();
//Change the user agent below suitably
curl_setopt($ch, CURLOPT_USERAGENT, 'application/x-www-form-urlencoded');
curl_setopt($ch, CURLOPT_URL, ($url));
curl_setopt($ch, CURLOPT_ENCODING, "UTF-8");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_COOKIEJAR, $settings['cookiefile']);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!empty($post)) curl_setopt($ch,CURLOPT_POSTFIELDS,$post);
#UNCOMMENT TO DEBUG TO output.tmp
curl_setopt($ch, CURLOPT_VERBOSE, true); // Display communication with server
$fp = fopen("output.tmp", "w");
curl_setopt($ch, CURLOPT_STDERR, $fp); // Display communication with server
$xml = curl_exec($ch);
if (!$xml) {
throw new Exception("Error getting data from server ($url): " . curl_error($ch));
}
//var_dump($xml);
curl_close($ch);
return $xml;
}
function login ($user, $pass, $token='') {
global $settings;
$url = $settings['wikiroot'] . "/api.php?action=login&format=xml";
$params = "action=login&lgname=$user&lgpassword=$pass";
if (!empty($settings['domain'])) {
$params .= "&lgdomain=" . $settings['domain'];
}
if (!empty($token)) {
$params .= "&lgtoken=$token";
}
$data = httpRequest($url, $params);
if (empty($data)) {
throw new Exception("No data received from server. Check that API is enabled.");
}
$xml = simplexml_load_string($data);
if (!empty($token)) {
//Check for successful login
$expr = "/api/login[@result='Success']";
$result = $xml->xpath($expr);
if(!count($result)) {
throw new Exception("Login failed");
}
} else {
$expr = "/api/login[@token]";
$result = $xml->xpath($expr);
if(!count($result)) {
throw new Exception("Login token not found in XML");
}
}
return $result[0]->attributes()->token;
}
try {
global $settings;
$token = login($settings['user'], $settings['pass']);
login($settings['user'], $settings['pass'], $token);
// get edit token
$result = httpRequest($settings['wikiroot'] . "/api.php?action=query&format=json&meta=tokens");
$result = json_decode($result);
$editToken = $result->query->tokens->csrftoken;
// Get a specific wiki page: Main Page for testing
$result = httpRequest($settings['wikiroot'] . "/api.php?action=query&titles=Main%20Page&format=json&aplimit=500");
$result = json_decode($result);
$allpages = $result->query->pages;
foreach ($allpages as $page) {
echo "Fetching '{$page->title}' ({$page->pageid})...\n";
$revisions = httpRequest(sprintf($settings['wikiroot'] . "/api.php?action=query&prop=revisions&rvlimit=1&format=json&rvprop=content&titles=%s", urlencode($page->title)));
$revisions = json_decode($revisions);
if (isset($revisions->error)) {
echo "ERROR: " . $revisions->error->info . "\n";
continue;
}
$content = $revisions->query->pages->{$page->pageid}->revisions[0]->$star;
if (preg_match("/\{\{migration_/", $content)) {
echo "Already marked ... skipping.\n";
continue;
}
echo "Updating...";
// add text to content and edit page
$content = $prepend . $content;
$post = sprintf("title=%s&text=%s&token=%s", urlencode($page->title), urlencode($content), urlencode($editToken));
$result = httpRequest($settings['wikiroot'] . "/api.php?action=edit&format=json", $post);
echo "done\n";
}
echo ("Finished (".sizeof($allpages)." pages).\n");
} catch (Exception $e) {
die("FAILED: " . $e->getMessage());
}
?>
谢谢你,Tgr。感谢您的指导。该项目现在暂停,我的同事正在寻找不同的方法。问候! – Cospefogo 2015-02-22 13:39:54