Passed
Branch master (96aa6d)
by Daimona
01:34
created
Category
includes/Task/UpdateList.php 1 patch
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -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,13 +116,13 @@  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
-			$oldUrl = $this->getConfig()->get( 'url' );
125
-			$this->getConfig()->set( 'url', 'https://meta.wikimedia.org/w/api.php' );
124
+			$oldUrl = $this->getConfig()->get('url');
125
+			$this->getConfig()->set('url', 'https://meta.wikimedia.org/w/api.php');
126 126
 			$admin .= '@itwiki';
127 127
 		}
128 128
 
@@ -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 ) ) {
143
-			$this->getConfig()->set( 'url', $oldUrl );
142
+		if (isset($oldUrl)) {
143
+			$this->getConfig()->set('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/Task/Task.php 1 patch
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -18,9 +18,9 @@  discard block
 block discarded – undo
18 18
 	/**
19 19
 	 * @param TaskDataProvider $dataProvider
20 20
 	 */
21
-	public function __construct( TaskDataProvider $dataProvider ) {
22
-		set_exception_handler( [ $this, 'handleException' ] );
23
-		set_error_handler( [ $this, 'handleError' ] );
21
+	public function __construct(TaskDataProvider $dataProvider) {
22
+		set_exception_handler([$this, 'handleException']);
23
+		set_error_handler([$this, 'handleError']);
24 24
 		parent::__construct();
25 25
 		$this->dataProvider = $dataProvider;
26 26
 	}
@@ -43,9 +43,9 @@  discard block
 block discarded – undo
43 43
 	 * @param \Throwable $ex
44 44
 	 * @protected
45 45
 	 */
46
-	public function handleException( \Throwable $ex ) {
46
+	public function handleException(\Throwable $ex) {
47 47
 		$this->getLogger()->error(
48
-			get_class( $ex ) . ': ' .
48
+			get_class($ex) . ': ' .
49 49
 			$ex->getMessage() . "\nTrace:\n" .
50 50
 			$ex->getTraceAsString()
51 51
 		);
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
60 60
 	 * @param int $errline
61 61
 	 * @protected
62 62
 	 */
63
-	public function handleError( $errno, $errstr, $errfile, $errline ) {
64
-		throw new \ErrorException( $errstr, 0, $errno, $errfile, $errline );
63
+	public function handleError($errno, $errstr, $errfile, $errline) {
64
+		throw new \ErrorException($errstr, 0, $errno, $errfile, $errline);
65 65
 	}
66 66
 
67 67
 	/**
Please login to merge, or discard this patch.
includes/Task/CreatePage.php 1 patch
Spacing   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -11,42 +11,42 @@  discard block
 block discarded – undo
11 11
 	 * @inheritDoc
12 12
 	 */
13 13
 	public function run() : TaskResult {
14
-		$this->getLogger()->info( 'Starting task CreatePage' );
14
+		$this->getLogger()->info('Starting task CreatePage');
15 15
 		$users = $this->getDataProvider()->getUsersToProcess();
16 16
 
17
-		foreach ( $users as $user => $groups ) {
18
-			$this->processUser( $user, $groups );
17
+		foreach ($users as $user => $groups) {
18
+			$this->processUser($user, $groups);
19 19
 		}
20 20
 
21
-		$this->getLogger()->info( 'Task CreatePage completed successfully' );
22
-		return new TaskResult( self::STATUS_OK );
21
+		$this->getLogger()->info('Task CreatePage completed successfully');
22
+		return new TaskResult(self::STATUS_OK);
23 23
 	}
24 24
 
25 25
 	/**
26 26
 	 * @param string $user
27 27
 	 * @param array $groups
28 28
 	 */
29
-	protected function processUser( string $user, array $groups ) {
29
+	protected function processUser(string $user, array $groups) {
30 30
 		try {
31
-			$num = $this->getLastPageNum( $user ) + 1;
32
-		} catch ( TaskException $e ) {
31
+			$num = $this->getLastPageNum($user) + 1;
32
+		} catch (TaskException $e) {
33 33
 			// The page was already created.
34
-			$this->getDataProvider()->removeUser( $user );
35
-			$this->getLogger()->warning( $e->getMessage() . "\nRemoving $user." );
34
+			$this->getDataProvider()->removeUser($user);
35
+			$this->getLogger()->warning($e->getMessage() . "\nRemoving $user.");
36 36
 			return;
37 37
 		}
38 38
 
39
-		$baseTitle = $this->getConfig()->get( 'ric-main-page' ) . "/$user";
39
+		$baseTitle = $this->getConfig()->get('ric-main-page') . "/$user";
40 40
 		$pageTitle = "$baseTitle/$num";
41
-		$this->doCreatePage( $pageTitle, $user, $groups );
41
+		$this->doCreatePage($pageTitle, $user, $groups);
42 42
 
43
-		$newText = str_replace( '$title', $pageTitle, $this->getConfig()->get( 'ric-base-page-text' ) );
44
-		if ( $num === 1 ) {
45
-			$this->createBasePage( $baseTitle, $newText );
43
+		$newText = str_replace('$title', $pageTitle, $this->getConfig()->get('ric-base-page-text'));
44
+		if ($num === 1) {
45
+			$this->createBasePage($baseTitle, $newText);
46 46
 		} else {
47
-			$this->updateBasePage( $baseTitle, $newText );
47
+			$this->updateBasePage($baseTitle, $newText);
48 48
 		}
49
-		$this->getDataProvider()->addCreatedPages( $pageTitle );
49
+		$this->getDataProvider()->addCreatedPages($pageTitle);
50 50
 	}
51 51
 
52 52
 	/**
@@ -54,9 +54,9 @@  discard block
 block discarded – undo
54 54
 	 * @return int
55 55
 	 * @throws TaskException
56 56
 	 */
57
-	protected function getLastPageNum( string $user ) : int {
58
-		$this->getLogger()->debug( "Retrieving previous pages for $user" );
59
-		$unprefixedTitle = explode( ':', $this->getConfig()->get( 'ric-main-page' ), 2 )[1];
57
+	protected function getLastPageNum(string $user) : int {
58
+		$this->getLogger()->debug("Retrieving previous pages for $user");
59
+		$unprefixedTitle = explode(':', $this->getConfig()->get('ric-main-page'), 2)[1];
60 60
 		$params = [
61 61
 			'action' => 'query',
62 62
 			'list' => 'allpages',
@@ -65,16 +65,16 @@  discard block
 block discarded – undo
65 65
 			'aplimit' => 'max'
66 66
 		];
67 67
 
68
-		$res = ( RequestBase::newFromParams( $params ) )->execute();
68
+		$res = (RequestBase::newFromParams($params))->execute();
69 69
 
70 70
 		$last = 0;
71
-		foreach ( $res['query']['allpages'] as $page ) {
72
-			if ( $this->pageWasCreatedToday( $page['title'] ) ) {
73
-				throw new TaskException( 'Page ' . $page['title'] . ' was already created.' );
71
+		foreach ($res['query']['allpages'] as $page) {
72
+			if ($this->pageWasCreatedToday($page['title'])) {
73
+				throw new TaskException('Page ' . $page['title'] . ' was already created.');
74 74
 			}
75
-			$bits = explode( '/', $page['title'] );
76
-			$cur = intval( end( $bits ) );
77
-			if ( is_numeric( $cur ) && $cur > $last ) {
75
+			$bits = explode('/', $page['title']);
76
+			$cur = intval(end($bits));
77
+			if (is_numeric($cur) && $cur > $last) {
78 78
 				$last = $cur;
79 79
 			}
80 80
 		}
@@ -85,7 +85,7 @@  discard block
 block discarded – undo
85 85
 	 * @param string $title
86 86
 	 * @return bool
87 87
 	 */
88
-	private function pageWasCreatedToday( string $title ) : bool {
88
+	private function pageWasCreatedToday(string $title) : bool {
89 89
 		$params = [
90 90
 			'action' => 'query',
91 91
 			'prop' => 'revisions',
@@ -96,10 +96,10 @@  discard block
 block discarded – undo
96 96
 			'rvdir' => 'newer'
97 97
 		];
98 98
 
99
-		$res = ( RequestBase::newFromParams( $params ) )->execute();
99
+		$res = (RequestBase::newFromParams($params))->execute();
100 100
 		$data = $res['query']['pages'];
101
-		$time = strtotime( reset( $data )['revisions'][0]['timestamp'] );
102
-		return date( 'z/Y' ) === date( 'z/Y', $time );
101
+		$time = strtotime(reset($data)['revisions'][0]['timestamp']);
102
+		return date('z/Y') === date('z/Y', $time);
103 103
 	}
104 104
 
105 105
 	/**
@@ -107,56 +107,56 @@  discard block
 block discarded – undo
107 107
 	 * @param string $user
108 108
 	 * @param array $groups
109 109
 	 */
110
-	protected function doCreatePage( string $title, string $user, array $groups ) {
111
-		$this->getLogger()->info( "Creating page $title" );
112
-		$text = $this->getConfig()->get( 'ric-page-text' );
110
+	protected function doCreatePage(string $title, string $user, array $groups) {
111
+		$this->getLogger()->info("Creating page $title");
112
+		$text = $this->getConfig()->get('ric-page-text');
113 113
 		$textParams = [
114 114
 			'$user' => $user,
115 115
 			'$date' => $groups['sysop'],
116
-			'$quorum' => '',##################################################################Handle on-wiki
116
+			'$quorum' => '', ##################################################################Handle on-wiki
117 117
 			'$burocrate' => $groups['bureaucrat'] ?? '',
118 118
 			'$checkuser' => $groups['checkuser'] ?? ''
119 119
 		];
120
-		$text = strtr( $text, $textParams );
120
+		$text = strtr($text, $textParams);
121 121
 
122 122
 		$params = [
123 123
 			'title' => $title,
124 124
 			'text' => $text,
125
-			'summary' => $this->getConfig()->get( 'ric-page-summary' )
125
+			'summary' => $this->getConfig()->get('ric-page-summary')
126 126
 		];
127 127
 
128
-		$this->getController()->editPage( $params );
128
+		$this->getController()->editPage($params);
129 129
 	}
130 130
 
131 131
 	/**
132 132
 	 * @param string $title
133 133
 	 * @param string $newText
134 134
 	 */
135
-	protected function createBasePage( string $title, string $newText ) {
136
-		$this->getLogger()->info( "Creating base page $title" );
135
+	protected function createBasePage(string $title, string $newText) {
136
+		$this->getLogger()->info("Creating base page $title");
137 137
 
138 138
 		$params = [
139 139
 			'title' => $title,
140 140
 			'text' => $newText,
141
-			'summary' => $this->getConfig()->get( 'ric-base-page-summary' )
141
+			'summary' => $this->getConfig()->get('ric-base-page-summary')
142 142
 		];
143 143
 
144
-		$this->getController()->editPage( $params );
144
+		$this->getController()->editPage($params);
145 145
 	}
146 146
 
147 147
 	/**
148 148
 	 * @param string $title
149 149
 	 * @param string $newText
150 150
 	 */
151
-	protected function updateBasePage( string $title, string $newText ) {
152
-		$this->getLogger()->info( "Updating base page $title" );
151
+	protected function updateBasePage(string $title, string $newText) {
152
+		$this->getLogger()->info("Updating base page $title");
153 153
 
154 154
 		$params = [
155 155
 			'title' => $title,
156 156
 			'appendtext' => $newText,
157
-			'summary' => $this->getConfig()->get( 'ric-base-page-summary-update' )
157
+			'summary' => $this->getConfig()->get('ric-base-page-summary-update')
158 158
 		];
159 159
 
160
-		$this->getController()->editPage( $params );
160
+		$this->getController()->editPage($params);
161 161
 	}
162 162
 }
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
@@ -22,27 +22,27 @@  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( 'url', $defaults['url'] );
32
-		$inst->set( 'list-title', $defaults['list-title'] );
33
-		$inst->set( 'username', $defaults['username'] );
34
-		$inst->set( 'password', $defaults['password'] );
31
+		$inst->set('url', $defaults['url']);
32
+		$inst->set('list-title', $defaults['list-title']);
33
+		$inst->set('username', $defaults['username']);
34
+		$inst->set('password', $defaults['password']);
35 35
 		self::$instance = $inst;
36 36
 
37 37
 		// On-wiki values
38 38
 		try {
39
-			$conf = ( new WikiController )->getPageContent( $defaults[ 'config-title' ] );
40
-		} catch ( MissingPageException $e ) {
41
-			throw new ConfigException( 'Please create a config page.' );
39
+			$conf = (new WikiController)->getPageContent($defaults['config-title']);
40
+		} catch (MissingPageException $e) {
41
+			throw new ConfigException('Please create a config page.');
42 42
 		}
43 43
 
44
-		foreach ( json_decode( $conf, true ) as $key => $val ) {
45
-			self::$instance->set( $key, $val );
44
+		foreach (json_decode($conf, true) as $key => $val) {
45
+			self::$instance->set($key, $val);
46 46
 		}
47 47
 	}
48 48
 
@@ -50,8 +50,8 @@  discard block
 block discarded – undo
50 50
 	 * @param string $key
51 51
 	 * @param mixed $value
52 52
 	 */
53
-	public function set( string $key, $value ) {
54
-		$this->opts[ $key ] = $value;
53
+	public function set(string $key, $value) {
54
+		$this->opts[$key] = $value;
55 55
 	}
56 56
 
57 57
 	/**
@@ -60,8 +60,8 @@  discard block
 block discarded – undo
60 60
 	 * @return self
61 61
 	 */
62 62
 	public static function getInstance() : self {
63
-		if ( !self::$instance ) {
64
-			throw new ConfigException( 'Config not yet initialized' );
63
+		if (!self::$instance) {
64
+			throw new ConfigException('Config not yet initialized');
65 65
 		}
66 66
 		return self::$instance;
67 67
 	}
@@ -70,10 +70,10 @@  discard block
 block discarded – undo
70 70
 	 * @param string $opt
71 71
 	 * @return mixed
72 72
 	 */
73
-	public function get( string $opt ) {
74
-		if ( !isset( $this->opts[ $opt ] ) ) {
75
-			throw new ConfigException( "Config option '$opt' not set." );
73
+	public function get(string $opt) {
74
+		if (!isset($this->opts[$opt])) {
75
+			throw new ConfigException("Config option '$opt' not set.");
76 76
 		}
77
-		return $this->opts[ $opt ];
77
+		return $this->opts[$opt];
78 78
 	}
79 79
 }
Please login to merge, or discard this patch.
includes/TaskResult.php 1 patch
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -17,7 +17,7 @@  discard block
 block discarded – undo
17 17
 	 * @param int $status One of the Task::STATUS_* constants
18 18
 	 * @param string[] $errors
19 19
 	 */
20
-	public function __construct( int $status, array $errors = [] ) {
20
+	public function __construct(int $status, array $errors = []) {
21 21
 		$this->status = $status;
22 22
 		$this->errors = $errors;
23 23
 	}
@@ -39,25 +39,25 @@  discard block
 block discarded – undo
39 39
 	/**
40 40
 	 * @param TaskResult $that
41 41
 	 */
42
-	public function merge( TaskResult $that ) {
42
+	public function merge(TaskResult $that) {
43 43
 		$this->status |= $that->status;
44
-		$this->errors = array_merge( $this->errors, $that->errors );
44
+		$this->errors = array_merge($this->errors, $that->errors);
45 45
 	}
46 46
 
47 47
 	/**
48 48
 	 * @return string
49 49
 	 */
50 50
 	public function __toString() {
51
-		if ( $this->isOK() ) {
51
+		if ($this->isOK()) {
52 52
 			$stat = 'OK';
53 53
 			$errs = "\tNo errors.";
54 54
 		} else {
55 55
 			$stat = 'ERROR';
56 56
 			$formattedErrs = [];
57
-			foreach ( $this->errors as $err ) {
57
+			foreach ($this->errors as $err) {
58 58
 				$formattedErrs[] = "\t - $err";
59 59
 			}
60
-			$errs = implode( "\n", $formattedErrs );
60
+			$errs = implode("\n", $formattedErrs);
61 61
 		}
62 62
 		return "=== RESULT ===\n - Status: $stat\n - Errors:\n$errs\n";
63 63
 	}
Please login to merge, or discard this patch.
includes/WikiController.php 1 patch
Spacing   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -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']['*'];
@@ -50,16 +50,16 @@  discard block
 block discarded – undo
50 50
 	 *
51 51
 	 * @param array $params
52 52
 	 */
53
-	public function editPage( array $params ) {
53
+	public function editPage(array $params) {
54 54
 		$this->login();
55 55
 
56 56
 		$params = [
57 57
 			'action' => 'edit',
58
-			'token' => $this->getToken( 'csrf' ),
59
-			'bot' => Config::getInstance()->get( 'bot-edits' )
58
+			'token' => $this->getToken('csrf'),
59
+			'bot' => Config::getInstance()->get('bot-edits')
60 60
 		] + $params;
61 61
 
62
-		$req = RequestBase::newFromParams( $params, true );
62
+		$req = RequestBase::newFromParams($params, true);
63 63
 		$req->execute();
64 64
 	}
65 65
 
@@ -67,55 +67,55 @@  discard block
 block discarded – undo
67 67
 	 * @throws LoginException
68 68
 	 */
69 69
 	public function login() {
70
-		if ( self::$loggedIn ) {
71
-			$this->logger->debug( 'Already logged in' );
70
+		if (self::$loggedIn) {
71
+			$this->logger->debug('Already logged in');
72 72
 			return;
73 73
 		}
74 74
 
75
-		$this->logger->debug( 'Logging in' );
75
+		$this->logger->debug('Logging in');
76 76
 
77 77
 		$params = [
78 78
 			'action' => 'login',
79
-			'lgname' => Config::getInstance()->get( 'username' ),
80
-			'lgpassword' => Config::getInstance()->get( 'password' ),
81
-			'lgtoken' => $this->getToken( 'login' )
79
+			'lgname' => Config::getInstance()->get('username'),
80
+			'lgpassword' => Config::getInstance()->get('password'),
81
+			'lgtoken' => $this->getToken('login')
82 82
 		];
83 83
 
84 84
 		try {
85
-			$req = RequestBase::newFromParams( $params, true );
85
+			$req = RequestBase::newFromParams($params, true);
86 86
 			$res = $req->execute();
87
-		} catch ( APIRequestException $e ) {
88
-			throw new LoginException( $e->getMessage() );
87
+		} catch (APIRequestException $e) {
88
+			throw new LoginException($e->getMessage());
89 89
 		}
90 90
 
91
-		if ( !isset( $res['login']['result'] ) || $res['login']['result'] !== 'Success' ) {
92
-			throw new LoginException( 'Unknown error' );
91
+		if (!isset($res['login']['result']) || $res['login']['result'] !== 'Success') {
92
+			throw new LoginException('Unknown error');
93 93
 		}
94 94
 
95 95
 		self::$loggedIn = true;
96 96
 		// Clear tokens cache
97 97
 		$this->tokens = [];
98
-		$this->logger->debug( 'Login succeeded' );
98
+		$this->logger->debug('Login succeeded');
99 99
 	}
100 100
 
101 101
 	/**
102 102
 	 * @param string $type
103 103
 	 * @return string
104 104
 	 */
105
-	public function getToken( string $type ) : string {
106
-		if ( !isset( $this->tokens[ $type ] ) ) {
105
+	public function getToken(string $type) : string {
106
+		if (!isset($this->tokens[$type])) {
107 107
 			$params = [
108 108
 				'action' => 'query',
109 109
 				'meta'   => 'tokens',
110 110
 				'type'   => $type
111 111
 			];
112 112
 
113
-			$req = RequestBase::newFromParams( $params );
113
+			$req = RequestBase::newFromParams($params);
114 114
 			$res = $req->execute();
115 115
 
116
-			$this->tokens[ $type ] = $res['query']['tokens']["{$type}token"];
116
+			$this->tokens[$type] = $res['query']['tokens']["{$type}token"];
117 117
 		}
118 118
 
119
-		return $this->tokens[ $type ];
119
+		return $this->tokens[$type];
120 120
 	}
121 121
 }
Please login to merge, or discard this patch.
includes/Bot.php 1 patch
Spacing   +11 added lines, -11 removed lines patch added patch discarded remove patch
@@ -14,13 +14,13 @@  discard block
 block discarded – undo
14 14
 	 * Entry point for the whole process
15 15
 	 */
16 16
 	public function run() {
17
-		$this->logger->info( 'Starting full process.' );
17
+		$this->logger->info('Starting full process.');
18 18
 		$manager = new TaskManager;
19
-		$res = $manager->run( TaskManager::MODE_COMPLETE );
20
-		if ( $res->isOK() ) {
21
-			$this->logger->info( 'Execution completed successfully.' );
19
+		$res = $manager->run(TaskManager::MODE_COMPLETE);
20
+		if ($res->isOK()) {
21
+			$this->logger->info('Execution completed successfully.');
22 22
 		} else {
23
-			$this->logger->error( "Execution failed.\n$res" );
23
+			$this->logger->error("Execution failed.\n$res");
24 24
 		}
25 25
 	}
26 26
 
@@ -29,14 +29,14 @@  discard block
 block discarded – undo
29 29
 	 *
30 30
 	 * @param string $task
31 31
 	 */
32
-	public function runSingle( string $task ) {
33
-		$this->logger->info( "Starting single task $task." );
32
+	public function runSingle(string $task) {
33
+		$this->logger->info("Starting single task $task.");
34 34
 		$manager = new TaskManager;
35
-		$res = $manager->run( TaskManager::MODE_SINGLE, $task );
36
-		if ( $res->isOK() ) {
37
-			$this->logger->info( "Execution of $task completed successfully." );
35
+		$res = $manager->run(TaskManager::MODE_SINGLE, $task);
36
+		if ($res->isOK()) {
37
+			$this->logger->info("Execution of $task completed successfully.");
38 38
 		} else {
39
-			$this->logger->error( "Execution of $task failed.\n$res" );
39
+			$this->logger->error("Execution of $task failed.\n$res");
40 40
 		}
41 41
 	}
42 42
 }
Please login to merge, or discard this patch.
includes/TaskManager.php 1 patch
Spacing   +20 added lines, -20 removed lines patch added patch discarded remove patch
@@ -29,7 +29,7 @@  discard block
 block discarded – undo
29 29
 	 * Should only be used for debugging purpose.
30 30
 	 */
31 31
 	public static function resetLastRunDate() {
32
-		file_put_contents( self::LOG_FILE, '' );
32
+		file_put_contents(self::LOG_FILE, '');
33 33
 	}
34 34
 
35 35
 	/**
@@ -37,12 +37,12 @@  discard block
 block discarded – undo
37 37
 	 * @param string|null $taskName Only used in MODE_SINGLE
38 38
 	 * @return TaskResult
39 39
 	 */
40
-	public function run( int $mode, string $taskName = null ) : TaskResult {
40
+	public function run(int $mode, string $taskName = null) : TaskResult {
41 41
 		$this->provider = new TaskDataProvider;
42
-		if ( $mode === self::MODE_COMPLETE ) {
42
+		if ($mode === self::MODE_COMPLETE) {
43 43
 			return $this->runAllTasks();
44 44
 		} else {
45
-			return $this->runTask( $taskName );
45
+			return $this->runTask($taskName);
46 46
 		}
47 47
 	}
48 48
 
@@ -50,9 +50,9 @@  discard block
 block discarded – undo
50 50
 	 * @return TaskResult
51 51
 	 */
52 52
 	protected function runAllTasks() : TaskResult {
53
-		if ( self::getLastFullRunDate() === date( 'd/m/Y' ) ) {
53
+		if (self::getLastFullRunDate() === date('d/m/Y')) {
54 54
 			// Really avoid executing twice the same day
55
-			return new TaskResult( TaskResult::STATUS_ERROR, [ 'A full run was already executed today.' ] );
55
+			return new TaskResult(TaskResult::STATUS_ERROR, ['A full run was already executed today.']);
56 56
 		}
57 57
 
58 58
 		// Order matters here
@@ -63,12 +63,12 @@  discard block
 block discarded – undo
63 63
 			'user-notice'
64 64
 		];
65 65
 
66
-		$res = new TaskResult( TaskResult::STATUS_OK );
66
+		$res = new TaskResult(TaskResult::STATUS_OK);
67 67
 		do {
68
-			$res->merge( $this->runTask( current( $list ) ) );
69
-		} while ( $res->isOK() && next( $list ) );
68
+			$res->merge($this->runTask(current($list)));
69
+		} while ($res->isOK() && next($list));
70 70
 
71
-		if ( $res->isOK() ) {
71
+		if ($res->isOK()) {
72 72
 			self::setLastFullRunDate();
73 73
 		}
74 74
 
@@ -79,8 +79,8 @@  discard block
 block discarded – undo
79 79
 	 * @return string|null d/m/Y or null if no last run registered
80 80
 	 */
81 81
 	public static function getLastFullRunDate() : ?string {
82
-		if ( file_exists( self::LOG_FILE ) ) {
83
-			return file_get_contents( self::LOG_FILE ) ?: null;
82
+		if (file_exists(self::LOG_FILE)) {
83
+			return file_get_contents(self::LOG_FILE) ?: null;
84 84
 		} else {
85 85
 			return null;
86 86
 		}
@@ -90,13 +90,13 @@  discard block
 block discarded – undo
90 90
 	 * @param string $task Defined in self::TASKS_MAP
91 91
 	 * @return TaskResult
92 92
 	 */
93
-	protected function runTask( string $task ) : TaskResult {
94
-		if ( !isset( self::TASKS_MAP[ $task ] ) ) {
95
-			throw new \InvalidArgumentException( "'$task' is not a valid task." );
93
+	protected function runTask(string $task) : TaskResult {
94
+		if (!isset(self::TASKS_MAP[$task])) {
95
+			throw new \InvalidArgumentException("'$task' is not a valid task.");
96 96
 		}
97 97
 
98
-		$class = self::TASKS_MAP[ $task ];
99
-		return $this->getTaskInstance( $class )->run();
98
+		$class = self::TASKS_MAP[$task];
99
+		return $this->getTaskInstance($class)->run();
100 100
 	}
101 101
 
102 102
 	/**
@@ -105,11 +105,11 @@  discard block
 block discarded – undo
105 105
 	 * @param string $class
106 106
 	 * @return Task
107 107
 	 */
108
-	private function getTaskInstance( string $class ) : Task {
109
-		return new $class( $this->provider );
108
+	private function getTaskInstance(string $class) : Task {
109
+		return new $class($this->provider);
110 110
 	}
111 111
 
112 112
 	public static function setLastFullRunDate() {
113
-		file_put_contents( self::LOG_FILE, date( 'd/m/Y' ) );
113
+		file_put_contents(self::LOG_FILE, date('d/m/Y'));
114 114
 	}
115 115
 }
Please login to merge, or discard this patch.
run.php 1 patch
Spacing   +19 added lines, -19 removed lines patch added patch discarded remove patch
@@ -5,8 +5,8 @@  discard block
 block discarded – undo
5 5
 use BotRiconferme\Config;
6 6
 use BotRiconferme\Bot;
7 7
 
8
-if ( PHP_SAPI !== 'cli' ) {
9
-	exit( 'CLI only!' );
8
+if (PHP_SAPI !== 'cli') {
9
+	exit('CLI only!');
10 10
 }
11 11
 
12 12
 /** MAIN PARAMS */
@@ -27,9 +27,9 @@  discard block
 block discarded – undo
27 27
 	'config-title:'
28 28
 ];
29 29
 
30
-$vals = getopt( '', $params );
31
-if ( count( $vals ) !== count( $params ) ) {
32
-	exit( 'Not enough params!' );
30
+$vals = getopt('', $params);
31
+if (count($vals) !== count($params)) {
32
+	exit('Not enough params!');
33 33
 }
34 34
 
35 35
 /* PASSWORD */
@@ -42,38 +42,38 @@  discard block
 block discarded – undo
42 42
  * --use-password-file
43 43
  * which will look for a $PWFILE file in the current directory containing only the plain password
44 44
  */
45
-$pwParams = getopt( '', [
45
+$pwParams = getopt('', [
46 46
 	'password:',
47 47
 	'use-password-file'
48
-] );
48
+]);
49 49
 
50
-if ( isset( $pwParams[ 'password' ] ) ) {
51
-	$pw = $pwParams[ 'password' ];
52
-} elseif ( isset( $pwParams[ 'use-password-file' ] ) ) {
53
-	if ( file_exists( $PWFILE ) ) {
54
-		$pw = trim( file_get_contents( $PWFILE ) );
50
+if (isset($pwParams['password'])) {
51
+	$pw = $pwParams['password'];
52
+} elseif (isset($pwParams['use-password-file'])) {
53
+	if (file_exists($PWFILE)) {
54
+		$pw = trim(file_get_contents($PWFILE));
55 55
 	} else {
56
-		exit( 'Please create a password.txt file to use with use-password-file' );
56
+		exit('Please create a password.txt file to use with use-password-file');
57 57
 	}
58 58
 } else {
59
-	exit( 'Please provide a password or use a password file' );
59
+	exit('Please provide a password or use a password file');
60 60
 }
61 61
 
62
-$vals[ 'password' ] = $pw;
62
+$vals['password'] = $pw;
63 63
 
64 64
 /* START */
65 65
 
66
-Config::init( $vals );
66
+Config::init($vals);
67 67
 
68 68
 $bot = new Bot();
69 69
 
70 70
 /*
71 71
  * E.g. --task=update-list
72 72
  */
73
-$taskOpts = getopt( '', [ 'task:' ] );
73
+$taskOpts = getopt('', ['task:']);
74 74
 
75
-if ( $taskOpts ) {
76
-	$bot->runSingle( $taskOpts[ 'task' ] );
75
+if ($taskOpts) {
76
+	$bot->runSingle($taskOpts['task']);
77 77
 } else {
78 78
 	$bot->run();
79 79
 }
Please login to merge, or discard this patch.