Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
report_links.inc
1 <?php
18 require_once SQ_CORE_PACKAGE_PATH.'/report/report.inc';
19 
31 register_implementation('report_links', 'bridge');
32 class Report_Links extends Report
33 {
34 
35 
42  function __construct($assetid=0)
43  {
44  $this->_ser_attrs = TRUE;
45  parent::__construct($assetid);
46 
47  }//end constructor
48 
49 
71  function getLinks($assetid, $link_types, $type_code='', $strict_type_code=TRUE, $side_of_link='major', $sort_by=NULL)
72  {
73  assert_equals($assetid, $this->id, 'The Links Report can only get its own links');
74 
75  if (!isset($this->_tmp['getLinks'])) {
76 
77  $am = $GLOBALS['SQ_SYSTEM']->am;
78  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
79  $report_path = $this->data_path.'/report.xml';
80  if (!is_file($report_path)) return Array();
81 
82  try {
83  $root = @new SimpleXMLElement($report_path, LIBXML_NOCDATA, TRUE);
84  } catch (Exception $e) {
85  trigger_error('Unable to parse report file "'.$report_path.'": '.$e->getMessage());
86  return Array();
87  }
88 
89  $links = Array();
90  $sort_order = 0;
91  foreach ($root->asset as $asset_element) {
92  $links[] = Array(
93  'linkid' => 0,
94  'minorid' => (int) $asset_element->attributes()->assetid,
95  'value' => '',
96  'link_type' => SQ_LINK_TYPE_1,
97  'minor_type_code' => (string) $asset_element->attributes()->type_code,
98  'sort_order' => $sort_order,
99  'is_dependant' => '0',
100  'is_exclusive' => '0',
101  );
102 
103  $sort_order++;
104  }
105 
106  $this->_tmp['getLinks'] = $links;
107 
108  }//end if not cached
109 
110  return $this->_tmp['getLinks'];
111 
112  }//end getLinks()
113 
114 
128  function getParents($assetid, $type_code='', $strict_type_code=TRUE)
129  {
130  assert_equals($assetid, $this->id, 'The Links Report can only get its own links');
131  return Array();
132 
133  }//end getParents()
134 
135 
156  function getChildren($assetid, $type_code='', $strict_type_code=TRUE, $dependant=NULL, $sort_by=NULL)
157  {
158  assert_equals($assetid, $this->id, 'The Links Report can only get its own links');
159  return Array();
160 
161  }//end getChildren()
162 
163 
174  function getLineageFromURL($assetid, $protocol, $url)
175  {
176  return Array();
177 
178  }//end getLineageFromURL()
179 
180 
191  function parseReport($show_all=FALSE)
192  {
193  require_once SQ_INCLUDE_PATH.'/general_occasional.inc';
194  require_once SQ_FUDGE_PATH.'/general/general.inc';
195 
196  $report_path = $this->data_path.'/report.xml';
197  if (!is_file($report_path)) {
198  echo translate('report_not_generated');
199  return Array();
200  }
201 
202  try {
203  $root = @new SimpleXMLElement($report_path, LIBXML_NOCDATA, TRUE);
204  } catch (Exception $e) {
205  trigger_error('Unable to parse report file "'.$report_path.'": '.$e->getMessage());
206  return Array();
207  }
208 
209  $reported_links = Array(
210  'internal' => Array(),
211  'external' => Array(),
212  'mailto' => Array(),
213  );
214 
215  $display_options = $this->attr('display_options');
216  $status_options = $this->attr('statuses');
217 
218  foreach ($root->asset as $asset_element) {
219  // only list links of assets which are of status required
220  if (!in_array($asset_element->attributes()->status,$status_options)) continue;
221  foreach ($asset_element->link as $link) {
222  $type = (string) $link->attributes()->type;
223  assert_isset_array_index($reported_links, $type, 'Unknown link type "'.$type.'"');
224 
225  $new_link = Array();
226  $new_link['html'] = (string) $link->html;
227 
228  // get the assetid from the html link
229  if ($type == 'internal') {
230  $to_assetid = preg_replace('|\./\?a=([0-9]+).*|', '\\1', (string) $link->html);
231  $new_link['data'] = $to_assetid;
232  } else {
233  $new_link['data'] = (string) $link->html;
234  }
235  $new_link['description'] = htmlentities((string) $link->description);
236 
237  $valid = (int) $link->attributes()->valid;
238  $new_link['valid'] = $valid;
239 
240  $display_link = TRUE;
241  switch ($type) {
242  case 'internal':
243  if ($valid) {
244  if (!$display_options['internal_working']) {
245  $display_link = FALSE;
246  }
247  } else {
248  if (!$display_options['internal_broken']) {
249  $display_link = FALSE;
250  }
251  }
252  break;
253  case 'external':
254  if ($valid) {
255  if (!$display_options['external_working']) {
256  $display_link = FALSE;
257  }
258  } else {
259  if (!$display_options['external_broken']) {
260  $display_link = FALSE;
261  }
262  }
263  break;
264  case 'mailto':
265  if ($valid) {
266  if (!$display_options['email_working']) {
267  $display_link = FALSE;
268  }
269  } else {
270  if (!$display_options['email_broken']) {
271  $display_link = FALSE;
272  }
273  }
274  break;
275  default:
276  break;
277  }//end switch
278  if ($display_link || $show_all) {
279  $reported_links[$type][(int) $asset_element->attributes()->assetid][] = $new_link;
280  }
281  }//end foreach
282  }//end foreach
283 
284  return $reported_links;
285 
286  }//end parseReport()
287 
288 
295  function reportBrokenLink()
296  {
297  $reported_links = $this->parseReport(TRUE);
298  if (empty($reported_links)) return FALSE;
299 
300  $asset_admins = Array();
301  $msg_reps = Array(
302  'link_report_name' => $this->name,
303  'link_report_id' => $this->id,
304  );
305 
306  $ms = $GLOBALS['SQ_SYSTEM']->getMessagingService();
307  $ms->openQueue();
308  foreach ($reported_links as $link_type => $info) {
309  $msg_reps['type'] = $link_type;
310 
311  foreach ($info as $assetid => $link_info) {
312  $msg_reps['assetid'] = $assetid;
313 
314  foreach ($link_info as $link_details) {
315  if (!$link_details['valid']) {
316  if (!isset($asset_admins[$assetid])) {
317  // index admins id in array, so that we do not do that same work more than once
318  $asset_admins[$assetid] = $GLOBALS['SQ_SYSTEM']->am->getPermission($assetid, SQ_PERMISSION_ADMIN, TRUE, FALSE, TRUE, FALSE, FALSE);
319  }
320  $msg_reps['link'] = $link_details['html'];
321  $msg_reps['description'] = $link_details['description'];
322 
323  if (!empty($asset_admins[$assetid])) {
324  $msg = $ms->newMessage();
325  $msg->to = $asset_admins[$assetid];
326  $msg->from = 0;
327  $msg->type = 'asset.report.brokenlink';
328  $msg->replacements = $msg_reps;
329  $ms->enqueueMessage($msg);
330  }
331  }
332  }
333 
334  }
335  }
336  $ms->closeQueue();
337 
338  // update the report info, so that the user cannot send multiple notification to asset admin
339  // disable the 'Report Broken Links Now' button if already sent
340  $this->setAttrValue('report_info', translate('cms_report_links_broken_info', $this->attr('generated_date')));
341  $this->saveAttributes();
342 
343  return TRUE;
344 
345  }//end reportBrokenLink()
346 
347 
348 }//end class
349 
350 ?>