Completed
Push — master ( 8b905c...fce320 )
by cam
04:38
created

installer.php ➔ spip_version_compare()   B

Complexity

Conditions 10
Paths 44

Size

Total Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
nc 44
nop 3
dl 0
loc 30
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/***************************************************************************\
4
 *  SPIP, Système de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright © avec tendresse depuis 2001                                 *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Rivière, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribué sous licence GNU/GPL.     *
10
 *  Pour plus de détails voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
/**
14
 * Gestion de l'installation des plugins
15
 *
16
 * @package SPIP\Core\Plugins
17
 **/
18
19
20
if (!defined('_ECRIRE_INC_VERSION')) {
21
	return;
22
}
23
24
/**
25
 * Installe ou retire un plugin
26
 *
27
 * Permet d'installer ou retirer un plugin en incluant les fichiers
28
 * associés et en lançant les fonctions spécifiques.
29
 *
30
 * 1. d'abord sur l'argument `test`,
31
 * 2. ensuite sur l'action demandée si le test repond `false`
32
 * 3. enfin sur l'argument `test` à nouveau.
33
 *
34
 * L'index `install_test` du tableau résultat est un tableau formé :
35
 *
36
 *  - du résultat 3
37
 *  - des echo de l'étape 2
38
 *
39
 * @note
40
 *     La fonction quitte (retourne false) si le plugin
41
 *     n'a pas de version d'installation définie
42
 *     (information `schema` dans le paquet.xml)
43
 *
44
 * @param string $plug
45
 *     Nom du plugin
46
 * @param string $action
47
 *     Nom de l'action (install|uninstall)
48
 * @param string $dir_type
49
 *     Répertoire du plugin
50
 * @return array|bool
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array<boolean|string>>|boolean.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
51
 *     - False si le plugin n'a pas d'installation,
52
 *     - true si déjà installé,
53
 *     - le tableau de get_infos sinon
54
 */
55
function plugins_installer_dist($plug, $action, $dir_type = '_DIR_PLUGINS') {
56
57
	// Charger les informations du XML du plugin et vérification de l'existence d'une installation
58
	$get_infos = charger_fonction('get_infos', 'plugins');
59
	$infos = $get_infos($plug, false, constant($dir_type));
60
	if (!isset($infos['install']) or !$infos['install']) {
61
		return false;
62
	}
63
64
	// Passer en chemin absolu si possible, c'est plus efficace
65
	$dir = str_replace('_DIR_', '_ROOT_', $dir_type);
66
	if (!defined($dir)) {
67
		$dir = $dir_type;
68
	}
69
	$dir = constant($dir);
70
	foreach ($infos['install'] as $file) {
71
		$file = $dir . $plug . "/" . trim($file);
72
		if (file_exists($file)) {
73
			include_once($file);
74
		}
75
	}
76
77
	// Détermination de la table meta et du nom de la meta plugin
78
	$table = 'meta';
79
	if (isset($infos['meta']) and ($infos['meta'] !== 'meta')) {
80
		$table = $infos['meta'];
81
		// S'assurer que les metas de la table spécifique sont bien accessibles dans la globale
82
		lire_metas($table);
83
	}
84
	$nom_meta = $infos['prefix'] . '_base_version';
85
86
	// Détermination de la fonction à appeler et de ses arguments
87
	$f = $infos['prefix'] . "_install";
88
	if (!function_exists($f)) {
89
		$f = isset($infos['schema']) ? 'spip_plugin_install' : '';
90
		$arg = $infos;
91
		// On passe la table et la meta pour éviter de les recalculer dans la fonction appelée
92
		$arg['meta'] = $table;
93
		$arg['nom_meta'] = $nom_meta;
94
	} else {
95
		// Ancienne méthode d'installation - TODO à supprimer à terme
96
		// stupide: info deja dans le nom
97
		$arg = $infos['prefix'];
98
	}
99
	$version = isset($infos['schema']) ? $infos['schema'] : '';
100
101
	if (!$f) {
102
		// installation sans operation particuliere
103
		$infos['install_test'] = array(true, '');
104
		return $infos;
105
	}
106
107
	// Tester si l'action demandée est nécessaire ou pas.
108
	$test = $f('test', $arg, $version);
109
	if ($action == 'uninstall') {
110
		$test = !$test;
111
	}
112
	// Si deja fait, on ne fait rien et on ne dit rien
113
	if ($test) {
114
		return true;
115
	}
116
117
	// Si install et que l'on a la meta d'installation, c'est un upgrade. On le consigne dans $infos
118
	// pour renvoyer le bon message en retour de la fonction.
119
	if ($action == 'install' && !empty($GLOBALS[$table][$nom_meta])) {
120
		$infos['upgrade'] = true;
121
	}
122
123
	// executer l'installation ou l'inverse
124
	// et renvoyer la trace (mais il faudrait passer en AJAX plutot)
125
	ob_start();
126
	$f($action, $arg, $version);
127
	$aff = ob_get_contents();
128
	ob_end_clean();
129
130
	// vider le cache des descriptions de tables a chaque (de)installation
131
	$trouver_table = charger_fonction('trouver_table', 'base');
132
	$trouver_table('');
133
	$infos['install_test'] = array($f('test', $arg, $version), $aff);
134
135
	// Si la table meta n'est pas spip_meta et qu'on est dans la première installation du plugin
136
	// on force la création du fichier cache à la date du moment.
137
	// On relit les metas de la table pour être sur que la globale soit à jour pour touch_meta.
138
	if (
139
		($table !== 'meta')
140
		and ($action == 'install')
141
		and empty($infos['upgrade'])
142
	) {
143
		touch_meta(false, $table);
144
	}
145
146
	return $infos;
147
}
148
149
/**
150
 * Fonction standard utilisée par defaut pour install/desinstall
151
 *
152
 * @param string $action
153
 *     Nom de l'action (install|uninstall)
154
 * @param array  $infos
155
 *     Tableau des informations du XML du plugin complété par le nom et la table meta
156
 * @param string $version_cible
157
 *     Référence de la version du schéma de données cible
158
 *
159
 * @return bool|void
160
 */
161
function spip_plugin_install($action, $infos, $version_cible) {
162
	$nom_meta = $infos['nom_meta'];
163
	$table = $infos['meta'];
164
	switch ($action) {
165
		case 'test':
166
			return (isset($GLOBALS[$table])
167
				and isset($GLOBALS[$table][$nom_meta])
168
				and spip_version_compare($GLOBALS[$table][$nom_meta], $version_cible, '>='));
169
			break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
170
		case 'install':
171
			if (function_exists($upgrade = $infos['prefix'] . '_upgrade')) {
172
				$upgrade($nom_meta, $version_cible, $table);
173
			}
174
			break;
175
		case 'uninstall':
176
			if (function_exists($vider_tables = $infos['prefix'] . '_vider_tables')) {
177
				$vider_tables($nom_meta, $table);
178
			}
179
			break;
180
	}
181
}
182
183
184
185
/**
186
 * Retourne un tableau des plugins activés sur le site
187
 *
188
 * Retourne la meta `plugin` désérialisée.
189
 * Chaque élément du tableau est lui-même un tableau contenant
190
 * les détails du plugin en question : répertoire et version.
191
 *
192
 * @note
193
 *   Si le contenu de la meta n’est pas un tableau, cette fonction transforme
194
 *   l’ancien format en tableau sérialisé pour être conforme au nouveau fonctionnement (SPIP >= 1.9.2)
195
 *
196
 * @return array Tableau des plugins actifs
197
 **/
198
function liste_plugin_actifs() {
199
	$liste = isset($GLOBALS['meta']['plugin']) ? $GLOBALS['meta']['plugin'] : '';
200
	if (!$liste) {
201
		return array();
202
	}
203
	if (!is_array($liste = unserialize($liste))) {
204
		// compatibilite pre 1.9.2, mettre a jour la meta
205
		spip_log("MAJ meta plugin vieille version : $liste", "plugin");
206
		$new = true;
207
		list(, $liste) = liste_plugin_valides(explode(",", $liste));
208
	} else {
209
		$new = false;
210
		// compat au moment d'une migration depuis version anterieure
211
		// si pas de dir_type, alors c'est _DIR_PLUGINS
212
		foreach ($liste as $prefix => $infos) {
213
			if (!isset($infos['dir_type'])) {
214
				$liste[$prefix]['dir_type'] = "_DIR_PLUGINS";
215
				$new = true;
216
			}
217
		}
218
	}
219
	if ($new) {
220
		ecrire_meta('plugin', serialize($liste));
221
	}
222
223
	return $liste;
224
}
225