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