CTTMonitor.php 3.97 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#!/usr/bin/php
<?php
include '../BotCore.php';

class IMPBotMonitor extends Core {
	/** CTTMonitor.php
	* Schaut ob der CTT-Job noch aktiv ist. Falls nicht: returns non zero exit status
	* @Version 0.8
	* @Status: Alpha
	*/
	protected $Running;
	public function IMPBotMonitor ($Account, $Job, $pUseHTTPS = true) {
		$this->initcurl($Account, $Job, $pUseHTTPS = true);
		$this->Running = true;
		if ($this->readPage ("Benutzer:Luke081515Bot/Import") !== "true") {
			echo ("\nERROR: Bot deactivated! Wanted outtage?\n");
			die (1);
		}
		echo ("\nCHECK: Bot has backlog? (1)");
		$this->addNewTranslations ();
		if ($this->Running === false) {
			echo ("\nWARNING: Bot has backlog. Waiting 120 seconds before checking again.");
			sleep (120);
			$this->Running = true;
			echo ("\nCHECK: Bot has backlog? (2)");
			$this->addNewTranslations ();
			if ($this->Running === false) {
				echo ("\nERROR: Bot has backlog! Seems like the bot is not working!\n");
				die (1);
			}
		}
		echo ("\nSUCCESS: No backlog!\n");
		die (0);
	}
    public function addNewTranslations () {
		$data = "action=query&list=recentchanges&format=php&rcdir=older&rctag=contenttranslation&rcprop=title&rclimit=5000&rcshow=!redirect&rawcontinue=&rctype=new";
		try {
			$result = $this->httpRequest($data, $this->job, 'GET');
		} catch (Exception $e) {
			throw $e;
		}
		$Answer = explode ("\"", $result);
		unset ($result);
		$i = 13;
		$a = 0;
		while (isset ($Answer [$i]) === true) {
			$Results [$a] = $Answer [$i];
			$Result [$a] = urldecode ($Results [$a]);
			$a++;
			$i = $i + 10;
		}
		unset ($i);
		$a=0;
		while (isset ($Result [$a]) === true)
		{
			if (strstr ($Result [$a], "Benutzer:") !== false) {
				$Username = substr ($Result [$a], 9);
				$Username = strstr ($Username, "/", true);
				$Notification = true;
			}
			else
				$Notification = false;
			$AlreadySet = $this->readPage ("Wikipedia:Importw&uuml;nsche");
			$IgnoreList =  $this->readPage ("Benutzer:Luke081515Bot/NoImport");
			if (strstr ($IgnoreList, "<!--Import:Aktiviert-->") !== false) {
				if ((strstr ($AlreadySet, $Result [$a]) === false) && (strstr ($IgnoreList, $Result [$a]) === false)) {
					if (strstr ($AlreadySet, "{{/Intro}}") !== false) {
						$data = "action=query&prop=revisions&format=xml&rvprop=comment&rvlimit=1&rvdir=newer&rvtag=contenttranslation&titles=" .  urlencode($Result [$a]);
						try {
							$result = $this->httpRequest($data, $this->job, 'GET');
						} catch (Exception $e) {
							throw $e;
						}
						$website = $result;
						if (strstr ($website, "[[") !== false) {
							$Source = strstr ($website, "[[");
							$Target = $Source;
							$Source = explode (":", $Source);
							$Lang = $Source [1];
							$Target = strstr ($Target, "|");
							$Target = substr ($Target, 1);
							$Target = strstr ($Target, "]]", true);
							$data = "action=query&prop=revisions&format=xml&rvprop=comment&rvlimit=1&rvdir=newer&titles=" .  urlencode($Result [$a]);
							try {
								$result = $this->httpRequest($data, $this->job, 'GET');
							} catch (Exception $e) {
								throw $e;
							}
							$result = $website;
							if (strstr ($website, "[[") !== false) {
								$Comparision = strstr ($website, "[[");
								$Comparision = strstr ($Comparision, "|");
								$Comparision = substr ($Comparision, 1);
								$Comparision = strstr ($Comparision, "]]", true);
								if ($Comparision === $Target) {
									if ($this->checkAlreadyImported ($Result [$a]) === false)
										$this->Running = false;
								}
							}
						}
					}
				}
			}
			$a++;
		}
	}
	public function checkAlreadyImported ($Site) {
		$data = "action=query&list=logevents&format=php&letype=import&ledir=older&letitle=" . urlencode ($Site) . "&lelimit=500&rawcontinue=";
		try {
			$result = $this->httpRequest($data, $this->job, 'GET');
		} catch (Exception $e) {
			throw $e;
		}
		if (strstr ($result, "query") === false || strstr ($result, "import") !== false)
			return true;
		return false;
	}
}
$Bot = new IMPBotMonitor('Luke081515Bot@dewiki', 'CTT');
?>