Commit 15b5d6f2 authored by Gorodkov Denis's avatar Gorodkov Denis

0_0

parent 13320772
...@@ -9,10 +9,9 @@ ...@@ -9,10 +9,9 @@
*/ */
function mymodule_cron() { function mymodule_cron() {
$api = \Drupal::service('mymodule.custom_services'); $api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
$queue_api = \Drupal::queue('mymodule_swapi'); $queue_api = \Drupal::queue('mymodule_swapi');
$time = \Drupal::state()->get('date_queue_swapi'); $time = \Drupal::state()->get('date_queue_swapi');
$result_api = $api->getResponse('https://swapi.dev/api');
foreach($result_api as $item) { foreach($result_api as $item) {
$bundle = $api->getTypeNode($item); $bundle = $api->getTypeNode($item);
...@@ -21,22 +20,11 @@ function mymodule_cron() { ...@@ -21,22 +20,11 @@ function mymodule_cron() {
while (!empty($uri)) { while (!empty($uri)) {
$result = $api->getResponse($uri); $result = $api->getResponse($uri);
foreach($result['results'] as $value) { foreach($result['results'] as $value) {
$bundle = $api->getTypeNode($value['url']); $node = $api->getNodeByUrl($value['url']);
$swapi_id = $api->getSwapiId($value['url']);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
foreach ($node as $val) {
$node_edited = $val;
}
if (!empty($node)) { if (!empty($node)) {
$changed = $node_edited->get('changed')->getValue(); $changed = $node->get('changed')->getValue();
$changed = $changed[0]['value']; $changed = $changed[0]['value'];
if ($time < $changed) {
if ($time - $changed < 0) {
$queue_api->createItem($value); $queue_api->createItem($value);
} }
} else { } else {
...@@ -49,10 +37,8 @@ function mymodule_cron() { ...@@ -49,10 +37,8 @@ function mymodule_cron() {
\Drupal::logger('Queue mymodule_api')->notice("Content type $bundle не создан!"); \Drupal::logger('Queue mymodule_api')->notice("Content type $bundle не создан!");
} }
} }
$time = \Drupal::time()->getCurrentTime();
$date = new DateTime(); Drupal::state()->set('date_queue_swapi', $time);
$date = $date->getTimestamp();
\Drupal::state()->set('date_queue_swapi', $date);
} }
/** /**
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\mymodule; namespace Drupal\mymodule;
use GuzzleHttp\ClientInterface; use GuzzleHttp\ClientInterface;
use Drupal\Core\Datetime\DrupalDateTime;
class API { class API {
protected $httpClient; protected $httpClient;
...@@ -15,21 +16,21 @@ class API { ...@@ -15,21 +16,21 @@ class API {
$request = $this->httpClient->request('GET', $uri); $request = $this->httpClient->request('GET', $uri);
$responseCode = $request->getStatusCode(); $responseCode = $request->getStatusCode();
if ($responseCode == 200) {
$response = $request->getBody()->getContents(); $response = $request->getBody()->getContents();
$response = json_decode($response, true); $response = json_decode($response, true);
return $response; return $response;
} }
return false;
}
public function getTypeNode($uri) { public function getTypeNode($uri) {
$type_node = explode('/', stristr($uri, 'api/')); $type_node = explode('/', stristr($uri, 'api/'));
return $type_node[1]; return $type_node[1];
} }
public function getSwapiId($uri) { public function getSwapiId($uri) {
$swapi_id = explode('/', stristr($uri, 'api/')); $swapi_id = explode('/', stristr($uri, 'api/'));
return $swapi_id[2]; return $swapi_id[2];
} }
...@@ -69,7 +70,6 @@ class API { ...@@ -69,7 +70,6 @@ class API {
'type' => $bundle, 'type' => $bundle,
'field_swapi_id' => $swapi_id, 'field_swapi_id' => $swapi_id,
]); ]);
foreach ($nodes as $value) { foreach ($nodes as $value) {
$node = $value; $node = $value;
} }
...@@ -81,7 +81,7 @@ class API { ...@@ -81,7 +81,7 @@ class API {
$field_name = "field_" . $key; $field_name = "field_" . $key;
$bool = (bool)($key != 'name' and $key != 'title' and $key != 'homeworld' and $key != 'url' and $key != 'created' and $key != 'people_exclude'); $bool = (bool)($key != 'name' and $key != 'title' and $key != 'homeworld' and $key != 'url' and $key != 'created' and $key != 'people_exclude');
if ($bool and !is_array($value)) { if ($bool and !is_array($value)) {
!empty($value) ? $node->set("field_" . strtolower($key), $value) : ''; isset($value) ? $node->set("field_" . strtolower($key), $value) : '';
} }
if (is_array($value)) { if (is_array($value)) {
$node->set($field_name, NULL); $node->set($field_name, NULL);
......
<?php
namespace Drupal\mymodule\Batch;
class Import {
public $batch;
public function __construct() {
$this->batch = [
'title' => 'Import nodes',
];
}
public function getNodes($time = '01.01.1970') {
$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api');
foreach($result_api as $item) {
$bundle = $api->getTypeNode($item);
if ($api->checkBundle($bundle)) {
$uri = $item;
while (!empty($uri)) {
$result = $api->getResponse($uri);
foreach($result['results'] as $value) {
$node = $api->getNodeByUrl($value['url']);
if (!empty($node)) {
$swapi_edited = date_parse($value['edited']);
$swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year'] . " " . $swapi_edited['hour'] . $swapi_edited['minute'] . $swapi_edited['second'];
$swapi_edited = strtotime($swapi_edited);
$time = strtotime($time);
if ($time < $swapi_edited) {
$this->setOperation([$value]);
}
} else {
$api->createNode($value);
$title = !empty($value['title']) ? $value['title'] : $value['name'];
#$this->output()->writeln("Страница $title создана");
$this->setOperation([$value]);
}
}
$uri = $result['next'];
}
} else {
#$this->output()->writeln("Content type $bundle не создан");
}
}
}
public function processItem($data) {
$api = \Drupal::service('mymodule.custom_services');
$node = $api->getNodeByUrl($data['url']);
$api->edited($data, $node);
}
public function setOperation($data) {
$this->batch['operations'][] = [[$this, 'processItem'], $data];
}
public function setBatch() {
batch_set($this->batch);
}
public function processBatch() {
batch_process();
}
public function finished($success, $results, $operations) {
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One post processed.', '@count posts processed.');
}
else {
$message = t('Finished with an error.');
}
// ВЫВЕСТИ $message
}
}
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Drupal\mymodule\Commands; namespace Drupal\mymodule\Commands;
use Drush\Commands\DrushCommands; use Drush\Commands\DrushCommands;
use Drupal\mymodule\Batch\Import;
/** /**
* A Drush command file. * A Drush command file.
...@@ -13,18 +14,23 @@ class BatchCommands extends DrushCommands { ...@@ -13,18 +14,23 @@ class BatchCommands extends DrushCommands {
/** /**
* Drush command that displays the given text. * Drush command that displays the given text.
* *
* @param string $date * @param string $time
* @command mymodule:custom * @command mymodule:custom
* @aliases custom * @aliases custom
* @usage drush custom $date * @usage drush custom $time
*/ */
public function custom($date = '01.01.1970') { public function custom($time = '01.01.1970') {
$api = \Drupal::service('mymodule.custom_services'); $import = new Import();
$import->getNodes();
batch_set($import->batch);
drush_backend_batch_process();
/*$api = \Drupal::service('mymodule.custom_services');
$result_api = $api->getResponse('https://swapi.dev/api'); $result_api = $api->getResponse('https://swapi.dev/api');
$queue_api = \Drupal::queue('mymodule_swapi_drush'); $queue_api = \Drupal::queue('mymodule_swapi_drush');
$queue_api->deleteQueue();
$count_create_node = 0; $count_create_node = 0;
$count_queue = 0; $count_queue = 0;
$queue_api->deleteQueue();
foreach($result_api as $item) { foreach($result_api as $item) {
$bundle = $api->getTypeNode($item); $bundle = $api->getTypeNode($item);
...@@ -33,22 +39,14 @@ class BatchCommands extends DrushCommands { ...@@ -33,22 +39,14 @@ class BatchCommands extends DrushCommands {
while (!empty($uri)) { while (!empty($uri)) {
$result = $api->getResponse($uri); $result = $api->getResponse($uri);
foreach($result['results'] as $value) { foreach($result['results'] as $value) {
$bundle = $api->getTypeNode($value['url']); $node = $api->getNodeByUrl($value['url']);
$swapi_id = $api->getSwapiId($value['url']);
$node = \Drupal::entityTypeManager()
->getStorage('node')
->loadByProperties([
'type' => $bundle,
'field_swapi_id' => $swapi_id,
]);
if (!empty($node)) { if (!empty($node)) {
$swapi_edited = date_parse($value['edited']); $swapi_edited = date_parse($value['edited']);
$swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year']; $swapi_edited = $swapi_edited['day'] . "." . $swapi_edited['month'] . "." . $swapi_edited['year'];
$swapi_edited = strtotime($swapi_edited); $swapi_edited = strtotime($swapi_edited);
$date = strtotime($date); $time = strtotime($time);
if ($date - $swapi_edited < 0) { if ($time < $swapi_edited) {
$queue_api->createItem($value); $queue_api->createItem($value);
$count_queue++; $count_queue++;
} }
...@@ -79,6 +77,6 @@ class BatchCommands extends DrushCommands { ...@@ -79,6 +77,6 @@ class BatchCommands extends DrushCommands {
} }
} }
$this->output()->writeln("Страниц обновлено " . $count_queue . ", страниц создано " . $count_create_node); $this->output()->writeln("Страниц обновлено " . $count_queue . ", страниц создано " . $count_create_node);*/
} }
} }
...@@ -7,6 +7,7 @@ namespace Drupal\mymodule\Form; ...@@ -7,6 +7,7 @@ namespace Drupal\mymodule\Form;
use Drupal\Core\Form\FormBase; use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Form\FormStateInterface;
use Drupal\mymodule\Batch\Import;
class BatchForm extends FormBase { class BatchForm extends FormBase {
...@@ -15,7 +16,7 @@ class BatchForm extends FormBase { ...@@ -15,7 +16,7 @@ class BatchForm extends FormBase {
} }
public function buildForm(array $form, FormStateInterface $form_state) { public function buildForm(array $form, FormStateInterface $form_state) {
$form['text'] = array( $form['date'] = array(
'#type' => 'datetime', '#type' => 'datetime',
'#title' => 'Date and time', '#title' => 'Date and time',
); );
...@@ -29,18 +30,15 @@ class BatchForm extends FormBase { ...@@ -29,18 +30,15 @@ class BatchForm extends FormBase {
} }
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
$api = \Drupal::service('mymodule.custom_services'); $date = $form_state->getValue('date');
$api->batch(); $date = $date->getTimestamp();
/*$nids = \Drupal::entityQuery('node')->condition('type','people')->execute(); $this->startImport();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);
$operations = [];
foreach ($nodes as $node) {
$operations[] = ['callback', $node];
} }
batch_set([
'title' => 'Nodes import', public function startImport() {
'operations' => $operations, $import = new Import();
]);*/ $import->getNodes();
batch_set($import->batch);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment