GenerateUnrevStats.php 7.07 KB
Newer Older
Luke081515's avatar
Luke081515 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#!/usr/bin/php
<?php
require_once __DIR__ . '/Cygnus/BotCore.php';
require_once __DIR__ . '/Cygnus/DBCore.php';
/** GenerateUnrevStats.php
* Generates stats about the lag
* @Author Luke081515
* @Version 0.1
* @Status Alpha
*/
class GetUnrevStats extends Core {
	protected $DB;
	public function __construct($Account, $Job) {
		$this->initcurl($Account, $Job);
		$this->connectToDatabase();
		$this->generateStats();
	}
	public function generateStats() {
Luke081515's avatar
Luke081515 committed
19
		$data = "action=query&format=json&list=unreviewedpages&rawcontinue=1&urlimit=max";
Luke081515's avatar
Luke081515 committed
20 21 22 23 24
		$i = 0;
		do {
			$result = $this->httpRequest($data, $this->job, "GET");
			$result = json_decode($result, true);
			if (isset($result["query-continue"])) {
Luke081515's avatar
Luke081515 committed
25
				$continue = $result["query-continue"]["unreviewedpages"]["urstart"];
Luke081515's avatar
Luke081515 committed
26 27 28
			} else {
				$continue = false;
			}
Luke081515's avatar
Luke081515 committed
29 30 31 32 33 34 35
			for($a = 0; isset($result["query"]["unreviewedpages"][$a]["title"]); $a++) {
				$temp = $this->getPageID($result["query"]["unreviewedpages"][$a]["title"]);
				if($temp !== false) {
					$resTitle[$i] = $result["query"]["unreviewedpages"][$a]["title"];
					$res[$i] = $temp;
					$i++;
				}
Luke081515's avatar
Luke081515 committed
36
			}
Luke081515's avatar
Luke081515 committed
37
			$data = "action=query&format=json&list=unreviewedpages&rawcontinue=1&urlimit=max&urstart=" . urlencode($continue);
Luke081515's avatar
Luke081515 committed
38 39
		} while ($continue !== false);
		$age[0] = 0;
Luke081515's avatar
Luke081515 committed
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
		if (!isset($res[0])) {
			$today = strtotime($hour . ':00:00');
			$sql = "INSERT INTO unrevage VALUES(CURRENT_TIMESTAMP, \"Average age\", 0);";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevage VALUES(CURRENT_TIMESTAMP, \"Low average\", 0);";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevage VALUES(CURRENT_TIMESTAMP, \"High average\", 0);";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (24h)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (48h)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (72h)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (5d)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (10d)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (15d)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (20d)\", 0, " . $today . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (25d)\", 0, " . $today . ");";
			$this->DB->modify($sql);
		} else {
			for ($i = 0; isset($res[$i]); $i++) {
				$temp = $this->getCreationDate($resTitle[$i]);
				if ($temp === false) {
					$age[$i] = 0;
				} else {
					$age[$i] = $temp;
				}
Luke081515's avatar
Luke081515 committed
72
			}
Luke081515's avatar
Luke081515 committed
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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
			$current = time();
			$lagtotal = 0;
			for ($i = 0; isset($age[$i]); $i++) {
				$lagstamp[$i] = strtotime($age[$i]);
				$lagdifference[$i] = $current - $lagstamp[$i];
				$lagdifference[$i] = $lagdifference[$i] / 60 / 60 / 24;
				$lagtotal = $lagtotal + $lagdifference[$i];
			}
			rsort($lagdifference);
			$lagAverage = $lagtotal / count($lagdifference);
			$sql = "INSERT INTO unrevage VALUES(CURRENT_TIMESTAMP, \"Average age\", " . $lagAverage . ");";
			$this->DB->modify($sql);
			$medium = count($lagdifference) / 2;
			$lowerAverage = $medium - 1;
			$lowerAverageLag = 0;
			for ($i = $medium; isset($lagdifference[$i]); $i++) {
				$lowerAverageLag = $lowerAverageLag + $lagdifference[$i];
			}
			$higherAverageLag = 0;
			for ($i = 0; $i <= $lowerAverage; $i++) {
				$higherAverageLag = $higherAverageLag + $lagdifference[$i];
			}
			$lowerAverageLag = $lowerAverageLag / $medium;
			$higherAverageLag = $higherAverageLag / $medium;
			$sql = "INSERT INTO unrevage VALUES(CURRENT_TIMESTAMP, \"Low average\", " . $lowerAverageLag . ");";
			$this->DB->modify($sql);
			$sql = "INSERT INTO unrevage VALUES(CURRENT_TIMESTAMP, \"High average\", " . $higherAverageLag . ");";
			$this->DB->modify($sql);
			$i = 0;
			while ($lagdifference[$i] > 1) {
				$i++;
			}
			$hour = date('H');
			$hour = $hour;
			$today = strtotime($hour . ':00:00');
			$stamp = strtotime('-1 day', $today);
			$i--;
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (24h)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while ($lagdifference[$i] > 2) {
				$i++;
			}
			$backlogPrevious = $i;
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (48h)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while ($lagdifference[$i] > 3) {
				$i++;
			}
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (72h)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while (isset($lagdifference[$i]) && $lagdifference[$i] > 5) {
				$i++;
			}
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (5d)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while (isset($lagdifference[$i]) && $lagdifference[$i] > 10) {
				$i++;
			}
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (10d)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while (isset($lagdifference[$i]) && $lagdifference[$i] > 15) {
				$i++;
			}
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (15d)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while (isset($lagdifference[$i]) && $lagdifference[$i] > 20) {
				$i++;
			}
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (20d)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
			$i = 0;
			while (isset($lagdifference[$i]) && $lagdifference[$i] > 25) {
				$i++;
			}
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Old backlog (25d)\", " . $i . ", " . $today . ");";
			$this->DB->modify($sql);
Luke081515's avatar
Luke081515 committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
		}
		try {
			$sql = "SELECT count FROM unrevbacklog WHERE recorded = " . $stamp . " AND type = \"Old backlog (24h)\";";
			$result = $this->DB->query($sql);
			$res = $result->fetch_assoc();
			$oldcount = $res['count'];
			$result->free();
			$newcount = $oldcount - $backlogPrevious;
			$sql = "INSERT INTO unrevbacklog VALUES(CURRENT_TIMESTAMP, \"Backlog difference\", " . $newcount . ", " . $today . ");";
			$this->DB->modify($sql);
		} catch (Exception $e) {
			echo "\nPrevious value is missing.";
		}
	}
	protected function connectToDatabase () {
		$this->DB = new DBCore('root@localhost', 'maintenance');
	}
	private function getCreationDate($page) {
Luke081515's avatar
Luke081515 committed
173
		$request = "action=query&format=json&prop=revisions&indexpageids=1&titles=" . urlencode($page) . "&rvprop=timestamp&rvlimit=1&rvdir=newer";
Luke081515's avatar
Luke081515 committed
174 175 176 177 178 179 180 181 182 183
		$result = json_decode($this->httpRequest($request, $this->job, 'GET'), true);
		$pageID = $result['query']['pageids'][0];
		if ($pageID == -1) {
			return false;
		}
		return $result['query']['pages'][$pageID]['revisions'][0]['timestamp'];
	}
}
$Bot = new GetUnrevStats ('Luke081515Bot@dewiki', 'GetUnrevStats');
?>