Blame view

sources/apps/search_lucene/ajax/lucene.php 2.37 KB
d1bafeea1   Kload   [fix] Upgrade to ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  <?php
  
  OCP\JSON::checkLoggedIn();
  OCP\JSON::callCheck();
  OCP\JSON::checkAppEnabled('search_lucene');
  session_write_close();
  
  //FIXME refactor db queries and logic into the lib folder
  
  function index() {
  	if ( isset($_GET['fileid']) ){
  		$fileIds = array($_GET['fileid']);
  	} else {
  		$fileIds = OCA\Search_Lucene\Indexer::getUnindexed();
  	}
  
  	$eventSource = new OC_EventSource();
  	$eventSource->send('count', count($fileIds));
  
  	$skippedDirs = explode(';', OCP\Config::getUserValue(OCP\User::getUser(), 'search_lucene', 'skipped_dirs', '.git;.svn;.CVS;.bzr'));
  
  	foreach ($fileIds as $id) {
  		$skipped = false;
  		
  		$fileStatus = OCA\Search_Lucene\Status::fromFileId($id);
  
  		try{
  			//before we start mark the file as error so we know there was a problem when the php execution dies
  			$fileStatus->markError();
  
  			$path = OC\Files\Filesystem::getPath($id);
  			$eventSource->send('indexing', $path);
  
  			foreach ($skippedDirs as $skippedDir) {
  				if (strpos($path, '/' . $skippedDir . '/') !== false //contains dir
  					|| strrpos($path, '/' . $skippedDir) === strlen($path) - (strlen($skippedDir) + 1) // ends with dir
  				) {
  					$result = $fileStatus->markSkipped();
  					$skipped = true;
  					break;
  				}
  			}
  			if (!$skipped) {
  				if (OCA\Search_Lucene\Indexer::indexFile($path, OCP\User::getUser())) {
  					$result = $fileStatus->markIndexed();
  				}
  			}
  
  			if (!$result) {
6d9380f96   Cédric Dupont   Update sources OC...
50
  				OCP\JSON::error(array('message' => 'Could not index file.'));
d1bafeea1   Kload   [fix] Upgrade to ...
51
52
53
54
55
56
57
58
  				$eventSource->send('error', $path);
  			}
  		} catch (Exception $e) { //sqlite might report database locked errors when stock filescan is in progress
  			//this also catches db locked exception that might come up when using sqlite
  			\OCP\Util::writeLog('search_lucene',
  				$e->getMessage() . ' Trace:
  ' . $e->getTraceAsString(),
  				\OCP\Util::ERROR);
6d9380f96   Cédric Dupont   Update sources OC...
59
  			OCP\JSON::error(array('message' => 'Could not index file.'));
d1bafeea1   Kload   [fix] Upgrade to ...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
  			$eventSource->send('error', $e->getMessage());
  			//try to mark the file as new to let it reindex
  			$fileStatus->markNew();  // Add UI to trigger rescan of files with status 'E'rror?
  		}
  	}
  
  	$eventSource->send('done', '');
  	$eventSource->close();
  }
  
  function handleOptimize() {
  	OCA\Search_Lucene\Lucene::optimizeIndex();
  }
  
  if ($_GET['operation']) {
  	switch ($_GET['operation']) {
  		case 'index':
  			index();
  			break;
  		case 'optimize':
  			handleOptimize();
  			break;
  		default:
  			OCP\JSON::error(array('cause' => 'Unknown operation'));
  	}
  }