Passed
Push — master ( aeb3c7...3e94dc )
by Daimona
01:51
created
includes/Task/CreatePage.php 1 patch
Spacing   +49 added lines, -49 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php declare( strict_types=1 );
1
+<?php declare(strict_types=1);
2 2
 
3 3
 namespace BotRiconferme\Task;
4 4
 
@@ -13,16 +13,16 @@  discard block
 block discarded – undo
13 13
 	/**
14 14
 	 * @inheritDoc
15 15
 	 */
16
-	public function run() : TaskResult {
17
-		$this->getLogger()->info( 'Starting task CreatePage' );
18
-		$users = $this->getDataProvider()->getUsersToProcess();
16
+	public function run () : TaskResult {
17
+		$this->getLogger ()->info ( 'Starting task CreatePage' );
18
+		$users = $this->getDataProvider ()->getUsersToProcess ();
19 19
 
20 20
 		foreach ( $users as $user => $groups ) {
21
-			$this->processUser( $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
 block discarded – undo
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;
36
+			$num = $this->getLastPageNum ( $user ) + 1;
37 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' ) );
48
+		$newText = str_replace ( '$title', $pageTitle, $this->getConfig ()->get ( 'ric-base-page-text' ) );
49 49
 		if ( $num === 1 ) {
50
-			$this->createBasePage( $baseTitle, $newText );
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
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
 block discarded – undo
103 103
 			'rvdir' => 'newer'
104 104
 		];
105 105
 
106
-		$res = ( RequestBase::newFromParams( $params ) )->execute();
107
-		$data = $res['query']['pages'];
108
-		$time = strtotime( reset( $data )['revisions'][0]['timestamp'] );
109
-		return date( 'z/Y' ) === date( 'z/Y', $time );
106
+		$res = ( RequestBase::newFromParams ( $params ) )->execute ();
107
+		$data = $res[ 'query' ][ 'pages' ];
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
 block discarded – undo
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
-			'$date' => $groups['sysop'],
125
-			'$burocrate' => $groups['bureaucrat'] ?? '',
126
-			'$checkuser' => $groups['checkuser'] ?? ''
124
+			'$date' => $groups[ 'sysop' ],
125
+			'$burocrate' => $groups[ 'bureaucrat' ] ?? '',
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
 block discarded – undo
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
 block discarded – undo
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
 }
Please login to merge, or discard this patch.
run.php 1 patch
Spacing   +14 added lines, -14 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php declare( strict_types=1 );
1
+<?php declare(strict_types=1);
2 2
 /**
3 3
  * Entry point for the bot, called by CLI
4 4
  */
@@ -29,15 +29,15 @@  discard block
 block discarded – undo
29 29
 	'config-title:'
30 30
 ];
31 31
 
32
-$vals = getopt( '', $params );
33
-if ( count( $vals ) !== count( $params ) ) {
32
+$vals = getopt ( '', $params );
33
+if ( count ( $vals ) !== count ( $params ) ) {
34 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'] ) ) {
40
-	RequestBase::$url = $url['force-url'];
38
+$url = getopt ( '', [ 'force-url:' ] );
39
+if ( isset( $url[ 'force-url' ] ) ) {
40
+	RequestBase::$url = $url[ 'force-url' ];
41 41
 }
42 42
 
43 43
 /* PASSWORD */
@@ -50,7 +50,7 @@  discard block
 block discarded – undo
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
 ] );
@@ -58,8 +58,8 @@  discard block
 block discarded – undo
58 58
 if ( isset( $pwParams[ 'password' ] ) ) {
59 59
 	$pw = $pwParams[ 'password' ];
60 60
 } elseif ( isset( $pwParams[ 'use-password-file' ] ) ) {
61
-	if ( file_exists( $PWFILE ) ) {
62
-		$pw = trim( file_get_contents( $PWFILE ) );
61
+	if ( file_exists ( $PWFILE ) ) {
62
+		$pw = trim ( file_get_contents ( $PWFILE ) );
63 63
 	} else {
64 64
 		exit( 'Please create a password.txt file to use with use-password-file' );
65 65
 	}
@@ -71,17 +71,17 @@  discard block
 block discarded – undo
71 71
 
72 72
 /* START */
73 73
 
74
-Config::init( $vals );
74
+Config::init ( $vals );
75 75
 
76
-$bot = new Bot();
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 83
 if ( $taskOpts ) {
84
-	$bot->runSingle( $taskOpts[ 'task' ] );
84
+	$bot->runSingle ( $taskOpts[ 'task' ] );
85 85
 } else {
86
-	$bot->run();
86
+	$bot->run ();
87 87
 }
Please login to merge, or discard this patch.
includes/TaskManager.php 1 patch
Spacing   +25 added lines, -25 removed lines patch added patch discarded remove patch
@@ -1,4 +1,4 @@  discard block
 block discarded – undo
1
-<?php declare( strict_types=1 );
1
+<?php declare(strict_types=1);
2 2
 
3 3
 namespace BotRiconferme;
4 4
 
@@ -31,8 +31,8 @@  discard block
 block discarded – undo
31 31
 	/**
32 32
 	 * Should only be used for debugging purpose.
33 33
 	 */
34
-	public static function resetLastRunDate() {
35
-		file_put_contents( self::LOG_FILE, '' );
34
+	public static function resetLastRunDate () {
35
+		file_put_contents ( self::LOG_FILE, '' );
36 36
 	}
37 37
 
38 38
 	/**
@@ -42,24 +42,24 @@  discard block
 block discarded – undo
42 42
 	 * @param string|null $taskName Only used in MODE_SINGLE
43 43
 	 * @return TaskResult
44 44
 	 */
45
-	public function run( int $mode, string $taskName = null ) : TaskResult {
45
+	public function run ( int $mode, string $taskName = null ) : TaskResult {
46 46
 		$this->provider = new TaskDataProvider;
47 47
 		if ( $mode === self::MODE_COMPLETE ) {
48
-			return $this->runAllTasks();
48
+			return $this->runAllTasks ();
49 49
 		} elseif ( $taskName === null ) {
50
-			throw new \BadMethodCallException( 'A task name must be specified in MODE_SINGLE' );
50
+			throw new \BadMethodCallException ( 'A task name must be specified in MODE_SINGLE' );
51 51
 		} else {
52
-			return $this->runTask( $taskName );
52
+			return $this->runTask ( $taskName );
53 53
 		}
54 54
 	}
55 55
 
56 56
 	/**
57 57
 	 * @return TaskResult
58 58
 	 */
59
-	protected function runAllTasks() : TaskResult {
60
-		if ( self::getLastFullRunDate() === date( 'd/m/Y' ) ) {
59
+	protected function runAllTasks () : TaskResult {
60
+		if ( self::getLastFullRunDate () === date ( 'd/m/Y' ) ) {
61 61
 			// Really avoid executing twice the same day
62
-			return new TaskResult( TaskResult::STATUS_ERROR, [ 'A full run was already executed today.' ] );
62
+			return new TaskResult ( TaskResult::STATUS_ERROR, [ 'A full run was already executed today.' ] );
63 63
 		}
64 64
 
65 65
 		// Order matters here
@@ -70,13 +70,13 @@  discard block
 block discarded – undo
70 70
 			'user-notice'
71 71
 		];
72 72
 
73
-		$res = new TaskResult( TaskResult::STATUS_OK );
73
+		$res = new TaskResult ( TaskResult::STATUS_OK );
74 74
 		do {
75
-			$res->merge( $this->runTask( current( $list ) ) );
76
-		} while ( $res->isOK() && next( $list ) );
75
+			$res->merge ( $this->runTask ( current ( $list ) ) );
76
+		} while ( $res->isOK () && next ( $list ) );
77 77
 
78
-		if ( $res->isOK() ) {
79
-			self::setLastFullRunDate();
78
+		if ( $res->isOK () ) {
79
+			self::setLastFullRunDate ();
80 80
 		}
81 81
 
82 82
 		return $res;
@@ -86,9 +86,9 @@  discard block
 block discarded – undo
86 86
 	 * Get the last execution date to ensure no more than one full run is executed every day
87 87
 	 * @return string|null d/m/Y or null if no last run registered
88 88
 	 */
89
-	public static function getLastFullRunDate() : ?string {
90
-		if ( file_exists( self::LOG_FILE ) ) {
91
-			return file_get_contents( self::LOG_FILE ) ?: null;
89
+	public static function getLastFullRunDate () : ?string {
90
+		if ( file_exists ( self::LOG_FILE ) ) {
91
+			return file_get_contents ( self::LOG_FILE ) ?: null;
92 92
 		} else {
93 93
 			return null;
94 94
 		}
@@ -98,13 +98,13 @@  discard block
 block discarded – undo
98 98
 	 * @param string $task Defined in self::TASKS_MAP
99 99
 	 * @return TaskResult
100 100
 	 */
101
-	protected function runTask( string $task ) : TaskResult {
101
+	protected function runTask ( string $task ) : TaskResult {
102 102
 		if ( !isset( self::TASKS_MAP[ $task ] ) ) {
103
-			throw new \InvalidArgumentException( "'$task' is not a valid task." );
103
+			throw new \InvalidArgumentException ( "'$task' is not a valid task." );
104 104
 		}
105 105
 
106 106
 		$class = self::TASKS_MAP[ $task ];
107
-		return $this->getTaskInstance( $class )->run();
107
+		return $this->getTaskInstance ( $class )->run ();
108 108
 	}
109 109
 
110 110
 	/**
@@ -113,11 +113,11 @@  discard block
 block discarded – undo
113 113
 	 * @param string $class
114 114
 	 * @return Task
115 115
 	 */
116
-	private function getTaskInstance( string $class ) : Task {
117
-		return new $class( $this->provider );
116
+	private function getTaskInstance ( string $class ) : Task {
117
+		return new $class ( $this->provider );
118 118
 	}
119 119
 
120
-	public static function setLastFullRunDate() {
121
-		file_put_contents( self::LOG_FILE, date( 'd/m/Y' ) );
120
+	public static function setLastFullRunDate () {
121
+		file_put_contents ( self::LOG_FILE, date ( 'd/m/Y' ) );
122 122
 	}
123 123
 }
Please login to merge, or discard this patch.