51 printStdErr(
"Asset title attribute assignment from a CSV file\n");
52 printStdErr(
'Usage: update_asset_titles [system root] [csv file]');
53 printStdErr(
'system root : The Matrix System root directory. The "title" attributes of assets on this system will be changed');
54 printStdErr(
"csv file : A comma separated values file containing (asset ID, asset title) pairs\n");
67 function printStdErr($string)
69 fwrite(STDERR,
"$string\n");
82 function getUserInput($valid_choices)
85 while (!$valid_input) {
86 $user_input = rtrim(strtolower(fgets(STDIN, 1024)));
87 $valid_input = in_array($user_input, $valid_choices);
104 function setAssetTitle($asset_id, $asset_title)
108 $asset =& $GLOBALS[
'SQ_SYSTEM']->am->getAsset($asset_id);
111 $current_title = $asset->attr(
'title');
112 if ($current_title == NULL) {
113 printStdErr(
'* Asset ID '.$asset_id.
" does not have a title field, so one cannot be assigned.\n");
115 printStdErr(
'User Options ------------');
116 printStdErr(
'(I)gnore this asset and continue with modification of titles');
117 printStdErr(
'(C)ancel modification script');
119 $user_choice = getUserInput(Array(
'i',
'c'));
120 if ($user_choice ==
'c') {
121 printStdErr(
"\n- Titles modification cancelled by user");
126 $asset->setAttrValue(
'title', $asset_title);
127 $asset->saveAttributes();
130 $success = ($asset->attr(
'title') == $asset_title);
132 printStdErr(
'* The title "'.$asset_title.
'" could not be set for asset ID '.$asset_id.
"\n");
134 printStdErr(
'User Options ------------');
135 printStdErr(
'(I)gnore this asset and continue with modification of titles');
136 printStdErr(
'(C)ancel modification script');
138 $user_choice = getUserInput(Array(
'i',
'c'));
139 if ($user_choice ==
'c') {
140 printStdErr(
"\n- Titles modification cancelled by user");
155 if ((php_sapi_name() !=
'cli')) {
156 trigger_error(
"You can only run this script from the command line\n", E_USER_ERROR);
160 $argv = $_SERVER[
'argv'];
161 $SYSTEM_ROOT = (isset($argv[1])) ? $argv[1] :
'';
162 if (empty($SYSTEM_ROOT) || !is_dir($SYSTEM_ROOT)) {
164 printStdErr(
"* The path to the System Root must be specified as the first parameter\n");
169 $csv_filename = $argv[2];
170 if (empty($csv_filename)) {
172 printStdErr(
"* A CSV filename must be specified as the second parameter\n");
177 $csv_fd = fopen($csv_filename,
'r');
180 printStdErr(
"* The supplied CSV file was not found\n");
185 require_once $GLOBALS[
'SYSTEM_ROOT'].
'/core/include/init.inc';
188 $system_name = SQ_CONF_SYSTEM_NAME;
189 echo(
'PLEASE NOTE: The titles of assets in the '.(($system_name !=
'') ?
'"'.$system_name.
'" ' :
'').
"system will be modified by this script.\n");
190 echo(
'(C)ancel or (O)k? ');
192 $user_choice = getUserInput(Array(
'c',
'o'));
193 if ($user_choice ==
'c') {
194 printStdErr(
"\n- Title modification cancelled by user");
199 $GLOBALS[
'SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
202 echo(
"\n- Assigning titles from CSV file...\n\n");
204 $num_assets_updated = 0;
205 $num_assets_in_file = 0;
208 while (($data = fgetcsv($csv_fd, 1024,
',')) !== FALSE) {
209 $num_fields = count($data);
212 if ($num_fields != 2) {
213 printStdErr(
"* Only two columns (asset ID, asset title) are expected in the supplied CSV file\n");
218 $data[0] = trim($data[0]);
220 $asset_id = (int)$data[0];
221 $asset_title = trim($data[1]);
224 if (($asset_id != $data[0]) || ($asset_id == 0)) {
225 printStdErr(
'* An invalid asset ID ('.$data[0].
") was encountered in the supplied CSV file\n");
229 $num_assets_in_file++;
232 echo
'-- Asset #'.$asset_id.
': '.$asset_title.
"\n";
233 $success = setAssetTitle($asset_id, $asset_title);
235 if ($success) $num_assets_updated++;
243 echo
"\n- All done, stats below:\n";
244 echo
'Asset titles modified : '.$num_assets_updated.
"\n";
245 echo
'Asset records in file : '.$num_assets_in_file.
"\n";
248 $GLOBALS[
'SQ_SYSTEM']->restoreRunLevel();