Total Complexity | 45 |
Total Lines | 285 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FunctionsExport often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FunctionsExport, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class FunctionsExport |
||
34 | { |
||
35 | /** |
||
36 | * Tidy up a gedcom record on export, for compatibility/portability. |
||
37 | * |
||
38 | * @param string $rec |
||
39 | * |
||
40 | * @return string |
||
41 | */ |
||
42 | public static function reformatRecord($rec) |
||
43 | { |
||
44 | global $WT_TREE; |
||
45 | |||
46 | $newrec = ''; |
||
47 | foreach (preg_split('/[\r\n]+/', $rec, -1, PREG_SPLIT_NO_EMPTY) as $line) { |
||
48 | // Split long lines |
||
49 | // The total length of a GEDCOM line, including level number, cross-reference number, |
||
50 | // tag, value, delimiters, and terminator, must not exceed 255 (wide) characters. |
||
51 | if (mb_strlen($line) > WT_GEDCOM_LINE_LENGTH) { |
||
52 | list($level, $tag) = explode(' ', $line, 3); |
||
53 | if ($tag != 'CONT' && $tag != 'CONC') { |
||
54 | $level++; |
||
55 | } |
||
56 | do { |
||
57 | // Split after $pos chars |
||
58 | $pos = WT_GEDCOM_LINE_LENGTH; |
||
59 | if ($WT_TREE->getPreference('WORD_WRAPPED_NOTES')) { |
||
60 | // Split on a space, and remove it (for compatibility with some desktop apps) |
||
61 | while ($pos && mb_substr($line, $pos - 1, 1) != ' ') { |
||
62 | --$pos; |
||
63 | } |
||
64 | if ($pos == strpos($line, ' ', 3) + 1) { |
||
65 | // No spaces in the data! Can’t split it :-( |
||
66 | break; |
||
67 | } else { |
||
68 | $newrec .= mb_substr($line, 0, $pos - 1) . WT_EOL; |
||
69 | $line = $level . ' CONC ' . mb_substr($line, $pos); |
||
70 | } |
||
71 | } else { |
||
72 | // Split on a non-space (standard gedcom behaviour) |
||
73 | while ($pos && mb_substr($line, $pos - 1, 1) == ' ') { |
||
74 | --$pos; |
||
75 | } |
||
76 | if ($pos == strpos($line, ' ', 3)) { |
||
77 | // No non-spaces in the data! Can’t split it :-( |
||
78 | break; |
||
79 | } |
||
80 | $newrec .= mb_substr($line, 0, $pos) . WT_EOL; |
||
81 | $line = $level . ' CONC ' . mb_substr($line, $pos); |
||
82 | } |
||
83 | } while (mb_strlen($line) > WT_GEDCOM_LINE_LENGTH); |
||
84 | } |
||
85 | $newrec .= $line . WT_EOL; |
||
86 | } |
||
87 | |||
88 | return $newrec; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Create a header for a (newly-created or already-imported) gedcom file. |
||
93 | * |
||
94 | * @param Tree $tree |
||
95 | * |
||
96 | * @return string |
||
97 | */ |
||
98 | public static function gedcomHeader(Tree $tree) |
||
99 | { |
||
100 | // Default values for a new header |
||
101 | $HEAD = "0 HEAD"; |
||
102 | $SOUR = "\n1 SOUR " . WT_WEBTREES . "\n2 NAME " . WT_WEBTREES . "\n2 VERS " . WT_VERSION; |
||
103 | $DEST = "\n1 DEST DISKETTE"; |
||
104 | $DATE = "\n1 DATE " . strtoupper(date("d M Y")) . "\n2 TIME " . date("H:i:s"); |
||
105 | $GEDC = "\n1 GEDC\n2 VERS 5.5.1\n2 FORM Lineage-Linked"; |
||
106 | $CHAR = "\n1 CHAR UTF-8"; |
||
107 | $FILE = "\n1 FILE " . $tree->getName(); |
||
108 | $LANG = ''; |
||
109 | $COPR = ''; |
||
110 | $SUBN = ''; |
||
111 | $SUBM = "\n1 SUBM @SUBM@\n0 @SUBM@ SUBM\n1 NAME " . Auth::user()->getUserName(); // The SUBM record is mandatory |
||
112 | |||
113 | // Preserve some values from the original header |
||
114 | $record = GedcomRecord::getInstance('HEAD', $tree); |
||
115 | $fact = $record->getFirstFact('LANG'); |
||
116 | if ($fact instanceof Fact) { |
||
117 | $LANG = $fact->getValue(); |
||
118 | } |
||
119 | $fact = $record->getFirstFact('SUBN'); |
||
120 | if ($fact instanceof Fact) { |
||
121 | $SUBN = $fact->getValue(); |
||
122 | } |
||
123 | $fact = $record->getFirstFact('COPR'); |
||
124 | if ($fact instanceof Fact) { |
||
125 | $COPR = $fact->getValue(); |
||
126 | } |
||
127 | // Link to actual SUBM/SUBN records, if they exist |
||
128 | $subn = |
||
129 | Database::prepare("SELECT o_id FROM `##other` WHERE o_type=? AND o_file=?") |
||
130 | ->execute(array('SUBN', $tree->getTreeId())) |
||
131 | ->fetchOne(); |
||
132 | if ($subn) { |
||
133 | $SUBN = "\n1 SUBN @{$subn}@"; |
||
134 | } |
||
135 | $subm = |
||
136 | Database::prepare("SELECT o_id FROM `##other` WHERE o_type=? AND o_file=?") |
||
137 | ->execute(array('SUBM', $tree->getTreeId())) |
||
138 | ->fetchOne(); |
||
139 | if ($subm) { |
||
140 | $SUBM = "\n1 SUBM @{$subm}@"; |
||
141 | } |
||
142 | |||
143 | return $HEAD . $SOUR . $DEST . $DATE . $GEDC . $CHAR . $FILE . $COPR . $LANG . $SUBN . $SUBM . "\n"; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Prepend the GEDCOM_MEDIA_PATH to media filenames. |
||
148 | * |
||
149 | * @param string $rec |
||
150 | * @param string $path |
||
151 | * |
||
152 | * @return string |
||
153 | */ |
||
154 | public static function convertMediaPath($rec, $path) |
||
155 | { |
||
156 | if ($path && preg_match('/\n1 FILE (.+)/', $rec, $match)) { |
||
157 | $old_file_name = $match[1]; |
||
158 | // Don’t modify external links |
||
159 | if (!preg_match('~^(https?|ftp):~', $old_file_name)) { |
||
160 | // Adding a windows path? Convert the slashes. |
||
161 | if (strpos($path, '\\') !== false) { |
||
162 | $new_file_name = preg_replace('~/+~', '\\', $old_file_name); |
||
163 | } else { |
||
164 | $new_file_name = $old_file_name; |
||
165 | } |
||
166 | // Path not present - add it. |
||
167 | if (strpos($new_file_name, $path) === false) { |
||
168 | $new_file_name = $path . $new_file_name; |
||
169 | } |
||
170 | $rec = str_replace("\n1 FILE " . $old_file_name, "\n1 FILE " . $new_file_name, $rec); |
||
171 | } |
||
172 | } |
||
173 | |||
174 | return $rec; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Export the database in GEDCOM format |
||
179 | * |
||
180 | * @param Tree $tree Which tree to export |
||
181 | * @param resource $gedout Handle to a writable stream |
||
182 | * @param string[] $exportOptions Export options are as follows: |
||
183 | * 'privatize': which Privacy rules apply? (none, visitor, user, manager) |
||
184 | * 'toANSI': should the output be produced in ISO-8859-1 instead of UTF-8? (yes, no) |
||
185 | * 'path': what constant should prefix all media file paths? (eg: media/ or c:\my pictures\my family |
||
186 | * 'slashes': what folder separators apply to media file paths? (forward, backward) |
||
187 | */ |
||
188 | public static function exportGedcom(Tree $tree, $gedout, $exportOptions) |
||
318 | } |
||
319 | } |
||
320 |