SetCatForBKL.php 3.66 KB
Newer Older
Luke081515's avatar
Luke081515 committed
1 2
#!/usr/bin/php
<?php
Luke081515's avatar
Luke081515 committed
3 4
require './Cygnus/BotCore.php';
require './Cygnus/DBCore.php';
Luke081515's avatar
Luke081515 committed
5 6 7 8 9 10 11
/** SetCatForBKL.php
* Sucht die Kategorien von Seiten, die durch eine BKL verlinkt werden
* Required: DB-Anbindung
* @Author Luke081515
* @Version 0.1
* @Status Alpha
*/
Luke081515's avatar
Luke081515 committed
12
class SetCatForBKL extends Core {
Luke081515's avatar
Luke081515 committed
13
	protected $DB;
Luke081515's avatar
Luke081515 committed
14 15
	public function __construct() {
		$GLOBALS['argv'];
Luke081515's avatar
Luke081515 committed
16
		$pwd = $argv[1];
Luke081515's avatar
Luke081515 committed
17 18 19 20 21 22
		$this->setSite('de.wikipedia.org');
		$this->setUsername('Luke081515Bot');
		$this->setPassword($pwd);
		$this->initcurlArgs('SetCatForBKL', true);
		$this->login();
		$this->connectToDatabase();
Luke081515's avatar
Luke081515 committed
23 24 25 26 27 28
		$this->SetCatForBKL();
	}
	/** connectToDatabase
	* Verbindet sich nach den Daten aus der Passwort-Datei mit der Datenbank
	*/
	protected function connectToDatabase () {
Luke081515's avatar
Luke081515 committed
29
		$this->DB = new DBCore('root@localhost', 'BKLLC');
Luke081515's avatar
Luke081515 committed
30
	}
Luke081515's avatar
Luke081515 committed
31 32 33 34 35 36
	/** manageDatabases
	* Sucht nach neuen Links von BKLs, schaut, ob bereits in den DBs vorhanden, falls nicht, werden die DBs aktualisiert
	*/
	protected function manangeDatabases () {
		$sql = 'SELECT DisID FROM Disambig;';
		$result = $this->DB->query ($sql);
Luke081515's avatar
Luke081515 committed
37
		$a = 0;
Luke081515's avatar
Luke081515 committed
38
		while ($row = $result->fetch_assoc()){
Luke081515's avatar
Luke081515 committed
39
			$BKLs[$a] = $row['DisID'];
Luke081515's avatar
Luke081515 committed
40 41 42 43
			$a++;
		}
		$Total = $a;
		$result->free();
Luke081515's avatar
Luke081515 committed
44
		$a = 0;
Luke081515's avatar
Luke081515 committed
45 46
		while (isset($BKLs[$a])) {
			$Page [$a] = $this->getLinks($BKLs[$a]);
Luke081515's avatar
Luke081515 committed
47 48
			$a++;
		}
Luke081515's avatar
Luke081515 committed
49
		$b = 0;
Luke081515's avatar
Luke081515 committed
50
		for ($a = 0; $a < $Total; $a++) {
Luke081515's avatar
Luke081515 committed
51
			$b = 0;
Luke081515's avatar
Luke081515 committed
52 53 54 55
			while (isset($Page[$a][$b])) {
				if ($Page[$a][$b] !== false) {
					$sql = 'SELECT PageID FROM Page WHERE PageID = ' . $Page [$a][$b] . ';';
					$result = $this->DB->query($sql);
Luke081515's avatar
Luke081515 committed
56
					if ($result === 0) {
Luke081515's avatar
Luke081515 committed
57
						$sql = 'INSERT INTO Page SET PageID = ' . $Page [$a][$b] . ', TotalLinks = 0;';
Luke081515's avatar
Luke081515 committed
58 59 60 61 62 63 64
						$this->DB->modify ($sql);
					}
				}
				$b++;
			}
		}
		for ($a = 0; $a < $Total; $a++) {
Luke081515's avatar
Luke081515 committed
65
			$b = 0;
Luke081515's avatar
Luke081515 committed
66 67 68 69
			while (isset($Page[$a][$b])) {
				if ($Page[$a][$b] !== false) {
					$sql = 'SELECT Disambig_DisID, Page_PageID FROM PageLinkDisambig WHERE Page_PageID = ' . $Page[$a][$b] . ' AND Disambig_DisID = ' . $BKLs[$a] . ';';
					$result = $this->DB->query($sql);
Luke081515's avatar
Luke081515 committed
70
					if ($result === 0) {
Luke081515's avatar
Luke081515 committed
71 72 73 74
						$sql = 'INSERT INTO PageLinkDisambig SET Page_PageID = ' . $Page[$a][$b] . ', Disambig_DisID = ' . $BKLs[$a] . ', LinksToDisambig = false, Inserted = true;';
						$this->DB->modify($sql);
						$sql = 'SELECT TotalLinks FROM Page WHERE PageID = ' . $Page[$a][$b] . ';';
						$result = $this->DB->query($sql);
Luke081515's avatar
Luke081515 committed
75 76 77
						$row = $result->fetch_assoc();
						$Tl = $row['TotalLinks'];
						$Tl++;
Luke081515's avatar
Luke081515 committed
78 79 80 81
						$sql = 'UPDATE Page SET TotalLinks = ' . $Tl . ' WHERE PageID = ' . $Page[$a][$b] . ';';
						$this->DB->modify($sql);
						$sql = 'SELECT TotalLinks FROM Disambig WHERE DisID = ' . $BKLs[$a] . ';';
						$result = $this->DB->query($sql);
Luke081515's avatar
Luke081515 committed
82 83 84
						$row = $result->fetch_assoc();
						$Tl = $row['TotalLinks'];
						$Tl++;
Luke081515's avatar
Luke081515 committed
85 86
						$sql = 'UPDATE Disambig SET TotalLinks = ' . $Tl . ' WHERE DisID = ' . $BKLs[$a] . ';';
						$this->DB->modify($sql);
Luke081515's avatar
Luke081515 committed
87
					} else {
Luke081515's avatar
Luke081515 committed
88 89
						$sql = 'UPDATE PageLinkDisambig SET Inserted = true WHERE Page_PageID = ' . $Page[$a][$b] . ' AND Disambig_DisID = ' . $BKLs[$a] . ';';
						$this->DB->modify($sql);
Luke081515's avatar
Luke081515 committed
90 91 92 93 94 95
					}
				}
				$b++;
			}
		}
		$sql = 'DELETE FROM PageLinkDisambig WHERE Inserted = false AND LinksToDisambig = false;';
Luke081515's avatar
Luke081515 committed
96
		$this->DB->modify($sql);
Luke081515's avatar
Luke081515 committed
97
		$sql = 'UPDATE PageLinkDisambig SET Inserted = false WHERE LinksToDisambig = false;';
Luke081515's avatar
Luke081515 committed
98
		$this->DB->modify($sql);
Luke081515's avatar
Luke081515 committed
99 100 101 102 103 104 105 106
	}
	/** isBKL
	* @Param PageID - ID der zu checkenden Seiten
	* @returns bool - true: Seite ist eine BKL, false: ist keine BKL
	* Erfordert aktuelle BKL-Database
	*/
	protected function isBKL ($PageID) {
		$sql = 'SELECT DisID FROM Disambig WHERE DisID = ' . $PageID . ';';
Luke081515's avatar
Luke081515 committed
107
		$result = $this->DB->query($sql);
Luke081515's avatar
Luke081515 committed
108
		if ($result === 0)
Luke081515's avatar
Luke081515 committed
109
			return false;
Luke081515's avatar
Luke081515 committed
110 111 112
		return true;
	}
}
Luke081515's avatar
Luke081515 committed
113
$Bot = new SetCatForBKL();
Luke081515's avatar
Luke081515 committed
114
?>