Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
remove_assets_of_type.php
1 <?php
27 error_reporting(E_ALL);
28 if ((php_sapi_name() != 'cli')) {
29  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
30 }
31 
32 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
33 if (empty($SYSTEM_ROOT)) {
34  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
35  exit();
36 }
37 
38 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
39  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
40  exit();
41 }
42 
43 $DELETING_ASSET_TYPE = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '';
44 if (empty($DELETING_ASSET_TYPE)) {
45  echo "ERROR: You need to supply an asset type code as the second argument\n";
46  exit();
47 }
48 
49 require_once $SYSTEM_ROOT.'/core/include/init.inc';
50 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
51 
52 
53 // ask for the root password for the system
54 echo 'Enter the root password for "'.SQ_CONF_SYSTEM_NAME.'": ';
55 system('stty -echo');
56 $root_password = rtrim(fgets(STDIN, 4094));
57 system('stty echo');
58 
59 // check that the correct root password was entered
60 $root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
61 if (!$root_user->comparePassword($root_password)) {
62  echo "ERROR: The root password entered was incorrect\n";
63  exit();
64 }
65 
66 // log in as root
67 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
68  echo "ERROR: Failed login in as root user\n";
69  exit();
70 }
71 
72 // make some noiz
73 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
74 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
75 
76 $sql = 'SELECT assetid FROM sq_ast WHERE type_code = :type_code';
77 $query = MatrixDAL::preparePdoQuery($sql);
78 MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
79 $assets_of_type = MatrixDAL::executePdoAssoc($query, 0);
80 
81 if (!empty($assets_of_type)) {
82 
83  // bugfix #3820 , use MatrixDAL to quote the asset ids
84  foreach($assets_of_type as $index => $asset_id) {
85  $assets_of_type[$index] = MatrixDAL::quote($asset_id);
86  }
87  $asset_ids_set = '('.implode(', ', $assets_of_type).')';
88 
89  $sql = 'SELECT linkid FROM sq_ast_lnk WHERE minorid in '.$asset_ids_set.' OR majorid in '.$asset_ids_set;
90  $query = MatrixDAL::preparePdoQuery($sql);
91  // bugfix #3820
92  $res = MatrixDAL::executePdoAssoc($query);
93 
94  // bugfix #3849 Script remove_assets_of_type.php leaves unwanted table entries
95  // while deleting the link make sure we call asset_manager function to do the same
96  // we should not be just deleting the links and treeids from the table as it can
97  // lead to unexpected behaviour. for more information see the Bug report
98  if (!empty($res)) {
99  $links_array = Array();
100  foreach ($res as $value) {
101  $GLOBALS['SQ_SYSTEM']->am->deleteAssetLink($value['linkid']);
102  array_push($links_array, MatrixDAL::quote($value['linkid']));
103  }
104  $links_set = '('.implode(', ', $links_array).')';
105 
106  $sql = 'DELETE FROM sq_ast_lnk WHERE linkid in '.$links_set;
107  $query = MatrixDAL::preparePdoQuery($sql);
108  MatrixDAL::execPdoQuery($query);
109  }
110  // we need to delete asset from sq_ast table after deleteAssetLink() call
111  // or else then function call will throw errors.
112  $sql = 'DELETE FROM sq_ast_attr_val WHERE assetid in '.$asset_ids_set;
113  $query = MatrixDAL::preparePdoQuery($sql);
114  MatrixDAL::execPdoQuery($query);
115 
116  $sql = 'DELETE FROM sq_ast WHERE type_code = :type_code';
117  $query = MatrixDAL::preparePdoQuery($sql);
118  MatrixDAL::bindValueToPdo($query, 'type_code', $DELETING_ASSET_TYPE);
119  MatrixDAL::execPdoQuery($query);
120 
121  $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
122  $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
123 }
124 
125 
126 ?>