Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
remove_asset_type.php
1 <?php
27 error_reporting(E_ALL);
28 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
29 
30 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
31 if (empty($SYSTEM_ROOT)) {
32  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
33  exit();
34 }
35 
36 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
37  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
38  exit();
39 }
40 
41 $DELETING_ASSET_TYPE = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '';
42 if (empty($DELETING_ASSET_TYPE)) {
43  echo "ERROR: You need to supply an asset type code as the second argument\n";
44  exit();
45 }
46 
47 require_once $SYSTEM_ROOT.'/core/include/init.inc';
48 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
49 
50 
51 // ask for the root password for the system
52 echo 'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.'": ';
53 system('stty -echo');
54 $root_password = rtrim(fgets(STDIN, 4094));
55 system('stty echo');
56 
57 // check that the correct root password was entered
58 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
59 if (!$root_user->comparePassword($root_password)) {
60  echo "ERROR: The root password entered was incorrect\n";
61  exit();
62 }
63 
64 // log in as root
65 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
66  echo "ERROR: Failed login in as root user\n";
67  exit();
68 }
69 
70 // make some noiz
71 $db = $GLOBALS['SQ_SYSTEM']->db;
72 
73 $sql = 'SELECT assetid FROM sq_ast WHERE type_code = :type_code';
74 $query = MatrixDAL::preparePdoQuery($sql);
75 MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
76 $assets_of_type = MatrixDAL::executePdoAssoc($query, 0);
77 
78 foreach($assets_of_type as $index => $val) {
79  $assets_of_type[$index] = MatrixDAL::quote($val);
80 }
81 
82 if (!empty($assets_of_type)) {
83  $asset_ids_set = '('.implode(', ', $assets_of_type).')';
84  $sql = 'DELETE FROM sq_ast_attr_val WHERE assetid in '.$asset_ids_set;
85  $query = MatrixDAL::preparePdoQuery($sql);
87 
88  $sql = 'DELETE FROM sq_ast_lnk WHERE minorid in '.$asset_ids_set;
89  $query = MatrixDAL::preparePdoQuery($sql);
91 
92  $sql = 'DELETE FROM sq_ast WHERE type_code = :type_code';
93  $query = MatrixDAL::preparePdoQuery($sql);
94  MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
96 }
97 
98 $sql = 'DELETE FROM sq_ast_attr WHERE type_code = :type_code OR owning_type_code = :owning_type_code';
99 $query = MatrixDAL::preparePdoQuery($sql);
100 MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
101 MatrixDAL::bindValueToPdo($query, 'owning_type_code', $DELETING_ASSET_TYPE);
103 
104 $sql = 'DELETE FROM sq_ast_typ WHERE type_code = :type_code';
105 $query = MatrixDAL::preparePdoQuery($sql);
106 MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
108 
109 $sql = 'DELETE FROM sq_ast_typ_inhd WHERE type_code = :type_code';
110 $query = MatrixDAL::preparePdoQuery($sql);
111 MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
113 
114 assert_true(unlink(dirname(dirname(__FILE__)).'/data/private/db/asset_types.inc'), 'failed removing asset_types.inc');
115 echo "\nDone\n";
116 ?>