Corrector.php 6.75 KB
Newer Older
Luke081515's avatar
Luke081515 committed
1 2
#!/usr/bin/php
<?php
Freddy's avatar
Freddy committed
3
require './Cygnus/BotCore.php';
Luke081515's avatar
Luke081515 committed
4
/** Corrector.php
Luke081515's avatar
Luke081515 committed
5
* Searches for occurences and allows you to correct them manually
Freddy's avatar
Freddy committed
6
* @Author Freddy2001, Luke081515
Luke081515's avatar
Luke081515 committed
7 8 9 10
* @Version 0.1
* @Status Alpha
*/
class Corrector extends Core {
Freddy's avatar
Freddy committed
11 12
	private $summary;
	private $search;
13
	private $pattern;
Freddy's avatar
Freddy committed
14 15
	private $replace;
	private $needle;
Luke081515's avatar
Luke081515 committed
16
	private $cont;
Freddy's avatar
Freddy committed
17
	private $accountname;
18
	private $minor;
Freddy's avatar
Freddy committed
19

Luke081515's avatar
Luke081515 committed
20
	public function __construct($Account, $Job, $pUseHTTPS = true) {
Luke081515's avatar
Luke081515 committed
21
		if ($this->ask("Use a botflag? [y/N] ", "required") === 'y') {
Luke081515's avatar
Luke081515 committed
22 23 24 25
			$this->initcurl($Account, $Job, $pUseHTTPS);
		} else {
			$this->initcurl($Account, $Job, $pUseHTTPS, "user");
		}
Freddy's avatar
Freddy committed
26 27 28 29 30 31
		$this->accountname = $Account;
		echo "\n";
		$this->defineConditions();
		$this->searchPages();
		$this->setSummary();
		$this->goAllPages();
Luke081515's avatar
Luke081515 committed
32 33 34 35
	}
	/** defineConditions
	* Defines the condition for search and replace
	*/
Freddy's avatar
Freddy committed
36
	private function defineConditions () {
Luke081515's avatar
Luke081515 committed
37
		if ($this->ask("Use a pattern? [y/N] ", "required") === 'y') {
Luke081515's avatar
Luke081515 committed
38
			$this->pattern = $this->ask("What should be the pattern? ", "required");
39
		}
Luke081515's avatar
Luke081515 committed
40
		$this->needle = $this->ask("What should be replaced? ", "required");
41 42 43
		if ($this->ask("Add a space to the old string? [y/N] ", "required") === 'y') {
			$this->needle = $this->needle . " ";
		}
Luke081515's avatar
Luke081515 committed
44
		$this->replace = $this->ask("What should be the replacement? ", "required");
Luke081515's avatar
Luke081515 committed
45
		if ($this->ask("Add a space to the replacement? [y/N] ", "required") === 'y') {
46 47
			$this->replace = $this->replace . " ";
		}
Luke081515's avatar
Luke081515 committed
48 49 50
		if ($this->ask("Continue a previous search request? [y/N] ", "required") === 'y') {
			$this->cont = $this->ask("What was the last title you've checked? ", "required");
		}
51 52 53 54 55
		if ($this->ask("Mark edits as minor? [y/N] ", "required") === 'y') {
			$this->minor = true;
		} else {
			$this->minor = false;
		}
Freddy's avatar
Freddy committed
56 57
	}
	/** setSummary
Luke081515's avatar
Luke081515 committed
58 59
	* @return void
	*/
Freddy's avatar
Freddy committed
60 61
	private function setSummary() {
		if($this->summary == null) {
Luke081515's avatar
Luke081515 committed
62
			$answer = strtoupper($this->ask('Set summary to "Replace ' . $this->needle . ' with ' . $this->replace . '"? [Y], [N]', "required"));
Freddy's avatar
Freddy committed
63 64
			if($this->validateAnswer($answer)) {
				if($answer == 'Y') {
Luke081515's avatar
Luke081515 committed
65
					$this->summary = "Replace " . $this->needle . " with " . $this->replace;
Freddy's avatar
Freddy committed
66
				} else {
Luke081515's avatar
Luke081515 committed
67
					$newsummary = $this->ask("Which summary should I use instead? ", "required");
Freddy's avatar
Freddy committed
68 69
					$this->summary = $newsummary;
				}
Luke081515's avatar
Luke081515 committed
70
				$this->echoMsg("OK, summary is now: " . $this->summary, "success");
Freddy's avatar
Freddy committed
71 72 73 74 75
			}
		}
	}

	/** search
Freddy's avatar
Freddy committed
76 77
	* Search
	* @return void
Luke081515's avatar
Luke081515 committed
78
	*/
Freddy's avatar
Freddy committed
79
	private function searchPages () {
Luke081515's avatar
Luke081515 committed
80
		$this->echoMsg("Searching…" . "\n", "notice");
81 82 83 84 85
		if (isset($this->pattern)) {
			$suche = urlencode($this->pattern);
		} else {
			$suche = urlencode('insource:' . $this->needle);
		}
Freddy's avatar
Freddy committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
		$i = 0;
		$Again = true;
		$Continue = '0';
		while ($Again === true) {
			$SearchResult = unserialize($this->httpRequest('action=query&format=php&list=search&srsearch=' . $suche . '&srnamespace=0&srlimit=max&sroffset=' . $Continue . '&srqiprofile=classic&srinfo=totalhits&srprop=', 'PersoenlichkeitenCirrus', 'GET'));
			foreach($SearchResult['query']['search'] as $Ergebnis) {
				$Seite[$i] = $Ergebnis['title'];
				$i++;
			}
			if (isset($SearchResult['continue']['sroffset']) === true) {
				$Continue = $SearchResult['continue']['sroffset'];
			} else {
				$Again = false;
			}
		}
Luke081515's avatar
Luke081515 committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		if (isset($this->cont)) {
			$i = 0;
			while (isset($Seite[$i]) && $Seite[$i] !== $this->cont) {
				$i++;
			}
			if (!isset($Seite[$i])) {
				$this->echoMsg("Found 0 Results.", "error");
				die(1);
			} else {
				$a = 0;
				$newSearch = array();
				while(isset($Seite[$i])) {
					$newSearch[$a] = $Seite[$i];
					$a++;
					$i++;
				}
				$this->echoMsg("Found " . count($newSearch) . " Results.", "notice");
				$this->search = $newSearch;
			}
		} else {
			$this->echoMsg("Found " . count($Seite) . " Results.", "notice");
			$this->search = $Seite;
		}
Freddy's avatar
Freddy committed
124 125
	}
	private function goAllPages() {
Luke081515's avatar
Luke081515 committed
126 127 128
		$count1 = count($this->search);
		$count2 = count(unserialize($this->getLoginName()));
		for($i = 0; $i < $count1; $i++) {
Luke081515's avatar
Luke081515 committed
129
			$this->echoMsg("\n\033[1mTitle:\033[0m " . $this->search[$i], "none");
Luke081515's avatar
Luke081515 committed
130
			for($j = 0; $j < $count2; $j++) {
Freddy's avatar
Freddy committed
131 132 133 134
				if(unserialize($this->getLoginName())[$j] == $this->accountname) {
					$loginhost = unserialize($this->getLoginHost())[$j];
				}
			}
Luke081515's avatar
Luke081515 committed
135
			$this->echoMsg("URL: https://" . $loginhost . "/w/index.php?title=" . urlencode($this->search[$i]), "none");
Freddy's avatar
Freddy committed
136
			$seite = $this->readPage($this->search[$i]);
Freddy's avatar
Freddy committed
137
			$this->display($this->search[$i], $seite, $this->needle, $this->replace);
Freddy's avatar
Freddy committed
138 139
		}
	}
Freddy's avatar
Freddy committed
140
	private function display ($seite, $html = '', $needle = '', $new = '') {
Freddy's avatar
Freddy committed
141 142 143
		$red = "\033[0;31m";
		$green = "\033[0;32m";
		$end = "\033[0m";
Freddy's avatar
Freddy committed
144
		$htmlarr = explode("\n", $html);
Luke081515's avatar
Luke081515 committed
145
		$count = count($htmlarr);
146
		while (true) {
Luke081515's avatar
Luke081515 committed
147
			for($i = 0; $i < $count; $i++) {
148 149 150 151 152 153 154 155 156 157 158 159
				$anzahl = substr_count($htmlarr[$i], $needle);
				if($anzahl > 0) {
					// cut off long lines
					if(stripos($htmlarr[$i], $needle) > 70) {
						$htmlarr[$i] = substr($htmlarr[$i], stripos($htmlarr[$i], $needle) - 50);
					}
					if(strripos($htmlarr[$i], $needle) + strlen($needle) + 70 < strlen($htmlarr[$i])) {
						$htmlarr[$i] = substr($htmlarr[$i], 0, strripos($htmlarr[$i], $needle) + strlen($needle) + 50);
					}
					$old = str_replace($needle, $red . $needle . $end, $htmlarr[$i]);
					$newstr = str_replace($needle, $new, $htmlarr[$i]);
					$newstr = str_replace($new, $green . $new . $end, $newstr);
Luke081515's avatar
Luke081515 committed
160
					$this->echoMsg('Zeile ' . ($i + 1) . ":\n– " . $old . "\n+ " . $newstr . "\n", "none");
161
				}
Freddy's avatar
Freddy committed
162
			}
Luke081515's avatar
Luke081515 committed
163
			$answer = strtoupper($this->ask("Replace all occurrences? [Y], [N] ", "required"));
164 165 166 167
			if($this->validateAnswer($answer)) {
				if($answer == 'Y') {
					// Do replace
					$newhtml = $this->replace($html, $needle, $new);
168 169 170 171 172
					if (!$this->minor) {
						$this->editPage($seite, $newhtml, $this->summary);
					} else {
						$this->editPageMinor($seite, $newhtml, $this->summary);
					}
Luke081515's avatar
Luke081515 committed
173
					$this->echoMsg("\n" . "Replaced.", "success");
174 175
					break;
				} else {
Luke081515's avatar
Luke081515 committed
176
					$answer = strtoupper($this->ask("Add a space? [Y], [N] ", "required"));
177 178 179 180
					if($answer == 'Y') {
						$new = $new . " ";
					} else {
						// Leave it as it is
Luke081515's avatar
Luke081515 committed
181
						$this->echoMsg("\n" . "Not replaced. Skipping…", "warning");
182 183 184
						break;
					}
				}
Freddy's avatar
Freddy committed
185
			}
Luke081515's avatar
Luke081515 committed
186
		}
Freddy's avatar
Freddy committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
	}
	/** replace
	 * Replaces a string
	 * @param String $html – Full string
	 * @param String $needle – What to replace
	 * @param String $replace – Replacement
	 * @return String replaced String
	 */
	private function replace($html, $needle, $replace) {
		return str_replace($needle, $replace, $html);
	}
	/** validateAnswer
	 * validates if the answer is Y or N
	 * @param String $answer – String of answer
	 * @return boolean true if string equals 'Y' or 'N'
	 */
	private function validateAnswer($answer) {
		if($answer == 'Y' || $answer == 'N') {
			return true;
		} else {
Luke081515's avatar
Luke081515 committed
207
			$this->validateAnswer(strtoupper($this->ask('Please answer [Y], [N]', "required")));
Freddy's avatar
Freddy committed
208
		}
Luke081515's avatar
Luke081515 committed
209 210
	}
}
Freddy's avatar
Freddy committed
211
new Corrector ('Luke081515@dewiki', 'Corrector');
Luke081515's avatar
Luke081515 committed
212
?>