|
109
|
1 |
<?php |
|
|
2 |
header('Content-type: application/json'); |
|
|
3 |
|
|
|
4 |
require('connect.inc.php'); |
|
|
5 |
|
|
|
6 |
$periode = (isset($_REQUEST['period']) ? pg_escape_string($_REQUEST['period']) : NULL); |
|
|
7 |
$label = (isset($_REQUEST['label']) ? pg_escape_string($_REQUEST['label']) : NULL); |
|
110
|
8 |
$contentlist = (isset($_REQUEST['contentlist']) ? pg_escape_string($_REQUEST['contentlist']) : NULL); |
|
109
|
9 |
$max_tag_order = (isset($_REQUEST['mto']) ? pg_escape_string($_REQUEST['mto']) : 12); |
|
110
|
10 |
$content_count = (isset($_REQUEST['contentcount']) ? pg_escape_string($_REQUEST['contentcount']) : 12); |
|
109
|
11 |
$tag_count = (isset($_REQUEST['tagcount']) ? pg_escape_string($_REQUEST['tagcount']) : 30); |
|
|
12 |
$cont_count = 0; |
|
|
13 |
|
|
115
|
14 |
if ($label or $periode or gettype($contentlist) != "NULL") { |
|
109
|
15 |
$globalsql = "SELECT id, title, description, url FROM hdabo_datasheet E"; |
|
|
16 |
$globalids = array(); |
|
|
17 |
$globalfilters = array(); |
|
|
18 |
if ($periode) { |
|
110
|
19 |
$years = split(',',$periode); |
|
109
|
20 |
$start_year = $years[0]; |
|
|
21 |
$end_year = (count($years) > 1 ? $years[1] : $start_year); |
|
|
22 |
$delta = max( 1, floor( ($end_year - $start_year ) / 2 ) ); |
|
|
23 |
$minstart = $start_year - $delta; |
|
|
24 |
$maxend = $end_year + $delta; |
|
|
25 |
$rq = pg_query("SELECT tag_id FROM hdaviz_years WHERE end_year >= $start_year AND start_year <= $end_year AND end_year <= $maxend AND start_year >= $minstart"); |
|
|
26 |
$ids = array(); |
|
|
27 |
while($ligne = pg_fetch_row($rq)) { |
|
|
28 |
$ids[] = $ligne[0]; |
|
|
29 |
} |
|
|
30 |
pg_free_result($rq); |
|
|
31 |
$globalids = $ids; |
|
|
32 |
$globalfilters[] = "EXISTS (SELECT * FROM hdabo_taggedsheet F WHERE F.tag_id IN (" . join(",", $ids) . ") AND F.order <= $max_tag_order AND F.datasheet_id = E.id)"; |
|
|
33 |
} |
|
|
34 |
if ($label) { |
|
110
|
35 |
$lblist = split(',',$label); |
|
109
|
36 |
foreach ($lblist as $txtlbl) { |
|
|
37 |
$rq = pg_query("SELECT id FROM hdabo_tag WHERE label ILIKE '$txtlbl'"); |
|
|
38 |
$ids = array(); |
|
|
39 |
while($ligne = pg_fetch_row($rq)) { |
|
|
40 |
$ids[] = $ligne[0]; |
|
|
41 |
} |
|
|
42 |
pg_free_result($rq); |
|
|
43 |
$globalids = array_merge($globalids,$ids); |
|
|
44 |
$globalfilters[] = "EXISTS (SELECT * FROM hdabo_taggedsheet G WHERE G.tag_id IN (" . join(",", $ids) . ") AND G.order <= $max_tag_order AND G.datasheet_id = E.id)"; |
|
|
45 |
} |
|
|
46 |
} |
|
115
|
47 |
if (gettype($contentlist) != "NULL") { |
|
|
48 |
if ($contentlist) { |
|
|
49 |
$globalfilters[] = "id IN ($contentlist)"; |
|
|
50 |
} else { |
|
|
51 |
$globalfilters[] = "id IS NULL"; |
|
|
52 |
} |
|
110
|
53 |
} |
|
109
|
54 |
$globalsql = $globalsql.(count($globalfilters) ? " WHERE ".join($globalfilters," AND ") : "" ); |
|
115
|
55 |
if (!$label && !$periode && gettype($contentlist)=="NULL") { |
|
109
|
56 |
$globalsql = $globalsql." ORDER BY RANDOM()"; |
|
|
57 |
} |
|
|
58 |
$rq = pg_query($globalsql); |
|
|
59 |
$contenus = array(); |
|
|
60 |
while($ligne = pg_fetch_assoc($rq)) { |
|
|
61 |
$cont_count++; |
|
|
62 |
$ligne["score"] = 0; |
|
|
63 |
$ligne["tags"] = array(); |
|
|
64 |
$contenus[$ligne["id"]] = $ligne; |
|
|
65 |
} |
|
|
66 |
pg_free_result($rq); |
|
110
|
67 |
$rq = pg_query("SELECT label, isocode FROM hdaviz_cc"); |
|
|
68 |
$countries = array(); |
|
|
69 |
while($ligne = pg_fetch_row($rq)) { |
|
|
70 |
$countries[$ligne[0]] = array("isocode" => $ligne[1], "score" => 0); |
|
|
71 |
} |
|
|
72 |
pg_free_result($rq); |
|
115
|
73 |
$rq = pg_query("SELECT DISTINCT label FROM hdabo_tag WHERE category_id=5"); |
|
|
74 |
$disciplines = array(); |
|
|
75 |
while($ligne = pg_fetch_row($rq)) { |
|
|
76 |
$disciplines[$ligne[0]] = array("label" => $ligne[0], "score" => 0); |
|
|
77 |
} |
|
|
78 |
pg_free_result($rq); |
|
109
|
79 |
$tags = array(); |
|
|
80 |
$contentids = join(",",array_keys($contenus)); |
|
115
|
81 |
if ($contentids) { |
|
|
82 |
$rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN ($contentids) AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC"); |
|
|
83 |
while($ligne = pg_fetch_row($rq)) { |
|
|
84 |
$match_tag = in_array($ligne[1], $globalids); |
|
|
85 |
$contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3], "match" => $match_tag); |
|
|
86 |
$tagscore = 2*$max_tag_order-$ligne[3]; |
|
|
87 |
if (!isset($tags[$ligne[1]])) { |
|
|
88 |
$tags[$ligne[1]] = array("id" => $ligne[1], "label" => $ligne[2], "score" => 0); |
|
|
89 |
} |
|
|
90 |
$tags[$ligne[1]]["score"] += $tagscore; |
|
|
91 |
if ($match_tag) { |
|
|
92 |
$contenus[$ligne[0]]["score"] += $tagscore; |
|
|
93 |
$tags[$ligne[1]]["match"] = True; |
|
|
94 |
} |
|
|
95 |
if (isset($countries[$ligne[2]])) { |
|
|
96 |
$countries[$ligne[2]]["score"] += $tagscore; |
|
|
97 |
} |
|
|
98 |
if (isset($disciplines[$ligne[2]])) { |
|
|
99 |
$disciplines[$ligne[2]]["score"] += $tagscore; |
|
|
100 |
} |
|
109
|
101 |
} |
|
115
|
102 |
pg_free_result($rq); |
|
109
|
103 |
} |
|
|
104 |
function triscore($a, $b) { |
|
|
105 |
return $b["score"] - $a["score"]; |
|
|
106 |
} |
|
110
|
107 |
if (!$contentlist) { |
|
|
108 |
usort($contenus,"triscore"); |
|
|
109 |
$contenus = array_slice($contenus, 0, $content_count); |
|
|
110 |
} else { |
|
|
111 |
$contenus = array_values($contenus); |
|
|
112 |
} |
|
109
|
113 |
usort($tags,"triscore"); |
|
115
|
114 |
usort($disciplines,"triscore"); |
|
110
|
115 |
$countries = array_values($countries); |
|
109
|
116 |
$tags = array_slice($tags, 0, $tag_count); |
|
115
|
117 |
$disciplines = array_slice($disciplines, 0, 10); |
|
109
|
118 |
$years = array(); |
|
115
|
119 |
if ($contentids) { |
|
|
120 |
$rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - V.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet V WHERE U.tag_id = V.tag_id AND V.order <= $max_tag_order AND V.datasheet_id IN ($contentids) GROUP BY U.start_year, U.end_year"); |
|
|
121 |
while($ligne = pg_fetch_row($rq)) { |
|
|
122 |
foreach(range($ligne[0], $ligne[1]) as $year) { |
|
|
123 |
$years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 ); |
|
|
124 |
} |
|
109
|
125 |
} |
|
115
|
126 |
pg_free_result($rq); |
|
109
|
127 |
} |
|
|
128 |
} else { |
|
|
129 |
$rq = pg_query("SELECT id, title, description, url FROM hdabo_datasheet ORDER BY RANDOM() LIMIT $content_count"); |
|
|
130 |
while($ligne = pg_fetch_assoc($rq)) { |
|
|
131 |
$ligne["tags"] = array(); |
|
|
132 |
$contenus[$ligne["id"]] = $ligne; |
|
|
133 |
} |
|
|
134 |
$rq = pg_query("SELECT COUNT(*) FROM hdabo_datasheet"); |
|
|
135 |
$ligne = pg_fetch_row($rq); |
|
|
136 |
$cont_count = $ligne[0]; |
|
|
137 |
pg_free_result($rq); |
|
|
138 |
$rq = pg_query("SELECT L.datasheet_id, L.tag_id, M.label, L.order FROM hdabo_taggedsheet L, hdabo_tag M WHERE L.datasheet_id IN (".join(",",array_keys($contenus)).") AND L.tag_id = M.id AND L.order <= $max_tag_order ORDER BY L.order ASC"); |
|
|
139 |
while($ligne = pg_fetch_row($rq)) { |
|
|
140 |
$contenus[$ligne[0]]["tags"][] = array("id" => $ligne[1], "label" => $ligne[2], "order" => $ligne[3]); |
|
|
141 |
} |
|
|
142 |
pg_free_result($rq); |
|
|
143 |
$contenus = array_values($contenus); |
|
110
|
144 |
$rq = pg_query("SELECT hdabo_tag.id, label, SUM(".(2*$max_tag_order)." - hdabo_taggedsheet.order) score FROM hdabo_tag, hdabo_taggedsheet WHERE hdabo_tag.id = hdabo_taggedsheet.tag_id AND hdabo_taggedsheet.order <= $max_tag_order GROUP BY hdabo_tag.id, label ORDER BY score DESC LIMIT $tag_count"); |
|
109
|
145 |
$tags = pg_fetch_all($rq); |
|
|
146 |
pg_free_result($rq); |
|
110
|
147 |
$rq = pg_query("SELECT C.isocode, SUM(".(2*$max_tag_order)." - B.order) score FROM hdabo_tag A, hdabo_taggedsheet B, hdaviz_cc C WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.label = C.label GROUP BY C.isocode"); |
|
|
148 |
$countries = pg_fetch_all($rq); |
|
|
149 |
pg_free_result($rq); |
|
115
|
150 |
$rq = pg_query("SELECT A.label, SUM(".(2*$max_tag_order)." - B.order) score FROM hdabo_tag A, hdabo_taggedsheet B WHERE A.id = B.tag_id AND B.order <= $max_tag_order AND A.category_id=5 GROUP BY A.label ORDER BY score DESC LIMIT 10"); |
|
|
151 |
$disciplines = pg_fetch_all($rq); |
|
|
152 |
pg_free_result($rq); |
|
109
|
153 |
$rq = pg_query("SELECT U.start_year, U.end_year, SUM(".(2*$max_tag_order)." - hdabo_taggedsheet.order)/(U.end_year + 1 - U.start_year) score FROM hdaviz_years U, hdabo_taggedsheet WHERE U.tag_id = hdabo_taggedsheet.tag_id AND hdabo_taggedsheet.order <= $max_tag_order GROUP BY U.start_year, U.end_year"); |
|
|
154 |
$years = array(); |
|
|
155 |
while($ligne = pg_fetch_row($rq)) { |
|
|
156 |
foreach(range($ligne[0], $ligne[1]) as $year) { |
|
|
157 |
$years[$year] = $ligne[2] + ( isset($years[$year]) ? $years[$year] : 0 ); |
|
|
158 |
} |
|
|
159 |
} |
|
|
160 |
pg_free_result($rq); |
|
|
161 |
} |
|
|
162 |
ksort($years); |
|
|
163 |
$yearchange = array(); |
|
|
164 |
foreach($years as $year => $score) { |
|
|
165 |
if ($year < 2011) { |
|
|
166 |
if (!isset($years[$year-1]) || $years[$year-1] != $score) { |
|
|
167 |
$yearchange[] = array('year' => $year, 'score' => $score); |
|
|
168 |
} |
|
|
169 |
if (!isset($years[$year+1]) && ($year != -1)) { |
|
|
170 |
$yearchange[] = array('year' => $year + 1, 'score' => 0); |
|
|
171 |
} |
|
|
172 |
} |
|
|
173 |
} |
|
115
|
174 |
$output = array("count" => $cont_count, "contents" => $contenus, "tags" => $tags, "sparkline" => $yearchange, "countries" => $countries, "disciplines" => $disciplines); |
|
109
|
175 |
echo json_encode($output); |
|
|
176 |
?> |