1: <?php
2: /**
3: * $Horde: luxor/lib/Tagger.php,v 1.9 2008/08/01 21:09:31 chuck Exp $
4: *
5: * @author Jan Schneider <jan@horde.org>
6: * @since Luxor 0.1
7: * @package Luxor
8: */
9: class Luxor_Tagger {
10:
11: /**
12: * Indexes a file.
13: * Parses the files contents for symbols and creates indexes in the
14: * storage backend for the file itself and the symbols it defines.
15: *
16: * @param Luxor_Driver $files An instance of a storage backend driver.
17: * @param string $pathname The (relative) pathname of the file to
18: * be processed.
19: * @param Luxor_Lang $lang The language object for $pathname.
20: *
21: * @return mixed A PEAR_Error if an error occured.
22: */
23: function processFile($files, $pathname, $lang)
24: {
25: global $index;
26:
27: /* Get the unique ID for this file. */
28: $fileId = $index->fileId($pathname);
29: if ($fileId === false) {
30: $fileId = $index->createFileId($pathname, '', $files->getFiletime($pathname));
31: } elseif (is_a($fileId, 'PEAR_Error')) {
32: return $fileId;
33: }
34:
35: /* Update the file's status. */
36: $result = $index->toIndex($fileId);
37: if ($result === false) {
38: return PEAR::raiseError(sprintf(_("%s was already indexed."), $pathname));
39: } elseif (is_a($result, 'PEAR_Error')) {
40: return $result;
41: }
42:
43: /* Empty symbol cache. */
44: $index->clearCache();
45:
46: /* Find symbols defined by this file. */
47: $path = $files->tmpFile($pathname);
48: if (!$path) {
49: return PEAR::raiseError(sprintf(_("Can't create copy of file %s."), $pathname));
50: }
51: $result = $lang->indexFile($path, $fileId);
52: if (is_a($result, 'PEAR_Error')) {
53: return $result;
54: }
55: }
56:
57: /**
58: * References a file.
59: * Parses the files contents for symbols and creates references to the
60: * files where these symbols are defined.
61: *
62: * @param Luxor_Driver $files An instance of a storage backend driver.
63: * @param string $pathname The (relative) pathname of the file to
64: * be referenced.
65: * @param Luxor_Lang $lang The language object for $pathname.
66: *
67: * @return mixed A PEAR_Error if an error occured.
68: */
69: function processRefs($files, $pathname, $lang)
70: {
71: global $index;
72:
73: /* Get the unique ID for this file. */
74: $fileId = $index->fileId($pathname);
75: if ($fileId === false) {
76: $fileId = $index->createFileId($pathname, '', $files->getFiletime($pathname));
77: }
78: if (is_a($fileId, 'PEAR_Error')) {
79: return $fileId;
80: }
81:
82: /* Update the file's status. */
83: $result = $index->toReference($fileId);
84: if ($result === false) {
85: return PEAR::raiseError(sprintf(_("%s was already indexed."), $pathname));
86: } elseif (is_a($result, 'PEAR_Error')) {
87: return $result;
88: }
89:
90: /* Empty symbol cache. */
91: $index->clearCache();
92:
93: /* Create references to symbol definitions. */
94: $path = $files->tmpFile($pathname);
95: if (!$path) {
96: return PEAR::raiseError(sprintf(_("Can't create copy of file %s."), $pathname));
97: }
98: $result = $lang->referenceFile($path, $fileId);
99: if (is_a($result, 'PEAR_Error')) {
100: return $result;
101: }
102: }
103:
104: }
105: