sort_sources.php (2328B)
1 <!--
2 SPDX-License-Identifier: AGPL-3.0-or-later
3 SPDX-FileCopyrightText: 2025 JayVii <jayvii+kontra[AT]posteo[DOT]de>
4 -->
5 <?php
6
7 // Load news sources file
8 $config = json_decode(
9 file_get_contents("./news-sources.json"),
10 true
11 );
12
13 /* sort each entry's arrays alphabetically */
14 foreach (array_keys($config["sources"]) as $key) {
15
16 /* sort publishers (should be only 1 anyways) */
17 asort($config["sources"][$key]["publisher"]);
18
19 /* sort regions */
20 asort($config["sources"][$key]["regions"]);
21
22 /* sort languages */
23 asort($config["sources"][$key]["languages"]);
24
25 /* sort access (should be only 1 anyways) */
26 asort($config["sources"][$key]["access"]);
27
28 /* sort medium (should be only 1 anyways) */
29 asort($config["sources"][$key]["medium"]);
30
31 /* sort topics */
32 asort($config["sources"][$key]["topics"]);
33 }
34
35 /* sort regions by alphabet */
36 ksort($config["regions"]);
37
38 /* sort languages by alphabet */
39 ksort($config["languages"]);
40
41 /* sort access by alphabet */
42 ksort($config["access"]);
43
44 /* sort medium by alphabet */
45 ksort($config["medium"]);
46
47 /* sort topics by alphabet */
48 ksort($config["topics"]);
49
50 /* sort publisher by alphabet */
51 ksort($config["publisher"]);
52
53 /* initial sorting of sources: by their title */
54 $titles = array();
55 foreach ($config["sources"] as $key => $source) {
56 $titles[$key] =$source["title"];
57 }
58 asort($titles); // sort alphabetically
59
60 /* sort source keys alphabetically by their topics */
61 $sources_keys = array();
62 foreach ($config["topics"] as $topic) {
63
64 foreach (array_keys($titles) as $key) {
65
66 $source = $config["sources"][$key];
67
68 /* if the current topic appears somewhere in the source's topics,
69 add it to the list! */
70 if (array_search($topic["id"], $source["topics"]) !== false) {
71 array_push($sources_keys, $key);
72 }
73 }
74 }
75
76 /* remove double entries (only use first occurence!) */
77 $sources_keys = array_unique($sources_keys);
78
79 /* sort sources array by the sources_keys */
80 $sources = array();
81 foreach ($sources_keys as $key) {
82 $sources[$key] = $config["sources"][$key];
83 }
84
85 /* override sources array in config object */
86 $config["sources"] = $sources;
87
88 /* re-write news-sources.json file */
89 file_put_contents(
90 "./news-sources.json",
91 json_encode($config, JSON_PRETTY_PRINT)
92 );
93
94
95 ?>
96