HCE Project DC service web UI  0.2
Hierarchical Cluster Engine DC service web UI
 All Classes Namespaces Files Functions Variables Pages
SiteUpdate.php
Go to the documentation of this file.
1 <?php
2 
4 {
5  public $opLog;
6  public $operation;
7 
8  function __construct($opLog = 'SITE_UPDATE', $operation = 'Update')
9  {
10  $this->opLog = $opLog;
11  $this->operation = $operation;
12  }
13 
14 
15  public function updateSiteInfo($criterions = null, $updateType = 1)
16  {
17  if (!$this->validateSite()) {
18  return false;
19  }
20  $userId = Yii::app()->user->id;
21  $id = $this->getId();
22  $rcArray = array();
23  $rcArray[] = Yii::app()->getRequest()->getPost('recrawlDate');
24 
25  if (is_array($rcArray[0])) {
26  if (($rcArray[0]['date'] == '') && ($rcArray[0]['time'] == '')) {
27  $recrawlDate = null;
28  } elseif (($rcArray[0]['date'] != '') && ($rcArray[0]['time'] != '')) {
29  $recrawlDate = $rcArray[0]['date'] . ' ' . $rcArray[0]['time'];
30  $recrawlDate = '"' . date('Y-m-d H:i:s', strtotime($recrawlDate)) . '"';
31  } elseif (($rcArray[0]['date'] == '') || ($rcArray[0]['time'] == '')) {
32  Yii::app()->user->setFlash('notification', 'Please, specify both date and time for "Recrawl date" !');
33  return false;
34  }
35  } else {
36  $recrawlDate = $rcArray[0];
37  }
38 
39  if ($updateType == 1) {
40  $urlsArr = $this->getUrlsArray();
41  $propsArr = $this->getPropsArray();
42  $filtersArr = $this->getFiltersArray($urlsArr);
43  } elseif ($updateType == 0) {
44  $urlsArr = array();
45  $propsArr = null;
46  $filtersArr = null;
47  }
48  $json = array(
49  "cDate" => null,
50  "description" => Yii::app()->getRequest()->getPost('description'),
51  "errorMask" => null,
52  "errors" => null,
53  "filters" => $filtersArr,
54  "httpTimeout" => Yii::app()->getRequest()->getPost('httpTimeout'),
55  "id" => $id,
56  "iterations" => null,
57  "maxErrors" => Yii::app()->getRequest()->getPost('maxErrors'),
58  "maxResourceSize" => Yii::app()->getRequest()->getPost('maxResourceSize'),
59  "maxResources" => Yii::app()->getRequest()->getPost('maxResources'),
60  "maxURLs" => Yii::app()->getRequest()->getPost('maxURLs'),
61  "priority" => Yii::app()->getRequest()->getPost('priority'),
62  "properties" => $propsArr,
63  "requestDelay" => Yii::app()->getRequest()->getPost('requestDelay'),
64  "resources" => null,
65  "state" => Yii::app()->getRequest()->getPost('state'),
66  "tcDate" => '',
67  "tcDateProcess" => '',
68  "uDate" => 'NOW()',
69  "updateType" => $updateType,
70  "urlType" => Yii::app()->getRequest()->getPost('urlType'),
71  "urls" => $urlsArr,
72  "contents" => null,
73  "processingDelay" => Yii::app()->getRequest()->getPost('processingDelay'),
74  "size" => null,
75  "avgSpeed" => null,
76  "avgSpeedCounter" => null,
77  "maxURLsFromPage" => Yii::app()->getRequest()->getPost('maxURLsFromPage'),
78  "userId" => $userId,
79  "recrawlPeriod" => Yii::app()->getRequest()->getPost('recrawlPeriod'),
80  "recrawlDate" => $recrawlDate,
81  "collectedURLs" => null,
82  "fetchType" => Yii::app()->getRequest()->getPost('fetchType'),
83  "criterions" => $criterions
84  );
85  Logger::log("Opertion ->" . $this->opLog, false);
86  foreach ($json as $key => $value) {
87  if ($value === "") {
88  $value = null;
89  $json[$key] = $value;
90  }
91  }
92  $json = CJSON::encode($json);
93  Logger::log("Request ->\n" . $json, false);
94  $reqFile = tempnam(Yii::app()->getBasePath() . '/json_temp', '');
95  $request = fopen($reqFile, "w");
96  fwrite($request, $json);
97  fclose($request);
98  $response = $this->commandUpdate($reqFile);
99  unlink($reqFile);
100  $UsersSitesRights = new UsersSitesRights();
101  $UsersSitesRights->setRightsSingleRecord(
102  array(
103  'userId' => $userId,
104  'siteId' => $id
105  ),
106  true
107  );
108 
109  return $response;
110  }
111 
112  public function getId()
113  {
114  $id = Yii::app()->getRequest()->getPost('id');
115  if ($id != null) {
116  return $id;
117  }
118  $urls = $this->getUrlsArray();
119  $id = md5(Yii::app()->user->id . $urls[0]);
120  return $id;
121  }
122 
123  public function getUrlsArray()
124  {
125  $urlsArray = null;
126  $rootUrls = Yii::app()->getRequest()->getPost('rootUrls');
127  $rootUrls = str_replace("\r", '', $rootUrls);
128  $rootUrls = explode("\n", $rootUrls);
129  foreach ($rootUrls as $item) {
130  if ($item != '')
131  $urlsArray[] = $item;
132  }
133  // $rootUrls = array_diff($rootUrls, array("\r", ''));
134  return $urlsArray;
135  }
136 
137  public function getFiltersArray()
138  {
139  $state = Yii::app()->getRequest()->getPost('fstate');
140  $action = Yii::app()->getRequest()->getPost('faction');
141  $stage = Yii::app()->getRequest()->getPost('fstage');
142  $opCode = Yii::app()->getRequest()->getPost('fopCode');
143  $subject = Yii::app()->getRequest()->getPost('fsubject');
144  $groupId = Yii::app()->getRequest()->getPost('fgroupId');
145  $type = Yii::app()->getRequest()->getPost('ftype');
146  $mode = Yii::app()->getRequest()->getPost('fmode');
147  $pattern = Yii::app()->getRequest()->getPost('fpattern');
148  $siteId = Yii::app()->getRequest()->getPost('id');
149  $filters = array();
150  for ($i = 0; $i < count($state); $i++) {
151  $filters[] = array(
152  'state' => $state[$i],
153  'action' => $action[$i],
154  'stage' => $stage[$i],
155  'opCode' => $opCode[$i],
156  'subject' => $subject[$i],
157  'groupId' => $groupId[$i],
158  'mode' => $mode[$i],
159  'pattern' => $pattern[$i],
160  'siteId' => $siteId,
161  'uDate' => 'NOW()'
162  );
163  }
164  return $filters;
165  }
166 
167  public function getPropsArray()
168  {
169  $propsn = array();
170  $propsv = array();
171  foreach ($_POST as $k => $v) {
172  if (preg_match("/^n_/", (string)$k))
173  $propsn[] = $v;
174  if (preg_match("/^v_/", (string)$k))
175  $propsv[] = $v;
176  }
177  $props = array_combine($propsn, $propsv);
178  return $props;
179  }
180 
181  public function commandUpdate($reqFile)
182  {
183  $api = Yii::app()->params['api'];
184  $path = Yii::app()->getBasePath() . '/shell/';
185  $cmd = "sh " . $path . "rnd_site_update.sh $api $reqFile";
186  $json = shell_exec($cmd);
187  $error = Errors::isError($json);
188  if ($error !== 0) {
189  Yii::app()->user->setFlash('error2', $this->operation . ' WARNING: ' . $error);
190  Logger::log("Response ->\n" . $json);
191  return false;
192  } else {
193  Yii::app()->user->setFlash('success2', $this->operation . ': SUCCESS');
194  Logger::log("Response ->\n" . $json);
195  return true;
196  }
197  }
198 
199  public function getDescriptions()
200  {
201  $list = parse_ini_file(Yii::app()->basePath.'/config/descriptions.ini', true);
202  return $this->setNl2br($list);
203  }
204 
205  public function setNl2br ($in)
206  {
207  if (!is_array($in)) {
208  return nl2br($in);
209  }
210  foreach ($in as $k=>$v) {
211  if (is_array($v)) {
212  $in[$k] = $this->setNl2br($v);
213  } else {
214  $in[$k] = nl2br($v);
215  }
216  }
217  return $in;
218  }
219 }