1 | <?php |
---|
2 | /** |
---|
3 | * Définit les actions du plugin Commits de projet |
---|
4 | * |
---|
5 | * @plugin Commits de projet |
---|
6 | * @copyright 2014-2017 |
---|
7 | * @author Teddy Payet |
---|
8 | * @licence GNU/GPL |
---|
9 | * @package SPIP\RSSCommits\Action |
---|
10 | */ |
---|
11 | |
---|
12 | if (!defined('_ECRIRE_INC_VERSION')) { |
---|
13 | return; |
---|
14 | } |
---|
15 | |
---|
16 | /** |
---|
17 | * Mise à jour des commits d'un projet |
---|
18 | * |
---|
19 | * @param null|int $id |
---|
20 | * `id` : son identifiant. En absence de `id` utilise l'argument de l'action sécurisée. |
---|
21 | * |
---|
22 | * @return bool |
---|
23 | */ |
---|
24 | function action_maj_commits_projet_dist($id = null) { |
---|
25 | if (is_null($id)) { |
---|
26 | $securiser_action = charger_fonction('securiser_action', 'inc'); |
---|
27 | $id = $securiser_action(); |
---|
28 | } |
---|
29 | |
---|
30 | $id_projet = intval($id); |
---|
31 | $import_auto = lire_config('rss_commits/import_auto', 'non'); |
---|
32 | |
---|
33 | if ($id_projet and $import_auto == 'oui') { |
---|
34 | include_spip('rss_commits_fonctions'); |
---|
35 | include_spip('base/abstract_sql'); |
---|
36 | $log = array(); |
---|
37 | $log[] = "\n----------\n" |
---|
38 | . date_format(date_create(), 'Y-m-d H:i:s') |
---|
39 | . ' : on lance ' |
---|
40 | . __FUNCTION__ |
---|
41 | . ' pour le projet n#' |
---|
42 | . $id_projet; |
---|
43 | $commits_anciens = array(); |
---|
44 | $commits_nouveaux = array(); |
---|
45 | $commits_en_bdd = sql_allfetsel( |
---|
46 | 'id_projet,url_revision', |
---|
47 | 'spip_commits', |
---|
48 | "url_revision !='' AND id_projet=$id_projet" |
---|
49 | ); |
---|
50 | if (count($commits_en_bdd) > 0) { |
---|
51 | foreach ($commits_en_bdd as $key => $value) { |
---|
52 | $commits_anciens[] = $value['id_projet'] . '|' . $value['url_revision']; |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | $commits = lister_rss_commits($id_projet, false); |
---|
57 | spip_log(print_r($commits, true), 'rss_commits'); |
---|
58 | if (count($commits) > 0) { |
---|
59 | foreach ($commits as $key => $value) { |
---|
60 | if (!in_array($value['id_projet'] . '|' . $value['url_revision'], $commits_anciens)) { |
---|
61 | // On stocke dans le tableau, les nouveaux commits qui doivent être ajoutés en BDD. |
---|
62 | $commits_nouveaux[] = $value; |
---|
63 | $log[] = 'Le commit ' |
---|
64 | . $value['url_revision'] |
---|
65 | . ' va être enregistré pour le projet n#' |
---|
66 | . $value['id_projet']; |
---|
67 | } else { |
---|
68 | $log[] = 'Le commit ' |
---|
69 | . $value['url_revision'] |
---|
70 | . ' est déjà enregistré pour le projet n#' |
---|
71 | . $value['id_projet']; |
---|
72 | } |
---|
73 | } |
---|
74 | // On insère par lot les nouveaux commits pour éviter un débordement de mémoire |
---|
75 | // cf. https://programmer.spip.net/sql_insertq_multi,591 |
---|
76 | if (count($commits_nouveaux) > 0) { |
---|
77 | sql_insertq_multi('spip_commits', $commits_nouveaux); |
---|
78 | } else { |
---|
79 | $log[] = 'Pas d\'ajout de nouveaux commits dans la BDD'; |
---|
80 | } |
---|
81 | } else { |
---|
82 | $log[] = 'Il n\'y a pas de nouveaux commits'; |
---|
83 | } |
---|
84 | $log[] = date_format(date_create(), 'Y-m-d H:i:s') |
---|
85 | . ' : ' |
---|
86 | . __FUNCTION__ |
---|
87 | . ' a fini son travail' |
---|
88 | . ' pour le projet n#' |
---|
89 | . $id_projet |
---|
90 | . "\n----------\n"; |
---|
91 | |
---|
92 | spip_log(implode("\n", $log), 'rss_commits'); |
---|
93 | |
---|
94 | } else { |
---|
95 | if ($import_auto == 'non') { |
---|
96 | spip_log(__FUNCTION__ . " / L'import automatique est désactivée.", 'rss_commits'); |
---|
97 | } else { |
---|
98 | spip_log(__FUNCTION__ . " / $id pas compris", 'rss_commits'); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | return true; |
---|
103 | } |
---|
104 | |
---|