ILIAS  Release_3_10_x_branch Revision 61812
 All Data Structures Namespaces Files Functions Variables Groups Pages
ilLinkChecker Class Reference

class for checking external links in page objects Normally used in Cron jobs, but should be extensible for use in learning modules. More...

+ Collaboration diagram for ilLinkChecker:

Public Member Functions

 ilLinkChecker (&$db, $a_validate_all=true)
 setCheckPeriod ($a_period)
 getCheckPeriod ()
 setMailStatus ($a_status)
 getMailStatus ()
 __setType ($a_type)
 __getType ()
 setObjId ($a_page_id)
 getObjId ()
 getValidateAll ()
 getLogMessages ()
 getInvalidLinks ()
 getInvalidLinksFromDB ()
 getLastCheckTimestamp ()
 checkWebResourceLinks ()
 checkLinks ()
 checkPear ()
 __txt ($language, $key, $module= 'common')
 __fetchUserData ($a_usr_id)
 __getTitle ($a_lm_obj_id)
 __sendMail ()
 __getNotifyLinks ()
 __clearInvalidLinks ()
 __appendInvalidLink ($a_link)
 __appendLogMessage ($a_string)
 __clearLogMessages ()
 __getLinks ($a_page)
 __getWebResourceLinks ()
 __validateLinks ($a_links)
 __getObjIdByPageId ($a_page_id)
 __isInvalid ($a_page_id, $a_url)
 __saveInDB ()
 __checkNotify ()
 __clearDBData ()

Data Fields

 $db = null
 $log_messages = array()
 $invalid_links = array()
 $validate_all = true
 $mail_status = false
 $page_id = 0

Detailed Description

class for checking external links in page objects Normally used in Cron jobs, but should be extensible for use in learning modules.

In this case set second parameter of contructor = false, and use setPageObjectId()

Author
Stefan Meyer smeye.nosp@m.r@da.nosp@m.tabay.nosp@m..de
Version
Id:
class.ilLinkChecker.php 15697 2008-01-08 20:04:33Z hschottm

Definition at line 32 of file class.ilLinkChecker.php.

Member Function Documentation

ilLinkChecker::__appendInvalidLink (   $a_link)

Definition at line 392 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
$this->invalid_links[] = $a_link;
}

+ Here is the caller graph for this function:

ilLinkChecker::__appendLogMessage (   $a_string)

Definition at line 398 of file class.ilLinkChecker.php.

Referenced by __sendMail(), __validateLinks(), checkLinks(), and checkWebResourceLinks().

{
$this->log_messages[] = $a_string;
}

+ Here is the caller graph for this function:

ilLinkChecker::__checkNotify ( )

Definition at line 565 of file class.ilLinkChecker.php.

References $res, and getInvalidLinks().

Referenced by __saveInDB().

{
global $ilDB;
foreach($this->getInvalidLinks() as $link)
{
$query = "SELECT * FROM link_check ".
"WHERE page_id = ".$ilDB->quote($link['page_id'])." ".
"AND url = ".$ilDB->quote((substr($link['complete'],0,255)))." ";
$res = $this->db->query($query);
if(!$res->numRows())
{
$this->notify["$link[obj_id]"][] = array('page_id' => $link['page_id'],
'url' => $link['complete']);
}
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__clearDBData ( )

Definition at line 585 of file class.ilLinkChecker.php.

References getObjId(), and getValidateAll().

Referenced by __saveInDB().

{
global $ilDB;
if($this->getValidateAll())
{
$query = "DELETE FROM link_check";
}
else
{
$query = "DELETE FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId())." ";
}
$this->db->query($query);
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__clearInvalidLinks ( )

Definition at line 388 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
$this->invalid_links = array();
}

+ Here is the caller graph for this function:

ilLinkChecker::__clearLogMessages ( )

Definition at line 402 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
return $this->log_messages = array();
}

+ Here is the caller graph for this function:

ilLinkChecker::__fetchUserData (   $a_usr_id)

Definition at line 277 of file class.ilLinkChecker.php.

References $data, and DB_FETCHMODE_OBJECT.

{
global $ilDB;
$query = "SELECT email FROM usr_data WHERE usr_id = ".$ilDB->quote($a_usr_id)."";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
$data['email'] = $row->email;
$query = "SELECT * FROM usr_pref ".
"WHERE usr_id = ".$ilDB->quote($a_usr_id)." ".
"AND keyword = 'language'";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
$data['lang'] = $row->value;
return $data;
}
ilLinkChecker::__getLinks (   $a_page)

Definition at line 407 of file class.ilLinkChecker.php.

References __getObjIdByPageId().

Referenced by checkLinks().

{
$matches = array();
$pattern_complete = '/<ExtLink Href="([^"]*)">/';
if(preg_match_all($pattern_complete,$a_page['content'],$matches))
{
for($i = 0;$i < count($matches[0]); ++$i)
{
$url_data = @parse_url($matches[1][$i]);
// continue if mailto link
if($url_data['scheme'] == 'mailto')
{
continue;
}
// PUH, HTTP_REQUEST needs a beginning http://
if(!$url_data['scheme'])
{
$matches[1][$i] = 'http://'.$matches[1][$i];
}
$lm_id = $this->__getObjIdByPageId($a_page['page_id']);
$link[] = array('page_id' => $a_page['page_id'],
'obj_id' => $lm_id,
'type' => $a_page['type'],
'complete' => $matches[1][$i],
'scheme' => isset($url_data['scheme']) ? $url_data['scheme'] : 'http',
'host' => isset($url_data['host']) ? $url_data['host'] : $url_data['path']);
}
}
return $link ? $link : array();
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__getNotifyLinks ( )

Definition at line 382 of file class.ilLinkChecker.php.

Referenced by __sendMail().

{
return $this->notify ? $this->notify : array();
}

+ Here is the caller graph for this function:

ilLinkChecker::__getObjIdByPageId (   $a_page_id)

Definition at line 512 of file class.ilLinkChecker.php.

References DB_FETCHMODE_OBJECT.

Referenced by __getLinks().

{
$query = "SELECT lm_id FROM lm_data ".
"WHERE obj_id = '".$a_page_id."'";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
return $row->lm_id ? $row->lm_id : 0;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getTitle (   $a_lm_obj_id)

Definition at line 298 of file class.ilLinkChecker.php.

References DB_FETCHMODE_OBJECT.

Referenced by __sendMail().

{
global $ilDB;
$query = "SELECT title FROM object_data ".
"WHERE obj_id = ".$ilDB->quote($a_lm_obj_id)." ";
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
return $row->title;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getType ( )

Definition at line 80 of file class.ilLinkChecker.php.

Referenced by __sendMail().

{
return $this->type;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getWebResourceLinks ( )

Definition at line 442 of file class.ilLinkChecker.php.

References getObjId().

Referenced by checkWebResourceLinks().

{
include_once 'Modules/WebResource/classes/class.ilLinkResourceItems.php';
$link_res_obj = new ilLinkResourceItems($this->getObjId());
foreach($check_links = $link_res_obj->getCheckItems($this->getCheckPeriod()) as $item_data)
{
$url_data = @parse_url($item_data['target']);
// PUH, HTTP_REQUEST needs a beginning http://
if(!$url_data['scheme'])
{
$item_data['target'] = 'http://'.$item_data['target'];
}
$link[] = array('page_id' => $item_data['link_id'],
'obj_id' => $this->getObjId(),
'type' => 'webr',
'complete' => $item_data['target'],
'scheme' => isset($url_data['scheme']) ? $url_data['scheme'] : 'http',
'host' => isset($url_data['host']) ? $url_data['host'] : $url_data['path']);
}
return $link ? $link : array();
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__isInvalid (   $a_page_id,
  $a_url 
)

Definition at line 522 of file class.ilLinkChecker.php.

References getInvalidLinks().

{
foreach($this->getInvalidLinks() as $link)
{
if($link['page_id'] == $a_page_id and
substr($link['complete'],0,255) == $a_url)
{
return true;
}
}
return false;
}

+ Here is the call graph for this function:

ilLinkChecker::__saveInDB ( )

Definition at line 535 of file class.ilLinkChecker.php.

References $res, __checkNotify(), __clearDBData(), getInvalidLinks(), and getMailStatus().

Referenced by checkLinks(), and checkWebResourceLinks().

{
global $ilDB;
if($this->getMailStatus())
{
$this->__checkNotify();
}
$this->__clearDBData();
foreach($this->getInvalidLinks() as $link)
{
$query = "INSERT INTO link_check ".
"SET page_id = ".$ilDB->quote($link['page_id']).", ".
"obj_id = ".$ilDB->quote($link['obj_id']).", ".
"url = ".$ilDB->quote((substr($link['complete'],0,255))).", ".
"parent_type = ".$ilDB->quote(($link['type'])).", ".
"http_status_code = ".$ilDB->quote(($link['http_status_code'])).", ".
"last_check = '".time()."'";
$res = $this->db->query($query);
}
// delete old values
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__sendMail ( )

Definition at line 310 of file class.ilLinkChecker.php.

References $data, __appendLogMessage(), __getNotifyLinks(), __getTitle(), __getType(), __txt(), ilLinkCheckNotify\_getAllNotifiers(), ilObjectFactory\getInstanceByObjId(), and getMailStatus().

Referenced by checkLinks(), and checkWebResourceLinks().

{
global $ilUser;
if(!count($notify = $this->__getNotifyLinks()))
{
// Nothing to do
return true;
}
if(!$this->getMailStatus())
{
return true;
}
include_once './classes/class.ilLinkCheckNotify.php';
foreach(ilLinkCheckNotify::_getAllNotifiers($this->db) as $usr_id => $obj_ids)
{
if(!is_object($tmp_user =& ilObjectFactory::getInstanceByObjId($usr_id,false)))
{
$this->__appendLogMessage('LinkChecker: Cannot find user with id: '.$usr_id);
continue;
}
$counter = 0;
foreach($obj_ids as $obj_id)
{
if(!isset($notify[$obj_id]))
{
continue;
}
++$counter;
switch($this->__getType())
{
case 'webr':
$body .= $this->__txt($tmp_user->getLanguage(),'obj_webr');
break;
case 'lm':
default:
$body .= $this->__txt($tmp_user->getLanguage(),'lo');
break;
}
$body .= ': ';
$body .= $this->__getTitle($obj_id)."\r\n";
// Print all invalid
foreach($notify[$obj_id] as $data)
{
$body .= $data['url']."\r\n";
}
$body .= "\r\n";
}
if($counter)
{
include_once "Services/Mail/classes/class.ilFormatMail.php";
$umail = new ilFormatMail($tmp_user->getId());
$subject = $this->__txt($tmp_user->getLanguage(),'link_check_subject');
$umail->sendMail($tmp_user->getLogin(),"","",$subject,$body,array(),array("normal"));
$this->__appendLogMessage('LinkChecker: Sent mail to '.$tmp_user->getEmail());
}
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__setType (   $a_type)

Definition at line 76 of file class.ilLinkChecker.php.

Referenced by checkLinks(), and checkWebResourceLinks().

{
$this->type = $a_type;
}

+ Here is the caller graph for this function:

ilLinkChecker::__txt (   $language,
  $key,
  $module = 'common' 
)

Definition at line 247 of file class.ilLinkChecker.php.

References $res, and DB_FETCHMODE_OBJECT.

Referenced by __sendMail().

{
global $ilDB;
$query = "SELECT value FROM lng_data ".
"WHERE module = ".$ilDB->quote($module)." ".
"AND identifier = ".$ilDB->quote($key)." ".
"AND lang_key = ".$ilDB->quote($language)."";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$value = $row->value;
}
if(!$value)
{
$query = "SELECT value FROM lng_data ".
"WHERE module = ".$ilDB->quote($module)." ".
"AND identifier = ".$ilDB->quote($key)." ".
"AND lang_key = 'en'";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$value = $row->value;
}
}
return $value ? $value : '-'.$key.'-';
}

+ Here is the caller graph for this function:

ilLinkChecker::__validateLinks (   $a_links)

Definition at line 470 of file class.ilLinkChecker.php.

References __appendLogMessage().

Referenced by checkLinks(), and checkWebResourceLinks().

{
if(!@include_once('HTTP/Request.php'))
{
$this->__appendLogMessage('LinkChecker: Pear HTTP_Request is not installed. Aborting');
return array();
}
foreach($a_links as $link)
{
if(gethostbyname($link['host']) == $link['host'])
{
$invalid[] = $link;
continue;
}
if($link['scheme'] !== 'http' and $link['scheme'] !== 'https')
{
continue;
}
$req =& new HTTP_Request($link['complete']);
$req->sendRequest();
switch($req->getResponseCode())
{
// EVERYTHING OK
case '200':
// In the moment 301 will be handled as ok
case '301':
case '302':
break;
default:
$link['http_status_code'] = $req->getResponseCode();
$invalid[] = $link;
break;
}
}
return $invalid ? $invalid : array();
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::checkLinks ( )

Definition at line 170 of file class.ilLinkChecker.php.

References $res, __appendInvalidLink(), __appendLogMessage(), __clearInvalidLinks(), __clearLogMessages(), __getLinks(), __saveInDB(), __sendMail(), __setType(), __validateLinks(), DB_FETCHMODE_OBJECT, getInvalidLinks(), getObjId(), and getValidateAll().

{
global $ilDB;
$pages = array();
$this->__setType('lm');
$this->__appendLogMessage('LinkChecker: Start checkLinks()');
if(!$this->getValidateAll() and !$this->getObjId())
{
echo "ilLinkChecker::checkLinks() No Page id given";
return false;
}
elseif(!$this->getValidateAll() and $this->getObjId())
{
$query = "SELECT * FROM page_object ".
"WHERE parent_id = ".$ilDB->quote($this->getObjId())." ".
"AND parent_type = 'lm'";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$pages[] = array('page_id' => $row->page_id,
'content' => $row->content,
'type' => $row->parent_type);
}
}
elseif($this->getValidateAll())
{
$query = "SELECT * FROM page_object ".
"WHERE parent_type = 'lm'";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$pages[] = array('page_id' => $row->page_id,
'content' => $row->content,
'type' => $row->parent_type);
}
}
// VALIDATE
foreach($pages as $page)
{
if(count($invalid = $this->__validateLinks($this->__getLinks($page))))
{
foreach($invalid as $invalid_item)
{
$this->__appendLogMessage('LinkChecker: found invalid link: '.$invalid_item['complete']);
$this->__appendInvalidLink($invalid_item);
}
}
}
$this->__appendLogMessage('LinkChecker: End checkLinks()');
$this->__saveInDB();
$this->__sendMail();
return $this->getInvalidLinks();
}

+ Here is the call graph for this function:

ilLinkChecker::checkPear ( )

Definition at line 236 of file class.ilLinkChecker.php.

{
if(!@include_once('HTTP/Request.php'))
{
return false;
}
return true;
}
ilLinkChecker::checkWebResourceLinks ( )

Definition at line 144 of file class.ilLinkChecker.php.

References __appendInvalidLink(), __appendLogMessage(), __clearInvalidLinks(), __clearLogMessages(), __getWebResourceLinks(), __saveInDB(), __sendMail(), __setType(), __validateLinks(), and getInvalidLinks().

{
$pages = array();
$this->__setType('webr');
$this->__appendLogMessage('LinkChecker: Start checkLinks()');
if(count($invalid = $this->__validateLinks($this->__getWebResourceLinks())))
{
foreach($invalid as $invalid_item)
{
$this->__appendLogMessage('LinkChecker: found invalid link: '.$invalid_item['complete']);
$this->__appendInvalidLink($invalid_item);
}
}
$this->__appendLogMessage('LinkChecker: End checkLinks()');
$this->__saveInDB();
$this->__sendMail();
return $this->getInvalidLinks();
}

+ Here is the call graph for this function:

ilLinkChecker::getCheckPeriod ( )

Definition at line 62 of file class.ilLinkChecker.php.

{
return $this->period;
}
ilLinkChecker::getInvalidLinks ( )

Definition at line 104 of file class.ilLinkChecker.php.

Referenced by __checkNotify(), __isInvalid(), __saveInDB(), checkLinks(), and checkWebResourceLinks().

{
return $this->invalid_links ? $this->invalid_links : array();
}

+ Here is the caller graph for this function:

ilLinkChecker::getInvalidLinksFromDB ( )

Definition at line 109 of file class.ilLinkChecker.php.

References $res, DB_FETCHMODE_OBJECT, and getObjId().

{
global $ilDB;
$query = "SELECT * FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId())." ";
$res = $this->db->query($query);
while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
{
$invalid[] = array('page_id' => $row->page_id,
'url' => $row->url);
}
return $invalid ? $invalid : array();
}

+ Here is the call graph for this function:

ilLinkChecker::getLastCheckTimestamp ( )

Definition at line 126 of file class.ilLinkChecker.php.

References DB_FETCHMODE_OBJECT, getObjId(), and getValidateAll().

{
global $ilDB;
if($this->getValidateAll())
{
$query = "SELECT MAX(last_check) as last_check FROM link_check ";
}
else
{
$query = "SELECT MAX(last_check) as last_check FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId())." ";
}
$row = $this->db->getRow($query,DB_FETCHMODE_OBJECT);
return $row->last_check ? $row->last_check : 0;
}

+ Here is the call graph for this function:

ilLinkChecker::getLogMessages ( )

Definition at line 99 of file class.ilLinkChecker.php.

{
return $this->log_messages ? $this->log_messages : array();
}
ilLinkChecker::getMailStatus ( )

Definition at line 71 of file class.ilLinkChecker.php.

References $mail_status.

Referenced by __saveInDB(), and __sendMail().

{
return (bool) $this->mail_status;
}

+ Here is the caller graph for this function:

ilLinkChecker::getObjId ( )

Definition at line 89 of file class.ilLinkChecker.php.

References $page_id.

Referenced by __clearDBData(), __getWebResourceLinks(), checkLinks(), getInvalidLinksFromDB(), and getLastCheckTimestamp().

{
}

+ Here is the caller graph for this function:

ilLinkChecker::getValidateAll ( )

Definition at line 94 of file class.ilLinkChecker.php.

Referenced by __clearDBData(), checkLinks(), and getLastCheckTimestamp().

{
return $this->validate_all ? true : false;
}

+ Here is the caller graph for this function:

ilLinkChecker::ilLinkChecker ( $db,
  $a_validate_all = true 
)

Definition at line 43 of file class.ilLinkChecker.php.

References $db.

{
global $ilDB;
define('DEBUG',1);
define('SOCKET_TIMEOUT',5);
$this->db =& $db;
// SET GLOBAL DB HANDLER FOR STATIC METHODS OTHER CLASSES
$ilDB =& $db;
$this->validate_all = $a_validate_all;
}
ilLinkChecker::setCheckPeriod (   $a_period)

Definition at line 58 of file class.ilLinkChecker.php.

{
$this->period = $a_period;
}
ilLinkChecker::setMailStatus (   $a_status)

Definition at line 67 of file class.ilLinkChecker.php.

{
$this->mail_status = (bool) $a_status;
}
ilLinkChecker::setObjId (   $a_page_id)

Definition at line 85 of file class.ilLinkChecker.php.

{
return $this->page_id = $a_page_id;
}

Field Documentation

ilLinkChecker::$db = null

Definition at line 34 of file class.ilLinkChecker.php.

Referenced by ilLinkChecker().

ilLinkChecker::$invalid_links = array()

Definition at line 36 of file class.ilLinkChecker.php.

ilLinkChecker::$log_messages = array()

Definition at line 35 of file class.ilLinkChecker.php.

ilLinkChecker::$mail_status = false

Definition at line 39 of file class.ilLinkChecker.php.

Referenced by getMailStatus().

ilLinkChecker::$page_id = 0

Definition at line 40 of file class.ilLinkChecker.php.

Referenced by getObjId().

ilLinkChecker::$validate_all = true

Definition at line 38 of file class.ilLinkChecker.php.


The documentation for this class was generated from the following file: