-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathsyncobjectattributes.php
More file actions
266 lines (225 loc) · 9.38 KB
/
syncobjectattributes.php
File metadata and controls
266 lines (225 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env php
<?php
// RELEASE: 20091112-1
//
// Created on: <10-Aug-2004 15:47:14 pk>
//
// Copyright (C) 1999-2014 eZ Systems AS. All rights reserved.
//
// This source file is part of the eZ publish (tm) Open Source Content
// Management System.
//
// This file may be distributed and/or modified under the terms of the
// "GNU General Public License" version 2 as published by the Free
// Software Foundation and appearing in the file LICENSE.GPL included in
// the packaging of this file.
//
// Licencees holding valid "eZ publish professional licences" may use this
// file in accordance with the "eZ publish professional licence" Agreement
// provided with the Software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
// THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE.
//
// The "eZ publish professional licence" is available at
// http://ez.no/products/licences/professional/. For pricing of this licence
// please contact us via e-mail to licence@ez.no. Further contact
// information is available at http://ez.no/home/contact/.
//
// The "GNU General Public License" (GPL) is available at
// http://www.gnu.org/copyleft/gpl.html.
//
// Contact licence@ez.no if any conditions of this licencing isn't clear to
// you.
//
/*! \file syncobjectattributes.php
*/
require 'autoload.php';
function updateClass( $classId, $scheduledScript )
{
$cli = eZCLI::instance();
/*
// If the class is not stored yet, store it now
$class = eZContentClass::fetch( $classId, true, eZContentClass::VERSION_STATUS_TEMPORARY );
if ( $class )
{
$cli->output( "Storing class" );
$class->storeDefined( $class->fetchAttributes() );
}
*/
// Fetch the stored class
$class = eZContentClass::fetch( $classId, true, eZContentClass::VERSION_STATUS_MODIFIED );
if ( !$class )
{
$cli->error( 'No class in a modified version status with ID: ' . $classId );
return;
}
// Fetch attributes and definitions
$attributes = $class->fetchAttributes( $classId, true, eZContentClass::VERSION_STATUS_MODIFIED );
$oldClassAttributes = $class->fetchAttributes( $classId, true, eZContentClass::VERSION_STATUS_DEFINED );
// Delete object attributes which have been removed.
foreach ( $oldClassAttributes as $oldClassAttribute )
{
$attributeExist = false;
$oldClassAttributeID = $oldClassAttribute->attribute( 'id' );
foreach ( $attributes as $newClassAttribute )
{
if ( $oldClassAttributeID == $newClassAttribute->attribute( 'id' ) )
{
$attributeExist = true;
}
}
if ( !$attributeExist )
{
$ezscriptmonitorINI = eZINI::instance( 'ezscriptmonitor.ini' );
$objectLimit = $ezscriptmonitorINI->variable( 'GeneralSettings', 'ObjectLimit' );
$limit = array( 'offset' => 0 , 'length' => $objectLimit );
do
{
$objectAttributes = eZContentObjectAttribute::fetchSameClassAttributeIDList( $oldClassAttributeID, false, false, false, $limit );
$objectAttributeCount = count( $objectAttributes );
$conditions = array( "contentclassattribute_id" => $oldClassAttributeID );
$totalObjectAttributeCount = eZContentObjectAttribute::count(
eZContentObjectAttribute::definition(),
array( "contentclassattribute_id" => $oldClassAttributeID )
);
if ( is_array( $objectAttributes ) && $objectAttributeCount > 0 )
{
$db = eZDB::instance();
$db->begin();
foreach ( $objectAttributes as $objectAttribute )
{
$objectAttribute = new eZContentObjectAttribute( $objectAttribute );
$objectAttribute->removeThis( $objectAttribute->attribute( 'id' ) );
}
$db->commit();
$limit['offset'] += $objectAttributeCount;
$percentage = round( ( 100 * $limit['offset'] ) / $totalObjectAttributeCount, 2 );
// for ezscriptmonitor 100 means the script is all the way done
if ( ( $percentage < 100 ) && ( $scheduledScript !== false ) )
{
$scheduledScript->updateProgress( $percentage );
}
$cli->output( "Removing attributes - Progress: " . $percentage. " %" );
}
} while ( $objectAttributeCount == $objectLimit );
}
}
$class->storeVersioned( $attributes, eZContentClass::VERSION_STATUS_DEFINED );
// Add object attributes which have been added.
foreach ( $attributes as $newClassAttribute )
{
$attributeExist = false;
foreach ( $oldClassAttributes as $oldClassAttribute )
{
if ( $oldClassAttribute->attribute( 'id' ) == $newClassAttribute->attribute( 'id' ) )
{
$attributeExist = true;
break;
}
}
if ( !$attributeExist )
{
$objects = null;
$cli->output( "Adding attribute : " . $newClassAttribute->attribute( 'name' ));
$newClassAttribute->initializeObjectAttributes( $objects );
}
}
if ( $scheduledScript !== false )
{
$scheduledScript->updateProgress( 100 );
}
}
// Init script
$cli = eZCLI::instance();
$script = eZScript::instance( array( 'description' => ( "Synchronize object attributes with the new definition of a class\n\n" .
"Will add missing content object attributes, and remove redundant ones, for a given class.\n" .
"If the class is not given, it will check all classes.\n" .
"\n" .
'syncobjectattributes.php -s admin --classid=42' ),
'use-session' => false,
'use-modules' => true,
'use-extensions' => true ) );
$script->startup();
$options = $script->getOptions( '[db-host:][db-user:][db-password:][db-database:][db-driver:][sql][classid:][admin-user:][scriptid:]',
'[name]',
array( 'db-host' => 'Database host',
'db-user' => 'Database user',
'db-password' => 'Database password',
'db-database' => 'Database name',
'db-driver' => 'Database driver',
'sql' => 'Display sql queries',
'classid' => 'ID of class to update',
'admin-user' => 'Alternative login for the user to perform operation as',
'scriptid' => 'Used by the Script Monitor extension, do not use manually' ) );
$script->initialize();
$dbUser = $options['db-user'] ? $options['db-user'] : false;
$dbPassword = $options['db-password'] ? $options['db-password'] : false;
$dbHost = $options['db-host'] ? $options['db-host'] : false;
$dbName = $options['db-database'] ? $options['db-database'] : false;
$dbImpl = $options['db-driver'] ? $options['db-driver'] : false;
$siteAccess = $options['siteaccess'] ? $options['siteaccess'] : false;
if ( $siteAccess )
{
$cli = eZCLI::instance();
if ( in_array( $siteAccess, eZINI::instance()->variable( 'SiteAccessSettings', 'AvailableSiteAccessList' ) ) )
{
$cli->output( "Using siteaccess $siteAccess" );
}
else
{
$cli->notice( "Siteaccess $siteAccess does not exist, using default siteaccess" );
}
}
$db = eZDB::instance();
if ( $dbHost or $dbName or $dbUser or $dbImpl )
{
$params = array();
if ( $dbHost !== false )
$params['server'] = $dbHost;
if ( $dbUser !== false )
{
$params['user'] = $dbUser;
$params['password'] = '';
}
if ( $dbPassword !== false )
$params['password'] = $dbPassword;
if ( $dbName !== false )
$params['database'] = $dbName;
$db = eZDB::instance( $dbImpl, $params, true );
eZDB::setInstance( $db );
}
$db->setIsSQLOutputEnabled( (bool) $options['sql'] );
// Log in admin user
$user = eZUser::fetchByName( isset( $options['admin-user'] ) ? $options['admin-user'] : 'admin' );
if ( $user )
eZUser::setCurrentlyLoggedInUser( $user, $user->attribute( 'contentobject_id' ) );
else
{
$cli->error( 'Could not fetch admin user object' );
$script->shutdown( 1 );
return;
}
// Take care of script monitoring
$scheduledScript = false;
if ( isset( $options['scriptid'] ) )
{
$scheduledScript = eZScheduledScript::fetch( $options['scriptid'] );
}
// Do the update
if ( isset( $options['classid'] ) )
{
updateClass( $options['classid'], $scheduledScript );
}
else
{
$cli->notice( 'The classid parameter was not given, will check all classes.' );
foreach ( eZContentClass::fetchList( eZContentClass::VERSION_STATUS_MODIFIED, false ) as $class )
{
$cli->output( 'Checking class with ID: ' . $class['id'] );
updateClass( $class['id'], $scheduledScript );
}
}
$script->shutdown();
?>