Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
processAPACommand.php
1 <?php
2 /**
3  * This command processes one channel with name $channel ,
4  * and if $clear = 1 truncate tables before fill them
5  *
6  * go to gportal/common:
7  * yiic processAPA --channel=test --clear=1
8  *
9  * Install incron :
10  * 1)If Debian / Ubuntu Linux:
11  * sudo apt-get install incron
12  *
13  * 2)If RHEL / Fedora / CentOS Linux:
14  * sudo yum install incron
15  *
16  * Start/Stop
17  * sudo /etc/init.d/incron stop
18  * sudo /etc/init.d/incron start
19  *
20  * More info here:
21  * http://www.cyberciti.biz/faq/linux-inotify-examples-to-replicate-directories/
22  *
23  * @author Andrew Voloshin <andrew.voloshin@oberig.com>
24  *
25  */
26 class processAPACommand extends CConsoleCommand
27 {
28  /**
29  * default action
30  * it processes channel with name $channel
31  *
32  * @param string $channel name of channel
33  * @param int $clear whether to truncate tables before import
34  */
35  public function actionIndex($channel = false,$clear = false) {
36 
37  Yii::app()->getModule('apalines');
38 
39  if ($clear) {
40  if ($channel) {
41  $channels = array($channel);
42  } else {
43  $channels = Yii::app()->getModule('apalines')->channels;
44  $channels = array_keys($channels);
45  }
46  $notExistFolders = array();
47  foreach ($channels as $name) {
48  $ch = new Channel($name);
49  if (!file_exists($ch->path)) {
50  $notExistFolders[] = $name;
51  continue;
52  }
53  $news = ApalinesNews::model()->findAll('deleted <> 1');
54  foreach ($news as $line) {
55  $line->delete();
56  }
57  }
58  $channels = implode(', ', $channels);
59  echo "News of the channels: {$channels} are removed\n";
60  if (!empty($notExistFolders)) {
61  $notExistFolders = implode(', ', $notExistFolders);
62  echo "Channel folders of: {$notExistFolders} are not exist\n";
63  }
64  Yii::app()->end();
65  }
66 
67  if (!$channel) {
68  echo "Channel name is not passed";
69  Yii::app()->end();
70  }
71 
72  $channel = new Channel($channel);
73 
74  if (!$channel->isNew()) {
75  echo "Fertig.txt does not exist in the channel folder\n";
76  Yii::app()->end();
77  }
78  $channel->clearOld();
79 
80  $channel->moveToWork();
81  $channel->processWork();
82  }
83  /**
84  * action sets up incrontab to execute import command with current module settings
85  */
86  public function actionSetup() {
87 
88  $incronPath = "/var/spool/incron/root";
89  $module = Yii::app()->getModule('apalines');
90  $channels = $module->channels;
91  $incronContent = '';
92  $yiicPath = Yii::getPathOfAlias('site.common').DIRECTORY_SEPARATOR."yiic";
93  $events = "IN_CREATE,IN_CLOSE_WRITE,IN_MOVED_TO";
94 
95  foreach($channels as $alias => $channel){
96 
97  $path = Yii::getPathOfAlias($channel['delivery_path']);
98  $command = "$yiicPath processApa index --channel=$alias";
99  $incronContent .= "$path $events $command\n";
100  }
101 
102  file_put_contents($incronPath, $incronContent);
103  }
104 }