Conditions | 56 |
Paths | 148 |
Total Lines | 206 |
Code Lines | 133 |
Lines | 49 |
Ratio | 23.79 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
240 | public function getListing($FOLDER, $showdel) { |
||
241 | // Get the listing from the database |
||
242 | $requery = false; |
||
243 | $uid = \OC::$server->getUserSession()->getUser()->getUID(); |
||
244 | $shared_items = \OCP\Share::getItemsSharedWith('ownnote', 'populated_shares'); |
||
245 | /** |
||
246 | * @var $results OwnNote[] |
||
247 | */ |
||
248 | $results = array_merge($this->findNotesFromUser($uid), $shared_items); |
||
249 | |||
250 | $results2 = $results; |
||
251 | if ($results) |
||
252 | foreach ($results as $result) { |
||
253 | if($result instanceof OwnNote) { |
||
254 | $result = $result->jsonSerialize(); |
||
255 | } |
||
256 | |||
257 | foreach ($results2 as $result2) { |
||
258 | if($result2 instanceof OwnNote) { |
||
259 | $result2 = $result2->jsonSerialize(); |
||
260 | } |
||
261 | View Code Duplication | if ($result['id'] != $result2['id'] && $result['name'] == $result2['name'] && $result['grouping'] == $result2['grouping']) { |
|
262 | // We have a duplicate that should not exist. Need to remove the offending record first |
||
263 | $delid = -1; |
||
264 | if ($result['mtime'] == $result2['mtime']) { |
||
265 | // If the mtime's match, delete the oldest ID. |
||
266 | $delid = $result['id']; |
||
267 | if ($result['id'] > $result2['id']) |
||
268 | $delid = $result2['id']; |
||
269 | } elseif ($result['mtime'] > $result2['mtime']) { |
||
270 | // Again, delete the oldest |
||
271 | $delid = $result2['id']; |
||
272 | } elseif ($result['mtime'] < $result2['mtime']) { |
||
273 | // The only thing left is if result is older |
||
274 | $delid = $result['id']; |
||
275 | } |
||
276 | if ($delid != -1) { |
||
277 | $this->delete('', $delid); |
||
278 | $requery = true; |
||
279 | } |
||
280 | } |
||
281 | } |
||
282 | } |
||
283 | View Code Duplication | if ($requery) { |
|
284 | $shared_items = \OCP\Share::getItemsSharedWith('ownnote', 'populated_shares'); |
||
285 | $results = array_merge($this->findNotesFromUser($uid), $shared_items); |
||
286 | $requery = false; |
||
287 | } |
||
288 | |||
289 | // Tests to add a bunch of notes |
||
290 | //$now = new DateTime(); |
||
291 | //for ($x = 0; $x < 199; $x++) { |
||
292 | //saveNote('', "Test ".$x, '', '', $now->getTimestamp()); |
||
293 | //} |
||
294 | $farray = array(); |
||
295 | if ($FOLDER != '') { |
||
296 | // Create the folder if it doesn't exist |
||
297 | View Code Duplication | if (!Filesystem::is_dir($FOLDER)) { |
|
298 | if (!Filesystem::mkdir($FOLDER)) { |
||
299 | \OCP\Util::writeLog('ownnote', 'Could not create ownNote directory.', \OCP\Util::ERROR); |
||
300 | exit; |
||
301 | } |
||
302 | } |
||
303 | // Synchronize files to the database |
||
304 | $filearr = array(); |
||
305 | if ($listing = Filesystem::opendir($FOLDER)) { |
||
306 | if (!$listing) { |
||
307 | \OCP\Util::writeLog('ownnote', 'Error listing directory.', \OCP\Util::ERROR); |
||
308 | exit; |
||
309 | } |
||
310 | while (($file = readdir($listing)) !== false) { |
||
311 | $tmpfile = $file; |
||
312 | if ($tmpfile == "." || $tmpfile == "..") continue; |
||
313 | if (!$this->utils->endsWith($tmpfile, ".htm") && !$this->utils->endsWith($tmpfile, ".html")) continue; |
||
314 | if ($info = Filesystem::getFileInfo($FOLDER . "/" . $tmpfile)) { |
||
315 | // Check for EVERNOTE but wait to rename them to get around: |
||
316 | // https://github.com/owncloud/core/issues/16202 |
||
317 | if ($this->utils->endsWith($tmpfile, ".html")) { |
||
318 | Evernote::checkEvernote($FOLDER, $tmpfile); |
||
319 | } |
||
320 | // Separate the name and group name |
||
321 | $name = preg_replace('/\\.[^.\\s]{3,4}$/', '', $tmpfile); |
||
322 | $group = ""; |
||
323 | View Code Duplication | if (substr($name, 0, 1) == "[") { |
|
324 | $end = strpos($name, ']'); |
||
325 | $group = substr($name, 1, $end - 1); |
||
326 | $name = substr($name, $end + 1, strlen($name) - $end + 1); |
||
327 | $name = trim($name); |
||
328 | } |
||
329 | // Set array for later checking |
||
330 | $filearr[] = $tmpfile; |
||
331 | // Check to see if the file is in the DB |
||
332 | $fileindb = false; |
||
333 | if ($results) |
||
334 | foreach ($results as $result) { |
||
335 | if($result instanceof OwnNote) { |
||
336 | $result = $result->jsonSerialize(); |
||
337 | } |
||
338 | if ($result['deleted'] == 0) |
||
339 | if ($name == $result['name'] && $group == $result['grouping']) { |
||
340 | $fileindb = true; |
||
341 | // If it is in the DB, check if the filesystem file is newer than the DB |
||
342 | if ($result['mtime'] < $info['mtime']) { |
||
343 | // File is newer, this could happen if a user updates a file |
||
344 | $html = ""; |
||
345 | $html = Filesystem::file_get_contents($FOLDER . "/" . $tmpfile); |
||
346 | $n = [ |
||
347 | 'id' => $result['id'], |
||
348 | 'mtime' => $info['mtime'], |
||
349 | 'note' => ($html) ? $html : '' |
||
350 | ]; |
||
351 | $this->update('', $n); |
||
352 | $requery = true; |
||
353 | } |
||
354 | } |
||
355 | } |
||
356 | if (!$fileindb) { |
||
357 | // If it's not in the DB, add it. |
||
358 | $html = ""; |
||
359 | if ($html = Filesystem::file_get_contents($FOLDER . "/" . $tmpfile)) { |
||
360 | } else { |
||
361 | $html = ""; |
||
362 | } |
||
363 | $n = [ |
||
364 | 'name' => $name, |
||
365 | 'group' => $group, |
||
366 | 'note' => $html, |
||
367 | 'mtime' => $info['mtime'], |
||
368 | 'uid' => $uid |
||
369 | ]; |
||
370 | $this->create('', $n, $uid); |
||
371 | $requery = true; |
||
372 | } |
||
373 | // We moved the rename down here to overcome the OC issue |
||
374 | View Code Duplication | if ($this->utils->endsWith($tmpfile, ".html")) { |
|
375 | $tmpfile = substr($tmpfile, 0, -1); |
||
376 | if (!Filesystem::file_exists($FOLDER . "/" . $tmpfile)) { |
||
377 | Filesystem::rename($FOLDER . "/" . $file, $FOLDER . "/" . $tmpfile); |
||
378 | } |
||
379 | } |
||
380 | } |
||
381 | } |
||
382 | } |
||
383 | View Code Duplication | if ($requery) { |
|
384 | $shared_items = \OCP\Share::getItemsSharedWith('ownnote', 'populated_shares'); |
||
385 | $results = array_merge($this->findNotesFromUser($uid), $shared_items); |
||
386 | } |
||
387 | // Now also make sure the files exist, they may not if the user switched folders in admin. |
||
388 | if ($results) |
||
389 | foreach ($results as $result) { |
||
390 | if($result instanceof OwnNote) { |
||
391 | $result = $result->jsonSerialize(); |
||
392 | } |
||
393 | if ($result['deleted'] == 0) { |
||
394 | $tmpfile = $result['name'] . ".htm"; |
||
395 | View Code Duplication | if ($result['grouping'] != '') |
|
396 | $tmpfile = '[' . $result['grouping'] . '] ' . $result['name'] . '.htm'; |
||
397 | $filefound = false; |
||
398 | foreach ($filearr as $f) { |
||
399 | if ($f == $tmpfile) { |
||
400 | $filefound = true; |
||
401 | break; |
||
402 | } |
||
403 | } |
||
404 | if (!$filefound) { |
||
405 | $this->update($FOLDER, $result); |
||
406 | } |
||
407 | } |
||
408 | } |
||
409 | } |
||
410 | // Now loop through and return the listing |
||
411 | if ($results) { |
||
412 | $count = 0; |
||
413 | $now = new \DateTime(); |
||
414 | $filetime = new \DateTime(); |
||
415 | |||
416 | foreach ($results as $result) { |
||
417 | if($result instanceof OwnNote) { |
||
418 | $result = $result->jsonSerialize(); |
||
419 | } |
||
420 | if ($result['deleted'] == 0 || $showdel == true) { |
||
421 | $filetime->setTimestamp($result['mtime']); |
||
422 | $timestring = $this->utils->getTimeString($filetime, $now); |
||
423 | $f = array(); |
||
424 | $f['id'] = $result['id']; |
||
425 | $f['uid'] = $result['uid']; |
||
426 | $f['name'] = $result['name']; |
||
427 | $f['group'] = ($result['grouping']) ? $result['grouping'] : ''; |
||
428 | $f['timestring'] = $timestring; |
||
429 | $f['mtime'] = $result['mtime']; |
||
430 | $f['timediff'] = $now->getTimestamp() - $result['mtime']; |
||
431 | $f['deleted'] = $result['deleted']; |
||
432 | $f['permissions'] = @$result['permissions']; |
||
433 | |||
434 | |||
435 | $shared_with = \OCP\Share::getUsersItemShared('ownnote', $result['id'], $result['uid']); |
||
436 | // add shares (all shares, if it's an owned note, only the user for shared notes (not disclosing other sharees)) |
||
437 | $f['shared_with'] = ($result['uid'] == $uid) ? $shared_with : [$uid]; |
||
438 | |||
439 | $farray[$count] = $f; |
||
440 | $count++; |
||
441 | } |
||
442 | } |
||
443 | } |
||
444 | return $farray; |
||
445 | } |
||
446 | |||
461 |