Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Channel Class Reference

Public Member Functions

 __construct ($name)
 isNew ()
 getExt ($filename)
 processWork ()
 processNews ($xml, $xml_filename=null, $specific=false)
 processMeldung ($parserXML, $xml_filename=null)
 processBild ($parserXML, $xml_filename=null)
 processRemoval ($xml, $xml_filename=null)
 clearOld ()
 removeFilesByName ($files)
 checkMMParents ()
 makeRelations ()
 makePath ($folder, $file)
 makeWorkPath ($file)
 makeDonePath ($file)
 makeErrorPath ($file)
 makeImagesPath ($file)
 makeVideosPath ($file)
 moveToWork ()
 moveFileToDone ($file)
 moveFileToError ($file)
 moveDoneFileToError ($file)

Public Attributes

 $name
 $fertigPath
 $path
 $workPath
 $donePath
 $errorPath
 $wwwPath
 $imagesPath
 $videosPath

Static Public Attributes

static $instance

Detailed Description

Created by JetBrains PhpStorm. User: Andrew Date: 07.06.13 Time: 19:35 To change this template use File | Settings | File Templates. Class for process a channel (import news from xml into database)

Author
Andrew Voloshin andre.nosp@m.w.vo.nosp@m.loshi.nosp@m.n@ob.nosp@m.erig..nosp@m.com

all parameters will be described in the part of current channel because 1 object of this class = 1 channel !!!

Definition at line 20 of file Channel.php.

Constructor & Destructor Documentation

Channel::__construct (   $name)

Constructor initializes all parameters and makes necessary folders if they are not

Definition at line 93 of file Channel.php.

References isNew().

{
$time = time();
$this->log_filename .= "log_{$time}.txt";
$this->name = $name;
$channelAlias = "apalines.content.$name";
$module = Yii::app()->getModule('apalines');
if(isset($module->channels[$name]['delivery_path'])){
$channelAlias = $module->channels[$name]['delivery_path'];
}
$this->path = Yii::getPathOfAlias($channelAlias);
$this->fertigPath = $this->path.DIRECTORY_SEPARATOR.'fertig.txt';
if (file_exists($this->fertigPath)) {
unlink($this->fertigPath);
$this->isNew = true;
}
//Log router
if (file_exists($this->path)) {
Yii::app()->log->routes['file']->setLogPath($this->path);
Yii::app()->log->routes['file']->setLogFile($this->log_filename);
}
//Uncomment to overwrite log file
/*
if(file_exists($this->makePath($this->path,$this->log_filename))){
unlink($this->makePath($this->path,$this->log_filename));
}
**/
$this->workPath = Yii::getPathOfAlias("$channelAlias.work");
$this->donePath = Yii::getPathOfAlias("$channelAlias.done");
$this->errorPath = Yii::getPathOfAlias("$channelAlias.error");
$wwwApaPath = Yii::getPathOfAlias("site.frontend.www.apalines");
$this->wwwPath = Yii::getPathOfAlias("site.frontend.www.apalines.$name");
$this->imagesPath = Yii::getPathOfAlias("site.frontend.www.apalines.$name.images");
$this->videosPath = Yii::getPathOfAlias("site.frontend.www.apalines.$name.videos");
if (file_exists($this->path)) {
if (!file_exists($this->workPath)) {
mkdir($this->workPath);
}
if (!file_exists($this->donePath)) {
mkdir($this->donePath);
}
if (!file_exists($this->errorPath)) {
mkdir($this->errorPath);
}
if (!file_exists($wwwApaPath)) {
mkdir($wwwApaPath);
}
if (!file_exists($this->wwwPath)) {
mkdir($this->wwwPath);
}
if (!file_exists($this->imagesPath)) {
mkdir($this->imagesPath);
}
if (!file_exists($this->videosPath)) {
mkdir($this->videosPath);
}
}
self::$instance = $this;
}

Member Function Documentation

Channel::checkMMParents ( )

Function removes multimedia records which have not parent news

Definition at line 483 of file Channel.php.

References ApalinesMM\model().

Referenced by processWork().

{
$multimedia = ApalinesMM::model()->findAll('channel = :channel AND deleted=0 AND related_maindoc_id=0', array(':channel'=> $this->name));
foreach ($multimedia as $mm) {
$mm->delete();
}
}
Channel::clearOld ( )

Function removes news and child news,files those lifetime is up

Definition at line 437 of file Channel.php.

References ApalinesNews\model().

{
$module = Yii::app()->getModule('apalines');
$max_age_of_news = false;
if (isset($module->channels[$this->name]['max_age_of_news'])) {
$max_age_of_news = $module->channels[$this->name]['max_age_of_news'];
}
if (!$max_age_of_news && isset($module->total_max_age_of_news)) {
$max_age_of_news = $module->total_max_age_of_news;
}
if (!$max_age_of_news) {
return;
}
$news = ApalinesNews::model()->findAll(array(
'condition' => 'channel = :channel AND deleted <> 1 AND (FROM_UNIXTIME(ndate) < (NOW() - interval :max_age_of_news hour))',
'params' => array(
':channel' => $this->name,
':max_age_of_news' => $max_age_of_news
)
));
foreach ($news as $line) {
$line->delete();
}
}
Channel::getExt (   $filename)

Function returns extension of filename

Parameters
string$filename- name of file
Returns
string - extension

Definition at line 175 of file Channel.php.

Referenced by processWork().

{
return substr(strrchr($filename, '.'), 1);
}
Channel::isNew ( )

Function checks whether channel has updates

Returns
boolean new or not

Definition at line 164 of file Channel.php.

Referenced by __construct().

{
return $this->isNew;
}
Channel::makeDonePath (   $file)

Function makes path to file which placed in done folder

Parameters
string$filename of the file
Returns
string full path to the file

Definition at line 548 of file Channel.php.

References makePath().

Referenced by moveDoneFileToError(), and moveFileToDone().

{
return $this->makePath($this->donePath, $file);
}
Channel::makeErrorPath (   $file)

Function makes path to file which placed in error folder

Parameters
string$filename of the file
Returns
string full path to the file

Definition at line 559 of file Channel.php.

References makePath().

Referenced by moveDoneFileToError(), and moveFileToError().

{
return $this->makePath($this->errorPath, $file);
}
Channel::makeImagesPath (   $file)

Function makes path to file which placed in images folder

Parameters
string$filename of the file
Returns
string full path to the file

Definition at line 570 of file Channel.php.

References makePath().

Referenced by processWork(), and removeFilesByName().

{
return $this->makePath($this->imagesPath, $file);
}
Channel::makePath (   $folder,
  $file 
)

Function makes path to the file which placed in recieved folder path

Parameters
string$folderpath to folder where the file placed
string$filename of the file
Returns
string full path to the file

Definition at line 526 of file Channel.php.

Referenced by makeDonePath(), makeErrorPath(), makeImagesPath(), makeVideosPath(), makeWorkPath(), and moveToWork().

{
return $folder.DIRECTORY_SEPARATOR.$file;
}
Channel::makeRelations ( )

Function make relations between database apa records

Definition at line 493 of file Channel.php.

References ApalinesNews\model(), and ApalinesMM\model().

Referenced by processWork().

{
foreach($this->relations as $parent_xml_filename => $children){
$parent = ApalinesNews::model()->find('xml_filename = :xml_filename',array(':xml_filename'=>$parent_xml_filename));
if (isset($parent)) {
// Make background relations
foreach ($children as $child_xml_filename) {
$child = ApalinesNews::model()->find('xml_filename = :xml_filename', array(':xml_filename'=>$child_xml_filename));
if (!isset($child)) {
$child = ApalinesMM::model()->find('xml_filename = :xml_filename', array(':xml_filename'=>$child_xml_filename));
}
if (isset($child)) {
// Relation is news-news
if ($child instanceof ApalinesNews) {
$parent->background_news = 1;
}
$child->related_maindoc_id = $parent->id;
$child->save();
}
}
$parent->save();
}
}
}
Channel::makeVideosPath (   $file)

Function makes path to file which placed in videos folder

Parameters
string$filename of the file
Returns
string full path to the file

Definition at line 581 of file Channel.php.

References makePath().

Referenced by processWork(), and removeFilesByName().

{
return $this->makePath($this->videosPath, $file);
}
Channel::makeWorkPath (   $file)

Function makes path to file which placed in work folder

Parameters
string$filename of the file
Returns
string full path to the file

Definition at line 537 of file Channel.php.

References makePath().

Referenced by moveFileToDone(), moveFileToError(), moveToWork(), and processWork().

{
return $this->makePath($this->workPath, $file);
}
Channel::moveDoneFileToError (   $file)

Function moves the file from done folder to error folder. This function is needed if multimedia was successfully imported but it has not parents, so it should be removed and marked as error

Definition at line 631 of file Channel.php.

References makeDonePath(), and makeErrorPath().

{
rename($this->makeDonePath($file),
$this->makeErrorPath($file));
}
Channel::moveFileToDone (   $file)

Function moves the file from work folder to done folder

Parameters
string$filename of the file

Definition at line 610 of file Channel.php.

References makeDonePath(), and makeWorkPath().

Referenced by processNews(), and processRemoval().

{
rename($this->makeWorkPath($file),
$this->makeDonePath($file));
}
Channel::moveFileToError (   $file)

Function moves the file from work folder to error folder

Parameters
string$filename of the file

Definition at line 620 of file Channel.php.

References makeErrorPath(), and makeWorkPath().

Referenced by processNews(), and processRemoval().

{
rename($this->makeWorkPath($file),
$this->makeErrorPath($file));
}
Channel::moveToWork ( )

Function moves all files from main channel folder to work folder

Definition at line 588 of file Channel.php.

References makePath(), and makeWorkPath().

{
if (is_dir($this->path)) {
if ($dh = opendir($this->path)) {
while (($file = readdir($dh)) !== false) {
$path = $this->makePath($this->path,$file);
$path_parts = pathinfo($path);
if(!is_file($path)||$path_parts['extension']=='txt'){
continue;
}
rename($this->makePath($this->path,$file),
$this->makeWorkPath($file));
}
closedir($dh);
}
}
}
Channel::processBild (   $parserXML,
  $xml_filename = null 
)

Function processes multimedia xml files

Parameters
object$parserXMLobject of parserXml class
string$xml_filenamename of xml file
Returns
boolean whether import finished successfully

Definition at line 345 of file Channel.php.

References ApalinesMM\model().

Referenced by processNews().

{
$line = ApalinesMM::model()->find('docid = :docid',
array(':docid'=>$parserXML->getDocId()));
$isExist = isset($line);
if (!isset($line)) {
$line = new ApalinesMM();
$line->docid = $parserXML->getDocId();
$line->channel = $this->name;
}
$line->apa_id = $parserXML->getApaId();
$line->title = $parserXML->getTitle();
$line->ndate = $parserXML->getDate();
$line->place = $parserXML->getPlace();
$line->source = $parserXML->getSource();
$line->keywords = $parserXML->getKeywords();
$line->author = $parserXML->getAuthor();
$line->filename = $parserXML->getFilename();
$line->xml_filename = $xml_filename;
$line->type = ($parserXML->getDocType() == 'DOK_BILD')? 0 : 1;
$files = $parserXML->getFiles();
if ($is_success = $line->save()) {
foreach($files as $file) {
$model = new ApalinesFile();
$model->attributes = $file;
switch ($model->size) {
case 'ORIGINAL':
$model->size = ApalinesFile::ORIGINAL;
break;
case 'THUMBNAIL':
$model->size = ApalinesFile::THUMBNAIL;
break;
case 'HIRES':
$model->size = ApalinesFile::HIRES;
break;
}
$model->parent_id = $line->id;
$model->save();
}
if ($isExist) {
Yii::log("{$line->xml_filename} done update doc_id:{$line->docid} title:{$line->title} BILD", 'info');
} else {
Yii::log("{$line->xml_filename} done insert doc_id:{$line->docid} title:{$line->title} BILD", 'info');
}
} else {
if ($isExist) {
Yii::log("{$line->xml_filename} error update doc_id:{$line->docid} title:{$line->title} BILD", 'info');
} else {
Yii::log("{$line->xml_filename} error insert doc_id:{$line->docid} title:{$line->title} BILD", 'info');
}
}
return $is_success;
}
Channel::processMeldung (   $parserXML,
  $xml_filename = null 
)

Function processes news xml files

Parameters
object$parserXMLobject of parserXml class
string$xml_filenamename of xml file
Returns
boolean whether import finished successfully

Definition at line 279 of file Channel.php.

References ApalinesNews\model().

Referenced by processNews().

{
$line = ApalinesNews::model()->find('docid = :docid',array(':docid'=>$parserXML->getDocId()));
$isExist = isset($line);
if (!isset($line)) {
$line = new ApalinesNews();
$line->docid = $parserXML->getDocId();
$line->channel = $this->name;
} else {
$line->deleteFiles();
}
$line->apa_id = $parserXML->getApaId();
$line->title = $parserXML->getTitle();
$line->ndate = $parserXML->getDate();
$line->content = $parserXML->getText();
$line->place = $parserXML->getPlace();
$line->source = $parserXML->getSource();
$line->keywords = $parserXML->getKeywords();
$line->author = $parserXML->getAuthor();
$line->teaser = $parserXML->getTeaser();
$line->filename = $parserXML->getFilename();
$line->xml_filename = $xml_filename;
$relations = $parserXML->getRelations();
$this->relations = CMap::mergeArray(
$this->relations,
array($line->xml_filename => $relations)
);
if (isset($line->id)) {
$childNews = ApalinesNews::model()->findAll('related_maindoc_id=:id', array(':id'=>$line->id));
foreach ($childNews as $child) {
if (!in_array($child->xml_filename, $relations)) {
$child->delete();
}
}
if (empty($relations)) {
$line->background_news = 0;
}
}
if ($is_success = $line->save()) {
if ($isExist) {
Yii::log("{$line->xml_filename} done update doc_id:{$line->docid} title:{$line->title} DOK_MELDUNG", 'info');
} else {
Yii::log("{$line->xml_filename} done insert doc_id:{$line->docid} title:{$line->title} DOK_MELDUNG", 'info');
}
} else {
if($isExist){
Yii::log("{$line->xml_filename} error update doc_id:{$line->docid} title:{$line->title} DOK_MELDUNG", 'info');
} else {
Yii::log("{$line->xml_filename} error insert doc_id:{$line->docid} title:{$line->title} DOK_MELDUNG", 'info');
}
}
return $is_success;
}
Channel::processNews (   $xml,
  $xml_filename = null,
  $specific = false 
)

Function processes: news and multimedia(images/video) xml files

Parameters
object$xmlSimpleXMLElement
string$xml_filenamename of xml file
array$specificwhat files should be processed

Definition at line 245 of file Channel.php.

References moveFileToDone(), moveFileToError(), processBild(), and processMeldung().

Referenced by processWork().

{
$parserXML = new parserXML($xml);
$isSuccess = false;
$docType = $parserXML->getDocType();
if ($specific && is_array($specific) && !in_array($docType, $specific)) {
return;
}
switch($parserXML->getDocType()) {
case 'DOK_MELDUNG':
$isSuccess = $this->processMeldung($parserXML, $xml_filename);
break;
case 'DOK_BILD' || 'DOK_VIDEO':
$isSuccess = $this->processBild($parserXML, $xml_filename);
break;
}
if ($isSuccess) {
//Moving to done folder
$this->moveFileToDone($xml_filename);
} else {
//Moving to error folder
$this->moveFileToError($xml_filename);
}
}
Channel::processRemoval (   $xml,
  $xml_filename = null 
)

Function processes removing xml files

Parameters
object$parserXMLobject of parserXml class
string$xml_filenamename of xml file
Returns
boolean whether import finished successfully

Definition at line 412 of file Channel.php.

References ApalinesNews\model(), ApalinesMM\model(), moveFileToDone(), and moveFileToError().

Referenced by processWork().

{
$parserXML = new parserXML($xml);
$deleteId = $parserXML->getDeleteId();
$line = ApalinesNews::model()->find('docid = :docid',array(':docid'=> $deleteId ));
if (!isset($line)) {
$line = ApalinesMM::model()->find('docid = :docid',array(':docid'=> $deleteId ));
}
if (isset($line)) {
$line->delete();
//Moving to done folder
$this->moveFileToDone($xml_filename);
Yii::log("{$line->xml_filename} done delete docid:{$line->docid} title:{$line->title}", 'info');
} else {
//Moving to error folder
$this->moveFileToError($xml_filename);
Yii::log("{$xml_filename} error delete", 'info');
}
}
Channel::processWork ( )

Function processes files in work folder: distributes what handler should process each file

Definition at line 182 of file Channel.php.

References checkMMParents(), getExt(), makeImagesPath(), makeRelations(), makeVideosPath(), makeWorkPath(), processNews(), and processRemoval().

{
if (is_dir($this->workPath)) {
if ($dh = opendir($this->workPath)) {
while (($file = readdir($dh)) !== false) {
if (!is_file($this->makeWorkPath($file)) || $file == '.DS_Store') {
continue;
}
//Process deletions
if (in_array($this->getExt($file),$this->removalExt)){
$xml = simplexml_load_file($this->makeWorkPath($file));
$this->processRemoval($xml, $file);
}
}
//Step 2. Update meldung
rewinddir($dh);
while (($file = readdir($dh)) !== false) {
if (!is_file($this->makeWorkPath($file)) || $file == '.DS_Store') {
continue;
}
//Process news, images, videos
else if (in_array($this->getExt($file),$this->newsExt)) {
$xml = simplexml_load_file($this->makeWorkPath($file));
$this->processNews($xml, $file, array('DOK_MELDUNG'));
}
}
//Step 3. Update build
rewinddir($dh);
while (($file = readdir($dh)) !== false) {
if (!is_file($this->makeWorkPath($file)) || $file == '.DS_Store') {
continue;
}
//Process multimedia
else if (in_array($this->getExt($file),$this->newsExt)) {
$xml = simplexml_load_file($this->makeWorkPath($file));
$this->processNews($xml, $file, array('DOK_BILD', 'DOK_VIDEO'));
}
//Move images
else if (in_array($this->getExt($file),$this->imagesExt)) {
rename($this->makeWorkPath($file),
$this->makeImagesPath($file));
}
//Move videos
else if (in_array($this->getExt($file),$this->videosExt)) {
rename($this->makeWorkPath($file),
$this->makeVideosPath($file));
}
}
closedir($dh);
}
}
//Make relations between imported news and multimedia
$this->makeRelations();
//Remove multimedia which have not parent news
$this->checkMMParents();
}
Channel::removeFilesByName (   $files)

Function removes multimedia files from public folder by file names

Parameters
array$files- names of files which should be removed

Definition at line 468 of file Channel.php.

References makeImagesPath(), and makeVideosPath().

{
foreach ($files as $file) {
if (file_exists($this->makeImagesPath($file))) {
unlink($this->makeImagesPath($file));
Yii::log("{$file} done delete image", 'info');
} else if(file_exists($this->makeVideosPath($file))) {
Yii::log("{$file} done delete video", 'info');
unlink($this->makeVideosPath($file));
}
}
}

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