Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
upgrade_safe_editing_for_thumbnails.php
1 <?php
28 error_reporting(E_ALL);
29 if ((php_sapi_name() != 'cli')) {
30  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
31 }
32 
33 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
34 if (empty($SYSTEM_ROOT)) {
35  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
36  exit();
37 }
38 
39 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
40  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
41  exit();
42 }
43 
44 require_once $SYSTEM_ROOT.'/core/include/init.inc';
45 require_once SQ_FUDGE_PATH.'/general/file_system.inc';
46 
47 // log in as root
48 $root_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('root_user');
49 if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($root_user)) {
50  echo "ERROR: Failed logging in as root user\n";
51  exit();
52 }
53 
54 // forced run level
55 $GLOBALS['SQ_SYSTEM']->setRunLevel(SQ_RUN_LEVEL_FORCED);
56 
57 $am = $GLOBALS['SQ_SYSTEM']->am;
58 $count = 0;
59 $done_asset = 1;
60 $bad_assetids = Array();
61 
62 // get all the assets in the system who are in safe edit status and have a thumbnail attached to it
63 $sql = "SELECT assetid from sq_ast where status = '64' and assetid IN (select majorid from sq_ast_lnk where value = 'thumbnail')";
64 
65 $results = MatrixDAL::executeSqlAssoc($sql);
66 echo "Found ".count($results)." assets that are in 'Safe Editing' status and has thumbnail applied.\n";
67 
68 foreach ($results as $result) {
69  $content = Array();
70  echo "Updating $done_asset of ".count($results)." Assets\r";
71  $assetid = $result['assetid'];
72  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($assetid);
73  $notice_link = $GLOBALS['SQ_SYSTEM']->am->getLink($assetid, SQ_LINK_NOTICE, '', TRUE, 'thumbnail');
74 
75  // if file exists, write to it else create new one
76  if (file_exists($asset->data_path.'/.sq_system/.sq_notice_links')) {
77  $content = unserialize(file_to_string($asset->data_path.'/.sq_system/.sq_notice_links'));
78 
79  $thumbnail_exists = FALSE;
80  foreach ($content as $link) {
81  if ($link['value'] == 'thumbnail') {
82  $thumbnail_exists = TRUE;
83  break;
84  }
85  }
86 
87  $content[] = $notice_link;
88  if (!$thumbnail_exists && !string_to_file(serialize($content), $asset->data_path.'/.sq_system/.sq_notice_links')) {
89  print_r('Still writing :(');
90  $count++;
91  $bad_assetids[] = $assetid;
92  }
93  } else {
94  $content[] = $notice_link;
95  if (!string_to_file(serialize($content), $asset->data_path.'/.sq_system/.sq_notice_links')) {
96  $count++;
97  $bad_assetids[] = $assetid;
98  }
99  }
100  $done_asset++;
101 
102  unset($assetid);
103 }
104 
105 
106 // Warn the user if an error happened
107 if (!empty($count)) {
108  echo "There were $count errors in creating .sq_notice_links files.\nThese assets may need to be reviewed manually.\n";
109  echo "Assetids that need to be looked into (These are in error.log too):\n";
110  print_r($bad_assetids);
111 
112  // put them in error.log too
113  log_dump("There were ".$count." errors in creating .sq_notice_links files.\nThese assets may need to be reviewed manually.");
114  log_dump("Assetids that need to be looked into :");
115  log_dump($bad_assetids);
116 } else {
117  echo "Successfully upgraded ".count($results)." assets.\n";
118 }
119 
120 $GLOBALS['SQ_SYSTEM']->restoreRunLevel();
121 
122 
123 ?>