Completed
Push — master ( 117d82...bd6ae7 )
by cam
04:12
created

repair.php ➔ admin_repair_plat()   B

Complexity

Conditions 9
Paths 17

Size

Total Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
nc 17
nop 0
dl 0
loc 38
rs 7.7564
c 0
b 0
f 0
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
 * Réparation de la base de données
15
 *
16
 * @package SPIP\Core\SQL\Reparation
17
 */
18
19
if (!defined('_ECRIRE_INC_VERSION')) {
20
	return;
21
}
22
23
/**
24
 * Action de réparation de la base de données
25
 *
26
 * Tente de réparer les tables, recalcule les héritages et secteurs
27
 * de rubriques. Affiche les erreurs s'il y en a eu.
28
 *
29
 * @pipeline_appel base_admin_repair
30
 * @uses admin_repair_tables()
31
 * @uses calculer_rubriques()
32
 * @uses propager_les_secteurs()
33
 *
34
 * @param string $titre Inutilisé
35
 * @param string $reprise Inutilisé
36
 **/
37
function base_repair_dist($titre = '', $reprise = '') {
0 ignored issues
show
Unused Code introduced by
The parameter $titre is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $reprise is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
39
	$res = admin_repair_tables();
40
	if (!$res) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $res of type false|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
41
		$res = "<div class='error'>" . _T('avis_erreur_mysql') . ' ' . sql_errno() . ': ' . sql_error() . "</div>\n";
42
	} else {
43
		include_spip('inc/rubriques');
44
		calculer_rubriques();
45
		propager_les_secteurs();
46
	}
47
	include_spip('inc/minipres');
48
	$res .= pipeline('base_admin_repair', $res);
49
	echo minipres(_T('texte_tentative_recuperation'),
50
		$res . generer_form_ecrire('accueil', '', '', _T('public:accueil_site')));
51
}
52
53
/**
54
 * Exécute une réparation de la base de données
55
 *
56
 * Crée les tables et les champs manquants.
57
 * Applique sur les tables un REPAIR en SQL (si le serveur SQL l'accepte).
58
 *
59
 * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be false|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...
60
 *     Code HTML expliquant les actions réalisées
61
 **/
62
function admin_repair_tables() {
63
64
	$repair = sql_repair('repair', null, 'continue');
65
66
	// recreer les tables manquantes eventuelles
67
	include_spip('base/create');
68
	creer_base();
69
	$tables = sql_alltable();
70
71
	$res = "";
72
	foreach ($tables as $tab) {
73
74
		$class = "";
75
		$m = "<strong>$tab</strong> ";
76
		spip_log("Repare $tab", _LOG_INFO_IMPORTANTE);
77
		// supprimer la meta avant de lancer la reparation
78
		// car le repair peut etre long ; on ne veut pas boucler
79
		effacer_meta('admin_repair');
80
		if ($repair) {
81
			$result_repair = sql_repair($tab);
82
			if (!$result_repair) {
83
				return false;
84
			}
85
		}
86
87
		// essayer de maj la table (creation de champs manquants)
88
		maj_tables($tab);
89
90
		$count = sql_countsel($tab);
91
92
		if ($count > 1) {
93
			$m .= "(" . _T('texte_compte_elements', array('count' => $count)) . ")\n";
94
		} else {
95
			if ($count == 1) {
96
				$m .= "(" . _T('texte_compte_element', array('count' => $count)) . ")\n";
97
			} else {
98
				$m .= "(" . _T('texte_vide') . ")\n";
99
			}
100
		}
101
102
		if ($result_repair
0 ignored issues
show
Bug introduced by
The variable $result_repair does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
103
			and $msg = join(" ",
104
					(is_resource($result_repair) or is_object($result_repair)) ? sql_fetch($result_repair) : $result_repair) . ' '
105
			and strpos($msg, ' OK ') === false
106
		) {
107
			$class = " class='notice'";
108
			$m .= "<br /><tt>" . spip_htmlentities($msg) . "</tt>\n";
109
		} else {
110
			$m .= " " . _T('texte_table_ok');
111
		}
112
113
		$res .= "<div$class>$m</div>";
114
	}
115
116
	return $res;
117
}
118