Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
search_replace_attribute_content.php
1 <?php
25 echo 'Comment the code that stops this script from working. Protection against accidental execution.';
26 echo "\n";
27 exit;
28 
29 // Assets you want to modify are here
30 $to_process = Array();
31 
32 // Supply Your Strings Here
33 $search_for = 'NOTHING';
34 $replace_with = 'SOMETHING';
35 
36 $attribute_name = 'html';
37 
38 // --
39 // No configuration options below this comment
40 // --
41 
42 $search_for = '/'.preg_quote($search_for, '/').'/';
43 $replace_with = $replace_with;
44 
45 error_reporting(E_ALL);
46 if ((php_sapi_name() != 'cli')) {
47  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
48 }
49 
50 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
51 if (empty($SYSTEM_ROOT) || !is_dir($SYSTEM_ROOT)) {
52  echo "You need to supply the path to the System Root as the first argument\n";
53  exit();
54 }
55 
56 define('SQ_SYSTEM_ROOT', $SYSTEM_ROOT);
57 require_once SQ_SYSTEM_ROOT.'/core/include/init.inc';
58 
59 
60 $root_user =& $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
61 
62 // log in as root
63 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
64  echo "Failed logging in as root user\n";
65  exit();
66 }
67 
68 $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
69 $GLOBALS['SQ_SYSTEM']->doTransaction('BEGIN');
70 
71 $db =& $GLOBALS['SQ_SYSTEM']->db;
72 $am =& $GLOBALS['SQ_SYSTEM']->am;
73 
74 $start = microtime(TRUE);
75 $done = 0;
76 $total = count($to_process);
77 foreach ($to_process as $assetid) {
78  // display progress
79  $done++;
80  if ($done % 50 == 0) {
81  $elapsed = microtime(TRUE) - $start;
82  if ($elapsed > 0) {
83  $frac = $done / $total;
84  $remain = $elapsed / $frac - $elapsed;
85  $pct = $frac * 100;
86  printf("%.2f%%: %d assets checked in %.2f seconds, %.2f remaining.\n", $pct, $done, $elapsed, $remain);
87  }
88  }
89 
90  $asset =& $am->getAsset($assetid);
91  $attr_content = $asset->attr($attribute_name);
92 
93  $result_content = preg_replace($search_for, $replace_with, $attr_content);
94  if ($result_content === $attr_content) {
95  // No change - don't bother saving.
96  echo "No change: $assetid\n";
97  $am->forgetAsset($asset);
98  continue;
99  }
100 
101  $lock_success = $am->acquireLock($assetid, 'attributes');
102  if (!$lock_success) {
103  echo "\n".'FAILED Processing asset: '.$assetid."\n";
104  }
105  $asset->setAttrValue($attribute_name, $result_content);
106 
107  $asset->saveAttributes();
108  $am->releaseLock($assetid, 'attributes');
109 
110  $am->forgetAsset($asset);
111 
112  echo "DONE: $assetid\n";
113 }
114 
115 
116 $GLOBALS['SQ_SYSTEM']->doTransaction('COMMIT');
117 $GLOBALS['SQ_SYSTEM']->restoreDatabaseConnection();
118 
119 echo "\n";
120 echo 'DONE';
121 echo "\n";
122 
123 ?>