Commit 0ec42fac authored by Luke081515's avatar Luke081515

Merge branch 'error-fishing' into 'master'

Make readPage handle errors

Closes #17, #18, and #29

See merge request !25
parents a4561785 69c0d630
......@@ -20,6 +20,7 @@ class Core extends Password {
private $version = "Cygnus-Framework V2.1 alpha";
private $ua;
private $maxlag;
private $FailedLoginCounter = 0;
/** initcurl
* initializes curl
......@@ -251,12 +252,16 @@ class Core extends Password {
* @return conflict - there is an edit conflict
*/
private function checkResult($result) {
if ($result === "maxlag" || $result === "readonly" || $result === "unknownerror-nocode" || $result === "unknownerror" || $result === "ratelimited") {
echo "\nEdit failed. Reason: $result. Please try again";
return "retry";
} else if ($result === "blocked" || $result === "confirmemail" || $result === "autoblocked") {
throw new Exception("You will not be able to edit soon. Reason: $result");
} else if ($result === "assertuserfailed" || $result === "assertbotfailed") {
if ($result === 'maxlag' || $result === 'readonly' || $result === 'unknownerror-nocode' || $result === 'unknownerror' || $result === 'ratelimited') {
echo '\Action failed. Reason: $result. Please try again';
return 'retry';
} else if ($result === 'blocked' || $result === 'confirmemail' || $result === 'autoblocked') {
throw new Exception('You will not be able to execute writing actions soon. Reason: $result');
} else if ($result === 'assertuserfailed' || $result === 'assertbotfailed') {
if($FailedLoginCounter > 5) {
throw new Exception("MaxLoginTrysExceeded"); // ToDo: Find a way to reset this on succesful actions without putting that into every function
}
$FailedLoginCounter++;
$this->login();
return "retry";
} else if ($result === "editconflict") {
......@@ -274,15 +279,28 @@ class Core extends Password {
* @return text of the page
*/
private function readPageEngine($request) {
$page = json_decode($this->httpRequest($request, $this->job, "GET"), true);
$pageID = $page["query"]["pageids"][0];
return $page["query"]["pages"][$pageID]["revisions"][0]["*"];
do {
$page = json_decode($this->httpRequest($request, $this->job, 'GET'), true);
if (isset($page['error'])) {
$errorcode = $this->checkResult($page['error']['code']);
if($errorcode === "fail") {
return false;
}
}
} while ($errorcode === "retry");
$pageID = $page['query']['pageids'][0];
if ($pageID === -1) {
return null;
}
return $page['query']['pages'][$pageID]['revisions'][0]['*'];
}
/** readPage
* Returns the content of a page
* @param $title - name of the page including namespaces
* @author MGChecker
* @return content of the page
** false if there is an unknown error
** null if the page does not exist
*/
public function readPage($title) {
$request = "action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&rvcontentformat=text%2Fx-wiki&titles=" . urlencode($title) .
......@@ -294,6 +312,8 @@ class Core extends Password {
* @param $pageID - ID of the page
* @author MGChecker
* @return content of the page
** false if there is an unknown error
** null if the page does not exist
*/
public function readPageID($pageID) {
$request = "action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&rvcontentformat=text%2Fx-wiki&pageids=" . urlencode($pageID) .
......@@ -305,6 +325,8 @@ class Core extends Password {
* @param $title - title of the page
* @author MGChecker
* @return text of the page
** false if there is an unknown error
** null if the page does not exist
*/
public function readPageJs($title) {
$request = "action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&rvcontentformat=text%2Fjavascript&titles=" . urlencode($title) .
......@@ -316,6 +338,8 @@ class Core extends Password {
* @param $title - title of the page
* @author MGChecker
* @return text of the page
** false if there is an unknown error
** null if the page does not exist
*/
public function readPageCss($title) {
$request = "action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&rvcontentformat=text%2Fcss&titles=" . urlencode($title) .
......@@ -328,6 +352,8 @@ class Core extends Password {
* @param $section - number of the section
* @author MGChecker
* @return text of the section
** false if there is an unknown error
** null if the page does not exist
*/
public function readSection($title, $section) {
$request = "action=query&prop=revisions&format=json&rvprop=content&rvlimit=1&rvcontentformat=text%2Fx-wiki&rvdir=older&indexpageids=1&rvsection=" . urlencode($section) .
......
cp Password.example.php Password.php
phpunit ./tests/BotCoreReadTest.php $readSite $readUser $readPwd
\ No newline at end of file
phpunit ./tests/BotCoreReadTest.php $readSite $readUser $readPwd
phpunit ./tests/BotCoreReadAssertFailTest.php $AssertWrongSite $AssertWrongUser $AssertWrongPwd
\ No newline at end of file
<?php
require_once __DIR__ . '/../TestCore.php';
use PHPUnit\Framework\TestCase;
/**
* @covers BotCore::readPage
* Tests for all read function
*/
final class BotCoreReadAssertFailTest extends TestCase {
public function testReadPageAssertFail() {
global $argv;
$i = 0;
for ($j = 1; isset($argv[$j]); $j++) {
$params[$i] = $argv[$j];
$i++;
}
$Core = new TestCore($params);
$this->setExpectedException('Exception');
$Core->execute(array("readPage", "Hauptseite"));
}
}
?>
\ No newline at end of file
......@@ -3,7 +3,6 @@ require_once __DIR__ . '/../TestCore.php';
use PHPUnit\Framework\TestCase;
/**
* @covers BotCore
* Tests for all read function
*/
final class BotCoreReadTest extends TestCase {
......@@ -19,23 +18,59 @@ final class BotCoreReadTest extends TestCase {
}
return new TestCore($params);
}
/**
* @covers BotCore::readPage
*/
public function testReadPageSuccessful() {
$Core = $this->createLogin();
$expected = "#WEITERLEITUNG [[Wikipedia:Hauptseite]]";
$actually = $Core->execute(array("readPage", "Hauptseite"));
$this->assertEquals($expected, $actually);
}
/**
* @covers BotCore::readPage
*/
public function testReadPageMissing() {
$Core = $this->createLogin();
$expected = null;
$actually = $Core->execute(array("readPage", "Sfdfsdf"));
$this->assertEquals($expected, $actually);
}
/**
* @covers BotCore::readPageJs
*/
public function testReadPageJsSuccessful() {
$Core = $this->createLogin();
$expected = "UTTest";
$actually = $Core->execute(array("readPageJs", "Benutzer:Luke081515Bot/Testpage.js"));
$this->assertEquals($expected, $actually);
}
/**
* @covers BotCore::readPageCss
*/
public function testReadPageCssSuccessful() {
$Core = $this->createLogin();
$expected = "UTTest";
$actually = $Core->execute(array("readPageCss", "Benutzer:Luke081515Bot/Testpage.css"));
$this->assertEquals($expected, $actually);
}
/**
* @covers BotCore::readSection
*/
public function testReadSection0Successful() {
$Core = $this->createLogin();
$expected = "text";
$actually = $Core->execute(array("readSection", "Benutzer:Luke081515Bot/SectionTest"));
$this->assertEquals($expected, $actually);
}
/**
* @covers BotCore::readSection
*/
public function testReadSection1Successful() {
$Core = $this->createLogin();
$expected = "== Section 1 ==\ntext 2";
$actually = $Core->execute(array("readSection", "Benutzer:Luke081515Bot/SectionTest"));
$this->assertEquals($expected, $actually);
}
}
?>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment