Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
publish_static_content.php
1 <?php
17 error_reporting(E_ALL);
18 
19 if ((php_sapi_name() != 'cli')) {
20  trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
21 }//end if
22 
23 if (count($_SERVER['argv']) < 4 || count($_SERVER['argv']) > 5) {
24  echo "This script needs to be run in the following format:\n\n";
25  echo "\tphp publish_static.php SYSTEM_ROOT asset_ids STORAGE_PATH [--_nocache]\n\n";
26  echo "\tEg. php scripts/publish_static.php . 21,54,113 /home/static_content\n";
27  exit(1);
28 }
29 
30 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
31 if (empty($SYSTEM_ROOT)) {
32  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
33  exit();
34 }
35 
36 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
37  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
38  exit();
39 }
40 
41 $asset_ids = Array();
42 if (isset($_SERVER['argv'][2])) {
43  $asset_ids = explode(',', $_SERVER['argv'][2]);
44 }
45 
46 $STORAGE_PATH = (isset($_SERVER['argv'][3])) ? $_SERVER['argv'][3] : '';
47 if (empty($STORAGE_PATH) || !is_dir($STORAGE_PATH)) {
48  echo 'ERROR: The directory you specified as the storage root does not exist, or is not a directory';
49  exit();
50 }
51 
52 $_nocache = (isset($_SERVER['argv'][4])) ? $_SERVER['argv'][4] : FALSE;
53 if ($_nocache != '--_nocache') $_nocache = FALSE;
54 
55 require_once $SYSTEM_ROOT.'/core/include/init.inc';
56 
57 $publish_urls = Array();
58 foreach ($asset_ids as $id) {
59  $urls = $GLOBALS['SQ_SYSTEM']->am->getURLs($id);
60  foreach ($urls as $url) {
61  $scheme = ($url['https'] == 1) ? 'https' : FALSE;
62  if ($scheme != FALSE) {
63  $publish_urls[] = $scheme.'://'.$url['url'];
64  if ($_nocache) $publish_urls[] = $scheme.'://'.$url['url'].'/_nocache';
65  }
66  $scheme = ($url['http'] == 1) ? 'http' : FALSE;
67  if ($scheme != FALSE) {
68  $publish_urls[] = $scheme.'://'.$url['url'];
69  if ($_nocache) $publish_urls[] = $scheme.'://'.$url['url'].'/_nocache';
70  }
71  }
72 }
73 
74 _disconnectFromMatrixDatabase();
75 $fork_num = 0;
76 while (!empty($publish_urls)) {
77  $publish_url = array_pop($publish_urls);
78  $pid_prepare = pcntl_fork();
79  $fork_num++;
80  switch ($pid_prepare) {
81  case -1:
82  trigger_error('Process failed to fork while publishing static content', E_USER_ERROR);
83  exit(1);
84  break;
85  case 0:
86  // Connect to DB within the child process
87  _connectToMatrixDatabase();
88 
89  // login as public_user
90  $user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('public_user');
91  if (!$GLOBALS['SQ_SYSTEM']->setCurrentUser($user)) {
92  trigger_error("Failed logging in as public user\n", E_USER_ERROR);
93  exit(1);
94  }
95 
96  render_static_url($publish_url);
97 
98  $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
99  // Disconnect from DB
100  _disconnectFromMatrixDatabase();
101 
102  exit(0);
103  break;
104  default:
105  if (empty($publish_urls)) {
106  // We wait for all the fork child to finish
107  while ($fork_num > 0) {
108  $status = null;
109  pcntl_waitpid(-1, $status);
110  $fork_num--;
111  }//end
112  }//end if
113  break;
114  }//end switch
115 
116 }
117 
118 
119 function render_static_url($url) {
120 
121  global $STORAGE_PATH;
122  global $SYSTEM_ROOT;
123  global $_nocache;
124 
125  // fake some variables required to correctly describe the current url
126  $parts = parse_url($url);
127  $method = 'GET';
128  $protocol = 'HTTP/1.1';
129  $scheme = $parts['scheme'];
130  $host = $parts['host'];
131  $path = $parts['path'];
132  $query = '';
133 
134  $_SERVER['SERVER_PROTOCOL'] = $protocol;
135  $_SERVER['REQUEST_METHOD'] = $method;
136  $_SERVER['QUERY_STRING'] = $query;
137  $_SERVER['REQUEST_URI'] = $path.(empty($query)?'':'?').$query;
138  $_SERVER['SCRIPT_NAME'] = $path;
139  $_SERVER['PHP_SELF'] = $path;
140  $_SERVER['HTTP_USER_AGENT'] = 'Static Site Generator';
141  $_SERVER['HTTP_HOST'] = $host;
142  $_SERVER['SERVER_NAME'] = $host;
143 
144  // ask Matrix to draw as if we were in apache
145  ob_start();
146  require_once $SYSTEM_ROOT.'/core/include/init.inc';
147  $GLOBALS['SQ_SYSTEM']->start();
148  $content = ob_get_clean();
149 
150  $storage_dir = "$STORAGE_PATH/$scheme/$host$path/index.html";
151  if (create_directory(dirname($storage_dir))){
152  file_put_contents($storage_dir, $content);
153  if ($_nocache == FALSE && is_dir(dirname($storage_dir).'/_nocache')) {
154  delete_directory(dirname($storage_dir).'/_nocache');
155  }
156  }
157 }
158 
159 
166 function _disconnectFromMatrixDatabase()
167 {
168  $conn_id = MatrixDAL::getCurrentDbId();
169  if (isset($conn_id) && !empty($conn_id)) {
171  MatrixDAL::dbClose($conn_id);
172  }//end if
173 
174 }//end _disconnectFromMatrixDatabase()
175 
176 
183 function _connectToMatrixDatabase()
184 {
185  $GLOBALS['SQ_SYSTEM']->changeDatabaseConnection('db2');
186 
187 }//end _connectToMatrixDatabase()
188 
189 ?>