-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathSortCallNumber.php
More file actions
231 lines (216 loc) · 8.75 KB
/
Copy pathSortCallNumber.php
File metadata and controls
231 lines (216 loc) · 8.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/*********************************************************************
* SortLC Takes in two LC Call #'s, Normalizes, then sorts them
* Can use usort or uasort to sort arrays based on the call number
*********************************************************************/
function SortLC($right, $left)
{
$right = NormalizeLC($right);
$left = NormalizeLC($left);
return (strcmp($right, $left));
} // end SortLC
/*********************************************************************
/*********************************************************************
* SortLCObject Takes in two Obects contaning LC Call # elements
* defined as call_number, normalizes, then sorts them
* Can use usort or uasort to sort arrays based on the call number
*********************************************************************/
function SortLCObject($right, $left)
{
$right_cn = $right["call_number"] ?? "";
$left_cn = $left["call_number"] ?? "";
$right_norm = NormalizeLC($right_cn);
$left_norm = NormalizeLC($left_cn);
return (strcmp($right_norm, $left_norm));
} // end SortLC
/*********************************************************************
* NormalizeLC
* Normalizes LC for sorting
*********************************************************************/
function NormalizeLC($lc_call_no_orig)
{
/*
User defined setting: set problems to top to sort unparsable
call numbers to the top of the list; false to sort them to the
bottom.
*/
$problems_to_top = "true";
if ($problems_to_top == "true") {
$unparsable = " ";
} else {
$unparsable = "~";
}
//Convert all alpha to uppercase
$lc_call_no = strtoupper($lc_call_no_orig);
// define special trimmings that indicate integer
$integer_markers = array("C.", "BD.", "DISC", "DISK", "NO.", "PT.", "T.", "v.", "V.", "VOL.");
foreach ($integer_markers as $mark) {
$mark = str_replace(".", "\.", $mark);
$lc_call_no = preg_replace("/$mark(\d+)/", "$mark$1;", $lc_call_no);
} // end foreach int marker
// Remove any inital white space
$lc_call_no = preg_replace("/\s*/", "", $lc_call_no);
if (preg_match("/^([A-Z]{1,3})\s*(\d+)\s*\.*(\d*)\s*\.*\s*([A-Z]*)(\d*)\s*([A-Z]*)(\d*)\s*(.*)$/", $lc_call_no, $m)) {
$initial_letters = $m[1];
$class_number = $m[2];
$decimal_number = $m[3];
$cutter_1_letter = $m[4];
$cutter_1_number = $m[5];
$cutter_2_letter = $m[6];
$cutter_2_number = $m[7];
$the_trimmings = $m[8];
} //end if call number match
else {
return ($unparsable);
} // return extreme answer if not a call number
if ($cutter_2_letter && !($cutter_2_number)) {
$the_trimmings = $cutter_2_letter . $the_trimmings;
$cutter_2_letter = '';
}
/* TESTING NEW SECTION TO HANDLE VOLUME & PART NUMBERS */
foreach ($integer_markers as $mark) {
if (preg_match("/(.*)($mark)(\d+)(.*)/", $the_trimmings, $m)) {
$trim_start = $m[1];
$int_mark = $m[2];
$int_no = $m[3];
$trim_rest = $m[4];
$int_no = sprintf("%5s", $int_no);
$the_trimmings = $trim_start . $int_mark . $int_no . $trim_rest;
} // end if markers in the trimmings
} // end foreach integer marker
/* END NEW SECTION */
if ($class_number) {
$class_number = sprintf("%5s", $class_number);
}
$decimal_number = sprintf("%-12s", $decimal_number);
if ($cutter_1_number) {
$cutter_1_number = " $cutter_1_number";
}
if ($cutter_2_letter) {
$cutter_2_letter = " $cutter_2_letter";
}
if ($cutter_2_number) {
$cutter_2_number = " $cutter_2_number";
}
if ($the_trimmings) {
$the_trimmings = preg_replace("/(\.)(\d)/", "$1 $2", $the_trimmings);
$the_trimmings = preg_replace("/(\d)\s*-\s*(\d)/", "$1-$2", $the_trimmings);
// $the_trimmings =~ s/(\d+)/sprintf("%5s", $1)/ge;
$the_trimmings = " $the_trimmings";
}
$normalized = "$initial_letters" . "$class_number"
. "$decimal_number" . "$cutter_1_letter"
. "$cutter_1_number" . "$cutter_2_letter"
. "$cutter_2_number" . "$the_trimmings";
return ("$normalized");
} // end NormalizeLC
//an adaptation of Koha's Dewey sort routine
//GPL info goes here
//problem call numbers
/*
709.04 M453
704.94978 S727
759.06 E96
759.1H766
759.1N
*/
//759.06 E96 should display as 759_060000000000000_E96
//$callNum = '759.06 E96';
/*********************************************************************
* SortDeweyObject Takes in two Obects contaning Dewey Call # elements
* defined as call_number, normalizes, then sorts them
* Can use usort or uasort to sort arrays based on the call number
*********************************************************************/
/*********************************************************************
* SortDewey Takes in two Dewey Call #'s, Normalizes, then sorts them
* Can use usort or uasort to sort arrays based on the call number
*********************************************************************/
function SortDewey($right, $left)
{
$right = normalizeDewey($right);
$left = normalizeDewey($left);
return (strcmp($right, $left));
} // end SortLC
/*********************************************************************/
function SortDeweyObject($right, $left)
{
$right_cn = $right["call_number"] ?? "";
$left_cn = $left["call_number"] ?? "";
$right_norm = normalizeDewey($right_cn);
$left_norm = normalizeDewey($left_cn);
return (strcmp($right_norm, $left_norm));
} // end SortLC
function normalizeDewey($callNum)
{
//Insert ! when any letter comes after number (case-insensitive)
$init = preg_replace('/([0-9])(?=[a-zA-Z])/', '$1!', $callNum);
//make all characters lowercase... sort works better this way for dewey...
$init = strtolower($init);
//get rid of leading whitespace
$init = preg_replace('/^\s+/', '', $init);
//get rid of extra whitespace at end of string
$init = preg_replace('/\s+$/', '', $init);
//get rid of at end of string
$init = preg_replace('/\&/', '', $init);
//remove any slashes
$init = preg_replace('/\//', '', $init);
//remove any backslashes
$init = stripslashes($init);
// replace newline characters
$init = preg_replace('/\n/', '', $init);
//set digit group count
$digit_group_count = 0;
//declare first digit group index variable
$first_digit_group_idx = null;
//split string into tokens by ., :, or space
$tokens = preg_split('/[\.:\s]+/', $init);
//loop through the tokens
for ($i = 0; $i < sizeof($tokens); $i++) {
//if the token begins and ends with digits
if (preg_match("/^\d+$/", $tokens[$i])) {
//increment the number of digit groups
$digit_group_count++;
//if it's the first one, store its index in first_digit_group_idx
if (1 == $digit_group_count) {
$first_digit_group_idx = $i;
}
//if there is a second group of digits, expand it to 15 places, adding 0s
if (2 == $digit_group_count) {
if ($i - $first_digit_group_idx == 1) {
$tokens[$i] = str_pad($tokens[$i], 15, "0", STR_PAD_RIGHT);
//$tokens[$i] =~ tr/ /0/;
} else {
$tokens[$first_digit_group_idx] .= '_000000000000000';
}
}
}
}
if (1 == $digit_group_count) {
$tokens[$first_digit_group_idx] .= '_000000000000000';
}
// Pad the numeric portion of cutter tokens (already lowercased, e.g. "n857" -> "n857000000000000")
// so that cutter numbers are treated as decimal fractions.
// Without this, "n8576" would sort before "n857" because the underscore
// separator following "n857" has a higher ASCII value than the digit "6".
$token_count = count($tokens);
for ($i = 0; $i < $token_count; $i++) {
if (preg_match('/^([a-z!]+)(\d+)(.*)$/', $tokens[$i], $m)) {
$tokens[$i] = $m[1] . str_pad($m[2], 15, "0", STR_PAD_RIGHT) . $m[3];
}
}
// Left-pad remaining purely numeric tokens (e.g. volume numbers: t.1, t.10)
// so that they sort numerically in string comparisons.
// Skip the first digit group (the Dewey class number) — it's already handled
// by the digit_group_count logic above and must not be reformatted.
$token_count2 = count($tokens);
for ($i = 0; $i < $token_count2; $i++) {
if (isset($first_digit_group_idx) && $i === $first_digit_group_idx)
continue;
if (preg_match('/^\d+$/', $tokens[$i]) && strlen($tokens[$i]) < 10) {
$tokens[$i] = str_pad($tokens[$i], 10, "0", STR_PAD_LEFT);
}
}
$key = implode("_", $tokens);
return $key;
}
?>