Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
cron_job_refresh_cache.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/system/cron/cron_job/cron_job.inc';
19 
34 {
35 
36 
43  function __construct($assetid=0)
44  {
45  parent::__construct($assetid);
46 
47  }//end constructor
48 
49 
59  protected function _getName($short_name=FALSE)
60  {
61  $id = $this->attr('asset');
62  if ($GLOBALS['SQ_SYSTEM']->am->assetExists($id)) {
63  $asset = $GLOBALS['SQ_SYSTEM']->am->getAsset($id);
64  $name = $asset->name;
65  }
66  return "Refresh Cache \"$name\" [#$asset->id]";
67 
68  }//end _getName()
69 
70 
84  protected function _exec(&$msg)
85  {
86  // Caching Off? Do nothing
87  $cm = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('cache_manager');
88  if (!$cm->attr('enabled')) {
89  return SQ_CRON_JOB_COMPLETED;
90  }
91  $am = $GLOBALS['SQ_SYSTEM']->am;
92 
93  // Start
94  $this->setAttrValue('running', TRUE);
95  $this->saveAttributes();
96 
97  // Clear the existing cache
98  $assetid = $this->attr('asset');
99  $cm->clearCache(Array($assetid));
100  $asset = $am->getAsset($assetid);
101  $urls_info = $asset->getURLs();
102  if (empty($urls_info)) {
103  return SQ_CRON_JOB_COMPLETED;
104  } else {
105  foreach ($urls_info as $url_info) {
106  if ($url_info['http'] == '1') {
107  $urls[] = 'http://'.$url_info['url'];
108  }
109  if ($url_info['https'] == '1') {
110  $urls[] = 'https://'.$url_info['url'];
111  }
112  }
113  }
114 
115  // Request each URL as public user
116  $public_user = $GLOBALS['SQ_SYSTEM']->am->getSystemAsset('public_user');
117  $GLOBALS['SQ_SYSTEM']->setCurrentUser($public_user);
118 
119  // Re-generate the cache per URL
120  foreach ($urls as $url) {
121  $url_info = parse_url($url);
122 
123  // Let's pretend this is web accessed
124  $_SERVER['HTTP_HOST'] = $url_info['host'];
125  $_SERVER['PHP_SELF'] = $url_info['path'];
126  $_SERVER['HTTP'] = ($url_info['scheme'] == 'http') ? 'on' : 'off';
127  $_SERVER['HTTPS'] = ($url_info['scheme'] == 'https') ? 'on' : 'off';
128 
129  // Re-generate cache
130  ob_start();
131  $asset->printBody();
132  ob_end_clean();
133  }
134  $GLOBALS['SQ_SYSTEM']->restoreCurrentUser();
135 
136  return SQ_CRON_JOB_COMPLETED;
137 
138  }//end _exec()
139 
140 
147  public function run()
148  {
149  $res = parent::run();
150  if ($res & SQ_CRON_JOB_COMPLETED) {
151  if ($GLOBALS['SQ_SYSTEM']->am->acquireLock($this->id, 'all')) {
152  $this->setAttrValue('running', FALSE);
153  $this->saveAttributes();
154  $GLOBALS['SQ_SYSTEM']->am->releaseLock($this->id, 'all');
155  }
156  }
157 
158  return $res;
159 
160  }//end run()
161 
162 
163 }//end class
164 
165 
166 ?>