Passed
Push — master ( 46604b...c69686 )
by Roeland
12:59 queued 10s
created
apps/files/lib/Command/ScanAppData.php 2 patches
Indentation   +239 added lines, -239 removed lines patch added patch discarded remove patch
@@ -44,243 +44,243 @@
 block discarded – undo
44 44
 
45 45
 class ScanAppData extends Base {
46 46
 
47
-	/** @var IRootFolder */
48
-	protected $root;
49
-	/** @var IConfig */
50
-	protected $config;
51
-	/** @var float */
52
-	protected $execTime = 0;
53
-	/** @var int */
54
-	protected $foldersCounter = 0;
55
-	/** @var int */
56
-	protected $filesCounter = 0;
57
-
58
-	public function __construct(IRootFolder $rootFolder, IConfig $config) {
59
-		parent::__construct();
60
-
61
-		$this->root = $rootFolder;
62
-		$this->config = $config;
63
-	}
64
-
65
-	protected function configure() {
66
-		parent::configure();
67
-
68
-		$this
69
-			->setName('files:scan-app-data')
70
-			->setDescription('rescan the AppData folder');
71
-
72
-		$this->addArgument('folder', InputArgument::OPTIONAL, 'The appdata subfolder to scan', '');
73
-	}
74
-
75
-	public function checkScanWarning($fullPath, OutputInterface $output) {
76
-		$normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
77
-		$path = basename($fullPath);
78
-
79
-		if ($normalizedPath !== $path) {
80
-			$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
81
-		}
82
-	}
83
-
84
-	protected function scanFiles(OutputInterface $output, string $folder): int {
85
-		try {
86
-			$appData = $this->getAppDataFolder();
87
-		} catch (NotFoundException $e) {
88
-			$output->writeln('<error>NoAppData folder found</error>');
89
-			return 1;
90
-		}
91
-
92
-		if ($folder !== '') {
93
-			try {
94
-				$appData = $appData->get($folder);
95
-			} catch (NotFoundException $e) {
96
-				$output->writeln('<error>Could not find folder: ' . $folder . '</error>');
97
-				return 1;
98
-			}
99
-		}
100
-
101
-		$connection = $this->reconnectToDatabase($output);
102
-		$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
103
-
104
-		# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
105
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
106
-			$output->writeln("\tFile   <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
107
-			++$this->filesCounter;
108
-			$this->abortIfInterrupted();
109
-		});
110
-
111
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
112
-			$output->writeln("\tFolder <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
113
-			++$this->foldersCounter;
114
-			$this->abortIfInterrupted();
115
-		});
116
-
117
-		$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
118
-			$output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
119
-		});
120
-
121
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
122
-			$this->checkScanWarning($path, $output);
123
-		});
124
-
125
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
126
-			$this->checkScanWarning($path, $output);
127
-		});
128
-
129
-		try {
130
-			$scanner->scan($appData->getPath());
131
-		} catch (ForbiddenException $e) {
132
-			$output->writeln('<error>Storage not writable</error>');
133
-			$output->writeln('<info>Make sure you\'re running the scan command only as the user the web server runs as</info>');
134
-			return 1;
135
-		} catch (InterruptedException $e) {
136
-			# exit the function if ctrl-c has been pressed
137
-			$output->writeln('<info>Interrupted by user</info>');
138
-			return 1;
139
-		} catch (NotFoundException $e) {
140
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
141
-			return 1;
142
-		} catch (\Exception $e) {
143
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
144
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
145
-			return 1;
146
-		}
147
-
148
-		return 0;
149
-	}
150
-
151
-
152
-	protected function execute(InputInterface $input, OutputInterface $output) {
153
-		# restrict the verbosity level to VERBOSITY_VERBOSE
154
-		if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
155
-			$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
156
-		}
157
-
158
-		$output->writeln('Scanning AppData for files');
159
-		$output->writeln('');
160
-
161
-		$folder = $input->getArgument('folder');
162
-
163
-		$this->initTools();
164
-
165
-		$exitCode = $this->scanFiles($output, $folder);
166
-		if ($exitCode === 0) {
167
-			$this->presentStats($output);
168
-		}
169
-		return $exitCode;
170
-	}
171
-
172
-	/**
173
-	 * Initialises some useful tools for the Command
174
-	 */
175
-	protected function initTools() {
176
-		// Start the timer
177
-		$this->execTime = -microtime(true);
178
-		// Convert PHP errors to exceptions
179
-		set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
180
-	}
181
-
182
-	/**
183
-	 * Processes PHP errors as exceptions in order to be able to keep track of problems
184
-	 *
185
-	 * @see https://secure.php.net/manual/en/function.set-error-handler.php
186
-	 *
187
-	 * @param int $severity the level of the error raised
188
-	 * @param string $message
189
-	 * @param string $file the filename that the error was raised in
190
-	 * @param int $line the line number the error was raised
191
-	 *
192
-	 * @throws \ErrorException
193
-	 */
194
-	public function exceptionErrorHandler($severity, $message, $file, $line) {
195
-		if (!(error_reporting() & $severity)) {
196
-			// This error code is not included in error_reporting
197
-			return;
198
-		}
199
-		throw new \ErrorException($message, 0, $severity, $file, $line);
200
-	}
201
-
202
-	/**
203
-	 * @param OutputInterface $output
204
-	 */
205
-	protected function presentStats(OutputInterface $output) {
206
-		// Stop the timer
207
-		$this->execTime += microtime(true);
208
-
209
-		$headers = [
210
-			'Folders', 'Files', 'Elapsed time'
211
-		];
212
-
213
-		$this->showSummary($headers, null, $output);
214
-	}
215
-
216
-	/**
217
-	 * Shows a summary of operations
218
-	 *
219
-	 * @param string[] $headers
220
-	 * @param string[] $rows
221
-	 * @param OutputInterface $output
222
-	 */
223
-	protected function showSummary($headers, $rows, OutputInterface $output) {
224
-		$niceDate = $this->formatExecTime();
225
-		if (!$rows) {
226
-			$rows = [
227
-				$this->foldersCounter,
228
-				$this->filesCounter,
229
-				$niceDate,
230
-			];
231
-		}
232
-		$table = new Table($output);
233
-		$table
234
-			->setHeaders($headers)
235
-			->setRows([$rows]);
236
-		$table->render();
237
-	}
238
-
239
-
240
-	/**
241
-	 * Formats microtime into a human readable format
242
-	 *
243
-	 * @return string
244
-	 */
245
-	protected function formatExecTime() {
246
-		$secs = round($this->execTime);
247
-		# convert seconds into HH:MM:SS form
248
-		return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
249
-	}
250
-
251
-	/**
252
-	 * @return \OCP\IDBConnection
253
-	 */
254
-	protected function reconnectToDatabase(OutputInterface $output) {
255
-		/** @var Connection | IDBConnection $connection*/
256
-		$connection = \OC::$server->getDatabaseConnection();
257
-		try {
258
-			$connection->close();
259
-		} catch (\Exception $ex) {
260
-			$output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
261
-		}
262
-		while (!$connection->isConnected()) {
263
-			try {
264
-				$connection->connect();
265
-			} catch (\Exception $ex) {
266
-				$output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
267
-				sleep(60);
268
-			}
269
-		}
270
-		return $connection;
271
-	}
272
-
273
-	/**
274
-	 * @return \OCP\Files\Folder
275
-	 * @throws NotFoundException
276
-	 */
277
-	private function getAppDataFolder() {
278
-		$instanceId = $this->config->getSystemValue('instanceid', null);
279
-
280
-		if ($instanceId === null) {
281
-			throw new NotFoundException();
282
-		}
283
-
284
-		return $this->root->get('appdata_'.$instanceId);
285
-	}
47
+    /** @var IRootFolder */
48
+    protected $root;
49
+    /** @var IConfig */
50
+    protected $config;
51
+    /** @var float */
52
+    protected $execTime = 0;
53
+    /** @var int */
54
+    protected $foldersCounter = 0;
55
+    /** @var int */
56
+    protected $filesCounter = 0;
57
+
58
+    public function __construct(IRootFolder $rootFolder, IConfig $config) {
59
+        parent::__construct();
60
+
61
+        $this->root = $rootFolder;
62
+        $this->config = $config;
63
+    }
64
+
65
+    protected function configure() {
66
+        parent::configure();
67
+
68
+        $this
69
+            ->setName('files:scan-app-data')
70
+            ->setDescription('rescan the AppData folder');
71
+
72
+        $this->addArgument('folder', InputArgument::OPTIONAL, 'The appdata subfolder to scan', '');
73
+    }
74
+
75
+    public function checkScanWarning($fullPath, OutputInterface $output) {
76
+        $normalizedPath = basename(\OC\Files\Filesystem::normalizePath($fullPath));
77
+        $path = basename($fullPath);
78
+
79
+        if ($normalizedPath !== $path) {
80
+            $output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
81
+        }
82
+    }
83
+
84
+    protected function scanFiles(OutputInterface $output, string $folder): int {
85
+        try {
86
+            $appData = $this->getAppDataFolder();
87
+        } catch (NotFoundException $e) {
88
+            $output->writeln('<error>NoAppData folder found</error>');
89
+            return 1;
90
+        }
91
+
92
+        if ($folder !== '') {
93
+            try {
94
+                $appData = $appData->get($folder);
95
+            } catch (NotFoundException $e) {
96
+                $output->writeln('<error>Could not find folder: ' . $folder . '</error>');
97
+                return 1;
98
+            }
99
+        }
100
+
101
+        $connection = $this->reconnectToDatabase($output);
102
+        $scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
103
+
104
+        # check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
105
+        $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
106
+            $output->writeln("\tFile   <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
107
+            ++$this->filesCounter;
108
+            $this->abortIfInterrupted();
109
+        });
110
+
111
+        $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
112
+            $output->writeln("\tFolder <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
113
+            ++$this->foldersCounter;
114
+            $this->abortIfInterrupted();
115
+        });
116
+
117
+        $scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
118
+            $output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
119
+        });
120
+
121
+        $scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
122
+            $this->checkScanWarning($path, $output);
123
+        });
124
+
125
+        $scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
126
+            $this->checkScanWarning($path, $output);
127
+        });
128
+
129
+        try {
130
+            $scanner->scan($appData->getPath());
131
+        } catch (ForbiddenException $e) {
132
+            $output->writeln('<error>Storage not writable</error>');
133
+            $output->writeln('<info>Make sure you\'re running the scan command only as the user the web server runs as</info>');
134
+            return 1;
135
+        } catch (InterruptedException $e) {
136
+            # exit the function if ctrl-c has been pressed
137
+            $output->writeln('<info>Interrupted by user</info>');
138
+            return 1;
139
+        } catch (NotFoundException $e) {
140
+            $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
141
+            return 1;
142
+        } catch (\Exception $e) {
143
+            $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
144
+            $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
145
+            return 1;
146
+        }
147
+
148
+        return 0;
149
+    }
150
+
151
+
152
+    protected function execute(InputInterface $input, OutputInterface $output) {
153
+        # restrict the verbosity level to VERBOSITY_VERBOSE
154
+        if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
155
+            $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
156
+        }
157
+
158
+        $output->writeln('Scanning AppData for files');
159
+        $output->writeln('');
160
+
161
+        $folder = $input->getArgument('folder');
162
+
163
+        $this->initTools();
164
+
165
+        $exitCode = $this->scanFiles($output, $folder);
166
+        if ($exitCode === 0) {
167
+            $this->presentStats($output);
168
+        }
169
+        return $exitCode;
170
+    }
171
+
172
+    /**
173
+     * Initialises some useful tools for the Command
174
+     */
175
+    protected function initTools() {
176
+        // Start the timer
177
+        $this->execTime = -microtime(true);
178
+        // Convert PHP errors to exceptions
179
+        set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
180
+    }
181
+
182
+    /**
183
+     * Processes PHP errors as exceptions in order to be able to keep track of problems
184
+     *
185
+     * @see https://secure.php.net/manual/en/function.set-error-handler.php
186
+     *
187
+     * @param int $severity the level of the error raised
188
+     * @param string $message
189
+     * @param string $file the filename that the error was raised in
190
+     * @param int $line the line number the error was raised
191
+     *
192
+     * @throws \ErrorException
193
+     */
194
+    public function exceptionErrorHandler($severity, $message, $file, $line) {
195
+        if (!(error_reporting() & $severity)) {
196
+            // This error code is not included in error_reporting
197
+            return;
198
+        }
199
+        throw new \ErrorException($message, 0, $severity, $file, $line);
200
+    }
201
+
202
+    /**
203
+     * @param OutputInterface $output
204
+     */
205
+    protected function presentStats(OutputInterface $output) {
206
+        // Stop the timer
207
+        $this->execTime += microtime(true);
208
+
209
+        $headers = [
210
+            'Folders', 'Files', 'Elapsed time'
211
+        ];
212
+
213
+        $this->showSummary($headers, null, $output);
214
+    }
215
+
216
+    /**
217
+     * Shows a summary of operations
218
+     *
219
+     * @param string[] $headers
220
+     * @param string[] $rows
221
+     * @param OutputInterface $output
222
+     */
223
+    protected function showSummary($headers, $rows, OutputInterface $output) {
224
+        $niceDate = $this->formatExecTime();
225
+        if (!$rows) {
226
+            $rows = [
227
+                $this->foldersCounter,
228
+                $this->filesCounter,
229
+                $niceDate,
230
+            ];
231
+        }
232
+        $table = new Table($output);
233
+        $table
234
+            ->setHeaders($headers)
235
+            ->setRows([$rows]);
236
+        $table->render();
237
+    }
238
+
239
+
240
+    /**
241
+     * Formats microtime into a human readable format
242
+     *
243
+     * @return string
244
+     */
245
+    protected function formatExecTime() {
246
+        $secs = round($this->execTime);
247
+        # convert seconds into HH:MM:SS form
248
+        return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
249
+    }
250
+
251
+    /**
252
+     * @return \OCP\IDBConnection
253
+     */
254
+    protected function reconnectToDatabase(OutputInterface $output) {
255
+        /** @var Connection | IDBConnection $connection*/
256
+        $connection = \OC::$server->getDatabaseConnection();
257
+        try {
258
+            $connection->close();
259
+        } catch (\Exception $ex) {
260
+            $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
261
+        }
262
+        while (!$connection->isConnected()) {
263
+            try {
264
+                $connection->connect();
265
+            } catch (\Exception $ex) {
266
+                $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
267
+                sleep(60);
268
+            }
269
+        }
270
+        return $connection;
271
+    }
272
+
273
+    /**
274
+     * @return \OCP\Files\Folder
275
+     * @throws NotFoundException
276
+     */
277
+    private function getAppDataFolder() {
278
+        $instanceId = $this->config->getSystemValue('instanceid', null);
279
+
280
+        if ($instanceId === null) {
281
+            throw new NotFoundException();
282
+        }
283
+
284
+        return $this->root->get('appdata_'.$instanceId);
285
+    }
286 286
 }
Please login to merge, or discard this patch.
Spacing   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -77,7 +77,7 @@  discard block
 block discarded – undo
77 77
 		$path = basename($fullPath);
78 78
 
79 79
 		if ($normalizedPath !== $path) {
80
-			$output->writeln("\t<error>Entry \"" . $fullPath . '" will not be accessible due to incompatible encoding</error>');
80
+			$output->writeln("\t<error>Entry \"".$fullPath.'" will not be accessible due to incompatible encoding</error>');
81 81
 		}
82 82
 	}
83 83
 
@@ -93,7 +93,7 @@  discard block
 block discarded – undo
93 93
 			try {
94 94
 				$appData = $appData->get($folder);
95 95
 			} catch (NotFoundException $e) {
96
-				$output->writeln('<error>Could not find folder: ' . $folder . '</error>');
96
+				$output->writeln('<error>Could not find folder: '.$folder.'</error>');
97 97
 				return 1;
98 98
 			}
99 99
 		}
@@ -102,27 +102,27 @@  discard block
 block discarded – undo
102 102
 		$scanner = new \OC\Files\Utils\Scanner(null, $connection, \OC::$server->query(IEventDispatcher::class), \OC::$server->getLogger());
103 103
 
104 104
 		# check on each file/folder if there was a user interrupt (ctrl-c) and throw an exception
105
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
105
+		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) {
106 106
 			$output->writeln("\tFile   <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
107 107
 			++$this->filesCounter;
108 108
 			$this->abortIfInterrupted();
109 109
 		});
110 110
 
111
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
111
+		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) {
112 112
 			$output->writeln("\tFolder <info>$path</info>", OutputInterface::VERBOSITY_VERBOSE);
113 113
 			++$this->foldersCounter;
114 114
 			$this->abortIfInterrupted();
115 115
 		});
116 116
 
117
-		$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function (StorageNotAvailableException $e) use ($output) {
118
-			$output->writeln('Error while scanning, storage not available (' . $e->getMessage() . ')', OutputInterface::VERBOSITY_VERBOSE);
117
+		$scanner->listen('\OC\Files\Utils\Scanner', 'StorageNotAvailable', function(StorageNotAvailableException $e) use ($output) {
118
+			$output->writeln('Error while scanning, storage not available ('.$e->getMessage().')', OutputInterface::VERBOSITY_VERBOSE);
119 119
 		});
120 120
 
121
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function ($path) use ($output) {
121
+		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFile', function($path) use ($output) {
122 122
 			$this->checkScanWarning($path, $output);
123 123
 		});
124 124
 
125
-		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function ($path) use ($output) {
125
+		$scanner->listen('\OC\Files\Utils\Scanner', 'scanFolder', function($path) use ($output) {
126 126
 			$this->checkScanWarning($path, $output);
127 127
 		});
128 128
 
@@ -137,11 +137,11 @@  discard block
 block discarded – undo
137 137
 			$output->writeln('<info>Interrupted by user</info>');
138 138
 			return 1;
139 139
 		} catch (NotFoundException $e) {
140
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
140
+			$output->writeln('<error>Path not found: '.$e->getMessage().'</error>');
141 141
 			return 1;
142 142
 		} catch (\Exception $e) {
143
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
144
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
143
+			$output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>');
144
+			$output->writeln('<error>'.$e->getTraceAsString().'</error>');
145 145
 			return 1;
146 146
 		}
147 147
 
@@ -245,7 +245,7 @@  discard block
 block discarded – undo
245 245
 	protected function formatExecTime() {
246 246
 		$secs = round($this->execTime);
247 247
 		# convert seconds into HH:MM:SS form
248
-		return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
248
+		return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60);
249 249
 	}
250 250
 
251 251
 	/**
Please login to merge, or discard this patch.