Debug.php 29.3 KB
Newer Older
Luke081515's avatar
Luke081515 committed
1 2 3 4 5 6 7 8 9 10 11 12 13
<?php
require_once __DIR__ . '/BotCore.php';
/** Debug.php
* This is a script for Cygnus which allows to execute single functions interactivly
* @Author Luke081515 <luke081515@tools.wmflabs.org>
* @Version 0.1
* @Status Alpha
*/
class Debug extends Core {
	public function __construct() {
		$this->setSite($this->askRequired('Enter the domain to use:'));
		$this->setUsername($this->askRequired('Enter the username to use:'));
		$this->setPassword($this->askRequired('Enter the password to use:'));
Luke081515's avatar
Luke081515 committed
14 15 16 17 18 19
		$patchAnswer = $this->askRequired('Use a non default target? [y/N]');
		if (strtolower($patchAnswer) !== 'n') {
			$this->setTarget($this->askRequired('Enter the value for the target:'));
		} else {
			$this->setTarget("w/api.php");
		}
Luke081515's avatar
Luke081515 committed
20 21 22 23 24 25 26
		$assert = $this->askRequired('Enter the value for assert:');
		$debug = $this->askRequired('Use verbose debug mode? [y/N]');
		if (strtolower($debug) !== 'n') {
			$debug = true;
		} else {
			$debug = false;
		}
Luke081515's avatar
Luke081515 committed
27
		$this->initcurlArgs('Debug', true, $assert, false, $debug);
Luke081515's avatar
Luke081515 committed
28
		$this->login();
Luke081515's avatar
Luke081515 committed
29 30 31 32
		do {
			$this->debug($this->askRequired("Name of the function you want to debug:"));
			$answer = $this->askRequired("Do you want to debug another function? [y/N]");
		} while (strtolower($answer) !== 'n');
Luke081515's avatar
Luke081515 committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
	}
	/** debug
	* This is the debug engine, here you need to define all methods the script can use
	*/
	private function debug($MethodName) {
		switch ($MethodName) {
			// Reading functions
			case 'readPage':
				$required = array("title");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of readPage...');
				$starttime = microtime (true);
				try {
					$ret = $this->readPage($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
48
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
49 50
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
51
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
52 53 54 55 56 57 58 59 60 61
				}
				break;
			case 'readPageID':
				$required = array("pageID");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of readPageID...');
				$starttime = microtime (true);
				try {
					$ret = $this->readPageID($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
62
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
63 64
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
65
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
66 67 68 69 70 71 72 73 74 75
				}
				break;
			case 'readPageJs':
				$required = array("title");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of readPageJs...');
				$starttime = microtime (true);
				try {
					$ret = $this->readPageJs($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
76
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
77 78
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
79
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
80 81 82 83 84 85 86 87 88 89
				}
				break;
			case 'readPageCss':
				$required = array("title");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of readPageCss...');
				$starttime = microtime (true);
				try {
					$ret = $this->readPageCss($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
90
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
91 92
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
93
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
94 95 96 97 98 99 100 101 102 103
				}
				break;
			case 'readSection':
				$required = array("title", "section");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of readSection...');
				$starttime = microtime (true);
				try {
					$ret = $this->readSection($Param[0], $Param[1]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
104
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
105 106
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
107
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
108 109 110
				}
				break;
			case 'getTableOfContents':
111
				$required = array("title");
Luke081515's avatar
Luke081515 committed
112 113 114 115 116 117
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getTableOfContents...');
				$starttime = microtime (true);
				try {
					$ret = $this->getTableOfContents($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
118
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
119 120
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
121
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
122 123 124 125 126 127 128 129 130 131 132 133 134
				}
				break;
			// editPage functions
			case 'editPage':
				$required = array("title", "content", "summary");
				$optional = array("noCreate", "overrideNobots");
				$optvalues = array(1, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of editPage...');
				$starttime = microtime (true);
				try {
					$ret = $this->editPage($Param[0], $Param[1], $Param[2], intval($Param[3]), boolval($Param[4]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
135
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
136 137
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
138
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
139 140 141 142 143 144 145 146 147 148 149 150
				}
				break;
			case 'editPageMinor':
				$required = array("title", "content", "summary");
				$optional = array("noCreate", "overrideNobots");
				$optvalues = array(1, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of editPageMinor...');
				$starttime = microtime (true);
				try {
					$ret = $this->editPageMinor($Param[0], $Param[1], $Param[2], intval($Param[3]), boolval($Param[4]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
151
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
152 153
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
154
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
155 156 157 158 159 160 161 162 163 164 165 166
				}
				break;
			case 'editPageD':
				$required = array("title", "content", "summary", "botflag", "minorflag");
				$optional = array("noCreate", "overrideNobots");
				$optvalues = array(1, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of editPageD...');
				$starttime = microtime (true);
				try {
					$ret = $this->editPageD($Param[0], $Param[1], $Param[2], $Param[3], $Param[4], intval($Param[5]), boolval($Param[6]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
167
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
168 169
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
170
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
171 172 173 174 175 176 177 178 179 180 181 182
				}
				break;
			case 'editSection':
				$required = array("title", "content", "summary", "sectionnumber");
				$optional = array("noCreate", "overrideNobots");
				$optvalues = array(1, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of editSection...');
				$starttime = microtime (true);
				try {
					$ret = $this->editSection($Param[0], $Param[1], $Param[2], $Param[3], intval($Param[4]), boolval($Param[5]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
183
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
184 185
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
186
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
187 188 189 190 191 192 193 194 195 196 197 198
				}
				break;
			case 'editSectionMinor':
				$required = array("title", "content", "summary", "sectionnumber");
				$optional = array("noCreate", "overrideNobots");
				$optvalues = array(1, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of editSectionMinor...');
				$starttime = microtime (true);
				try {
					$ret = $this->editSectionMinor($Param[0], $Param[1], $Param[2], $Param[3], intval($Param[4]), boolval($Param[5]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
199
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
200 201
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
202
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
203 204 205 206 207 208 209 210 211 212 213 214
				}
				break;
			case 'editSectionD':
				$required = array("title", "content", "summary", "sectionnumber", "botflag", "minorflag");
				$optional = array("noCreate", "overrideNobots");
				$optvalues = array(1, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of editSectionD...');
				$starttime = microtime (true);
				try {
					$ret = $this->editSectionD($Param[0], $Param[1], $Param[2], $Param[3], $Param[4], $Param[5], intval($Param[6]), boolval($Param[7]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
215
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
216 217
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
218
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
219 220 221
				}
				break;
			case 'movePage':
222
				$required = array("oldTitle", "newTitle", "reason");
Luke081515's avatar
Luke081515 committed
223 224 225 226 227 228 229 230
				$optional = array("bot", "movetalk", "noredirect");
				$optvalues = array(0, 1, 1);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of movePage...');
				$starttime = microtime (true);
				try {
					$ret = $this->movePage($Param[0], $Param[1], $Param[2], intval($Param[3]), intval($Param[4]), intval($Param[5]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
231
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
232 233
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
234
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
235 236
				}
				break;
Luke081515's avatar
Luke081515 committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
			case 'rollback':
				$required = array("title", "user");
				$optional = array("summary", "markbot");
				$optvalues = array("", 0);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of rollback...');
				$starttime = microtime (true);
				try {
					$ret = $this->rollback($Param[0], $Param[1], $Param[2], intval($Param[3]));
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
253 254 255 256 257 258 259 260
			case 'watch':
				$required = array("title");
				$optional = array("unwatch");
				$optvalues = array(0);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of watch...');
				$starttime = microtime (true);
				try {
261
					$ret = $this->watch($Param[0], intval($Param[1]));
Luke081515's avatar
Luke081515 committed
262
					$endtime = microtime (true);
263
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
264 265
				} catch (Exception $e) {
					$endtime = microtime (true);
266
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
267 268
				}
				break;
Luke081515's avatar
Luke081515 committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
			case 'purge':
				$required = array("title");
				$optional = array("forcelinkupdate", "forcerecursivelinkupdate");
				$optvalues = array(0, 0);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of purge...');
				$starttime = microtime (true);
				try {
					$ret = $this->purge($Param[0], intval($Param[1]), intval($Param[2]));
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
			case 'patrol':
				$required = array("id");
				$optional = array("revid");
				$optvalues = array(true);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of patrol...');
				$starttime = microtime (true);
				try {
					$ret = $this->patrol($Param[0], boolval($Param[1]));
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
301 302 303 304 305 306 307 308
			case 'review':
				$required = array("revid", "comment", "reason");
				$optional = array("unapprove");
				$optvalues = array(0);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of review...');
				$starttime = microtime (true);
				try {
Luke081515's avatar
Luke081515 committed
309
					$ret = $this->review($Param[0], $Param[1], $Param[2], intval($Param[3]));
Luke081515's avatar
Luke081515 committed
310
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
311
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
312 313
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
314
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
315 316
				}
				break;
Luke081515's avatar
Luke081515 committed
317
			// User information functions
318 319 320 321 322 323 324 325
			case 'checkUserExistence':
				$required = array("username");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of checkUserExistence...');
				$starttime = microtime(true);
				try {
					$ret = $this->checkUserExistence($Param[0]);
					$endtime = microtime(true);
Luke081515's avatar
Luke081515 committed
326
					$this->processFunction($ret, $starttime, $endtime);
327
				} catch (Exception $e) {
Luke081515's avatar
Luke081515 committed
328 329
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
330 331
				}
				break;
Luke081515's avatar
Luke081515 committed
332 333 334 335 336 337 338 339
			case 'getUserEditcount':
				$required = array("username");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getUserEditcount...');
				$starttime = microtime(true);
				try {
					$ret = $this->getUserEditcount($Param[0]);
					$endtime = microtime(true);
Luke081515's avatar
Luke081515 committed
340
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
341
				} catch (Exception $e) {
Luke081515's avatar
Luke081515 committed
342 343
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
344 345
				}
				break;
Luke081515's avatar
Luke081515 committed
346 347 348 349 350 351 352 353
			case 'checkUserBlock':
				$required = array("username");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of checkUserBlock...');
				$starttime = microtime(true);
				try {
					$ret = $this->checkUserBlock($Param[0]);
					$endtime = microtime(true);
Luke081515's avatar
Luke081515 committed
354
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
355
				} catch (Exception $e) {
Luke081515's avatar
Luke081515 committed
356 357
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
358 359
				}
				break;
Luke081515's avatar
Luke081515 committed
360 361 362 363 364 365 366 367
			case 'checkUserMail':
				$required = array("username");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of checkUserMail...');
				$starttime = microtime(true);
				try {
					$ret = $this->checkUserMail($Param[0]);
					$endtime = microtime(true);
Luke081515's avatar
Luke081515 committed
368
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
369
				} catch (Exception $e) {
Luke081515's avatar
Luke081515 committed
370 371
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
372 373
				}
				break;
Luke081515's avatar
Luke081515 committed
374 375 376 377 378 379 380 381
			case 'getUserGroups':
				$required = array("username");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getUserGroups...');
				$starttime = microtime(true);
				try {
					$ret = $this->getUserGroups($Param[0]);
					$endtime = microtime(true);
382
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
383 384 385 386 387
				} catch (Exception $e) {
					$endtime = microtime(true);
					$this->processError($e, $endtime);
				}
				break;
~rs's avatar
~rs committed
388 389 390 391 392 393 394 395 396 397 398 399 400 401
			case 'getUserGender':
				$required = array("username");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getUserGender...');
				$starttime = microtime(true);
				try {
					$ret = $this->getUserGender($Param[0]);
					$endtime = microtime(true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
402
			// Query functions
Luke081515's avatar
Luke081515 committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416
			case 'checkTemplate':
				$required = array("page", "template");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of checkTemplate...');
				$starttime = microtime (true);
				try {
					$ret = $this->checkTemplate($Param[0], $Param[1]);
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
417 418 419 420 421 422 423 424 425 426
			case 'getCatMembers':
				$required = array("kat");
				$optional = array("onlySubCats", "excludeWls");
				$optvalues = array(false, false);
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of getCatMembers...');
				$starttime = microtime (true);
				try {
					$ret = $this->getCatMembers($Param[0], boolval($Param[1]), boolval($Param[2]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
427
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
428 429
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
430
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
431 432 433
				}
				break;
			case 'getPageCats':
434
				$required = array("title");
Luke081515's avatar
Luke081515 committed
435 436 437 438 439 440
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getPageCats...');
				$starttime = microtime (true);
				try {
					$ret = $this->getPageCats($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
441
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
442 443
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
444
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
445 446
				}
				break;
447
			case 'getAllEmbeddings':
Luke081515's avatar
Luke081515 committed
448 449
				$required = array("templ");
				$Param = $this->getParams($required);
450
				$this->echoNotice('Starting the function call of getAllEmbeddings...');
Luke081515's avatar
Luke081515 committed
451 452
				$starttime = microtime (true);
				try {
453
					$ret = $this->getAllEmbeddings($Param[0]);
Luke081515's avatar
Luke081515 committed
454
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
455
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
456 457
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
458
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
459 460
				}
				break;
461 462 463 464 465 466 467 468 469 470 471 472 473 474
			case 'getMissingLinks':
				$required = array("page");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getMissingLinks...');
				$starttime = microtime(true);
				try {
					$ret = $this->getMissingLinks($Param[0]);
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
475 476 477 478 479 480 481 482
			case 'getAllPages':
				$required = array("namespace");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getAllPages...');
				$starttime = microtime (true);
				try {
					$ret = $this->getAllPages($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
483
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
484 485
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
486
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
487 488 489
				}
				break;
			case 'getPageID':
490
				$required = array("title");
Luke081515's avatar
Luke081515 committed
491 492 493 494 495 496
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getPageID...');
				$starttime = microtime (true);
				try {
					$ret = $this->getPageID($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
497
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
498 499
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
500
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
501 502 503
				}
				break;
			case 'getLinks':
504
				$required = array("title");
Luke081515's avatar
Luke081515 committed
505 506 507 508 509 510
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getLinks...');
				$starttime = microtime (true);
				try {
					$ret = $this->getLinks($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
511
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
512 513
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
514
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
515 516 517 518 519 520 521 522 523 524
				}
				break;
			case 'getSectionTitle':
				$required = array("title", "section");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of getSectionTitle...');
				$starttime = microtime (true);
				try {
					$ret = $this->getSectionTitle($Param[0]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
525
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
526 527
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
528
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
529 530
				}
				break;
~rs's avatar
~rs committed
531
			case 'search':
532 533 534 535
				$required = array("pattern");
				$optional = array("ns", "prop", "limit", "offset", "what");
				$optvalues = array(0, "size|wordcount|timestamp|snippet", 50, 0, "text");
				$Param = $this->getParams($required, $optional, $optvalues);
~rs's avatar
~rs committed
536 537 538
				$this->echoNotice('Starting the function call of search...');
				$starttime = microtime (true);
				try {
~rs's avatar
~rs committed
539
					$ret = $this->search($Param[0], $Param[1], $Param[2], $Param[3], $Param[4], $Param[5]);
~rs's avatar
~rs committed
540
					$endtime = microtime (true);
~rs's avatar
~rs committed
541
					$this->processFunction($ret, $starttime, $endtime, true);
~rs's avatar
~rs committed
542 543 544 545 546
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
547 548
			// Admin functions
			case 'deletePage':
549
				$required = array("title", "reason");
Luke081515's avatar
Luke081515 committed
550 551 552 553 554 555
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of deletePage...');
				$starttime = microtime (true);
				try {
					$ret = $this->deletePage($Param[0], $Param[1]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
556
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
557 558
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
559
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
560 561 562 563 564 565
				}
				break;
			case 'blockUser':
				$required = array("user", "reason", "expiry");
				$optional = array("anononly", "nocreate", "autoblock", "noemail", "hidename", "allowusertalk", "reblock");
				$optvalues = array(1, 1, 1, 0, 0, 1, 0);
Luke081515's avatar
Luke081515 committed
566
				$Param = $this->getParams($required, $optional, $optvalues);
Luke081515's avatar
Luke081515 committed
567 568 569 570 571 572
				$this->echoNotice('Starting the function call of blockUser...');
				$starttime = microtime (true);
				try {
					$ret = $this->blockUser($Param[0], $Param[1], $Param[2], intval($Param[3]), intval($Param[4]),
						intval($Param[5]), intval($Param[6]), intval($Param[7]), intval($Param[8]), intval($Param[9]));
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
573
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
574 575
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
576
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
577 578 579 580 581 582 583 584 585 586
				}
				break;
			case 'unblockUser':
				$required = array("user", "reason");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of unblockUser...');
				$starttime = microtime (true);
				try {
					$ret = $this->unblockUser($Param[0], $Param[1]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
587
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
588 589
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
590
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
591 592
				}
				break;
Luke081515's avatar
Luke081515 committed
593 594 595 596 597
			case 'blockGlobal':
				$required = array("user", "reason", "expiry");
				$optional = array("unblock", "anononly", "modify");
				$optvalues = array(0, 1, 0);
				$Param = $this->getParams($required, $optional, $optvalues);
Luke081515's avatar
Luke081515 committed
598
				$this->echoNotice('Starting the function call of blockGlobal...');
Luke081515's avatar
Luke081515 committed
599 600 601 602 603 604 605 606 607 608 609
				$starttime = microtime (true);
				try {
					$ret = $this->blockGlobal($Param[0], $Param[1], $Param[2], intval($Param[3]), intval($Param[4]),
						intval($Param[5]));
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
610 611 612 613 614 615
			case 'lockGlobal':
				$required = array("user", "lock", "suppress", "reason");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of lockGlobal...');
				$starttime = microtime (true);
				try {
616
					$ret = $this->lockGlobal($Param[0], $Param[1], $Param[2], $Param[3], $Param[4]);
Luke081515's avatar
Luke081515 committed
617 618 619 620 621 622 623
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
624 625 626 627 628 629 630 631 632
			case 'changeUserrights':
				$required = array("username", "groupAdd", "groupRemove", "reason");
				$optional = array("expiry");
				$optvalues = array("infinite");
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of changeUserrights...');
				$starttime = microtime (true);
				try {
					$ret = $this->changeUserrights($Param[0], $Param[1], $Param[2], $Param[3], $Param[4]);
Luke081515's avatar
Luke081515 committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
			case 'changeGlobalUserrights':
				$required = array("username", "groupAdd", "groupRemove", "reason");
				$optional = array("expiry");
				$optvalues = array("infinite");
				$Param = $this->getParams($required, $optional, $optvalues);
				$this->echoNotice('Starting the function call of changeGlobalUserrights...');
				$starttime = microtime (true);
				try {
					$ret = $this->changeGlobalUserrights($Param[0], $Param[1], $Param[2], $Param[3], $Param[4]);
Luke081515's avatar
Luke081515 committed
649 650 651 652 653 654 655
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
656
			case 'protectPage':
657
				$required = array("title", "reason", "protections", "expiry", "cascade");
Luke081515's avatar
Luke081515 committed
658 659 660 661 662 663
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of protectPage...');
				$starttime = microtime (true);
				try {
					$ret = $this->protectPage($Param[0], $Param[1], $Param[2], $Param[3], $Param[4]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
664
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
665 666
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
667
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
668 669 670 671 672 673 674 675 676 677
				}
				break;
			case 'stabilize':
				$required = array("title", "expiry", "reason", "default", "autoreview", "review");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of stabilize...');
				$starttime = microtime (true);
				try {
					$ret = $this->stabilize($Param[0], $Param[1], $Param[2], $Param[3], $Param[4]);
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
678
					$this->processFunction($ret, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
679 680
				} catch (Exception $e) {
					$endtime = microtime (true);
Luke081515's avatar
Luke081515 committed
681
					$this->processError($e, $starttime, $endtime);
Luke081515's avatar
Luke081515 committed
682 683
				}
				break;
Luke081515's avatar
Luke081515 committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697
			case 'writeToFile':
				$required = array("filename", "text");
				$Param = $this->getParams($required);
				$this->echoNotice('Starting the function call of writeToFile...');
				$starttime = microtime (true);
				try {
					$ret = $this->writeToFile($Param[0], $Param[1]);
					$endtime = microtime (true);
					$this->processFunction($ret, $starttime, $endtime);
				} catch (Exception $e) {
					$endtime = microtime (true);
					$this->processError($e, $starttime, $endtime);
				}
				break;
Luke081515's avatar
Luke081515 committed
698
			default:
699
				echo "This function does not exist, or is not configured.";
Luke081515's avatar
Luke081515 committed
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
		}
	}
	/** getParams
	* This method gets the params from the user
	* There are two lists, one with params, one with optional params
	*/
	private function getParams($ParamList, $ParamOptList = null, $ParamOptDefault = null) {
		$a = 0;
		while (isset($ParamList[$a])) {
			$Param[$a] = $this->askRequired('Enter the value for the param ' . $ParamList[$a] . ' to use:');
			$a++;
		}
		$b = 0;
		while (isset($ParamOptList[$b])) {
			$res = $this->askOptional('Enter the value for the optional param ' . $ParamOptList[$b] . ' (Leave blank to use default):');
Luke081515's avatar
Luke081515 committed
715
			if ($res === "") {
Luke081515's avatar
Luke081515 committed
716
				$Param[$a] = $ParamOptDefault[$b];
Luke081515's avatar
Luke081515 committed
717 718 719
			} else {
				$Param[$a] = $res;
			}
Luke081515's avatar
Luke081515 committed
720 721 722 723 724 725 726 727 728 729 730
			$a++;
			$b++;
		}
		return $Param;
	}
	/** echoCritical
	* Writes out a critical message
	* Use this only for critical messages
	* Prints out the message in red
	*/
	private function echoCritical($msg) {
731
		echo "\n\033[01;31m" . $msg . "\033[0m";
Luke081515's avatar
Luke081515 committed
732 733 734 735 736 737
	}
	/** echoSuccessful
	* Use this only when a task was successful
	* Prints out the message in green
	*/
	private function echoSuccessful($msg) {
738
		echo "\n\033[01;32m" . $msg . "\033[0m";
Luke081515's avatar
Luke081515 committed
739 740 741 742 743 744 745
	}
	/** echoWarning
	* Writes out a warning
	* Use this only for non-critical warnings
	* Prints the message out in yellow
	*/
	private function echoWarning($msg) {
746
		echo "\n\033[01;33m" . $msg . "\033[0m";
Luke081515's avatar
Luke081515 committed
747 748 749 750 751 752 753
	}
	/** echoNotice
	* Writes out a notice
	* Use this only for notices, no output, no warnings/errors
	* Prints the message out in blue
	*/
	private function echoNotice($msg) {
754
		echo "\n\033[01;34m" . $msg . "\033[0m";
Luke081515's avatar
Luke081515 committed
755 756 757 758 759 760 761
	}
	/** echoOutput
	* Writes out the output of a function
	* Use this only for function output
	* Prints the message out in purple
	*/
	private function echoOutput($msg) {
762 763 764 765
		if ($msg === true) {
			echo "\n\033[01;35mReturned value: true\033[0m";
		} else if ($msg === false) {
			echo "\n\033[01;35mReturned value: false\033[0m";
766 767
		} else if ($msg === null) {
			echo "\n\033[01;35mReturned value: NULL\033[0m";
768 769 770
		} else {
			echo "\n\033[01;35m" . $msg . "\033[0m";
		}
Luke081515's avatar
Luke081515 committed
771 772 773 774 775 776 777
	}
	/** askRequired
	* Asks for something
	* Use this only if the answer is required
	* Prints the answer in yellow
	*/
	private function askRequired($msg) {
778
		return $this->askOperator("\n\033[01;33m" . $msg . "\033[0m");
Luke081515's avatar
Luke081515 committed
779 780 781 782 783 784 785
	}
	/** askOptional
	* Asks for something
	* Use this only if the answer is optional (additional param for example)
	* Prints the answer in cyan
	*/
	private function askOptional($msg) {
786
		return $this->askOperator("\n\033[01;36m" . $msg . "\033[0m");
Luke081515's avatar
Luke081515 committed
787 788 789 790
	}
	/** processFunction
	* Internal, used for successful calls
	*/
791
	private function processFunction($ret, $starttime, $endtime) {
Luke081515's avatar
Luke081515 committed
792 793 794 795
		$total = $endtime - $starttime;
		$this->echoSuccessful('Function call succeeded');
		$this->echoNotice('Performance: ' . $total . ' seconds');
		$answer = $this->askRequired('Display the result now? [y/N]');
Luke081515's avatar
Luke081515 committed
796
		if (strtolower($answer) !== 'n') {
797
			if (is_array($ret)) {
798
				var_dump($ret);
799 800
			} else {
				$this->echoOutput($ret);
801
			}
Luke081515's avatar
Luke081515 committed
802
		}
Luke081515's avatar
Luke081515 committed
803 804 805 806
	}
	/** processError
	* Internal, used for errors
	*/
Luke081515's avatar
Luke081515 committed
807
	private function processError ($err, $starttime, $endtime) {
Luke081515's avatar
Luke081515 committed
808 809 810 811 812 813 814 815 816
		$total = $endtime - $starttime;
		$this->echoCritical('Function call failed after ' . $total . ' seconds');
		$answer = $this->askRequired('Display the error now? [y/N]');
		if (strtolower($answer) !== 'n')
			$this->echoWarning($err);
	}
}
$Bot = new Debug();
?>