Completed
Push — spip-3.0 ( 5d8b58 )
by cam
53:01 queued 42:30
created

editer_article.php ➔ article_instituer()   F

Complexity

Conditions 26
Paths 840

Size

Total Lines 108
Code Lines 62

Duplication

Lines 29
Ratio 26.85 %

Importance

Changes 0
Metric Value
cc 26
eloc 62
c 0
b 0
f 0
nc 840
nop 3
dl 29
loc 108
rs 2.2136

How to fix   Long Method    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, Systeme de publication pour l'internet                           *
5
 *                                                                         *
6
 *  Copyright (c) 2001-2016                                                *
7
 *  Arnaud Martin, Antoine Pitrou, Philippe Riviere, Emmanuel Saint-James  *
8
 *                                                                         *
9
 *  Ce programme est un logiciel libre distribue sous licence GNU/GPL.     *
10
 *  Pour plus de details voir le fichier COPYING.txt ou l'aide en ligne.   *
11
\***************************************************************************/
12
13
if (!defined('_ECRIRE_INC_VERSION')) return;
14
15
// http://doc.spip.org/@action_editer_article_dist
16
function action_editer_article_dist($arg=null) {
17
	include_spip('inc/autoriser');
18
	$err="";
19
	if (is_null($arg)){
20
		$securiser_action = charger_fonction('securiser_action', 'inc');
21
		$arg = $securiser_action();
22
	}
23
24
	// si id_article n'est pas un nombre, c'est une creation 
25
	// mais on verifie qu'on a toutes les donnees qu'il faut.
26
	if (!$id_article = intval($arg)) {
27
		$id_parent = _request('id_parent');
28
		if (!$id_parent)
29
			$err = _L("creation interdite d'un article sans rubrique");
30
		elseif(!autoriser('creerarticledans','rubrique',$id_parent))
31
			$err = _T("info_creerdansrubrique_non_autorise");
32
		else
33
			$id_article = article_inserer($id_parent);
34
	}
35
36
	// Enregistre l'envoi dans la BD
37
	if ($id_article > 0) $err = article_modifier($id_article);
38
39
	if ($err)
40
		spip_log("echec editeur article: $err",_LOG_ERREUR);
41
42
	return array($id_article,$err);
43
}
44
45
/**
46
 * Appelle toutes les fonctions de modification d'un article
47
 * $err est de la forme chaine de langue ou vide si pas d'erreur
48
 * http://doc.spip.org/@articles_set
49
 *
50
 * @param  $id_article
51
 * @param null $set
52
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|string?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
53
 */
54
function article_modifier($id_article, $set=null) {
55
56
	// unifier $texte en cas de texte trop long
57
	trop_longs_articles();
58
59
	include_spip('inc/modifier');
60
	include_spip('inc/filtres');
61
	$c = collecter_requests(
62
		// white list
63
		objet_info('article','champs_editables'),
64
		// black list
65
		array('date','statut','id_parent'),
66
		// donnees eventuellement fournies
67
		$set
68
	);
69
70
	// Si l'article est publie, invalider les caches et demander sa reindexation
71
	$t = sql_getfetsel("statut", "spip_articles", "id_article=".intval($id_article));
72
	$invalideur = $indexation = false;
73
	if ($t == 'publie') {
74
		$invalideur = "id='article/$id_article'";
75
		$indexation = true;
76
	}
77
78
	if ($err = objet_modifier_champs('article', $id_article,
79
		array(
80
			'data' => $set,
81
			'nonvide' => array('titre' => _T('info_nouvel_article')." "._T('info_numero_abbreviation').$id_article),
82
			'invalideur' => $invalideur,
83
			'indexation' => $indexation,
84
			'date_modif' => 'date_modif' // champ a mettre a date('Y-m-d H:i:s') s'il y a modif
85
		),
86
		$c))
87
		return $err;
88
89
	// Modification de statut, changement de rubrique ?
90
	$c = collecter_requests(array('date', 'statut', 'id_parent'),array(),$set);
91
	$err = article_instituer($id_article, $c);
92
93
	return $err;
94
}
95
96
/**
97
 * Inserer un nouvel article en base
98
 * http://doc.spip.org/@insert_article
99
 *
100
 * @param int $id_rubrique
101
 * @return int
102
 */
103
function article_inserer($id_rubrique) {
104
105
106
	// Si id_rubrique vaut 0 ou n'est pas definie, creer l'article
107
	// dans la premiere rubrique racine
108 View Code Duplication
	if (!$id_rubrique = intval($id_rubrique)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
		$row = sql_fetsel("id_rubrique, id_secteur, lang", "spip_rubriques", "id_parent=0",'', '0+titre,titre', "1");
110
		$id_rubrique = $row['id_rubrique'];
111
	} else $row = sql_fetsel("lang, id_secteur", "spip_rubriques", "id_rubrique=$id_rubrique");
112
113
	// eviter $id_secteur = NULL (erreur sqlite) si la requete precedente echoue 
114
	// cas de id_rubrique = -1 par exemple avec plugin "pages"
115
	$id_secteur = isset($row['id_secteur']) ? $row['id_secteur'] : 0;
116
	
117
	$lang_rub = $row['lang'];
118
119
	$lang = "";
120
	$choisie = 'non';
121
	// La langue a la creation : si les liens de traduction sont autorises
122
	// dans les rubriques, on essaie avec la langue de l'auteur,
123
	// ou a defaut celle de la rubrique
124
	// Sinon c'est la langue de la rubrique qui est choisie + heritee
125
	if (in_array('spip_articles',explode(',',$GLOBALS['meta']['multi_objets']))) {
126
		lang_select($GLOBALS['visiteur_session']['lang']);
127
		if (in_array($GLOBALS['spip_lang'],
128
		explode(',', $GLOBALS['meta']['langues_multilingue']))) {
129
			$lang = $GLOBALS['spip_lang'];
130
			$choisie = 'oui';
131
		}
132
	}
133
134
	if (!$lang) {
135
		$choisie = 'non';
136
		$lang = $lang_rub ? $lang_rub : $GLOBALS['meta']['langue_site'];
137
	}
138
139
	$champs = array(
140
		'id_rubrique' => $id_rubrique,
141
		'id_secteur' =>  $id_secteur,
142
		'statut' =>  'prepa',
143
		'date' => date('Y-m-d H:i:s'),
144
		'lang' => $lang,
145
		'langue_choisie' =>$choisie);
146
147
	// Envoyer aux plugins
148
	$champs = pipeline('pre_insertion',
149
		array(
150
			'args' => array(
151
				'table' => 'spip_articles',
152
			),
153
			'data' => $champs
154
		)
155
	);
156
157
	$id_article = sql_insertq("spip_articles", $champs);
158
159
	pipeline('post_insertion',
160
		array(
161
			'args' => array(
162
				'table' => 'spip_articles',
163
				'id_objet' => $id_article
164
			),
165
			'data' => $champs
166
		)
167
	);
168
169
	// controler si le serveur n'a pas renvoye une erreur
170 View Code Duplication
	if ($id_article > 0){
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
		$id_auteur = ((is_null(_request('id_auteur')) AND isset($GLOBALS['visiteur_session']['id_auteur']))?
172
			$GLOBALS['visiteur_session']['id_auteur']
173
			:_request('id_auteur'));
174
		if ($id_auteur){
175
			include_spip('action/editer_auteur');
176
			auteur_associer($id_auteur, array('article'=>$id_article));
177
		}
178
	}
179
180
	return $id_article;
181
}
182
183
184
// $c est un array ('statut', 'id_parent' = changement de rubrique)
185
//
186
// statut et rubrique sont lies, car un admin restreint peut deplacer
187
// un article publie vers une rubrique qu'il n'administre pas
188
// http://doc.spip.org/@instituer_article
189
function article_instituer($id_article, $c, $calcul_rub=true) {
190
191
	include_spip('inc/autoriser');
192
	include_spip('inc/rubriques');
193
	include_spip('inc/modifier');
194
195
	$row = sql_fetsel("statut, date, id_rubrique", "spip_articles", "id_article=$id_article");
196
	$id_rubrique = $row['id_rubrique'];
197
	$statut_ancien = $statut = $row['statut'];
198
	$date_ancienne = $date = $row['date'];
199
	$champs = array();
200
201
	$d = isset($c['date'])?$c['date']:null;
202
	$s = isset($c['statut'])?$c['statut']:$statut;
203
204
	// cf autorisations dans inc/instituer_article
205
	if ($s != $statut OR ($d AND $d != $date)) {
206
		if (autoriser('publierdans', 'rubrique', $id_rubrique))
207
			$statut = $champs['statut'] = $s;
208 View Code Duplication
		else if (autoriser('modifier', 'article', $id_article) AND $s != 'publie')
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
209
			$statut = $champs['statut'] = $s;
210
		else
211
			spip_log("editer_article $id_article refus " . join(' ', $c));
212
213
		// En cas de publication, fixer la date a "maintenant"
214
		// sauf si $c commande autre chose
215
		// ou si l'article est deja date dans le futur
216
		// En cas de proposition d'un article (mais pas depublication), idem
217 View Code Duplication
		if ($champs['statut'] == 'publie'
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
218
		 OR ($champs['statut'] == 'prop' AND ($d OR !in_array($statut_ancien, array('publie', 'prop'))))
219
		) {
220
			if ($d OR strtotime($d=$date)>time())
221
				$champs['date'] = $date = $d;
222
			else
223
				$champs['date'] = $date = date('Y-m-d H:i:s');
224
		}
225
	}
226
227
	// Verifier que la rubrique demandee existe et est differente
228
	// de la rubrique actuelle
229 View Code Duplication
	if ($id_parent = $c['id_parent']
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
230
	AND $id_parent != $id_rubrique
231
	AND (sql_fetsel('1', "spip_rubriques", "id_rubrique=$id_parent"))) {
232
		$champs['id_rubrique'] = $id_parent;
233
234
		// si l'article etait publie
235
		// et que le demandeur n'est pas admin de la rubrique de destination
236
		// repasser l'article en statut 'propose'.
237
		if ($statut == 'publie'
238
		AND !autoriser('publierdans', 'rubrique', $id_parent))
239
			$champs['statut'] = 'prop';
240
	}
241
242
	// Envoyer aux plugins
243
	$champs = pipeline('pre_edition',
244
		array(
245
			'args' => array(
246
				'table' => 'spip_articles',
247
				'id_objet' => $id_article,
248
				'action'=>'instituer',
249
				'statut_ancien' => $statut_ancien,
250
				'date_ancienne' => $date_ancienne,
251
			),
252
			'data' => $champs
253
		)
254
	);
255
256
	if (!count($champs)) return '';
257
258
	// Envoyer les modifs.
259
260
	editer_article_heritage($id_article, $id_rubrique, $statut_ancien, $champs, $calcul_rub);
261
262
	// Invalider les caches
263
	include_spip('inc/invalideur');
264
	suivre_invalideur("id='article/$id_article'");
265
266
	if ($date) {
267
		$t = strtotime($date);
268
		$p = @$GLOBALS['meta']['date_prochain_postdate'];
269
		if ($t > time() AND (!$p OR ($t < $p))) {
270
			ecrire_meta('date_prochain_postdate', $t);
271
		}
272
	}
273
274
	// Pipeline
275
	pipeline('post_edition',
276
		array(
277
			'args' => array(
278
				'table' => 'spip_articles',
279
				'id_objet' => $id_article,
280
				'action'=>'instituer',
281
				'statut_ancien' => $statut_ancien,
282
				'date_ancienne' => $date_ancienne,
283
			),
284
			'data' => $champs
285
		)
286
	);
287
288
	// Notifications
289 View Code Duplication
	if ($notifications = charger_fonction('notifications', 'inc')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
290
		$notifications('instituerarticle', $id_article,
291
			array('statut' => $statut, 'statut_ancien' => $statut_ancien, 'date'=>$date, 'date_ancienne' => $date_ancienne)
292
		);
293
	}
294
295
	return ''; // pas d'erreur
296
}
297
298
// fabrique la requete de modification de l'article, avec champs herites
299
300
// http://doc.spip.org/@editer_article_heritage
301
function editer_article_heritage($id_article, $id_rubrique, $statut, $champs, $cond=true) {
302
303
	// Si on deplace l'article
304
	//  changer aussi son secteur et sa langue (si heritee)
305
	if (isset($champs['id_rubrique'])) {
306
307
		$row_rub = sql_fetsel("id_secteur, lang", "spip_rubriques", "id_rubrique=".sql_quote($champs['id_rubrique']));
308
309
		$langue = $row_rub['lang'];
310
		$champs['id_secteur'] = $row_rub['id_secteur'];
311
		if (sql_fetsel('1', 'spip_articles', "id_article=$id_article AND langue_choisie<>'oui' AND lang<>" . sql_quote($langue))) {
312
			$champs['lang'] = $langue;
313
		}
314
	}
315
316
	if (!$champs) return;
317
318
	sql_updateq('spip_articles', $champs, "id_article=$id_article");
319
320
	// Changer le statut des rubriques concernees 
321
322
	if ($cond) {
323
		include_spip('inc/rubriques');
324
		$postdate = ($GLOBALS['meta']["post_dates"] == "non" AND isset($champs['date']) AND (strtotime($champs['date']) < time()))?$champs['date']:false;
325
		calculer_rubriques_if($id_rubrique, $champs, $statut, $postdate);
326
	}
327
}
328
329
//
330
// Reunit les textes decoupes parce que trop longs
331
//
332
333
// http://doc.spip.org/@trop_longs_articles
334
function trop_longs_articles() {
335
	if (is_array($plus = _request('texte_plus'))) {
336
		foreach ($plus as $n=>$t) {
337
			$plus[$n] = preg_replace(",<!--SPIP-->[\n\r]*,","", $t);
338
		}
339
		set_request('texte', join('',$plus) . _request('texte'));
340
	}
341
}
342
343
344
// obsoletes
345
function revisions_articles ($id_article, $c=false) {
346
	return article_modifier($id_article,$c);
347
}
348
function revision_article ($id_article, $c=false) {
349
	return article_modifier($id_article,$c);
350
}
351
function articles_set($id_article, $set=null) {
352
	return article_modifier($id_article,$set);
353
}
354
function insert_article($id_rubrique) {
355
	return article_inserer($id_rubrique);
356
}
357
function instituer_article($id_article, $c, $calcul_rub=true) {
358
	return article_instituer($id_article,$c,$calcul_rub);
359
}
360
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
361