ILIAS  Release_4_0_x_branch Revision 61816
 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 21679 2009-09-15 13:20:59Z smeyer

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

Member Function Documentation

ilLinkChecker::__appendInvalidLink (   $a_link)

Definition at line 351 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 357 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 520 of file class.ilLinkChecker.php.

References $ilDB, $query, $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'],'integer')." ".
"AND url = ".$ilDB->quote(substr($link['complete'],0,255),'text')." ";
$res = $ilDB->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 540 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, 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(),'integer');
}
$res = $ilDB->manipulate($query);
return true;
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__clearInvalidLinks ( )

Definition at line 347 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 361 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 236 of file class.ilLinkChecker.php.

References $data, $ilDB, $query, $row, 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;
$set = $ilDB->query("SELECT * FROM usr_pref ".
"WHERE usr_id = ".$ilDB->quote($a_usr_id, "integer")." ".
"AND keyword = ".$ilDB->quote('language', "text"));
$row = $ilDB->fetchObject($set);
$data['lang'] = $row->value;
return $data;
}
ilLinkChecker::__getLinks (   $a_page)

Definition at line 366 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 341 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 471 of file class.ilLinkChecker.php.

References $query, $row, and 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 257 of file class.ilLinkChecker.php.

References $ilDB, $query, $row, and DB_FETCHMODE_OBJECT.

Referenced by __sendMail().

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

+ Here is the caller graph for this function:

ilLinkChecker::__getType ( )

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

References $type.

Referenced by __sendMail().

{
return $this->type;
}

+ Here is the caller graph for this function:

ilLinkChecker::__getWebResourceLinks ( )

Definition at line 401 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 481 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 494 of file class.ilLinkChecker.php.

References $ilDB, $query, $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 (obj_id,page_id,url,parent_type,http_status_code,last_check) ".
"VALUES ( ".
$ilDB->quote($link['obj_id'],'integer').", ".
$ilDB->quote($link['page_id'],'integer').", ".
$ilDB->quote(substr($link['complete'],0,255),'text').", ".
$ilDB->quote($link['type'],'text').", ".
$ilDB->quote($link['http_status_code'] ? $link['http_status_code'] : 0,'integer').", ".
$ilDB->quote(time(),'integer')." ".
")";
$res = $ilDB->manipulate($query);
}
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__sendMail ( )

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

References $data, $usr_id, __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 56 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 228 of file class.ilLinkChecker.php.

References $ilDB, $key, and ilLanguage\_lookupEntry().

Referenced by __sendMail().

{
global $ilDB;
include_once './Services/Language/classes/class.ilLanguage.php';
return ilLanguage::_lookupEntry($language, $module, $key);
}

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

ilLinkChecker::__validateLinks (   $a_links)

Definition at line 429 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 151 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, $row, __appendInvalidLink(), __appendLogMessage(), __clearInvalidLinks(), __clearLogMessages(), __getLinks(), __saveInDB(), __sendMail(), __setType(), __validateLinks(), DB_FETCHMODE_OBJECT, elseif(), 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);
}
}
{
$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 217 of file class.ilLinkChecker.php.

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

Definition at line 125 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 42 of file class.ilLinkChecker.php.

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

Definition at line 84 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 89 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, $row, DB_FETCHMODE_OBJECT, and getObjId().

{
global $ilDB;
$query = "SELECT * FROM link_check ".
"WHERE obj_id = ".$ilDB->quote($this->getObjId(),'integer')." ";
$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 106 of file class.ilLinkChecker.php.

References $ilDB, $query, $res, $row, getObjId(), and getValidateAll().

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

+ Here is the call graph for this function:

ilLinkChecker::getLogMessages ( )

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

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

Definition at line 51 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 69 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 74 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 23 of file class.ilLinkChecker.php.

References $db, and $ilDB.

{
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 38 of file class.ilLinkChecker.php.

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

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

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

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

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

Field Documentation

ilLinkChecker::$db = null

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

Referenced by ilLinkChecker().

ilLinkChecker::$invalid_links = array()

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

ilLinkChecker::$log_messages = array()

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

ilLinkChecker::$mail_status = false

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

Referenced by getMailStatus().

ilLinkChecker::$page_id = 0

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

Referenced by getObjId().

ilLinkChecker::$validate_all = true

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


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