@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme; |
| 4 | 4 | |
@@ -17,7 +17,7 @@ discard block |
||
| 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 |
||
| 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 | } |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme; |
| 4 | 4 | |
@@ -29,7 +29,7 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | } |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme\Request; |
| 4 | 4 | |
@@ -35,8 +35,8 @@ discard block |
||
| 35 | 35 | * @param array $params |
| 36 | 36 | * @param bool $isPOST |
| 37 | 37 | */ |
| 38 | - protected function __construct( array $params, bool $isPOST = false ) { |
|
| 39 | - $this->params = [ 'format' => 'json' ] + $params; |
|
| 38 | + protected function __construct(array $params, bool $isPOST = false) { |
|
| 39 | + $this->params = ['format' => 'json'] + $params; |
|
| 40 | 40 | $this->method = $isPOST ? 'POST' : 'GET'; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -47,11 +47,11 @@ discard block |
||
| 47 | 47 | * @param bool $isPOST |
| 48 | 48 | * @return self |
| 49 | 49 | */ |
| 50 | - public static function newFromParams( array $params, bool $isPOST = false ) : self { |
|
| 51 | - if ( extension_loaded( 'curl' ) ) { |
|
| 52 | - $ret = new CurlRequest( $params, $isPOST ); |
|
| 50 | + public static function newFromParams(array $params, bool $isPOST = false) : self { |
|
| 51 | + if (extension_loaded('curl')) { |
|
| 52 | + $ret = new CurlRequest($params, $isPOST); |
|
| 53 | 53 | } else { |
| 54 | - $ret = new NativeRequest( $params, $isPOST ); |
|
| 54 | + $ret = new NativeRequest($params, $isPOST); |
|
| 55 | 55 | } |
| 56 | 56 | return $ret; |
| 57 | 57 | } |
@@ -65,19 +65,19 @@ discard block |
||
| 65 | 65 | $curParams = $this->params; |
| 66 | 66 | $sets = []; |
| 67 | 67 | do { |
| 68 | - $res = $this->makeRequestInternal( $curParams ); |
|
| 68 | + $res = $this->makeRequestInternal($curParams); |
|
| 69 | 69 | |
| 70 | - $this->handleErrorAndWarnings( $res ); |
|
| 70 | + $this->handleErrorAndWarnings($res); |
|
| 71 | 71 | $sets[] = $res; |
| 72 | 72 | |
| 73 | 73 | $finished = true; |
| 74 | - if ( isset( $res->continue ) ) { |
|
| 75 | - $curParams = array_merge( $curParams, get_object_vars( $res->continue ) ); |
|
| 74 | + if (isset($res->continue)) { |
|
| 75 | + $curParams = array_merge($curParams, get_object_vars($res->continue)); |
|
| 76 | 76 | $finished = false; |
| 77 | 77 | } |
| 78 | - } while ( !$finished ); |
|
| 78 | + } while (!$finished); |
|
| 79 | 79 | |
| 80 | - return $this->mergeSets( $sets ); |
|
| 80 | + return $this->mergeSets($sets); |
|
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | /** |
@@ -86,16 +86,16 @@ discard block |
||
| 86 | 86 | * @param array $params |
| 87 | 87 | * @return \stdClass |
| 88 | 88 | */ |
| 89 | - private function makeRequestInternal( array $params ) : \stdClass { |
|
| 90 | - if ( $this->method === 'POST' ) { |
|
| 89 | + private function makeRequestInternal(array $params) : \stdClass { |
|
| 90 | + if ($this->method === 'POST') { |
|
| 91 | 91 | $params['maxlag'] = self::MAXLAG; |
| 92 | 92 | } |
| 93 | - $params = http_build_query( $params ); |
|
| 93 | + $params = http_build_query($params); |
|
| 94 | 94 | |
| 95 | - $body = $this->reallyMakeRequest( $params ); |
|
| 95 | + $body = $this->reallyMakeRequest($params); |
|
| 96 | 96 | |
| 97 | - $this->setCookies( $this->newCookies ); |
|
| 98 | - return json_decode( $body ); |
|
| 97 | + $this->setCookies($this->newCookies); |
|
| 98 | + return json_decode($body); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | 101 | /** |
@@ -104,16 +104,16 @@ discard block |
||
| 104 | 104 | * @param string $params |
| 105 | 105 | * @return string |
| 106 | 106 | */ |
| 107 | - abstract protected function reallyMakeRequest( string $params ) : string; |
|
| 107 | + abstract protected function reallyMakeRequest(string $params) : string; |
|
| 108 | 108 | |
| 109 | 109 | /** |
| 110 | 110 | * @param array $cookies |
| 111 | 111 | */ |
| 112 | - protected function setCookies( array $cookies ) { |
|
| 113 | - foreach ( $cookies as $cookie ) { |
|
| 114 | - $bits = explode( ';', $cookie ); |
|
| 115 | - list( $name, $value ) = explode( '=', $bits[0] ); |
|
| 116 | - self::$cookiesToSet[ $name ] = $value; |
|
| 112 | + protected function setCookies(array $cookies) { |
|
| 113 | + foreach ($cookies as $cookie) { |
|
| 114 | + $bits = explode(';', $cookie); |
|
| 115 | + list($name, $value) = explode('=', $bits[0]); |
|
| 116 | + self::$cookiesToSet[$name] = $value; |
|
| 117 | 117 | } |
| 118 | 118 | } |
| 119 | 119 | |
@@ -121,9 +121,9 @@ discard block |
||
| 121 | 121 | * @param \stdClass $res |
| 122 | 122 | * @throws APIRequestException |
| 123 | 123 | */ |
| 124 | - protected function handleErrorAndWarnings( $res ) { |
|
| 125 | - if ( isset( $res->error ) ) { |
|
| 126 | - switch ( $res->error->code ) { |
|
| 124 | + protected function handleErrorAndWarnings($res) { |
|
| 125 | + if (isset($res->error)) { |
|
| 126 | + switch ($res->error->code) { |
|
| 127 | 127 | case 'missingtitle': |
| 128 | 128 | $ex = new MissingPageException; |
| 129 | 129 | break; |
@@ -131,13 +131,13 @@ discard block |
||
| 131 | 131 | $ex = new ProtectedPageException; |
| 132 | 132 | break; |
| 133 | 133 | default: |
| 134 | - $ex = new APIRequestException( $res->error->code . ' - ' . $res->error->info ); |
|
| 134 | + $ex = new APIRequestException($res->error->code . ' - ' . $res->error->info); |
|
| 135 | 135 | } |
| 136 | 136 | throw $ex; |
| 137 | - } elseif ( isset( $res->warnings ) ) { |
|
| 138 | - $act = $this->params[ 'action' ]; |
|
| 137 | + } elseif (isset($res->warnings)) { |
|
| 138 | + $act = $this->params['action']; |
|
| 139 | 139 | $warning = $res->warnings->$act; |
| 140 | - throw new APIRequestException( reset( $warning ) ); |
|
| 140 | + throw new APIRequestException(reset($warning)); |
|
| 141 | 141 | } |
| 142 | 142 | } |
| 143 | 143 | |
@@ -147,16 +147,16 @@ discard block |
||
| 147 | 147 | * @param \stdClass[] $sets |
| 148 | 148 | * @return array |
| 149 | 149 | */ |
| 150 | - private function mergeSets( array $sets ) : array { |
|
| 151 | - $sets = $this->objectToArray( $sets ); |
|
| 150 | + private function mergeSets(array $sets) : array { |
|
| 151 | + $sets = $this->objectToArray($sets); |
|
| 152 | 152 | // Use the first set as template |
| 153 | - $ret = array_shift( $sets ); |
|
| 153 | + $ret = array_shift($sets); |
|
| 154 | 154 | $act = $this->params['action']; |
| 155 | 155 | |
| 156 | - foreach ( $sets as $set ) { |
|
| 156 | + foreach ($sets as $set) { |
|
| 157 | 157 | $ret[$act] = array_merge_recursive( |
| 158 | - $this->objectToArray( $ret[$act] ), |
|
| 159 | - $this->objectToArray( $set[$act] ) |
|
| 158 | + $this->objectToArray($ret[$act]), |
|
| 159 | + $this->objectToArray($set[$act]) |
|
| 160 | 160 | ); |
| 161 | 161 | } |
| 162 | 162 | return $ret; |
@@ -169,14 +169,14 @@ discard block |
||
| 169 | 169 | * @param \stdClass|array $objOrArray |
| 170 | 170 | * @return array |
| 171 | 171 | */ |
| 172 | - private function objectToArray( $objOrArray ) : array { |
|
| 172 | + private function objectToArray($objOrArray) : array { |
|
| 173 | 173 | $array = []; |
| 174 | - if ( is_object( $objOrArray ) ) { |
|
| 175 | - $objOrArray = get_object_vars( $objOrArray ); |
|
| 174 | + if (is_object($objOrArray)) { |
|
| 175 | + $objOrArray = get_object_vars($objOrArray); |
|
| 176 | 176 | } |
| 177 | - foreach ( $objOrArray as $key => $value ) { |
|
| 178 | - if ( is_object( $value ) || is_array( $value ) ) { |
|
| 179 | - $value = $this->objectToArray( $value ); |
|
| 177 | + foreach ($objOrArray as $key => $value) { |
|
| 178 | + if (is_object($value) || is_array($value)) { |
|
| 179 | + $value = $this->objectToArray($value); |
|
| 180 | 180 | } |
| 181 | 181 | $array[$key] = $value; |
| 182 | 182 | } |
@@ -188,12 +188,12 @@ discard block |
||
| 188 | 188 | */ |
| 189 | 189 | protected function getHeaders() :array { |
| 190 | 190 | $ret = self::HEADERS; |
| 191 | - if ( self::$cookiesToSet ) { |
|
| 191 | + if (self::$cookiesToSet) { |
|
| 192 | 192 | $cookies = []; |
| 193 | - foreach ( self::$cookiesToSet as $cname => $cval ) { |
|
| 194 | - $cookies[] = trim( "$cname=$cval" ); |
|
| 193 | + foreach (self::$cookiesToSet as $cname => $cval) { |
|
| 194 | + $cookies[] = trim("$cname=$cval"); |
|
| 195 | 195 | } |
| 196 | - $ret[] = 'Cookie: ' . implode( '; ', $cookies ); |
|
| 196 | + $ret[] = 'Cookie: ' . implode('; ', $cookies); |
|
| 197 | 197 | } |
| 198 | 198 | return $ret; |
| 199 | 199 | } |
@@ -202,9 +202,9 @@ discard block |
||
| 202 | 202 | * @param array $headers |
| 203 | 203 | * @return string |
| 204 | 204 | */ |
| 205 | - protected function buildHeadersString( array $headers ) : string { |
|
| 205 | + protected function buildHeadersString(array $headers) : string { |
|
| 206 | 206 | $ret = ''; |
| 207 | - foreach ( $headers as $header ) { |
|
| 207 | + foreach ($headers as $header) { |
|
| 208 | 208 | $ret .= "$header\r\n"; |
| 209 | 209 | } |
| 210 | 210 | return $ret; |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme\Request; |
| 4 | 4 | |
@@ -11,32 +11,32 @@ discard block |
||
| 11 | 11 | /** |
| 12 | 12 | * @inheritDoc |
| 13 | 13 | */ |
| 14 | - protected function reallyMakeRequest( string $params ) : string { |
|
| 14 | + protected function reallyMakeRequest(string $params) : string { |
|
| 15 | 15 | $curl = curl_init(); |
| 16 | - curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); |
|
| 17 | - curl_setopt( $curl, CURLOPT_HEADER, true ); |
|
| 18 | - curl_setopt( $curl, CURLOPT_HEADERFUNCTION, [ $this, 'headersHandler' ] ); |
|
| 19 | - curl_setopt( $curl, CURLOPT_HTTPHEADER, $this->getHeaders() ); |
|
| 16 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 17 | + curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 18 | + curl_setopt($curl, CURLOPT_HEADERFUNCTION, [$this, 'headersHandler']); |
|
| 19 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->getHeaders()); |
|
| 20 | 20 | |
| 21 | 21 | $url = self::$url; |
| 22 | - if ( $this->method === 'POST' ) { |
|
| 23 | - curl_setopt( $curl, CURLOPT_URL, $url ); |
|
| 24 | - curl_setopt( $curl, CURLOPT_POST, true ); |
|
| 25 | - curl_setopt( $curl, CURLOPT_POSTFIELDS, $params ); |
|
| 22 | + if ($this->method === 'POST') { |
|
| 23 | + curl_setopt($curl, CURLOPT_URL, $url); |
|
| 24 | + curl_setopt($curl, CURLOPT_POST, true); |
|
| 25 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $params); |
|
| 26 | 26 | } else { |
| 27 | - curl_setopt( $curl, CURLOPT_URL, "$url?$params" ); |
|
| 27 | + curl_setopt($curl, CURLOPT_URL, "$url?$params"); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - $result = curl_exec( $curl ); |
|
| 30 | + $result = curl_exec($curl); |
|
| 31 | 31 | |
| 32 | - if ( $result === false ) { |
|
| 33 | - throw new APIRequestException( curl_error( $curl ) ); |
|
| 32 | + if ($result === false) { |
|
| 33 | + throw new APIRequestException(curl_error($curl)); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | // Extract response body |
| 37 | - $headerSize = curl_getinfo( $curl, CURLINFO_HEADER_SIZE ); |
|
| 38 | - $body = substr( $result, $headerSize ); |
|
| 39 | - curl_close( $curl ); |
|
| 37 | + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 38 | + $body = substr($result, $headerSize); |
|
| 39 | + curl_close($curl); |
|
| 40 | 40 | |
| 41 | 41 | return $body; |
| 42 | 42 | } |
@@ -49,12 +49,12 @@ discard block |
||
| 49 | 49 | * @return int |
| 50 | 50 | * @internal Only used as CB for cURL |
| 51 | 51 | */ |
| 52 | - public function headersHandler( $ch, string $header ) : int { |
|
| 53 | - $bits = explode( ':', $header, 2 ); |
|
| 54 | - if ( trim( $bits[0] ) === 'Set-Cookie' ) { |
|
| 52 | + public function headersHandler($ch, string $header) : int { |
|
| 53 | + $bits = explode(':', $header, 2); |
|
| 54 | + if (trim($bits[0]) === 'Set-Cookie') { |
|
| 55 | 55 | $this->newCookies[] = $bits[1]; |
| 56 | 56 | } |
| 57 | 57 | // @phan-suppress-next-line PhanTypeMismatchReturn WTF? Why does phan thinks this is a string? |
| 58 | - return strlen( $header ); |
|
| 58 | + return strlen($header); |
|
| 59 | 59 | } |
| 60 | 60 | } |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme\Request; |
| 4 | 4 | |
@@ -9,25 +9,25 @@ discard block |
||
| 9 | 9 | /** |
| 10 | 10 | * @inheritDoc |
| 11 | 11 | */ |
| 12 | - protected function reallyMakeRequest( string $params ) : string { |
|
| 12 | + protected function reallyMakeRequest(string $params) : string { |
|
| 13 | 13 | $context = [ |
| 14 | 14 | 'http' => [ |
| 15 | 15 | 'method' => $this->method, |
| 16 | - 'header' => $this->buildHeadersString( $this->getHeaders() ) |
|
| 16 | + 'header' => $this->buildHeadersString($this->getHeaders()) |
|
| 17 | 17 | ] |
| 18 | 18 | ]; |
| 19 | 19 | $url = self::$url; |
| 20 | - if ( $this->method === 'POST' ) { |
|
| 20 | + if ($this->method === 'POST') { |
|
| 21 | 21 | $context['http']['content'] = $params; |
| 22 | 22 | } else { |
| 23 | 23 | $url = "$url?$params"; |
| 24 | 24 | } |
| 25 | - $context = stream_context_create( $context ); |
|
| 26 | - $body = file_get_contents( $url, false, $context ); |
|
| 25 | + $context = stream_context_create($context); |
|
| 26 | + $body = file_get_contents($url, false, $context); |
|
| 27 | 27 | |
| 28 | - foreach ( $http_response_header as $header ) { |
|
| 29 | - $bits = explode( ':', $header, 2 ); |
|
| 30 | - if ( trim( $bits[0] ) === 'Set-Cookie' ) { |
|
| 28 | + foreach ($http_response_header as $header) { |
|
| 29 | + $bits = explode(':', $header, 2); |
|
| 30 | + if (trim($bits[0]) === 'Set-Cookie') { |
|
| 31 | 31 | $this->newCookies[] = $bits[1]; |
| 32 | 32 | } |
| 33 | 33 | } |
@@ -1,4 +1,4 @@ |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme\Exception; |
| 4 | 4 | |
@@ -1,4 +1,4 @@ |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme\Exception; |
| 4 | 4 | |
@@ -1,4 +1,4 @@ |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | |
| 3 | 3 | namespace BotRiconferme\Exception; |
| 4 | 4 | |
@@ -1,4 +1,4 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | } |