Passed
Push — master ( 212138...8c07b6 )
by Roeland
12:08 queued 11s
created
apps/files/lib/Command/ScanAppData.php 2 patches
Indentation   +231 added lines, -231 removed lines patch added patch discarded remove patch
@@ -44,235 +44,235 @@
 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) {
85
-		try {
86
-			$appData = $this->getAppDataFolder();
87
-		} catch (NotFoundException $e) {
88
-			$output->writeln('NoAppData folder found');
89
-			return;
90
-		}
91
-
92
-		if ($folder !== '') {
93
-			try {
94
-				$appData = $appData->get($folder);
95
-			} catch (NotFoundException $e) {
96
-				$output->writeln('Could not find folder: ' . $folder);
97
-				return;
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('Make sure you\'re running the scan command only as the user the web server runs as');
134
-		} catch (InterruptedException $e) {
135
-			# exit the function if ctrl-c has been pressed
136
-			$output->writeln('Interrupted by user');
137
-		} catch (NotFoundException $e) {
138
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
139
-		} catch (\Exception $e) {
140
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
141
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
142
-		}
143
-	}
144
-
145
-
146
-	protected function execute(InputInterface $input, OutputInterface $output) {
147
-		# restrict the verbosity level to VERBOSITY_VERBOSE
148
-		if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
149
-			$output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
150
-		}
151
-
152
-		$output->writeln("\nScanning AppData for files");
153
-
154
-		$folder = $input->getArgument('folder');
155
-
156
-		$this->initTools();
157
-
158
-		$this->scanFiles($output, $folder);
159
-
160
-		$this->presentStats($output);
161
-	}
162
-
163
-	/**
164
-	 * Initialises some useful tools for the Command
165
-	 */
166
-	protected function initTools() {
167
-		// Start the timer
168
-		$this->execTime = -microtime(true);
169
-		// Convert PHP errors to exceptions
170
-		set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
171
-	}
172
-
173
-	/**
174
-	 * Processes PHP errors as exceptions in order to be able to keep track of problems
175
-	 *
176
-	 * @see https://secure.php.net/manual/en/function.set-error-handler.php
177
-	 *
178
-	 * @param int $severity the level of the error raised
179
-	 * @param string $message
180
-	 * @param string $file the filename that the error was raised in
181
-	 * @param int $line the line number the error was raised
182
-	 *
183
-	 * @throws \ErrorException
184
-	 */
185
-	public function exceptionErrorHandler($severity, $message, $file, $line) {
186
-		if (!(error_reporting() & $severity)) {
187
-			// This error code is not included in error_reporting
188
-			return;
189
-		}
190
-		throw new \ErrorException($message, 0, $severity, $file, $line);
191
-	}
192
-
193
-	/**
194
-	 * @param OutputInterface $output
195
-	 */
196
-	protected function presentStats(OutputInterface $output) {
197
-		// Stop the timer
198
-		$this->execTime += microtime(true);
199
-		$output->writeln("");
200
-
201
-		$headers = [
202
-			'Folders', 'Files', 'Elapsed time'
203
-		];
204
-
205
-		$this->showSummary($headers, null, $output);
206
-	}
207
-
208
-	/**
209
-	 * Shows a summary of operations
210
-	 *
211
-	 * @param string[] $headers
212
-	 * @param string[] $rows
213
-	 * @param OutputInterface $output
214
-	 */
215
-	protected function showSummary($headers, $rows, OutputInterface $output) {
216
-		$niceDate = $this->formatExecTime();
217
-		if (!$rows) {
218
-			$rows = [
219
-				$this->foldersCounter,
220
-				$this->filesCounter,
221
-				$niceDate,
222
-			];
223
-		}
224
-		$table = new Table($output);
225
-		$table
226
-			->setHeaders($headers)
227
-			->setRows([$rows]);
228
-		$table->render();
229
-	}
230
-
231
-
232
-	/**
233
-	 * Formats microtime into a human readable format
234
-	 *
235
-	 * @return string
236
-	 */
237
-	protected function formatExecTime() {
238
-		$secs = round($this->execTime);
239
-		# convert seconds into HH:MM:SS form
240
-		return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
241
-	}
242
-
243
-	/**
244
-	 * @return \OCP\IDBConnection
245
-	 */
246
-	protected function reconnectToDatabase(OutputInterface $output) {
247
-		/** @var Connection | IDBConnection $connection*/
248
-		$connection = \OC::$server->getDatabaseConnection();
249
-		try {
250
-			$connection->close();
251
-		} catch (\Exception $ex) {
252
-			$output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
253
-		}
254
-		while (!$connection->isConnected()) {
255
-			try {
256
-				$connection->connect();
257
-			} catch (\Exception $ex) {
258
-				$output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
259
-				sleep(60);
260
-			}
261
-		}
262
-		return $connection;
263
-	}
264
-
265
-	/**
266
-	 * @return \OCP\Files\Folder
267
-	 * @throws NotFoundException
268
-	 */
269
-	private function getAppDataFolder() {
270
-		$instanceId = $this->config->getSystemValue('instanceid', null);
271
-
272
-		if ($instanceId === null) {
273
-			throw new NotFoundException();
274
-		}
275
-
276
-		return $this->root->get('appdata_'.$instanceId);
277
-	}
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) {
85
+        try {
86
+            $appData = $this->getAppDataFolder();
87
+        } catch (NotFoundException $e) {
88
+            $output->writeln('NoAppData folder found');
89
+            return;
90
+        }
91
+
92
+        if ($folder !== '') {
93
+            try {
94
+                $appData = $appData->get($folder);
95
+            } catch (NotFoundException $e) {
96
+                $output->writeln('Could not find folder: ' . $folder);
97
+                return;
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('Make sure you\'re running the scan command only as the user the web server runs as');
134
+        } catch (InterruptedException $e) {
135
+            # exit the function if ctrl-c has been pressed
136
+            $output->writeln('Interrupted by user');
137
+        } catch (NotFoundException $e) {
138
+            $output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
139
+        } catch (\Exception $e) {
140
+            $output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
141
+            $output->writeln('<error>' . $e->getTraceAsString() . '</error>');
142
+        }
143
+    }
144
+
145
+
146
+    protected function execute(InputInterface $input, OutputInterface $output) {
147
+        # restrict the verbosity level to VERBOSITY_VERBOSE
148
+        if ($output->getVerbosity() > OutputInterface::VERBOSITY_VERBOSE) {
149
+            $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
150
+        }
151
+
152
+        $output->writeln("\nScanning AppData for files");
153
+
154
+        $folder = $input->getArgument('folder');
155
+
156
+        $this->initTools();
157
+
158
+        $this->scanFiles($output, $folder);
159
+
160
+        $this->presentStats($output);
161
+    }
162
+
163
+    /**
164
+     * Initialises some useful tools for the Command
165
+     */
166
+    protected function initTools() {
167
+        // Start the timer
168
+        $this->execTime = -microtime(true);
169
+        // Convert PHP errors to exceptions
170
+        set_error_handler([$this, 'exceptionErrorHandler'], E_ALL);
171
+    }
172
+
173
+    /**
174
+     * Processes PHP errors as exceptions in order to be able to keep track of problems
175
+     *
176
+     * @see https://secure.php.net/manual/en/function.set-error-handler.php
177
+     *
178
+     * @param int $severity the level of the error raised
179
+     * @param string $message
180
+     * @param string $file the filename that the error was raised in
181
+     * @param int $line the line number the error was raised
182
+     *
183
+     * @throws \ErrorException
184
+     */
185
+    public function exceptionErrorHandler($severity, $message, $file, $line) {
186
+        if (!(error_reporting() & $severity)) {
187
+            // This error code is not included in error_reporting
188
+            return;
189
+        }
190
+        throw new \ErrorException($message, 0, $severity, $file, $line);
191
+    }
192
+
193
+    /**
194
+     * @param OutputInterface $output
195
+     */
196
+    protected function presentStats(OutputInterface $output) {
197
+        // Stop the timer
198
+        $this->execTime += microtime(true);
199
+        $output->writeln("");
200
+
201
+        $headers = [
202
+            'Folders', 'Files', 'Elapsed time'
203
+        ];
204
+
205
+        $this->showSummary($headers, null, $output);
206
+    }
207
+
208
+    /**
209
+     * Shows a summary of operations
210
+     *
211
+     * @param string[] $headers
212
+     * @param string[] $rows
213
+     * @param OutputInterface $output
214
+     */
215
+    protected function showSummary($headers, $rows, OutputInterface $output) {
216
+        $niceDate = $this->formatExecTime();
217
+        if (!$rows) {
218
+            $rows = [
219
+                $this->foldersCounter,
220
+                $this->filesCounter,
221
+                $niceDate,
222
+            ];
223
+        }
224
+        $table = new Table($output);
225
+        $table
226
+            ->setHeaders($headers)
227
+            ->setRows([$rows]);
228
+        $table->render();
229
+    }
230
+
231
+
232
+    /**
233
+     * Formats microtime into a human readable format
234
+     *
235
+     * @return string
236
+     */
237
+    protected function formatExecTime() {
238
+        $secs = round($this->execTime);
239
+        # convert seconds into HH:MM:SS form
240
+        return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
241
+    }
242
+
243
+    /**
244
+     * @return \OCP\IDBConnection
245
+     */
246
+    protected function reconnectToDatabase(OutputInterface $output) {
247
+        /** @var Connection | IDBConnection $connection*/
248
+        $connection = \OC::$server->getDatabaseConnection();
249
+        try {
250
+            $connection->close();
251
+        } catch (\Exception $ex) {
252
+            $output->writeln("<info>Error while disconnecting from database: {$ex->getMessage()}</info>");
253
+        }
254
+        while (!$connection->isConnected()) {
255
+            try {
256
+                $connection->connect();
257
+            } catch (\Exception $ex) {
258
+                $output->writeln("<info>Error while re-connecting to database: {$ex->getMessage()}</info>");
259
+                sleep(60);
260
+            }
261
+        }
262
+        return $connection;
263
+    }
264
+
265
+    /**
266
+     * @return \OCP\Files\Folder
267
+     * @throws NotFoundException
268
+     */
269
+    private function getAppDataFolder() {
270
+        $instanceId = $this->config->getSystemValue('instanceid', null);
271
+
272
+        if ($instanceId === null) {
273
+            throw new NotFoundException();
274
+        }
275
+
276
+        return $this->root->get('appdata_'.$instanceId);
277
+    }
278 278
 }
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('Could not find folder: ' . $folder);
96
+				$output->writeln('Could not find folder: '.$folder);
97 97
 				return;
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
 
@@ -135,10 +135,10 @@  discard block
 block discarded – undo
135 135
 			# exit the function if ctrl-c has been pressed
136 136
 			$output->writeln('Interrupted by user');
137 137
 		} catch (NotFoundException $e) {
138
-			$output->writeln('<error>Path not found: ' . $e->getMessage() . '</error>');
138
+			$output->writeln('<error>Path not found: '.$e->getMessage().'</error>');
139 139
 		} catch (\Exception $e) {
140
-			$output->writeln('<error>Exception during scan: ' . $e->getMessage() . '</error>');
141
-			$output->writeln('<error>' . $e->getTraceAsString() . '</error>');
140
+			$output->writeln('<error>Exception during scan: '.$e->getMessage().'</error>');
141
+			$output->writeln('<error>'.$e->getTraceAsString().'</error>');
142 142
 		}
143 143
 	}
144 144
 
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
 	protected function formatExecTime() {
238 238
 		$secs = round($this->execTime);
239 239
 		# convert seconds into HH:MM:SS form
240
-		return sprintf('%02d:%02d:%02d', ($secs/3600), ($secs/60%60), $secs%60);
240
+		return sprintf('%02d:%02d:%02d', ($secs / 3600), ($secs / 60 % 60), $secs % 60);
241 241
 	}
242 242
 
243 243
 	/**
Please login to merge, or discard this patch.