Passed
Push — master ( 0bf550...734688 )
by Daimona
02:16
created
includes/Task/UpdateList.php 1 patch
Spacing   +63 added lines, -63 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php declare( strict_types=1 );
1
+<?php declare(strict_types=1);
2 2
 
3 3
 namespace BotRiconferme\Task;
4 4
 
@@ -16,18 +16,18 @@  discard block
 block discarded – undo
16 16
 	 * @inheritDoc
17 17
 	 */
18 18
 	public function run() : TaskResult {
19
-		$this->getLogger()->info( 'Starting task UpdateList' );
19
+		$this->getLogger()->info('Starting task UpdateList');
20 20
 		$this->actualList = $this->getActualAdmins();
21 21
 		$this->botList = $this->getList();
22 22
 
23 23
 		$missing = $this->getMissingGroups();
24 24
 		$extra = $this->getExtraGroups();
25 25
 
26
-		if ( $missing || $extra ) {
27
-			$this->doUpdateList( $this->getNewContent( $missing, $extra ) );
26
+		if ($missing || $extra) {
27
+			$this->doUpdateList($this->getNewContent($missing, $extra));
28 28
 		}
29 29
 
30
-		if ( $this->errors ) {
30
+		if ($this->errors) {
31 31
 			// We're fine with it, but don't run other tasks
32 32
 			$msg = 'Task UpdateList completed with warnings.';
33 33
 			$status = self::STATUS_ERROR;
@@ -36,15 +36,15 @@  discard block
 block discarded – undo
36 36
 			$status = self::STATUS_OK;
37 37
 		}
38 38
 
39
-		$this->getLogger()->info( $msg );
40
-		return new TaskResult( $status, $this->errors );
39
+		$this->getLogger()->info($msg);
40
+		return new TaskResult($status, $this->errors);
41 41
 	}
42 42
 
43 43
 	/**
44 44
 	 * @return array
45 45
 	 */
46 46
 	protected function getActualAdmins() : array {
47
-		$this->getLogger()->debug( 'Retrieving admins - API' );
47
+		$this->getLogger()->debug('Retrieving admins - API');
48 48
 		$params = [
49 49
 			'action' => 'query',
50 50
 			'list' => 'allusers',
@@ -53,23 +53,23 @@  discard block
 block discarded – undo
53 53
 			'aulimit' => 'max',
54 54
 		];
55 55
 
56
-		$req = RequestBase::newFromParams( $params );
57
-		return $this->extractAdmins( $req->execute() );
56
+		$req = RequestBase::newFromParams($params);
57
+		return $this->extractAdmins($req->execute());
58 58
 	}
59 59
 
60 60
 	/**
61 61
 	 * @param array $data
62 62
 	 * @return array
63 63
 	 */
64
-	protected function extractAdmins( array $data ) : array {
64
+	protected function extractAdmins(array $data) : array {
65 65
 		$ret = [];
66
-		$blacklist = $this->getConfig()->get( 'exclude-admins' );
67
-		foreach ( $data['query']['allusers'] as $u ) {
68
-			if ( in_array( $u['name'], $blacklist ) ) {
66
+		$blacklist = $this->getConfig()->get('exclude-admins');
67
+		foreach ($data['query']['allusers'] as $u) {
68
+			if (in_array($u['name'], $blacklist)) {
69 69
 				continue;
70 70
 			}
71
-			$interestingGroups = array_intersect( $u['groups'], [ 'sysop', 'bureaucrat', 'checkuser' ] );
72
-			$ret[ $u['name'] ] = $interestingGroups;
71
+			$interestingGroups = array_intersect($u['groups'], ['sysop', 'bureaucrat', 'checkuser']);
72
+			$ret[$u['name']] = $interestingGroups;
73 73
 		}
74 74
 		return $ret;
75 75
 	}
@@ -78,10 +78,10 @@  discard block
 block discarded – undo
78 78
 	 * @return array
79 79
 	 */
80 80
 	protected function getList() : array {
81
-		$this->getLogger()->debug( 'Retrieving admins - JSON list' );
82
-		$content = $this->getController()->getPageContent( $this->getConfig()->get( 'list-title' ) );
81
+		$this->getLogger()->debug('Retrieving admins - JSON list');
82
+		$content = $this->getController()->getPageContent($this->getConfig()->get('list-title'));
83 83
 
84
-		return json_decode( $content, true );
84
+		return json_decode($content, true);
85 85
 	}
86 86
 
87 87
 	/**
@@ -89,20 +89,20 @@  discard block
 block discarded – undo
89 89
 	 */
90 90
 	protected function getMissingGroups() : array {
91 91
 		$missing = [];
92
-		foreach ( $this->actualList as $adm => $groups ) {
93
-			if ( !isset( $this->botList[ $adm ] ) ) {
92
+		foreach ($this->actualList as $adm => $groups) {
93
+			if (!isset($this->botList[$adm])) {
94 94
 				$groupsList = $groups;
95
-			} elseif ( count( $groups ) > count( $this->botList[$adm] ) ) {
95
+			} elseif (count($groups) > count($this->botList[$adm])) {
96 96
 				// Only some groups are missing
97
-				$groupsList = array_diff_key( $groups, $this->botList[$adm] );
97
+				$groupsList = array_diff_key($groups, $this->botList[$adm]);
98 98
 			} else {
99 99
 				continue;
100 100
 			}
101 101
 
102
-			foreach ( $groupsList as $group ) {
102
+			foreach ($groupsList as $group) {
103 103
 				try {
104
-					$missing[ $adm ][ $group ] = $this->getFlagDate( $adm, $group );
105
-				} catch ( TaskException $e ) {
104
+					$missing[$adm][$group] = $this->getFlagDate($adm, $group);
105
+				} catch (TaskException $e) {
106 106
 					$this->errors[] = $e->getMessage();
107 107
 				}
108 108
 			}
@@ -116,10 +116,10 @@  discard block
 block discarded – undo
116 116
 	 * @return string
117 117
 	 * @throws TaskException
118 118
 	 */
119
-	protected function getFlagDate( string $admin, string $group ) : string {
120
-		$this->getLogger()->info( "Retrieving $group flag date for $admin" );
119
+	protected function getFlagDate(string $admin, string $group) : string {
120
+		$this->getLogger()->info("Retrieving $group flag date for $admin");
121 121
 
122
-		if ( $group === 'checkuser' ) {
122
+		if ($group === 'checkuser') {
123 123
 			// Little hack
124 124
 			$oldUrl = RequestBase::$url;
125 125
 			RequestBase::$url = 'https://meta.wikimedia.org/w/api.php';
@@ -135,19 +135,19 @@  discard block
 block discarded – undo
135 135
 			'lelimit' => 'max'
136 136
 		];
137 137
 
138
-		$req = RequestBase::newFromParams( $params );
138
+		$req = RequestBase::newFromParams($params);
139 139
 		$data = $req->execute();
140
-		$ts = $this->extractTimestamp( $data, $group );
140
+		$ts = $this->extractTimestamp($data, $group);
141 141
 
142
-		if ( isset( $oldUrl ) ) {
142
+		if (isset($oldUrl)) {
143 143
 			RequestBase::$url = $oldUrl;
144 144
 		}
145 145
 
146
-		if ( $ts === null ) {
147
-			throw new TaskException( "$group flag date unavailable for $admin" );
146
+		if ($ts === null) {
147
+			throw new TaskException("$group flag date unavailable for $admin");
148 148
 		}
149 149
 
150
-		return date( "d/m/Y", strtotime( $ts ) );
150
+		return date("d/m/Y", strtotime($ts));
151 151
 	}
152 152
 
153 153
 	/**
@@ -155,15 +155,15 @@  discard block
 block discarded – undo
155 155
 	 * @param string $group
156 156
 	 * @return string|null
157 157
 	 */
158
-	private function extractTimestamp( array $data, string $group ) : ?string {
158
+	private function extractTimestamp(array $data, string $group) : ?string {
159 159
 		$ts = null;
160
-		foreach ( $data['query']['logevents'] as $entry ) {
161
-			if ( !isset( $entry['params'] ) ) {
160
+		foreach ($data['query']['logevents'] as $entry) {
161
+			if (!isset($entry['params'])) {
162 162
 				// Old entries
163 163
 				continue;
164 164
 			}
165
-			if ( in_array( $group, $entry['params']['newgroups'] ) &&
166
-				!in_array( $group, $entry['params']['oldgroups'] )
165
+			if (in_array($group, $entry['params']['newgroups']) &&
166
+				!in_array($group, $entry['params']['oldgroups'])
167 167
 			) {
168 168
 				$ts = $entry['timestamp'];
169 169
 				break;
@@ -176,11 +176,11 @@  discard block
 block discarded – undo
176 176
 	 */
177 177
 	protected function getExtraGroups() : array {
178 178
 		$extra = [];
179
-		foreach ( $this->botList as $name => $groups ) {
180
-			if ( !isset( $this->actualList[ $name ] ) ) {
181
-				$extra[ $name ] = $groups;
182
-			} elseif ( count( $groups ) > count( $this->actualList[ $name ] ) ) {
183
-				$extra[ $name ] = array_diff_key( $groups, $this->actualList[ $name ] );
179
+		foreach ($this->botList as $name => $groups) {
180
+			if (!isset($this->actualList[$name])) {
181
+				$extra[$name] = $groups;
182
+			} elseif (count($groups) > count($this->actualList[$name])) {
183
+				$extra[$name] = array_diff_key($groups, $this->actualList[$name]);
184 184
 			}
185 185
 		}
186 186
 		return $extra;
@@ -189,25 +189,25 @@  discard block
 block discarded – undo
189 189
 	/**
190 190
 	 * @param array $newContent
191 191
 	 */
192
-	protected function doUpdateList( array $newContent ) {
193
-		ksort( $newContent );
192
+	protected function doUpdateList(array $newContent) {
193
+		ksort($newContent);
194 194
 
195
-		if ( $newContent !== $this->botList ) {
196
-			$this->getLogger()->info( 'Updating admin list' );
195
+		if ($newContent !== $this->botList) {
196
+			$this->getLogger()->info('Updating admin list');
197 197
 		} else {
198
-			$this->getLogger()->info( 'Admin list already up-to-date' );
198
+			$this->getLogger()->info('Admin list already up-to-date');
199 199
 			return;
200 200
 		}
201 201
 
202
-		$stringified = json_encode( $newContent );
202
+		$stringified = json_encode($newContent);
203 203
 
204 204
 		$params = [
205
-			'title' => $this->getConfig()->get( 'list-title' ),
205
+			'title' => $this->getConfig()->get('list-title'),
206 206
 			'text' => $stringified,
207
-			'summary' => $this->getConfig()->get( 'list-update-summary' )
207
+			'summary' => $this->getConfig()->get('list-update-summary')
208 208
 		];
209 209
 
210
-		$this->getController()->editPage( $params );
210
+		$this->getController()->editPage($params);
211 211
 	}
212 212
 
213 213
 	/**
@@ -215,19 +215,19 @@  discard block
 block discarded – undo
215 215
 	 * @param array[] $extra
216 216
 	 * @return array[]
217 217
 	 */
218
-	protected function getNewContent( array $missing, array $extra ) : array {
218
+	protected function getNewContent(array $missing, array $extra) : array {
219 219
 		$newContent = $this->botList;
220
-		foreach ( $newContent as $user => $groups ) {
221
-			if ( isset( $missing[ $user ] ) ) {
222
-				$newContent[ $user ] = array_merge( $groups, $missing[ $user ] );
223
-				unset( $missing[ $user ] );
224
-			} elseif ( isset( $extra[ $user ] ) ) {
225
-				$newContent[ $user ] = array_diff_key( $groups, $extra[ $user ] );
220
+		foreach ($newContent as $user => $groups) {
221
+			if (isset($missing[$user])) {
222
+				$newContent[$user] = array_merge($groups, $missing[$user]);
223
+				unset($missing[$user]);
224
+			} elseif (isset($extra[$user])) {
225
+				$newContent[$user] = array_diff_key($groups, $extra[$user]);
226 226
 			}
227 227
 		}
228 228
 		// Add users which don't have an entry at all
229
-		$newContent = array_merge( $newContent, $missing );
229
+		$newContent = array_merge($newContent, $missing);
230 230
 		// And remove empty users
231
-		return array_filter( $newContent );
231
+		return array_filter($newContent);
232 232
 	}
233 233
 }
Please login to merge, or discard this patch.
includes/Config.php 1 patch
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php declare( strict_types=1 );
1
+<?php declare(strict_types=1);
2 2
 
3 3
 namespace BotRiconferme;
4 4
 
@@ -22,26 +22,26 @@  discard block
 block discarded – undo
22 22
 	 *
23 23
 	 * @param array $defaults
24 24
 	 */
25
-	public static function init( array $defaults ) {
26
-		if ( self::$instance ) {
27
-			throw new ConfigException( 'Config was already initialized' );
25
+	public static function init(array $defaults) {
26
+		if (self::$instance) {
27
+			throw new ConfigException('Config was already initialized');
28 28
 		}
29 29
 
30 30
 		$inst = new self;
31
-		$inst->set( 'list-title', $defaults['list-title'] );
32
-		$inst->set( 'username', $defaults['username'] );
33
-		$inst->set( 'password', $defaults['password'] );
31
+		$inst->set('list-title', $defaults['list-title']);
32
+		$inst->set('username', $defaults['username']);
33
+		$inst->set('password', $defaults['password']);
34 34
 		self::$instance = $inst;
35 35
 
36 36
 		// On-wiki values
37 37
 		try {
38
-			$conf = ( new WikiController )->getPageContent( $defaults[ 'config-title' ] );
39
-		} catch ( MissingPageException $e ) {
40
-			throw new ConfigException( 'Please create a config page.' );
38
+			$conf = (new WikiController)->getPageContent($defaults['config-title']);
39
+		} catch (MissingPageException $e) {
40
+			throw new ConfigException('Please create a config page.');
41 41
 		}
42 42
 
43
-		foreach ( json_decode( $conf, true ) as $key => $val ) {
44
-			self::$instance->set( $key, $val );
43
+		foreach (json_decode($conf, true) as $key => $val) {
44
+			self::$instance->set($key, $val);
45 45
 		}
46 46
 	}
47 47
 
@@ -49,8 +49,8 @@  discard block
 block discarded – undo
49 49
 	 * @param string $key
50 50
 	 * @param mixed $value
51 51
 	 */
52
-	public function set( string $key, $value ) {
53
-		$this->opts[ $key ] = $value;
52
+	public function set(string $key, $value) {
53
+		$this->opts[$key] = $value;
54 54
 	}
55 55
 
56 56
 	/**
@@ -59,8 +59,8 @@  discard block
 block discarded – undo
59 59
 	 * @return self
60 60
 	 */
61 61
 	public static function getInstance() : self {
62
-		if ( !self::$instance ) {
63
-			throw new ConfigException( 'Config not yet initialized' );
62
+		if (!self::$instance) {
63
+			throw new ConfigException('Config not yet initialized');
64 64
 		}
65 65
 		return self::$instance;
66 66
 	}
@@ -69,10 +69,10 @@  discard block
 block discarded – undo
69 69
 	 * @param string $opt
70 70
 	 * @return mixed
71 71
 	 */
72
-	public function get( string $opt ) {
73
-		if ( !isset( $this->opts[ $opt ] ) ) {
74
-			throw new ConfigException( "Config option '$opt' not set." );
72
+	public function get(string $opt) {
73
+		if (!isset($this->opts[$opt])) {
74
+			throw new ConfigException("Config option '$opt' not set.");
75 75
 		}
76
-		return $this->opts[ $opt ];
76
+		return $this->opts[$opt];
77 77
 	}
78 78
 }
Please login to merge, or discard this patch.
includes/WikiController.php 1 patch
Spacing   +30 added lines, -30 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php declare( strict_types=1 );
1
+<?php declare(strict_types=1);
2 2
 
3 3
 namespace BotRiconferme;
4 4
 
@@ -24,8 +24,8 @@  discard block
 block discarded – undo
24 24
 	 * @return string
25 25
 	 * @throws MissingPageException
26 26
 	 */
27
-	public function getPageContent( string $title ) : string {
28
-		$this->logger->debug( "Retrieving page $title" );
27
+	public function getPageContent(string $title) : string {
28
+		$this->logger->debug("Retrieving page $title");
29 29
 		$params = [
30 30
 			'action' => 'query',
31 31
 			'titles' => $title,
@@ -35,11 +35,11 @@  discard block
 block discarded – undo
35 35
 			'rvlimit' => 1
36 36
 		];
37 37
 
38
-		$req = RequestBase::newFromParams( $params );
38
+		$req = RequestBase::newFromParams($params);
39 39
 		$data = $req->execute();
40
-		$page = reset( $data['query']['pages'] );
41
-		if ( isset( $page['missing'] ) ) {
42
-			throw new MissingPageException( $title );
40
+		$page = reset($data['query']['pages']);
41
+		if (isset($page['missing'])) {
42
+			throw new MissingPageException($title);
43 43
 		}
44 44
 
45 45
 		return $page['revisions'][0]['slots']['main']['*'];
@@ -51,19 +51,19 @@  discard block
 block discarded – undo
51 51
 	 * @param array $params
52 52
 	 * @throws APIRequestException
53 53
 	 */
54
-	public function editPage( array $params ) {
54
+	public function editPage(array $params) {
55 55
 		$this->login();
56 56
 
57 57
 		$params = [
58 58
 			'action' => 'edit',
59
-			'token' => $this->getToken( 'csrf' ),
60
-			'bot' => Config::getInstance()->get( 'bot-edits' )
59
+			'token' => $this->getToken('csrf'),
60
+			'bot' => Config::getInstance()->get('bot-edits')
61 61
 		] + $params;
62 62
 
63
-		$req = RequestBase::newFromParams( $params, true );
63
+		$req = RequestBase::newFromParams($params, true);
64 64
 		$res = $req->execute();
65
-		if ( $res['edit']['result'] !== 'Success' ) {
66
-			throw new APIRequestException( $res['edit']['info'] );
65
+		if ($res['edit']['result'] !== 'Success') {
66
+			throw new APIRequestException($res['edit']['info']);
67 67
 		}
68 68
 	}
69 69
 
@@ -71,55 +71,55 @@  discard block
 block discarded – undo
71 71
 	 * @throws LoginException
72 72
 	 */
73 73
 	public function login() {
74
-		if ( self::$loggedIn ) {
75
-			$this->logger->debug( 'Already logged in' );
74
+		if (self::$loggedIn) {
75
+			$this->logger->debug('Already logged in');
76 76
 			return;
77 77
 		}
78 78
 
79
-		$this->logger->debug( 'Logging in' );
79
+		$this->logger->debug('Logging in');
80 80
 
81 81
 		$params = [
82 82
 			'action' => 'login',
83
-			'lgname' => Config::getInstance()->get( 'username' ),
84
-			'lgpassword' => Config::getInstance()->get( 'password' ),
85
-			'lgtoken' => $this->getToken( 'login' )
83
+			'lgname' => Config::getInstance()->get('username'),
84
+			'lgpassword' => Config::getInstance()->get('password'),
85
+			'lgtoken' => $this->getToken('login')
86 86
 		];
87 87
 
88 88
 		try {
89
-			$req = RequestBase::newFromParams( $params, true );
89
+			$req = RequestBase::newFromParams($params, true);
90 90
 			$res = $req->execute();
91
-		} catch ( APIRequestException $e ) {
92
-			throw new LoginException( $e->getMessage() );
91
+		} catch (APIRequestException $e) {
92
+			throw new LoginException($e->getMessage());
93 93
 		}
94 94
 
95
-		if ( !isset( $res['login']['result'] ) || $res['login']['result'] !== 'Success' ) {
96
-			throw new LoginException( 'Unknown error' );
95
+		if (!isset($res['login']['result']) || $res['login']['result'] !== 'Success') {
96
+			throw new LoginException('Unknown error');
97 97
 		}
98 98
 
99 99
 		self::$loggedIn = true;
100 100
 		// Clear tokens cache
101 101
 		$this->tokens = [];
102
-		$this->logger->debug( 'Login succeeded' );
102
+		$this->logger->debug('Login succeeded');
103 103
 	}
104 104
 
105 105
 	/**
106 106
 	 * @param string $type
107 107
 	 * @return string
108 108
 	 */
109
-	public function getToken( string $type ) : string {
110
-		if ( !isset( $this->tokens[ $type ] ) ) {
109
+	public function getToken(string $type) : string {
110
+		if (!isset($this->tokens[$type])) {
111 111
 			$params = [
112 112
 				'action' => 'query',
113 113
 				'meta'   => 'tokens',
114 114
 				'type'   => $type
115 115
 			];
116 116
 
117
-			$req = RequestBase::newFromParams( $params );
117
+			$req = RequestBase::newFromParams($params);
118 118
 			$res = $req->execute();
119 119
 
120
-			$this->tokens[ $type ] = $res['query']['tokens']["{$type}token"];
120
+			$this->tokens[$type] = $res['query']['tokens']["{$type}token"];
121 121
 		}
122 122
 
123
-		return $this->tokens[ $type ];
123
+		return $this->tokens[$type];
124 124
 	}
125 125
 }
Please login to merge, or discard this patch.