@@ -25,7 +25,8 @@ my $MIRRORCACHE_MAX_PATH = int($ENV{MIRRORCACHE_MAX_PATH} // 512);
25
25
26
26
sub mirrors_query {
27
27
my (
28
- $self , $country , $region , $folder_id , $file_id , $project_id ,
28
+ $self , $country , $region , $realfolder_id , $folder_id , $file_id ,
29
+ $realproject_id , $project_id ,
29
30
$capability , $ipv , $lat , $lng , $avoid_countries ,
30
31
$limit , $avoid_region , $schemastrict , $ipvstrict , $vpn
31
32
) = @_ ;
@@ -36,6 +37,9 @@ sub mirrors_query {
36
37
$lat = 0 unless $lat ;
37
38
$lng = 0 unless $lng ;
38
39
40
+ $realproject_id = $project_id unless $realproject_id ;
41
+ $realfolder_id = ($folder_id // 0) unless $realfolder_id ;
42
+
39
43
my $rsource = $self -> result_source;
40
44
my $schema = $rsource -> schema;
41
45
my $dbh = $schema -> storage-> dbh;
@@ -96,15 +100,15 @@ sub mirrors_query {
96
100
my $join_server_project = " " ;
97
101
my $condition_our_regions = $schema -> condition_our_regions;
98
102
my $condition_server_project = " " ;
99
- if ($project_id ) {
100
- $join_server_project = " left join server_project sp on (project_id, sp.server_id) = ($project_id ,s.id ) and state < 1" ;
103
+ if ($project_id and $realfolder_id == $folder_id ) {
104
+ $join_server_project = " left join server_project sp on sp.server_id = s.id and project_id in ($project_id ,$realproject_id ) and state < 1" ;
101
105
$condition_server_project = " and sp.server_id IS NULL" ;
102
106
}
103
107
104
108
my $join_file_cond = " fl.folder_id = fd.folder_id" ;
105
109
# license.tar* and info.xml* might be kept with the same name through updates, so timestamp on them is unreliable in mirrorlist for folders
106
110
my $file_dt = " , max(case when fdf.file_id is null and fl.name ~ '[0-9]' and fl.name not like '%license .tar.%' and fl.name not like '%info .xml.%' then fl.mtime else null end) as mtime" ;
107
- my $group_by = " group by s.id, s.hostname, s.hostname_vpn, s.urldir, s.region, s.country, s.lat, s.lng, s.score" ;
111
+ my $group_by = " group by s.id, s.hostname, s.hostname_vpn, s.urldir, s.region, s.country, s.lat, s.lng, s.score, fd.folder_id " ;
108
112
109
113
if ($file_id ) {
110
114
$join_file_cond = " fl.id = ?" ;
@@ -122,6 +126,7 @@ mtime,
122
126
dist,
123
127
case $weight_country_case when region $avoid_region = '$region ' then 1 else 0 end rating_country,
124
128
score, country, region, lat, lng,
129
+ realfolderscore,
125
130
support_scheme,
126
131
rating_scheme,
127
132
support_ipv,
@@ -136,22 +141,22 @@ case when $lat=0 and $lng=0 then 0
136
141
else
137
142
( 6371 * acos( cos( radians($lat ) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng ) ) + sin( radians($lat ) ) * sin( radians( lat ) ) ) )
138
143
end as dist,
139
- s.country, s.region, s.score,
144
+ s.country, s.region, s.score, case when f.id = $realfolder_id then 0 else 1 end realfolderscore,
140
145
CASE WHEN COALESCE(stability_scheme.rating, 0) > 0 OR COALESCE(stability_schemex.rating, 0) = 0 THEN 1 ELSE 0 END AS support_scheme, -- we show here 0 only when opposite scheme is supported
141
146
CASE WHEN COALESCE(stability_scheme.rating, 0) > 0 OR COALESCE(stability_schemex.rating, 0) = 0 THEN ( scf.capability is NULL AND COALESCE(scd.enabled, 't') = 't' ) ELSE ( scf2.capability is NULL AND COALESCE(scd2.enabled, 't') = 't' ) END AS not_disabled,
142
147
CASE WHEN COALESCE(stability_scheme.rating, 0) > 0 THEN stability_scheme.rating WHEN COALESCE(stability_schemex.rating, 0) > 0 THEN stability_schemex.rating ELSE 0 END AS rating_scheme,
143
148
CASE WHEN COALESCE(stability_ipv.rating, 0) > 0 THEN 1 ELSE 0 END AS support_ipv,
144
149
CASE WHEN COALESCE(stability_ipv.rating, 0) > 0 THEN stability_ipv.rating WHEN COALESCE(stability_ipvx.rating, 0) > 0 THEN stability_ipvx.rating ELSE 0 END AS rating_ipv
145
150
from (
146
- select s.id, s.hostname, s.hostname_vpn, s.urldir, s.region, s.country, s.lat, s.lng, s.score $file_dt
151
+ select s.id, s.hostname, s.hostname_vpn, s.urldir, s.region, s.country, s.lat, s.lng, s.score $file_dt , fd.folder_id
147
152
from folder_diff fd
148
153
join file fl on $join_file_cond
149
154
join folder_diff_server fds on fd.id = fds.folder_diff_id and date_trunc('second', fl.dt) <= fds.dt
150
155
join server s on fds.server_id = s.id and s.enabled $country_condition $condition_our_regions
151
156
left join server_capability_declaration scd on s.id = scd.server_id and scd.capability = 'country'
152
157
left join folder_diff_file fdf on fdf.file_id = fl.id and fdf.folder_diff_id = fd.id
153
158
$join_server_project
154
- where fd.folder_id = ? and fdf.file_id is NULL $condition_server_project
159
+ where fd.folder_id in (?,?) and coalesce(fd.realfolder_id, ?) = ? and ( fdf.file_id is NULL and fl.folder_id = ?) $condition_server_project
155
160
and ( -- here mirrors may be declared to handle only specific countries
156
161
scd.server_id is null
157
162
or
@@ -163,7 +168,7 @@ from (
163
168
)
164
169
$group_by
165
170
) s
166
- join folder f on f.id = ?
171
+ join folder f on f.id = s.folder_id
167
172
left join server_capability_declaration scd on s.id = scd.server_id and scd.capability = '$capability ' and NOT scd.enabled
168
173
left join server_capability_force scf on s.id = scf.server_id and scf.capability = '$capability '
169
174
left join server_capability_declaration scd2 on s.id = scd2.server_id and scd.capability = '$ipv ' and NOT scd.enabled
@@ -174,10 +179,10 @@ left join server_stability stability_ipv on s.id = stability_ipv.server_id
174
179
left join server_stability stability_ipvx on s.id = stability_ipvx.server_id and stability_ipvx.capability = '$ipvx '
175
180
) x
176
181
WHERE not_disabled $extra
177
- order by rating_country desc, (dist/100)::int, support_scheme desc, rating_scheme desc, support_ipv desc, rating_ipv desc, score, random()
182
+ order by rating_country desc, (dist/100)::int, support_scheme desc, rating_scheme desc, support_ipv desc, rating_ipv desc, score, realfolderscore, random()
178
183
limit $limit1
179
184
) xx
180
- order by support_scheme desc, rating_scheme desc, support_ipv desc, rating_ipv desc, rating_country desc, (dist/100)::int, score, random()
185
+ order by support_scheme desc, rating_scheme desc, support_ipv desc, rating_ipv desc, rating_country desc, (dist/100)::int, score, realfolderscore, random()
181
186
limit $limit ;
182
187
END_SQL
183
188
@@ -189,9 +194,9 @@ END_SQL
189
194
my $prep = $dbh -> prepare($sql );
190
195
191
196
if ($file_id ) {
192
- $prep -> execute($file_id , @country_params , $folder_id , $country , $country , $country , $folder_id );
197
+ $prep -> execute($file_id , @country_params , $realfolder_id , $ folder_id , $realfolder_id , $realfolder_id , $realfolder_id , $ country , $country , $country );
193
198
} else {
194
- $prep -> execute(@country_params , $folder_id , $country , $country , $country , $folder_id );
199
+ $prep -> execute(@country_params , $realfolder_id , $ folder_id , $realfolder_id , $realfolder_id , $realfolder_id , $ country , $country , $country );
195
200
}
196
201
my $server_arrayref = $dbh -> selectall_arrayref($prep , { Slice => {} });
197
202
return $server_arrayref ;
0 commit comments