Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
update_asset_titles.php
1 <?php
49 function printUsage()
50 {
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");
55 
56 }//end printUsage()
57 
58 
67 function printStdErr($string)
68 {
69  fwrite(STDERR, "$string\n");
70 
71 }//end printStdErr()
72 
73 
82 function getUserInput($valid_choices)
83 {
84  $valid_input = FALSE;
85  while (!$valid_input) {
86  $user_input = rtrim(strtolower(fgets(STDIN, 1024)));
87  $valid_input = in_array($user_input, $valid_choices);
88  }
89 
90  return $user_input;
91 
92 }//end getUserInput()
93 
94 
104 function setAssetTitle($asset_id, $asset_title)
105 {
106  $success = FALSE;
107 
108  $asset =& $GLOBALS['SQ_SYSTEM']->am->getAsset($asset_id);
109  if ($asset->id) {
110  // Make sure that we have a 'title' attribute
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");
114 
115  printStdErr('User Options ------------');
116  printStdErr('(I)gnore this asset and continue with modification of titles');
117  printStdErr('(C)ancel modification script');
118 
119  $user_choice = getUserInput(Array('i','c'));
120  if ($user_choice == 'c') {
121  printStdErr("\n- Titles modification cancelled by user");
122  exit(-7);
123  }
124  } else {
125  // Set the 'title' attribute for the asset
126  $asset->setAttrValue('title', $asset_title);
127  $asset->saveAttributes();
128 
129  // Read back the title to ensure that it is set as expected
130  $success = ($asset->attr('title') == $asset_title);
131  if (!$success) {
132  printStdErr('* The title "'.$asset_title.'" could not be set for asset ID '.$asset_id."\n");
133 
134  printStdErr('User Options ------------');
135  printStdErr('(I)gnore this asset and continue with modification of titles');
136  printStdErr('(C)ancel modification script');
137 
138  $user_choice = getUserInput(Array('i','c'));
139  if ($user_choice == 'c') {
140  printStdErr("\n- Titles modification cancelled by user");
141  exit(-8);
142  }
143 
144  }
145  }
146  }//end if
147 
148  return $success;
149 
150 }//end setAssetTitle()
151 
152 
153 /************************** MAIN PROGRAM ****************************/
154 
155 if ((php_sapi_name() != 'cli')) {
156  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
157 }
158 
159 // Matrix system root directory
160 $argv = $_SERVER['argv'];
161 $SYSTEM_ROOT = (isset($argv[1])) ? $argv[1] : '';
162 if (empty($SYSTEM_ROOT) || !is_dir($SYSTEM_ROOT)) {
163  printUsage();
164  printStdErr("* The path to the System Root must be specified as the first parameter\n");
165  exit(-1);
166 }
167 
168 // Has a CSV filename been supplied?
169 $csv_filename = $argv[2];
170 if (empty($csv_filename)) {
171  printUsage();
172  printStdErr("* A CSV filename must be specified as the second parameter\n");
173  exit(-2);
174 }
175 
176 // Does the supplied CSV file exist?
177 $csv_fd = fopen($csv_filename, 'r');
178 if (!$csv_fd) {
179  printUsage();
180  printStdErr("* The supplied CSV file was not found\n");
181  exit(-3);
182 }
183 
184 // Initialise Matrix
185 require_once $GLOBALS['SYSTEM_ROOT'].'/core/include/init.inc';
186 
187 // Provide the user with one last chance to abort this important operation
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? ');
191 
192 $user_choice = getUserInput(Array('c','o'));
193 if ($user_choice == 'c') {
194  printStdErr("\n- Title modification cancelled by user");
195  exit(-4);
196 }
197 
198 // Forcibly set the title when modifying assets
199 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
200 
201 // Yippee, we have the appropriate file. Let's process it now
202 echo("\n- Assigning titles from CSV file...\n\n");
203 
204 $num_assets_updated = 0;
205 $num_assets_in_file = 0;
206 
207 // Loop through each CSV record
208 while (($data = fgetcsv($csv_fd, 1024, ',')) !== FALSE) {
209  $num_fields = count($data);
210 
211  // Ensure that we have the two columns required for the script
212  if ($num_fields != 2) {
213  printStdErr("* Only two columns (asset ID, asset title) are expected in the supplied CSV file\n");
214  exit(-5);
215  }
216 
217  // The asset ID should be in the first column, and the title should be in the second
218  $data[0] = trim($data[0]);
219 
220  $asset_id = (int)$data[0];
221  $asset_title = trim($data[1]);
222 
223  // Ensure that the asset ID is a valid number which is greater than zero
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");
226  exit(-6);
227  }
228 
229  $num_assets_in_file++;
230 
231  // Now set the title for the asset
232  echo '-- Asset #'.$asset_id.': '.$asset_title."\n";
233  $success = setAssetTitle($asset_id, $asset_title);
234 
235  if ($success) $num_assets_updated++;
236 
237 }
238 
239 // Close the CSV file
240 fclose($csv_fd);
241 
242 // We're done. Display some totals to show what has been accomplished
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";
246 
247 // Restore the Matrix run level
248 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
249 
250 ?>