如何使用wiki bot定期创建页面

时间:2014-12-11 05:47:05

标签: php mediawiki bots

我的主要目标是获取页面,解析文本并根据文本定期创建子页面。要获取页面,创建并登录,我有以下代码.Php version-5.3.3,server:localhost

private function login($username, $password, $wiki) {
      $response = $this->postAPI($wiki, 'api.php?', 'action=login&lgname=' . urlencode($username) . '&lgpassword=' . urlencode($password));
      if ($response['login']['result'] == "Success") {
          //Unpatched server, all done
      } elseif ($response['login']['result'] == "NeedToken") {
          //Patched server, going fine
          $token = $response['login']['token'];
          $newresponse = $this->postAPI($wiki, 'api.php?', 'action=login&lgname=' . urlencode($username) . '&lgpassword=' . urlencode($password) . '&lgtoken=' . $token);
          if ($newresponse['login']['result'] == "Success") {
              //All done
          } else {
              echo "Forced by server to wait. Automatically trying again.<br />\n";
              sleep(10);
              $this->login($username, $password, $wiki);
          }
      } else {
          //Problem
          if (isset($response['login']['wait']) || (isset($response['error']['code']) && $response['error']['code'] == "maxlag")) {
              echo "Forced by server to wait. Automatically trying again.<br />\n";
              sleep(10);
              $this->login($username, $password, $wiki);
          } else {
              die("Login failed: " . $response . "\r<br />\n");
          }
      }
  }

获取页面的功能是:

  public function get_page($page, $wiki = "")//get page's content
  {
      $response = $this->callAPI($wiki, 'api.php?action=query&prop=revisions&titles=' . urlencode($page) . '&rvprop=content');
      if (is_array($response)) {
          $array = $response['query']['pages'];
          $array = array_shift($array);
          $pageid = $array["pageid"];
          return $response['query']['pages'][$pageid]['revisions'][0]["*"];
      } else {
          echo "Unknown get_page error.<br />\n";
          return false;
      }
  }

登录时遇到问题。我总是被服务器强制等待。无论我的密码和ID是否正确,都会自动重试。事实上,如果手动给出,URI可以正常工作。如果我尝试创建页面或获取类别,我会收到以下错误:

  

无法修改标头信息 - 已在serverlocation / includes / WebResponse.php中发送的标头(在serverlocation / Phpwikibot.php:188处开始输出)

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

你说“localhost”,所以你有服务器端访问权限,你应该使用内部PHP API,而不是web API。特别是,要编辑页面,您可以使用maintenance/edit.php。请参阅a real world example我用于某些维基媒体wiki:

#!/bin/bash
{
  # Stuff
  # Fetch stuff
  echo -e $stuff
} | php edit.php --user "FuzzyBot" \
    --bot --summary "Update stats" "Meta:Babylon/Translation_stats"