@@ -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,35 +11,35 @@ 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 | - if ( $curl === false ) { |
|
| 17 | - throw new APIRequestException( 'Cannot open cURL handler.' ); |
|
| 16 | + if ($curl === false) { |
|
| 17 | + throw new APIRequestException('Cannot open cURL handler.'); |
|
| 18 | 18 | } |
| 19 | - curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true ); |
|
| 20 | - curl_setopt( $curl, CURLOPT_HEADER, true ); |
|
| 21 | - curl_setopt( $curl, CURLOPT_HEADERFUNCTION, [ $this, 'headersHandler' ] ); |
|
| 22 | - curl_setopt( $curl, CURLOPT_HTTPHEADER, $this->getHeaders() ); |
|
| 19 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); |
|
| 20 | + curl_setopt($curl, CURLOPT_HEADER, true); |
|
| 21 | + curl_setopt($curl, CURLOPT_HEADERFUNCTION, [$this, 'headersHandler']); |
|
| 22 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $this->getHeaders()); |
|
| 23 | 23 | |
| 24 | 24 | $url = self::$url; |
| 25 | - if ( $this->method === 'POST' ) { |
|
| 26 | - curl_setopt( $curl, CURLOPT_URL, $url ); |
|
| 27 | - curl_setopt( $curl, CURLOPT_POST, true ); |
|
| 28 | - curl_setopt( $curl, CURLOPT_POSTFIELDS, $params ); |
|
| 25 | + if ($this->method === 'POST') { |
|
| 26 | + curl_setopt($curl, CURLOPT_URL, $url); |
|
| 27 | + curl_setopt($curl, CURLOPT_POST, true); |
|
| 28 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $params); |
|
| 29 | 29 | } else { |
| 30 | - curl_setopt( $curl, CURLOPT_URL, "$url?$params" ); |
|
| 30 | + curl_setopt($curl, CURLOPT_URL, "$url?$params"); |
|
| 31 | 31 | } |
| 32 | 32 | |
| 33 | - $result = curl_exec( $curl ); |
|
| 33 | + $result = curl_exec($curl); |
|
| 34 | 34 | |
| 35 | - if ( $result === false ) { |
|
| 36 | - throw new APIRequestException( curl_error( $curl ) ); |
|
| 35 | + if ($result === false) { |
|
| 36 | + throw new APIRequestException(curl_error($curl)); |
|
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | // Extract response body |
| 40 | - $headerSize = curl_getinfo( $curl, CURLINFO_HEADER_SIZE ); |
|
| 41 | - $body = substr( $result, $headerSize ); |
|
| 42 | - curl_close( $curl ); |
|
| 40 | + $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE); |
|
| 41 | + $body = substr($result, $headerSize); |
|
| 42 | + curl_close($curl); |
|
| 43 | 43 | |
| 44 | 44 | return $body; |
| 45 | 45 | } |
@@ -52,12 +52,12 @@ discard block |
||
| 52 | 52 | * @return int |
| 53 | 53 | * @internal Only used as CB for cURL |
| 54 | 54 | */ |
| 55 | - public function headersHandler( $ch, string $header ) : int { |
|
| 56 | - $bits = explode( ':', $header, 2 ); |
|
| 57 | - if ( trim( $bits[0] ) === 'Set-Cookie' ) { |
|
| 55 | + public function headersHandler($ch, string $header) : int { |
|
| 56 | + $bits = explode(':', $header, 2); |
|
| 57 | + if (trim($bits[0]) === 'Set-Cookie') { |
|
| 58 | 58 | $this->newCookies[] = $bits[1]; |
| 59 | 59 | } |
| 60 | 60 | // @phan-suppress-next-line PhanTypeMismatchReturn WTF? Why does phan thinks this is a string? |
| 61 | - return strlen( $header ); |
|
| 61 | + return strlen($header); |
|
| 62 | 62 | } |
| 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 | |
@@ -30,7 +30,7 @@ discard block |
||
| 30 | 30 | * Should only be used for debugging purpose. |
| 31 | 31 | */ |
| 32 | 32 | public static function resetLastRunDate() { |
| 33 | - file_put_contents( self::LOG_FILE, '' ); |
|
| 33 | + file_put_contents(self::LOG_FILE, ''); |
|
| 34 | 34 | } |
| 35 | 35 | |
| 36 | 36 | /** |
@@ -38,14 +38,14 @@ discard block |
||
| 38 | 38 | * @param string|null $taskName Only used in MODE_SINGLE |
| 39 | 39 | * @return TaskResult |
| 40 | 40 | */ |
| 41 | - public function run( int $mode, string $taskName = null ) : TaskResult { |
|
| 41 | + public function run(int $mode, string $taskName = null) : TaskResult { |
|
| 42 | 42 | $this->provider = new TaskDataProvider; |
| 43 | - if ( $mode === self::MODE_COMPLETE ) { |
|
| 43 | + if ($mode === self::MODE_COMPLETE) { |
|
| 44 | 44 | return $this->runAllTasks(); |
| 45 | - } elseif ( $taskName === null ) { |
|
| 46 | - throw new BadMethodCallException( 'A task name must be specified in MODE_SINGLE' ); |
|
| 45 | + } elseif ($taskName === null) { |
|
| 46 | + throw new BadMethodCallException('A task name must be specified in MODE_SINGLE'); |
|
| 47 | 47 | } else { |
| 48 | - return $this->runTask( $taskName ); |
|
| 48 | + return $this->runTask($taskName); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
@@ -53,9 +53,9 @@ discard block |
||
| 53 | 53 | * @return TaskResult |
| 54 | 54 | */ |
| 55 | 55 | protected function runAllTasks() : TaskResult { |
| 56 | - if ( self::getLastFullRunDate() === date( 'd/m/Y' ) ) { |
|
| 56 | + if (self::getLastFullRunDate() === date('d/m/Y')) { |
|
| 57 | 57 | // Really avoid executing twice the same day |
| 58 | - return new TaskResult( TaskResult::STATUS_ERROR, [ 'A full run was already executed today.' ] ); |
|
| 58 | + return new TaskResult(TaskResult::STATUS_ERROR, ['A full run was already executed today.']); |
|
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | // Order matters here |
@@ -66,12 +66,12 @@ discard block |
||
| 66 | 66 | 'user-notice' |
| 67 | 67 | ]; |
| 68 | 68 | |
| 69 | - $res = new TaskResult( TaskResult::STATUS_OK ); |
|
| 69 | + $res = new TaskResult(TaskResult::STATUS_OK); |
|
| 70 | 70 | do { |
| 71 | - $res->merge( $this->runTask( current( $list ) ) ); |
|
| 72 | - } while ( $res->isOK() && next( $list ) ); |
|
| 71 | + $res->merge($this->runTask(current($list))); |
|
| 72 | + } while ($res->isOK() && next($list)); |
|
| 73 | 73 | |
| 74 | - if ( $res->isOK() ) { |
|
| 74 | + if ($res->isOK()) { |
|
| 75 | 75 | self::setLastFullRunDate(); |
| 76 | 76 | } |
| 77 | 77 | |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | * @return string|null d/m/Y or null if no last run registered |
| 83 | 83 | */ |
| 84 | 84 | public static function getLastFullRunDate() : ?string { |
| 85 | - if ( file_exists( self::LOG_FILE ) ) { |
|
| 86 | - return file_get_contents( self::LOG_FILE ) ?: null; |
|
| 85 | + if (file_exists(self::LOG_FILE)) { |
|
| 86 | + return file_get_contents(self::LOG_FILE) ?: null; |
|
| 87 | 87 | } else { |
| 88 | 88 | return null; |
| 89 | 89 | } |
@@ -93,13 +93,13 @@ discard block |
||
| 93 | 93 | * @param string $task Defined in self::TASKS_MAP |
| 94 | 94 | * @return TaskResult |
| 95 | 95 | */ |
| 96 | - protected function runTask( string $task ) : TaskResult { |
|
| 97 | - if ( !isset( self::TASKS_MAP[ $task ] ) ) { |
|
| 98 | - throw new \InvalidArgumentException( "'$task' is not a valid task." ); |
|
| 96 | + protected function runTask(string $task) : TaskResult { |
|
| 97 | + if (!isset(self::TASKS_MAP[$task])) { |
|
| 98 | + throw new \InvalidArgumentException("'$task' is not a valid task."); |
|
| 99 | 99 | } |
| 100 | 100 | |
| 101 | - $class = self::TASKS_MAP[ $task ]; |
|
| 102 | - return $this->getTaskInstance( $class )->run(); |
|
| 101 | + $class = self::TASKS_MAP[$task]; |
|
| 102 | + return $this->getTaskInstance($class)->run(); |
|
| 103 | 103 | } |
| 104 | 104 | |
| 105 | 105 | /** |
@@ -108,11 +108,11 @@ discard block |
||
| 108 | 108 | * @param string $class |
| 109 | 109 | * @return Task |
| 110 | 110 | */ |
| 111 | - private function getTaskInstance( string $class ) : Task { |
|
| 112 | - return new $class( $this->provider ); |
|
| 111 | + private function getTaskInstance(string $class) : Task { |
|
| 112 | + return new $class($this->provider); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | public static function setLastFullRunDate() { |
| 116 | - file_put_contents( self::LOG_FILE, date( 'd/m/Y' ) ); |
|
| 116 | + file_put_contents(self::LOG_FILE, date('d/m/Y')); |
|
| 117 | 117 | } |
| 118 | 118 | } |
@@ -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 | |
@@ -10,83 +10,83 @@ discard block |
||
| 10 | 10 | * @inheritDoc |
| 11 | 11 | */ |
| 12 | 12 | public function run() : TaskResult { |
| 13 | - $this->getLogger()->info( 'Starting task UpdatesAround' ); |
|
| 13 | + $this->getLogger()->info('Starting task UpdatesAround'); |
|
| 14 | 14 | |
| 15 | - foreach ( $this->getDataProvider()->getCreatedPages() as $page ) { |
|
| 15 | + foreach ($this->getDataProvider()->getCreatedPages() as $page) { |
|
| 16 | 16 | // Wikipedia:Amministratori/Riconferma annuale |
| 17 | - $this->addToMainPage( $page ); |
|
| 17 | + $this->addToMainPage($page); |
|
| 18 | 18 | // WP:Wikipediano/Votazioni |
| 19 | - $this->addVote( $page ); |
|
| 19 | + $this->addVote($page); |
|
| 20 | 20 | // Template:VotazioniRCnews |
| 21 | - $this->addNews( $page ); |
|
| 21 | + $this->addNews($page); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - $this->getLogger()->info( 'Task UpdatesAround completed successfully' ); |
|
| 25 | - return new TaskResult( self::STATUS_OK ); |
|
| 24 | + $this->getLogger()->info('Task UpdatesAround completed successfully'); |
|
| 25 | + return new TaskResult(self::STATUS_OK); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
| 29 | 29 | * @param string $page |
| 30 | 30 | */ |
| 31 | - protected function addToMainPage( string $page ) { |
|
| 32 | - $this->getLogger()->info( "Adding $page to main" ); |
|
| 31 | + protected function addToMainPage(string $page) { |
|
| 32 | + $this->getLogger()->info("Adding $page to main"); |
|
| 33 | 33 | |
| 34 | 34 | $params = [ |
| 35 | - 'title' => $this->getConfig()->get( 'ric-main-page' ), |
|
| 35 | + 'title' => $this->getConfig()->get('ric-main-page'), |
|
| 36 | 36 | 'appendtext' => '{{' . $page . '}}', |
| 37 | - 'summary' => $this->getConfig()->get( 'ric-main-page-summary' ) |
|
| 37 | + 'summary' => $this->getConfig()->get('ric-main-page-summary') |
|
| 38 | 38 | ]; |
| 39 | 39 | |
| 40 | - $this->getController()->editPage( $params ); |
|
| 40 | + $this->getController()->editPage($params); |
|
| 41 | 41 | } |
| 42 | 42 | |
| 43 | 43 | /** |
| 44 | 44 | * @param string $page |
| 45 | 45 | */ |
| 46 | - protected function addVote( string $page ) { |
|
| 47 | - $this->getLogger()->info( "Adding $page to votes" ); |
|
| 48 | - $votePage = $this->getConfig()->get( 'ric-vote-page' ); |
|
| 46 | + protected function addVote(string $page) { |
|
| 47 | + $this->getLogger()->info("Adding $page to votes"); |
|
| 48 | + $votePage = $this->getConfig()->get('ric-vote-page'); |
|
| 49 | 49 | |
| 50 | - $content = $this->getController()->getPageContent( $votePage ); |
|
| 50 | + $content = $this->getController()->getPageContent($votePage); |
|
| 51 | 51 | // Remove comments etc. |
| 52 | - $visibleContent = strip_tags( $content ); |
|
| 53 | - $user = explode( '/', $page )[2]; |
|
| 52 | + $visibleContent = strip_tags($content); |
|
| 53 | + $user = explode('/', $page)[2]; |
|
| 54 | 54 | $time = $this->getTimeWithArticle(); |
| 55 | 55 | |
| 56 | 56 | $newLine = "*[[Utente:$user|]]. La [[$page|procedura]] termina $time"; |
| 57 | 57 | |
| 58 | 58 | $introReg = '!^;È in corso la .*riconferma tacita.* degli .*amministratori.+!m'; |
| 59 | - if ( preg_match( $introReg, $visibleContent ) ) { |
|
| 60 | - $newContent = preg_replace( $introReg, '$0' . "\n$newLine;", $content, 1 ); |
|
| 59 | + if (preg_match($introReg, $visibleContent)) { |
|
| 60 | + $newContent = preg_replace($introReg, '$0' . "\n$newLine;", $content, 1); |
|
| 61 | 61 | } else { |
| 62 | 62 | $matches = []; |
| 63 | - if ( preg_match( $introReg, $content, $matches ) === false ) { |
|
| 64 | - throw new TaskException( 'Intro not found in vote page' ); |
|
| 63 | + if (preg_match($introReg, $content, $matches) === false) { |
|
| 64 | + throw new TaskException('Intro not found in vote page'); |
|
| 65 | 65 | } |
| 66 | 66 | $beforeReg = '!INSERIRE LA NOTIZIA PIÙ NUOVA IN CIMA.+!m'; |
| 67 | - $newContent = preg_replace( $beforeReg, "$0\n{$matches[0]}\n$newLine.", $content, 1 ); |
|
| 67 | + $newContent = preg_replace($beforeReg, "$0\n{$matches[0]}\n$newLine.", $content, 1); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | $params = [ |
| 71 | 71 | 'title' => $votePage, |
| 72 | 72 | 'text' => $newContent, |
| 73 | - 'summary' => $this->getConfig()->get( 'ric-vote-page-summary' ) |
|
| 73 | + 'summary' => $this->getConfig()->get('ric-vote-page-summary') |
|
| 74 | 74 | ]; |
| 75 | 75 | |
| 76 | - $this->getController()->editPage( $params ); |
|
| 76 | + $this->getController()->editPage($params); |
|
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | /** |
| 80 | 80 | * @return string |
| 81 | 81 | */ |
| 82 | 82 | private function getTimeWithArticle() : string { |
| 83 | - $oldLoc = setlocale( LC_TIME, 'it_IT', 'Italian_Italy', 'Italian' ); |
|
| 84 | - $endTS = time() + ( 60 * 60 * 24 * 7 ); |
|
| 85 | - $endTime = strftime( '%e %B alle %R', $endTS ); |
|
| 83 | + $oldLoc = setlocale(LC_TIME, 'it_IT', 'Italian_Italy', 'Italian'); |
|
| 84 | + $endTS = time() + (60 * 60 * 24 * 7); |
|
| 85 | + $endTime = strftime('%e %B alle %R', $endTS); |
|
| 86 | 86 | // Remove the left space if day has a single digit |
| 87 | - $endTime = ltrim( $endTime ); |
|
| 88 | - $artic = in_array( date( 'j', $endTS ), [ 8, 11 ] ) ? "l'" : "il "; |
|
| 89 | - setlocale( LC_TIME, $oldLoc ); |
|
| 87 | + $endTime = ltrim($endTime); |
|
| 88 | + $artic = in_array(date('j', $endTS), [8, 11]) ? "l'" : "il "; |
|
| 89 | + setlocale(LC_TIME, $oldLoc); |
|
| 90 | 90 | |
| 91 | 91 | return $artic . $endTime; |
| 92 | 92 | } |
@@ -94,27 +94,27 @@ discard block |
||
| 94 | 94 | /** |
| 95 | 95 | * @param string $page |
| 96 | 96 | */ |
| 97 | - protected function addNews( string $page ) { |
|
| 98 | - $this->getLogger()->info( "Adding $page to news" ); |
|
| 99 | - $newsPage = $this->getConfig()->get( 'ric-news-page' ); |
|
| 97 | + protected function addNews(string $page) { |
|
| 98 | + $this->getLogger()->info("Adding $page to news"); |
|
| 99 | + $newsPage = $this->getConfig()->get('ric-news-page'); |
|
| 100 | 100 | |
| 101 | - $content = $this->getController()->getPageContent( $newsPage ); |
|
| 101 | + $content = $this->getController()->getPageContent($newsPage); |
|
| 102 | 102 | $reg = '!(\| *riconferme[ _]tacite[ _]amministratori *= *)(\d+)!'; |
| 103 | 103 | |
| 104 | 104 | $matches = []; |
| 105 | - if ( preg_match( $reg, $content, $matches ) === false ) { |
|
| 106 | - throw new TaskException( 'Param not found in news page' ); |
|
| 105 | + if (preg_match($reg, $content, $matches) === false) { |
|
| 106 | + throw new TaskException('Param not found in news page'); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | $newNum = (int)$matches[2] + 1; |
| 110 | - $newContent = preg_replace( $reg, '${1}' . $newNum, $content ); |
|
| 110 | + $newContent = preg_replace($reg, '${1}' . $newNum, $content); |
|
| 111 | 111 | |
| 112 | 112 | $params = [ |
| 113 | 113 | 'title' => $newsPage, |
| 114 | 114 | 'text' => $newContent, |
| 115 | - 'summary' => $this->getConfig()->get( 'ric-news-page-summary' ) |
|
| 115 | + 'summary' => $this->getConfig()->get('ric-news-page-summary') |
|
| 116 | 116 | ]; |
| 117 | 117 | |
| 118 | - $this->getController()->editPage( $params ); |
|
| 118 | + $this->getController()->editPage($params); |
|
| 119 | 119 | } |
| 120 | 120 | } |