Skip to content

Commit aa00854

Browse files
author
Joao Inacio
committed
EZP-26058: update script for non-breaking spaces in ezxmltext
1 parent 1305cbe commit aa00854

File tree

1 file changed

+120
-0
lines changed

1 file changed

+120
-0
lines changed
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
<?php
2+
/**
3+
* File containing the updatenbxmlcontents script.
4+
*
5+
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
6+
* @license For full copyright and license information view LICENSE file distributed with this source code.
7+
* @version //autogentag//
8+
*/
9+
10+
require_once 'autoload.php';
11+
$cli = eZCLI::instance();
12+
13+
$script = eZScript::instance(
14+
array(
15+
'description' => "Updates non-break space encoding in ezxml contents. See issue EZP-18220\n",
16+
'use-session' => true,
17+
'use-modules' => false,
18+
'use-extensions' => true
19+
)
20+
);
21+
$script->startup();
22+
23+
$options = $script->getOptions(
24+
"[dry-run][n][v][iteration-sleep:][iteration-limit:]",
25+
"",
26+
array(
27+
'dry-run' => 'Dry run',
28+
'iteration-sleep' => 'Sleep duration between batches, in seconds (default: 1)',
29+
'iteration-limit' => 'Batch size (default: 100)',
30+
'n' => 'Do not wait 30 seconds before starting',
31+
)
32+
);
33+
$optDryRun = (bool)$options['dry-run'];
34+
$optIterationSleep = $options['iteration-sleep'] ? (int)$options['iteration-sleep'] : 1;
35+
$optIterationLimit = $options['iteration-limit'] ? (int)$options['iteration-limit'] : 100;
36+
$verboseLevel = $script->verboseOutputLevel();
37+
38+
$limit = array(
39+
"offset" => 0,
40+
"limit" => $optIterationLimit,
41+
);
42+
43+
$script->initialize();
44+
$db = eZDB::instance();
45+
46+
if ( $optDryRun )
47+
{
48+
$cli->warning( "dry-run mode" );
49+
}
50+
51+
/**
52+
* Updates non-breaking spaces from existing "&amp;nbsp;" to proper "\xC2\xA0"
53+
*
54+
* @param array $attribute
55+
*/
56+
function updateEzxmlNonbreakSpaces( $attribute, $optDryRun, $verbose )
57+
{
58+
$id = $attribute['id'];
59+
$contentId = $attribute['contentobject_id'];
60+
$version = $attribute['version'];
61+
$xmlData = $attribute['data_text'];
62+
63+
$matchTags = implode('|', array( 'paragraph', 'header') );
64+
$pattern = '/(<(?<tag>' . $matchTags . ')[^>]*\>)(.*)&amp;nbsp;(.*)(<\/(?P=tag)>)/';
65+
$replace = "\\1\\3\xC2\xA0\\4\\5";
66+
67+
do {
68+
$xmlData = preg_replace( $pattern, $replace, $xmlData, -1, $countReplaced );
69+
} while ($countReplaced > 0);
70+
71+
if ( $verbose ) {
72+
eZCLI::instance()->output( "Updating data for content #$contentId (ver. $version) ..." );
73+
}
74+
if ( !$optDryRun ) {
75+
eZDB::instance()->query( "UPDATE ezcontentobject_attribute SET data_text='$xmlData' WHERE id='$id'" );
76+
}
77+
}
78+
79+
if ( !$options['n'] )
80+
{
81+
$cli->output();
82+
$cli->warning( "You have 30 seconds to break the script before actual processing starts (press Ctrl-C)." );
83+
$cli->warning( "Execute the script with '-n' switch to skip this delay." );
84+
sleep( 30 );
85+
}
86+
87+
$attributeCount = $db->arrayQuery(
88+
"SELECT count(id) as count " .
89+
"FROM ezcontentobject_attribute attr " .
90+
"WHERE data_type_string='ezxmltext' AND data_text LIKE '%&amp;nbsp;%' "
91+
);
92+
$attributeCount = $attributeCount[0]['count'];
93+
94+
$cli->output( "Number of xml attributes to update: " . $attributeCount );
95+
96+
// main loop
97+
do {
98+
$rows = $db->arrayQuery(
99+
"SELECT id, contentobject_id, version, data_text " .
100+
"FROM ezcontentobject_attribute attr " .
101+
"WHERE data_type_string='ezxmltext' AND data_text LIKE '%&amp;nbsp;%' ",
102+
$limit
103+
);
104+
105+
$db->begin();
106+
foreach ( $rows as $attribute )
107+
{
108+
updateEzxmlNonbreakSpaces( $attribute, $optDryRun, $verboseLevel );
109+
}
110+
$db->commit();
111+
112+
$cli->output(".");
113+
114+
$limit["offset"] += $optIterationLimit;
115+
sleep( $optIterationSleep );
116+
} while ( count($rows) == $optIterationLimit );
117+
118+
$cli->output( "Update has been completed." );
119+
120+
$script->shutdown();

0 commit comments

Comments
 (0)