1 : <?php
2 :
3 : /**
4 : * Dictionaries of Old French and Latin
5 : *
6 : * PHP 5
7 : *
8 : * @category DicFro
9 : * @package Model
10 : * @subpackage Search
11 : * @author Michel Corne <mcorne@yahoo.com>
12 : * @copyright 2008-2010 Michel Corne
13 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
14 : * @link http://www.micmap.org/dicfro
15 : */
16 :
17 : // note: classes are included as needed
18 :
19 : /**
20 : * Search a Godefroy like dictionary
21 : *
22 : * @category DicFro
23 : * @package Model
24 : * @subpackage Search
25 : * @author Michel Corne <mcorne@yahoo.com>
26 : * @copyright 2008-2010 Michel Corne
27 : * @license http://opensource.org/licenses/gpl-3.0.html GNU General Public License, version 3 (GPLv3)
28 : */
29 :
30 : abstract class Model_Search_GdfLike
31 : {
32 : /**
33 : * Template to create an image number
34 : */
35 : const BUILD_IMAGE_NUMBER_TPL = '%\'02s%s%\'04s';
36 :
37 : /**
38 : * Template to parse an image number
39 : */
40 : const PARSE_IMAGE_NUMBER_TPL = '~(\d\d)(?:0|1)(\d\d\d\d)~i';
41 :
42 : /**
43 : * Template of the name of an image file
44 : */
45 : const IMAGE_DEFAULT_PATH_TPL = 'dictionary/%s/mImg/%s.gif';
46 :
47 : /**
48 : * Name of the dictionaries directory
49 : * @var string
50 : */
51 : public $directory = '.';
52 :
53 : /**
54 : * Name of the dictionary
55 : * @var string
56 : */
57 : public $dictionary;
58 :
59 : /**
60 : * Template of the image file names
61 : * @var string
62 : */
63 : public $imagePath;
64 :
65 : /**
66 : * Template of the errata file names
67 : * @var string
68 : */
69 : public $errataFiles;
70 :
71 : /**
72 : * Query object
73 : * @var object
74 : */
75 : public $query;
76 :
77 : /**
78 : * Flag to search or not the text errata
79 : * @var boolean
80 : */
81 : public $needErrataText = false;
82 :
83 : /**
84 : * Flag to search or not the image errata
85 : * @var boolean
86 : */
87 : public $needErrataImages = false;
88 :
89 : /**
90 : * Flag to search or not the ghostwords
91 : * @var boolean
92 : */
93 : public $needGhostwords = false;
94 :
95 : /**
96 : * Flag to search or not words in the Tobler
97 : * @var boolean
98 : */
99 : public $needTobler = false;
100 :
101 : /**
102 : * Flag to search or not words in the Whitaker
103 : * @var boolean
104 : */
105 : public $needWhitaker = false;
106 :
107 : /**
108 : * Flag to search or not words in the Tcaf
109 : * @var boolean
110 : */
111 : public $needTcaf = false;
112 :
113 : /**
114 : * Extracts the volume and page numbers from the image number
115 : *
116 : * @param string $imageNumber the image number
117 : * @return array the volume and page numbers
118 : */
119 : public function extractVolumeAndPage($imageNumber)
120 : {
121 12 : return preg_match(self::PARSE_IMAGE_NUMBER_TPL, $imageNumber, $match)?
122 12 : array($match[1], $match[2]) :
123 12 : array();
124 : }
125 :
126 : /**
127 : * Goes to the next page
128 : *
129 : * @param string $volume the dictionary volume
130 : * @param string $page the page in the volume to go from
131 : * @return array the page details
132 : */
133 : public function goToNextPage($volume, $page)
134 : {
135 2 : $imageNumber = $this->setImageNumber($volume, $page);
136 2 : $result = $this->query->goToNextPage($imageNumber);
137 :
138 2 : return $this->updateResult($result);
139 : }
140 :
141 : /**
142 : * Goes to a given page
143 : *
144 : * @param string $volume the dictionary volume
145 : * @param string $page the page in the volume to go to
146 : * @return array the page details
147 : */
148 : public function goToPage($volume, $page)
149 : {
150 2 : $imageNumber = $this->setImageNumber($volume, $page);
151 2 : $result = $this->query->goToPage($imageNumber);
152 :
153 2 : return $this->updateResult($result);
154 : }
155 :
156 : /**
157 : * Goes to the previous page
158 : *
159 : * @param string $volume the dictionary volume
160 : * @param string $page the page in the volume to go from
161 : * @return array the page details
162 : */
163 : public function goToPreviousPage($volume, $page)
164 : {
165 2 : $imageNumber = $this->setImageNumber($volume, $page);
166 2 : $result = $this->query->goToPreviousPage($imageNumber);
167 :
168 2 : return $this->updateResult($result);
169 : }
170 :
171 : /**
172 : * Searches the errata of a given page
173 : *
174 : * @param string $imageNumber the image number
175 : * @return array the list of errata file names
176 : */
177 : public function searchErrata($imageNumber)
178 : {
179 6 : list($volume, $page) = $this->extractVolumeAndPage($imageNumber);
180 6 : $pattern = sprintf($this->errataFiles, $volume, (int)$page);
181 :
182 6 : return glob($pattern);
183 : }
184 :
185 : /**
186 : * Searches the ghostwords in a given page
187 : *
188 : * @param array $foundWord the details of the word to search from, including this word
189 : * @param array $nextWord the details of the word to search to, excluding this word
190 : * @return mixed the list of ghostwords
191 : */
192 : public function searchGhostwords($foundWord, $nextWord)
193 : {
194 0 : require_once 'Model/Query/Ghostwords.php';
195 6 : $query = new Model_Query_Ghostwords($this->directory);
196 :
197 6 : return $query->searchWords($foundWord['ascii'], $nextWord['ascii']);
198 : }
199 :
200 : /**
201 : * Searches verbs in the Tcaf matching a word form
202 : *
203 : * @param string $word the word form to search
204 : * @return array the list of identified verbs
205 : */
206 : public function searchTcaf($word)
207 : {
208 0 : require_once 'Model/Query/Tcaf.php';
209 3 : $query = new Model_Query_Tcaf($this->directory);
210 :
211 3 : return $query->searchVerbs($word);
212 : }
213 :
214 : /**
215 : * Searches words in the Tobler matching a word form
216 : *
217 : * @param string $word the word form to search
218 : * @return object the list of identified words
219 : */
220 : public function searchTobler($word)
221 : {
222 0 : require_once 'Model/Query/Tobler.php';
223 3 : $query = new Model_Query_Tobler($this->directory);
224 :
225 3 : return $query->searchWords($word);
226 : }
227 :
228 : /**
229 : * Searches words in the Whitaker matching a word form
230 : *
231 : * @param string $word the word form to search
232 : * @return object the list of identified words
233 : */
234 : public function searchWhitaker($word)
235 : {
236 0 : require_once 'Model/Query/Whitaker.php';
237 3 : $query = new Model_Query_Whitaker($this->directory);
238 :
239 3 : return $query->searchWords($word);
240 : }
241 :
242 : /**
243 : * Searches a word in the dictionary
244 : *
245 : * @param string $word the word to search
246 : * @return array the word details
247 : */
248 : public function searchWord($word)
249 : {
250 3 : $result = $this->query->searchWord($word);
251 :
252 3 : return $this->updateResult($result, $word);
253 : }
254 :
255 : /**
256 : * Sets the image number from the volume and page numbers
257 : *
258 : * @param string $volume the dictionary volume
259 : * @param string $page the page in the volume to go from
260 : * @param integer $digit Additional digit used in some dictionaries,
261 : * ex. in the "Godefroy Complement"
262 : * @return string the image number
263 : */
264 : public function setImageNumber($volume, $page, $digit = 0)
265 : {
266 8 : return sprintf(self::BUILD_IMAGE_NUMBER_TPL, $volume, $digit, $page);
267 : }
268 :
269 : /**
270 : * Sets the image path
271 : *
272 : * @param array $foundWord the word details
273 : * @return string the image path
274 : */
275 : public function setImagePath($foundWord)
276 : {
277 11 : if ($this->imagePath) {
278 1 : $path = sprintf($this->imagePath, $foundWord['image']);
279 : } else {
280 11 : $path = sprintf(self::IMAGE_DEFAULT_PATH_TPL, $this->dictionary, $foundWord['image']);
281 : }
282 :
283 11 : return $path;
284 : }
285 :
286 : /**
287 : * Updates the result or the word search or page change
288 : *
289 : * @param array $result the result set
290 : * @param string $word the word to search
291 : * @return array the updated result set
292 : */
293 : public function updateResult($result, $word = null)
294 : {
295 10 : @list($foundWord, $nextWord) = $result;
296 :
297 10 : list($volume, $page) = $this->extractVolumeAndPage($foundWord['image']);
298 10 : $volume = (int)$volume or $volume = '';
299 10 : $page = (int)$page or $page = '';
300 :
301 : return array(
302 10 : $this->setImagePath($foundWord),
303 10 : $this->needErrataImages?
304 10 : $this->searchErrata($foundWord['image']) :
305 10 : null,
306 10 : $this->needErrataText?
307 10 : $foundWord['errata'] :
308 10 : null,
309 10 : $this->needGhostwords?
310 10 : $this->searchGhostwords($foundWord, $nextWord) :
311 10 : null,
312 10 : ($word and $this->needTobler)?
313 4 : $this->searchTobler($word) :
314 10 : null,
315 10 : ($word and $this->needTcaf)?
316 4 : $this->searchTcaf($word) :
317 10 : null,
318 10 : ($word and $this->needWhitaker)?
319 4 : $this->searchWhitaker($word) :
320 10 : null,
321 10 : $volume,
322 10 : $page,
323 10 : $foundWord['original'],
324 10 : );
325 : }
|