Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
system_integrity_run_tidy.php
1 <?php
26 error_reporting(E_ALL);
27 if ((php_sapi_name() != 'cli')) trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
28 
29 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
30 if (empty($SYSTEM_ROOT)) {
31  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
32  exit();
33 }
34 
35 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
36  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
37  exit();
38 }
39 
40 require_once $SYSTEM_ROOT.'/core/include/init.inc';
41 require_once SQ_DATA_PATH.'/private/conf/tools.inc';
42 $ROOT_PATH = SQ_FUDGE_PATH.'/wysiwyg/';
43 require_once SQ_FUDGE_PATH.'/wysiwyg/plugins/html_tidy/html_tidy.inc';
44 
45 $ROOT_ASSETID = (isset($_SERVER['argv'][2])) ? $_SERVER['argv'][2] : '1';
46 if ($ROOT_ASSETID == 1) {
47  echo "\nWARNING: You are running this checker on the whole system.\nThis is fine but:\n\tit may take a long time; and\n\tit will acquire locks on many of your assets (meaning you wont be able to edit content for a while)\n\n";
48 }
49 
50 // check that the correct root password was entered
51 $root_user = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
52 
53 // log in as root
54 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
55  echo "ERROR: Failed loggin in as root user\n";
56  exit();
57 }
58 
59 // find the cache dirs that are currently in the cache repository
60 $tmp->_running_vars['cache_dirs'] = Array();
61 $dh = opendir(SQ_CACHE_PATH);
62 while (false !== ($file = readdir($dh))) {
63  if ($file == '.' || $file == '..') continue;
64  if (!is_dir(SQ_CACHE_PATH.'/'.$file)) continue;
65 
66  // cache directories should only be 4 digits long
67  if (!preg_match('|\d{4}|', $file)) continue;
68 
69  // just add the relative path to the cache dir so
70  // we can compare the name with the asset hash
71  $tmp->_running_vars['cache_dirs'][] = $file;
72 }
73 closedir($dh);
74 
75 // go through each wysiwyg in the system, lock it, validate it, unlock it
76 $wysiwygids = $GLOBALS['SQ_SYSTEM']->am->getChildren($ROOT_ASSETID, 'content_type_wysiwyg', false);
77 foreach ($wysiwygids as $wysiwygid => $type_code_data) {
78  $type_code = $type_code_data[0]['type_code'];
79  $wysiwyg = &$GLOBALS['SQ_SYSTEM']->am->getAsset($wysiwygid, $type_code);
80  printAssetName('WYSIWYG #'.$wysiwyg->id);
81 
82  // try to lock the WYSIWYG
83  if (!$GLOBALS['SQ_SYSTEM']->am->acquireLock($wysiwyg->id, 'attributes')) {
84  printUpdateStatus('LOCKED');
85  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg);
86  continue;
87  }
88 
89  $old_html = $wysiwyg->attr('html');
90  $html = $old_html;
91 
92  // Start Tidy up
93  $tidy = new HTML_Tidy();
94  $tidy->process($html);
95 
96  if ($tidy->htmltidy_status != 'pass' || !$wysiwyg->setAttrValue('html', $html) || !$wysiwyg->setAttrValue('htmltidy_status', $tidy->htmltidy_status) || !$wysiwyg->setAttrValue('htmltidy_errors', $tidy->htmltidy_errors) || !$wysiwyg->saveAttributes()) {
97  printUpdateStatus('TIDY FAIL');
98  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg);
99  unset($tidy);
100  continue;
101  }
102 
103  unset($tidy);
104 
105  // try to unlock the WYSIWYG
106  if (!$GLOBALS['SQ_SYSTEM']->am->releaseLock($wysiwyg->id, 'attributes')) {
107  printUpdateStatus('UNLOCK FAIL');
108  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg);
109  continue;
110  }
111 
112  printUpdateStatus('TIDY PASS');
113 
114  // try to recreate the parent content_files (bodycopy container and bodycopy)
115  $parents = $GLOBALS['SQ_SYSTEM']->am->getDependantParents($wysiwyg->id);
116  foreach ($parents as $parent) {
117  $parent_asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($parent);
118  if (!$parent_asset->saveSystemVersion()) {
119  printAssetName($parent_asset->name.' (#'.$parent_asset->id.')');
120  printUpdateStatus('CONTENT FAIL');
121  continue;
122  } else {
123  printAssetName($parent_asset->name.' (#'.$parent_asset->id.')');
124  printUpdateStatus('CONTENT PASS');
125 
126  // Clear the Cache for each asset
127  $cm = &$GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
128 
129  // get the group that is used as the first 2 digits of the
130  // cache directory names, and get the asset id code whics is used as
131  // the first string on each of the cache files
132 
133  $assetid_code = md5($parent_asset->id);
134  $group = $cm->getAssetHash($assetid_code);
135 
136  foreach ($tmp->_running_vars['cache_dirs'] as $dir) {
137  if (substr($dir, 0, strlen($group)) == $group) {
138 
139  // this directory contains cache entries for this particular asset
140  // now files the files in this cache dir that belong to this asset
141  $dh = opendir(SQ_CACHE_PATH.'/'.$dir);
142 
143  while (false !== ($file = readdir($dh))) {
144  $abs_path = SQ_CACHE_PATH.'/'.$dir.'/'.$file;
145  if (is_dir($abs_path)) continue;
146  // if the file starts with the assetid_code for the asset
147  // then back up in the unlinking with the resurection
148  if (substr($file, 0, strlen($assetid_code)) == $assetid_code) unlink($abs_path);
149  printAssetName($parent_asset->name.' (#'.$parent_asset->id.')');
150  printUpdateStatus('CACHE CLEAR');
151  }
152  closedir($dh);
153  }
154  }//end foreach
155 
156  }
157  }
158 
159  $GLOBALS['SQ_SYSTEM']->am->forgetAsset($wysiwyg);
160 
161 }//end foreach
162 
163 
174 function printAssetName($name)
175 {
176  printf ('%s%'.(30 - strlen($name)).'s', $name, '');
177 
178 }//end printAssetName()
179 
180 
189 function printUpdateStatus($status)
190 {
191  echo " [ $status ] \n";
192 
193 }//end printUpdateStatus()
194 
195 
196 ?>