Conditions | 20 |
Paths | 15750 |
Total Lines | 130 |
Code Lines | 96 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
188 | public static function exportGedcom(Tree $tree, $gedout, $exportOptions) |
||
189 | { |
||
190 | switch ($exportOptions['privatize']) { |
||
191 | case 'gedadmin': |
||
192 | $access_level = Auth::PRIV_NONE; |
||
193 | break; |
||
194 | case 'user': |
||
195 | $access_level = Auth::PRIV_USER; |
||
196 | break; |
||
197 | case 'visitor': |
||
198 | $access_level = Auth::PRIV_PRIVATE; |
||
199 | break; |
||
200 | case 'none': |
||
201 | $access_level = Auth::PRIV_HIDE; |
||
202 | break; |
||
203 | } |
||
204 | |||
205 | $head = self::gedcomHeader($tree); |
||
206 | if ($exportOptions['toANSI'] == 'yes') { |
||
207 | $head = str_replace('UTF-8', 'ANSI', $head); |
||
208 | $head = utf8_decode($head); |
||
209 | } |
||
210 | $head = self::reformatRecord($head); |
||
211 | fwrite($gedout, $head); |
||
212 | |||
213 | // Buffer the output. Lots of small fwrite() calls can be very slow when writing large gedcoms. |
||
214 | $buffer = ''; |
||
215 | |||
216 | // Generate the OBJE/SOUR/REPO/NOTE records first, as their privacy calcualations involve |
||
217 | // database queries, and we wish to avoid large gaps between queries due to MySQL connection timeouts. |
||
218 | $tmp_gedcom = ''; |
||
219 | $rows = Database::prepare( |
||
220 | "SELECT m_id AS xref, m_gedcom AS gedcom" . |
||
221 | " FROM `##media` WHERE m_file = :tree_id ORDER BY m_id" |
||
222 | )->execute(array( |
||
223 | 'tree_id' => $tree->getTreeId(), |
||
224 | ))->fetchAll(); |
||
225 | |||
226 | foreach ($rows as $row) { |
||
227 | $rec = Media::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level); |
||
|
|||
228 | $rec = self::convertMediaPath($rec, $exportOptions['path']); |
||
229 | if ($exportOptions['toANSI'] === 'yes') { |
||
230 | $rec = utf8_decode($rec); |
||
231 | } |
||
232 | $tmp_gedcom .= self::reformatRecord($rec); |
||
233 | } |
||
234 | |||
235 | $rows = Database::prepare( |
||
236 | "SELECT s_id AS xref, s_file AS gedcom_id, s_gedcom AS gedcom" . |
||
237 | " FROM `##sources` WHERE s_file = :tree_id ORDER BY s_id" |
||
238 | )->execute(array( |
||
239 | 'tree_id' => $tree->getTreeId(), |
||
240 | ))->fetchAll(); |
||
241 | |||
242 | foreach ($rows as $row) { |
||
243 | $rec = Source::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level); |
||
244 | if ($exportOptions['toANSI'] === 'yes') { |
||
245 | $rec = utf8_decode($rec); |
||
246 | } |
||
247 | $tmp_gedcom .= self::reformatRecord($rec); |
||
248 | } |
||
249 | |||
250 | $rows = Database::prepare( |
||
251 | "SELECT o_type AS type, o_id AS xref, o_gedcom AS gedcom" . |
||
252 | " FROM `##other` WHERE o_file = :tree_id AND o_type NOT IN ('HEAD', 'TRLR') ORDER BY o_id" |
||
253 | )->execute(array( |
||
254 | 'tree_id' => $tree->getTreeId(), |
||
255 | ))->fetchAll(); |
||
256 | |||
257 | foreach ($rows as $row) { |
||
258 | switch ($row->type) { |
||
259 | case 'NOTE': |
||
260 | $record = Note::getInstance($row->xref, $tree, $row->gedcom); |
||
261 | break; |
||
262 | case 'REPO': |
||
263 | $record = Repository::getInstance($row->xref, $tree, $row->gedcom); |
||
264 | break; |
||
265 | default: |
||
266 | $record = GedcomRecord::getInstance($row->xref, $tree, $row->gedcom); |
||
267 | break; |
||
268 | } |
||
269 | |||
270 | $rec = $record->privatizeGedcom($access_level); |
||
271 | if ($exportOptions['toANSI'] === 'yes') { |
||
272 | $rec = utf8_decode($rec); |
||
273 | } |
||
274 | $tmp_gedcom .= self::reformatRecord($rec); |
||
275 | } |
||
276 | |||
277 | $rows = Database::prepare( |
||
278 | "SELECT i_id AS xref, i_gedcom AS gedcom" . |
||
279 | " FROM `##individuals` WHERE i_file = :tree_id ORDER BY i_id" |
||
280 | )->execute(array( |
||
281 | 'tree_id' => $tree->getTreeId(), |
||
282 | ))->fetchAll(); |
||
283 | |||
284 | foreach ($rows as $row) { |
||
285 | $rec = Individual::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level); |
||
286 | if ($exportOptions['toANSI'] === 'yes') { |
||
287 | $rec = utf8_decode($rec); |
||
288 | } |
||
289 | $buffer .= self::reformatRecord($rec); |
||
290 | if (strlen($buffer) > 65536) { |
||
291 | fwrite($gedout, $buffer); |
||
292 | $buffer = ''; |
||
293 | } |
||
294 | } |
||
295 | |||
296 | $rows = Database::prepare( |
||
297 | "SELECT f_id AS xref, f_gedcom AS gedcom" . |
||
298 | " FROM `##families` WHERE f_file = :tree_id ORDER BY f_id" |
||
299 | )->execute(array( |
||
300 | 'tree_id' => $tree->getTreeId(), |
||
301 | ))->fetchAll(); |
||
302 | |||
303 | foreach ($rows as $row) { |
||
304 | $rec = Family::getInstance($row->xref, $tree, $row->gedcom)->privatizeGedcom($access_level); |
||
305 | if ($exportOptions['toANSI'] === 'yes') { |
||
306 | $rec = utf8_decode($rec); |
||
307 | } |
||
308 | $buffer .= self::reformatRecord($rec); |
||
309 | if (strlen($buffer) > 65536) { |
||
310 | fwrite($gedout, $buffer); |
||
311 | $buffer = ''; |
||
312 | } |
||
313 | } |
||
314 | |||
315 | fwrite($gedout, $buffer); |
||
316 | fwrite($gedout, $tmp_gedcom); |
||
317 | fwrite($gedout, '0 TRLR' . WT_EOL); |
||
318 | } |
||
320 |