22 $strict_type_code = FALSE;
27 'Remove <font> tags' => 1,
28 'Remove double spaces' => 1,
29 'Remove non-HTML tags' => 1,
30 'Change Microsoft Words bullets' => 1,
31 'Remove soft hyphens' => 1,
33 'Remove style attribute' => 0,
34 'Remove class attribute' => 0,
35 'Remove <table> tags' => 0,
36 'Remove <span> tags' => 0,
37 'Remove all empty tags' => 0,
38 'Remove all tags attributes (except HREF and SRC)' => 0,
43 error_reporting(E_ALL);
44 if ((php_sapi_name() !=
'cli')) trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
46 $args = count($_SERVER[
'argv']);
47 if ($args > 4 || $args < 3) {
48 echo
"This script needs to be run in the following format:\n\n";
49 echo
"\tphp replace_text.php SYSTEM_ROOT root_node_ids [exclude_root_node_ids]\n\n";
50 echo
"\tEg. php scripts/replace_text.php . 10,5 7,2\n\n";
51 echo
"Also note there are 3 configurable options in the script: By default non-extreme options are run on all 'page' types\n\n";
55 $SYSTEM_ROOT = (isset($_SERVER[
'argv'][1])) ? $_SERVER[
'argv'][1] :
'';
56 if (empty($SYSTEM_ROOT)) {
57 echo
"ERROR: You need to supply the path to the System Root as the first argument\n";
61 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.
'/core/include/init.inc')) {
62 echo
"ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
66 if (isset($_SERVER[
'argv'][2])) {
67 $root_nodes = explode(
',', $_SERVER[
'argv'][2]);
70 $excl_nodes = Array();
71 if (isset($_SERVER[
'argv'][3])) {
72 $excl_nodes = explode(
',', $_SERVER[
'argv'][3]);
75 require_once $SYSTEM_ROOT.
'/core/include/init.inc';
77 $root_user =& $GLOBALS[
'SQ_SYSTEM']->am->getSystemAsset(
'root_user');
78 $GLOBALS[
'SQ_SYSTEM']->setCurrentUser($root_user);
79 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
83 foreach ($excl_nodes as $node) {
84 $children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($node, $type_code, $strict_type_code);
85 foreach ($children as $child_id => $info) {
86 $excl_ids[] = $child_id;
90 foreach ($root_nodes as $node) {
91 $children = $GLOBALS[
'SQ_SYSTEM']->am->getChildren($node, $type_code, $strict_type_code, FALSE);
92 foreach ($children as $child_id => $info) {
93 if (!in_array($child_id, $excl_nodes) && !in_array($child_id, $excl_ids)) {
94 $contents = $GLOBALS[
'SQ_SYSTEM']->am->getEditableContents($child_id);
96 foreach ($contents as $id => $edit) {
97 echo
"Examining wysiwyg content type of Asset ID: $id\n";
98 $edited = process_replace_text($edit, $options);
99 if ($edited !== FALSE) {
100 $GLOBALS[
'SQ_SYSTEM']->am->setEditableContents($id, $edited);
102 die (
"There is a crazy error in this script. Most likey the options array has been misconfigured\n");
106 if (isset ($info[0][
'type_code'])) {
107 $type_info = $GLOBALS[
'SQ_SYSTEM']->am->getAssetTypeAttributes($info[0][
'type_code'], Array(
'name',
'type'));
111 foreach ($type_info as $name => $type) {
112 if ($type[
'type'] ==
'wysiwyg'){
113 $asset = $GLOBALS[
'SQ_SYSTEM']->am->getAsset($child_id);
114 $contents = $asset->attr($name);
115 echo
"Examining wysiwyg contents of attribute '$name' of Asset ID: $child_id\n";
116 $edited = process_replace_text($contents, $options);
117 if ($edited === FALSE) die (
"There is a crazy error in this script. Most likey the options array has been misconfigured\n");
118 $asset->setAttrValue($name, $edited);
119 $asset->saveAttributes();
128 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();
129 $GLOBALS[
'SQ_SYSTEM']->restoreCurrentUser();
140 function process_replace_text($html, $options)
145 $HTMLtags =
"!--|a|abbr|above|acronym|address|applet|array|area|b|base|basefont|bdo|bgsound|big|blink|blockquote|body|box|br|blink|button|caption|center|cite|code|col|colgroup|comment|dd|del|dfn|dir|div|dl|dt|em|embed|fieldset|fig|font|form|frame|frameset|h1|h2|h3|h4|h5|h6|head|hr|html|i|id|iframe|ilayer|img|input|ins|isindex|kbd|label|layer|legend|li|link|listing|map|marquee|menu|meta|multicol|nextid|nobr|noframes|nolayer|note|noscript|object|ol|option|keygen|optgroup|p|param|pre|q|quote|range|root|s|samp|script|select|small|sound|spacer|span|sqrt|strike|strong|style|sub|sup|table|tbody|td|text|textarea|tfoot|th|thead|title|tr|tt|u|ul|var|wbr|xmp";
146 $bullet = urldecode(
"%B7");
147 $shy = urldecode(
"%AD");
148 foreach ($options as $key => $value) {
151 $localreg =
"%<link rel=\"[^\"]*\" href=\"file[^\"]*\">%i";
153 $curHTML = preg_replace($localreg, $localrep, $curHTML);
154 $wordreg =
"%<w\:[^>]*>(.*?)<\/w\:[^>]*>%i";
156 $curHTML = preg_replace($wordreg, $wordrep, $curHTML);
158 case 'Remove <font> tags':
159 $reg =
"%<\/?font ?[^>]*>%i";
162 case 'Remove double spaces':
164 $reg =
"%(\s| ){2,}%i";
167 case 'Remove non-HTML tags':
168 $reg =
"%<(?!(\/?(".$HTMLtags.
")[> ]))([^>]*)>%i";
171 case 'Change Microsoft Words bullets':
172 $reg =
"%<p[^>]*>(".$bullet.
"|·)(.*?)<\/p>%i";
175 case 'Remove soft hyphens':
176 $reg =
"%(­?|".$shy.
")%i";
179 case 'Remove style attribute':
180 $reg =
"% style=\"?[^\">]*\"?%i";
183 case 'Remove class attribute':
184 $reg =
"% class=\"?[^\">]*[\"]?%i";
187 case 'Remove <table> tags':
188 $reg =
"%<(table|/table|tr|tbody|/tbody|td|th) ?[^>]*>%i";
190 $curHTML = preg_replace($reg, $rep, $curHTML);
191 $reg =
"%<(/tr|/td|/th)>%i";
194 case 'Remove <span> tags':
195 $reg =
"%<\/?span( [^>]*>|>)%i";
198 case 'Remove all empty tags':
199 $reg =
"%<([A-Z][A-Z0-9]*)( [^>]*)?>( | |\n|\t)*<\/\\1>%i";
202 case 'Remove all tags attributes (except HREF and SRC)':
203 $reg =
'%<([^/ >]+)[^>]*?([^>]*?( (src|href)="?[^>"]*"?)[^>]*?)*[^>]*?>%i';
206 default :
return false;
209 if ($key ==
'Remove all empty tags') {
210 $reg2 =
"%(<A NAME[^>]*?>)( | |\n|\t)*(</A>)%i";
211 $rep2 =
"$1matrix_anchor_tmp$3";
212 $curHTML = preg_replace($reg2, $rep2, $curHTML);
213 $reg2 =
"%(<A ID[^>]*?>)( | |\n|\t)*(</A>)%i";
214 $rep2 =
"$1matrix_anchor_tmp$3";
215 $curHTML = preg_replace($reg2, $rep2, $curHTML);
217 $curHTML = preg_replace($reg, $rep, $curHTML);
219 if ($key ==
'Remove all empty tags') {
220 $reg3 =
"%(<A NAME[^>]*?>)matrix_anchor_tmp(</A>)%i";
222 $curHTML = preg_replace($reg3, $rep3, $curHTML);
223 $reg3 =
"%(<A ID[^>]*?>)matrix_anchor_tmp(</A>)%i";
225 $curHTML = preg_replace($reg3, $rep3, $curHTML);