@@ -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 | |
@@ -14,15 +14,15 @@ discard block |
||
| 14 | 14 | * @inheritDoc |
| 15 | 15 | */ |
| 16 | 16 | public function run() : TaskResult { |
| 17 | - $this->getLogger()->info( 'Starting task CreatePage' ); |
|
| 17 | + $this->getLogger()->info('Starting task CreatePage'); |
|
| 18 | 18 | $users = $this->getDataProvider()->getUsersToProcess(); |
| 19 | 19 | |
| 20 | - foreach ( $users as $user => $groups ) { |
|
| 21 | - $this->processUser( $user, $groups ); |
|
| 20 | + foreach ($users as $user => $groups) { |
|
| 21 | + $this->processUser($user, $groups); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - $this->getLogger()->info( 'Task CreatePage completed successfully' ); |
|
| 25 | - return new TaskResult( self::STATUS_OK ); |
|
| 24 | + $this->getLogger()->info('Task CreatePage completed successfully'); |
|
| 25 | + return new TaskResult(self::STATUS_OK); |
|
| 26 | 26 | } |
| 27 | 27 | |
| 28 | 28 | /** |
@@ -31,27 +31,27 @@ discard block |
||
| 31 | 31 | * @param string $user |
| 32 | 32 | * @param array $groups |
| 33 | 33 | */ |
| 34 | - protected function processUser( string $user, array $groups ) { |
|
| 34 | + protected function processUser(string $user, array $groups) { |
|
| 35 | 35 | try { |
| 36 | - $num = $this->getLastPageNum( $user ) + 1; |
|
| 37 | - } catch ( TaskException $e ) { |
|
| 36 | + $num = $this->getLastPageNum($user) + 1; |
|
| 37 | + } catch (TaskException $e) { |
|
| 38 | 38 | // The page was already created today. PLZ let this poor bot work! |
| 39 | - $this->getDataProvider()->removeUser( $user ); |
|
| 40 | - $this->getLogger()->warning( $e->getMessage() . "\nRemoving $user." ); |
|
| 39 | + $this->getDataProvider()->removeUser($user); |
|
| 40 | + $this->getLogger()->warning($e->getMessage() . "\nRemoving $user."); |
|
| 41 | 41 | return; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - $baseTitle = $this->getConfig()->get( 'ric-main-page' ) . "/$user"; |
|
| 44 | + $baseTitle = $this->getConfig()->get('ric-main-page') . "/$user"; |
|
| 45 | 45 | $pageTitle = "$baseTitle/$num"; |
| 46 | - $this->doCreatePage( $pageTitle, $user, $groups ); |
|
| 46 | + $this->doCreatePage($pageTitle, $user, $groups); |
|
| 47 | 47 | |
| 48 | - $newText = str_replace( '$title', $pageTitle, $this->getConfig()->get( 'ric-base-page-text' ) ); |
|
| 49 | - if ( $num === 1 ) { |
|
| 50 | - $this->createBasePage( $baseTitle, $newText ); |
|
| 48 | + $newText = str_replace('$title', $pageTitle, $this->getConfig()->get('ric-base-page-text')); |
|
| 49 | + if ($num === 1) { |
|
| 50 | + $this->createBasePage($baseTitle, $newText); |
|
| 51 | 51 | } else { |
| 52 | - $this->updateBasePage( $baseTitle, $newText ); |
|
| 52 | + $this->updateBasePage($baseTitle, $newText); |
|
| 53 | 53 | } |
| 54 | - $this->getDataProvider()->addCreatedPages( $pageTitle ); |
|
| 54 | + $this->getDataProvider()->addCreatedPages($pageTitle); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | * @return int |
| 62 | 62 | * @throws TaskException |
| 63 | 63 | */ |
| 64 | - protected function getLastPageNum( string $user ) : int { |
|
| 65 | - $this->getLogger()->debug( "Retrieving previous pages for $user" ); |
|
| 66 | - $unprefixedTitle = explode( ':', $this->getConfig()->get( 'ric-main-page' ), 2 )[1]; |
|
| 64 | + protected function getLastPageNum(string $user) : int { |
|
| 65 | + $this->getLogger()->debug("Retrieving previous pages for $user"); |
|
| 66 | + $unprefixedTitle = explode(':', $this->getConfig()->get('ric-main-page'), 2)[1]; |
|
| 67 | 67 | $params = [ |
| 68 | 68 | 'action' => 'query', |
| 69 | 69 | 'list' => 'allpages', |
@@ -72,16 +72,16 @@ discard block |
||
| 72 | 72 | 'aplimit' => 'max' |
| 73 | 73 | ]; |
| 74 | 74 | |
| 75 | - $res = ( RequestBase::newFromParams( $params ) )->execute(); |
|
| 75 | + $res = (RequestBase::newFromParams($params))->execute(); |
|
| 76 | 76 | |
| 77 | 77 | $last = 0; |
| 78 | - foreach ( $res['query']['allpages'] as $page ) { |
|
| 79 | - if ( $this->pageWasCreatedToday( $page['title'] ) ) { |
|
| 80 | - throw new TaskException( 'Page ' . $page['title'] . ' was already created.' ); |
|
| 78 | + foreach ($res['query']['allpages'] as $page) { |
|
| 79 | + if ($this->pageWasCreatedToday($page['title'])) { |
|
| 80 | + throw new TaskException('Page ' . $page['title'] . ' was already created.'); |
|
| 81 | 81 | } |
| 82 | - $bits = explode( '/', $page['title'] ); |
|
| 83 | - $cur = intval( end( $bits ) ); |
|
| 84 | - if ( is_numeric( $cur ) && $cur > $last ) { |
|
| 82 | + $bits = explode('/', $page['title']); |
|
| 83 | + $cur = intval(end($bits)); |
|
| 84 | + if (is_numeric($cur) && $cur > $last) { |
|
| 85 | 85 | $last = $cur; |
| 86 | 86 | } |
| 87 | 87 | } |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | * @param string $title |
| 93 | 93 | * @return bool |
| 94 | 94 | */ |
| 95 | - private function pageWasCreatedToday( string $title ) : bool { |
|
| 95 | + private function pageWasCreatedToday(string $title) : bool { |
|
| 96 | 96 | $params = [ |
| 97 | 97 | 'action' => 'query', |
| 98 | 98 | 'prop' => 'revisions', |
@@ -103,10 +103,10 @@ discard block |
||
| 103 | 103 | 'rvdir' => 'newer' |
| 104 | 104 | ]; |
| 105 | 105 | |
| 106 | - $res = ( RequestBase::newFromParams( $params ) )->execute(); |
|
| 106 | + $res = (RequestBase::newFromParams($params))->execute(); |
|
| 107 | 107 | $data = $res['query']['pages']; |
| 108 | - $time = strtotime( reset( $data )['revisions'][0]['timestamp'] ); |
|
| 109 | - return date( 'z/Y' ) === date( 'z/Y', $time ); |
|
| 108 | + $time = strtotime(reset($data)['revisions'][0]['timestamp']); |
|
| 109 | + return date('z/Y') === date('z/Y', $time); |
|
| 110 | 110 | } |
| 111 | 111 | |
| 112 | 112 | /** |
@@ -116,24 +116,24 @@ discard block |
||
| 116 | 116 | * @param string $user |
| 117 | 117 | * @param array $groups |
| 118 | 118 | */ |
| 119 | - protected function doCreatePage( string $title, string $user, array $groups ) { |
|
| 120 | - $this->getLogger()->info( "Creating page $title" ); |
|
| 121 | - $text = $this->getConfig()->get( 'ric-page-text' ); |
|
| 119 | + protected function doCreatePage(string $title, string $user, array $groups) { |
|
| 120 | + $this->getLogger()->info("Creating page $title"); |
|
| 121 | + $text = $this->getConfig()->get('ric-page-text'); |
|
| 122 | 122 | $textParams = [ |
| 123 | 123 | '$user' => $user, |
| 124 | 124 | '$date' => $groups['sysop'], |
| 125 | 125 | '$burocrate' => $groups['bureaucrat'] ?? '', |
| 126 | 126 | '$checkuser' => $groups['checkuser'] ?? '' |
| 127 | 127 | ]; |
| 128 | - $text = strtr( $text, $textParams ); |
|
| 128 | + $text = strtr($text, $textParams); |
|
| 129 | 129 | |
| 130 | 130 | $params = [ |
| 131 | 131 | 'title' => $title, |
| 132 | 132 | 'text' => $text, |
| 133 | - 'summary' => $this->getConfig()->get( 'ric-page-summary' ) |
|
| 133 | + 'summary' => $this->getConfig()->get('ric-page-summary') |
|
| 134 | 134 | ]; |
| 135 | 135 | |
| 136 | - $this->getController()->editPage( $params ); |
|
| 136 | + $this->getController()->editPage($params); |
|
| 137 | 137 | } |
| 138 | 138 | |
| 139 | 139 | /** |
@@ -142,16 +142,16 @@ discard block |
||
| 142 | 142 | * @param string $title |
| 143 | 143 | * @param string $newText |
| 144 | 144 | */ |
| 145 | - protected function createBasePage( string $title, string $newText ) { |
|
| 146 | - $this->getLogger()->info( "Creating base page $title" ); |
|
| 145 | + protected function createBasePage(string $title, string $newText) { |
|
| 146 | + $this->getLogger()->info("Creating base page $title"); |
|
| 147 | 147 | |
| 148 | 148 | $params = [ |
| 149 | 149 | 'title' => $title, |
| 150 | 150 | 'text' => $newText, |
| 151 | - 'summary' => $this->getConfig()->get( 'ric-base-page-summary' ) |
|
| 151 | + 'summary' => $this->getConfig()->get('ric-base-page-summary') |
|
| 152 | 152 | ]; |
| 153 | 153 | |
| 154 | - $this->getController()->editPage( $params ); |
|
| 154 | + $this->getController()->editPage($params); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | /** |
@@ -159,15 +159,15 @@ discard block |
||
| 159 | 159 | * @param string $title |
| 160 | 160 | * @param string $newText |
| 161 | 161 | */ |
| 162 | - protected function updateBasePage( string $title, string $newText ) { |
|
| 163 | - $this->getLogger()->info( "Updating base page $title" ); |
|
| 162 | + protected function updateBasePage(string $title, string $newText) { |
|
| 163 | + $this->getLogger()->info("Updating base page $title"); |
|
| 164 | 164 | |
| 165 | 165 | $params = [ |
| 166 | 166 | 'title' => $title, |
| 167 | 167 | 'appendtext' => $newText, |
| 168 | - 'summary' => $this->getConfig()->get( 'ric-base-page-summary-update' ) |
|
| 168 | + 'summary' => $this->getConfig()->get('ric-base-page-summary-update') |
|
| 169 | 169 | ]; |
| 170 | 170 | |
| 171 | - $this->getController()->editPage( $params ); |
|
| 171 | + $this->getController()->editPage($params); |
|
| 172 | 172 | } |
| 173 | 173 | } |
@@ -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 | |
@@ -26,26 +26,26 @@ discard block |
||
| 26 | 26 | * @param array $defaults |
| 27 | 27 | * @throws ConfigException |
| 28 | 28 | */ |
| 29 | - public static function init( array $defaults ) { |
|
| 30 | - if ( self::$instance ) { |
|
| 31 | - throw new ConfigException( 'Config was already initialized' ); |
|
| 29 | + public static function init(array $defaults) { |
|
| 30 | + if (self::$instance) { |
|
| 31 | + throw new ConfigException('Config was already initialized'); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | 34 | $inst = new self; |
| 35 | - $inst->set( 'list-title', $defaults['list-title'] ); |
|
| 36 | - $inst->set( 'username', $defaults['username'] ); |
|
| 37 | - $inst->set( 'password', $defaults['password'] ); |
|
| 35 | + $inst->set('list-title', $defaults['list-title']); |
|
| 36 | + $inst->set('username', $defaults['username']); |
|
| 37 | + $inst->set('password', $defaults['password']); |
|
| 38 | 38 | self::$instance = $inst; |
| 39 | 39 | |
| 40 | 40 | // On-wiki values |
| 41 | 41 | try { |
| 42 | - $conf = ( new WikiController )->getPageContent( $defaults[ 'config-title' ] ); |
|
| 43 | - } catch ( MissingPageException $e ) { |
|
| 44 | - throw new ConfigException( 'Please create a config page.' ); |
|
| 42 | + $conf = (new WikiController)->getPageContent($defaults['config-title']); |
|
| 43 | + } catch (MissingPageException $e) { |
|
| 44 | + throw new ConfigException('Please create a config page.'); |
|
| 45 | 45 | } |
| 46 | 46 | |
| 47 | - foreach ( json_decode( $conf, true ) as $key => $val ) { |
|
| 48 | - self::$instance->set( $key, $val ); |
|
| 47 | + foreach (json_decode($conf, true) as $key => $val) { |
|
| 48 | + self::$instance->set($key, $val); |
|
| 49 | 49 | } |
| 50 | 50 | } |
| 51 | 51 | |
@@ -55,8 +55,8 @@ discard block |
||
| 55 | 55 | * @param string $key |
| 56 | 56 | * @param mixed $value |
| 57 | 57 | */ |
| 58 | - protected function set( string $key, $value ) { |
|
| 59 | - $this->opts[ $key ] = $value; |
|
| 58 | + protected function set(string $key, $value) { |
|
| 59 | + $this->opts[$key] = $value; |
|
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
@@ -66,8 +66,8 @@ discard block |
||
| 66 | 66 | * @throws ConfigException |
| 67 | 67 | */ |
| 68 | 68 | public static function getInstance() : self { |
| 69 | - if ( !self::$instance ) { |
|
| 70 | - throw new ConfigException( 'Config not yet initialized' ); |
|
| 69 | + if (!self::$instance) { |
|
| 70 | + throw new ConfigException('Config not yet initialized'); |
|
| 71 | 71 | } |
| 72 | 72 | return self::$instance; |
| 73 | 73 | } |
@@ -79,10 +79,10 @@ discard block |
||
| 79 | 79 | * @return mixed |
| 80 | 80 | * @throws ConfigException |
| 81 | 81 | */ |
| 82 | - public function get( string $opt ) { |
|
| 83 | - if ( !isset( $this->opts[ $opt ] ) ) { |
|
| 84 | - throw new ConfigException( "Config option '$opt' not set." ); |
|
| 82 | + public function get(string $opt) { |
|
| 83 | + if (!isset($this->opts[$opt])) { |
|
| 84 | + throw new ConfigException("Config option '$opt' not set."); |
|
| 85 | 85 | } |
| 86 | - return $this->opts[ $opt ]; |
|
| 86 | + return $this->opts[$opt]; |
|
| 87 | 87 | } |
| 88 | 88 | } |
@@ -1,4 +1,4 @@ discard block |
||
| 1 | -<?php declare( strict_types=1 ); |
|
| 1 | +<?php declare(strict_types=1); |
|
| 2 | 2 | /** |
| 3 | 3 | * Entry point for the bot, called by CLI |
| 4 | 4 | */ |
@@ -9,8 +9,8 @@ discard block |
||
| 9 | 9 | use BotRiconferme\Bot; |
| 10 | 10 | use BotRiconferme\Request\RequestBase; |
| 11 | 11 | |
| 12 | -if ( PHP_SAPI !== 'cli' ) { |
|
| 13 | - exit( 'CLI only!' ); |
|
| 12 | +if (PHP_SAPI !== 'cli') { |
|
| 13 | + exit('CLI only!'); |
|
| 14 | 14 | } |
| 15 | 15 | |
| 16 | 16 | /** MAIN PARAMS */ |
@@ -29,14 +29,14 @@ discard block |
||
| 29 | 29 | 'config-title:' |
| 30 | 30 | ]; |
| 31 | 31 | |
| 32 | -$vals = getopt( '', $params ); |
|
| 33 | -if ( count( $vals ) !== count( $params ) ) { |
|
| 34 | - exit( 'Not enough params!' ); |
|
| 32 | +$vals = getopt('', $params); |
|
| 33 | +if (count($vals) !== count($params)) { |
|
| 34 | + exit('Not enough params!'); |
|
| 35 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /* URL (for debugging purpose) */ |
| 38 | -$url = getopt( '', [ 'force-url:' ] ); |
|
| 39 | -if ( isset( $url['force-url'] ) ) { |
|
| 38 | +$url = getopt('', ['force-url:']); |
|
| 39 | +if (isset($url['force-url'])) { |
|
| 40 | 40 | RequestBase::$url = $url['force-url']; |
| 41 | 41 | } |
| 42 | 42 | |
@@ -50,38 +50,38 @@ discard block |
||
| 50 | 50 | * --use-password-file |
| 51 | 51 | * which will look for a $PWFILE file in the current directory containing only the plain password |
| 52 | 52 | */ |
| 53 | -$pwParams = getopt( '', [ |
|
| 53 | +$pwParams = getopt('', [ |
|
| 54 | 54 | 'password:', |
| 55 | 55 | 'use-password-file' |
| 56 | -] ); |
|
| 56 | +]); |
|
| 57 | 57 | |
| 58 | -if ( isset( $pwParams[ 'password' ] ) ) { |
|
| 59 | - $pw = $pwParams[ 'password' ]; |
|
| 60 | -} elseif ( isset( $pwParams[ 'use-password-file' ] ) ) { |
|
| 61 | - if ( file_exists( $PWFILE ) ) { |
|
| 62 | - $pw = trim( file_get_contents( $PWFILE ) ); |
|
| 58 | +if (isset($pwParams['password'])) { |
|
| 59 | + $pw = $pwParams['password']; |
|
| 60 | +} elseif (isset($pwParams['use-password-file'])) { |
|
| 61 | + if (file_exists($PWFILE)) { |
|
| 62 | + $pw = trim(file_get_contents($PWFILE)); |
|
| 63 | 63 | } else { |
| 64 | - exit( 'Please create a password.txt file to use with use-password-file' ); |
|
| 64 | + exit('Please create a password.txt file to use with use-password-file'); |
|
| 65 | 65 | } |
| 66 | 66 | } else { |
| 67 | - exit( 'Please provide a password or use a password file' ); |
|
| 67 | + exit('Please provide a password or use a password file'); |
|
| 68 | 68 | } |
| 69 | 69 | |
| 70 | -$vals[ 'password' ] = $pw; |
|
| 70 | +$vals['password'] = $pw; |
|
| 71 | 71 | |
| 72 | 72 | /* START */ |
| 73 | 73 | |
| 74 | -Config::init( $vals ); |
|
| 74 | +Config::init($vals); |
|
| 75 | 75 | |
| 76 | 76 | $bot = new Bot(); |
| 77 | 77 | |
| 78 | 78 | /* |
| 79 | 79 | * E.g. --task=update-list |
| 80 | 80 | */ |
| 81 | -$taskOpts = getopt( '', [ 'task:' ] ); |
|
| 81 | +$taskOpts = getopt('', ['task:']); |
|
| 82 | 82 | |
| 83 | -if ( $taskOpts ) { |
|
| 84 | - $bot->runSingle( $taskOpts[ 'task' ] ); |
|
| 83 | +if ($taskOpts) { |
|
| 84 | + $bot->runSingle($taskOpts['task']); |
|
| 85 | 85 | } else { |
| 86 | 86 | $bot->run(); |
| 87 | 87 | } |