src/Controller/Admin/ApiIndexingController.php line 143

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Admin;
  3. //require_once '../vendor/autoload.php';
  4. use Google\Client;
  5. use App\Entity\InstantIndexing;
  6. use Symfony\Component\Mercure\Update;
  7. use App\Repository\LanguageRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Google\Http\Batch as Google_Http_Batch;
  10. use Symfony\Component\Mercure\HubInterface;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Console\Input\ArrayInput;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. use Symfony\Component\HttpKernel\KernelInterface;
  16. use Symfony\Component\HttpFoundation\JsonResponse;
  17. use Symfony\Component\Console\Output\BufferedOutput;
  18. use Google\Service\Indexing as Google_Service_Indexing;
  19. use Symfony\Bundle\FrameworkBundle\Console\Application;
  20. use Google\Service\Webmasters as Google_Service_Webmasters;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
  22. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  23. use Google\Service\SearchConsole as Google_Service_SearchConsole;
  24. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  25. use Google\Service\Indexing\UrlNotification as Google_Service_Indexing_UrlNotification;
  26. use Google\Service\SearchConsole\InspectUrlIndexRequest as Google_Service_SearchConsole_InspectUrlIndexRequest;
  27. use Google\Service\Webmasters\SearchAnalyticsQueryRequest as Google_Service_Webmasters_SearchAnalyticsQueryRequest;
  28. class ApiIndexingController extends AbstractController 
  29. {
  30.       private $em;
  31.       private $languages;
  32.       public function __construct(EntityManagerInterface $em,LanguageRepository $languages){
  33.             $this->em $em;
  34.             $this->languages $languages->findActifLang();
  35.       } 
  36.       /**
  37.        * @Security("not is_granted('ROLE_BLOG')")
  38.        * @Route("/console/instant-indexing", methods={"GET","POST"}, name="admin_instant_indexing")
  39.        */
  40.       public function InstantIndexing(Request $request)
  41.       {  
  42.           $action 'edit_config';
  43.           $quotas null;
  44.           $config_indexing $this->getDoctrine()->getRepository(InstantIndexing::class)->find(1);
  45.           if(!$config_indexing){
  46.               $action 'create_config';
  47.               $config_indexing = new InstantIndexing();
  48.           }elseif($config_indexing->getApiRequests() != null){
  49.               $quotas $this->get_limits($config_indexing->getApiRequests());
  50.           }
  51.           if ($request->isMethod('POST')) {
  52.               $config $request->request->get('config');
  53.               if(empty($config)){
  54.                   $config_indexing->setConfig(null);
  55.               }else{
  56.                   $config_indexing->setConfig($config); 
  57.               }
  58.         
  59.               if($action == "create_config"){
  60.                   $this->em->persist($config_indexing);
  61.               }
  62.               $this->em->flush();
  63.               return $this->redirectToRoute('admin_instant_indexing');
  64.           }
  65.           return $this->render('admin/dashboards/instant_indexing/index.html.twig',[
  66.              'config_indexing'=> $config_indexing,
  67.              'quotas'=> $quotas,
  68.           ]); 
  69.       }
  70.       /**
  71.        * @Security("not is_granted('ROLE_BLOG')")
  72.        * @Route("/console/instant-indexing-api", methods={"POST"}, name="admin_instant_indexing_api", condition="request.isXmlHttpRequest()")
  73.        */
  74.       public function send_to_api(Request $request)
  75.       {  
  76.           $data = [];
  77.           $config_indexing $this->getDoctrine()->getRepository(InstantIndexing::class)->find(1);
  78.           $urls preg_split('/\r\n|\n\r|\r|\n/',$request->request->get('urls'));
  79.           $urls_count count($urls);
  80.           $action $request->request->get('action');
  81.           if($config_indexing){
  82.              $client = new Client();
  83.              $client->setAuthConfig(json_decode($config_indexing->getConfig(),true));
  84.              $client->setConfig'base_path''https://indexing.googleapis.com' );
  85.              $client->addScope'https://www.googleapis.com/auth/indexing' );
  86.              $client->setUseBatchtrue );
  87.              $service = new Google_Service_Indexing($client);
  88.              $batch   = new Google_Http_Batch($clientfalse'https://indexing.googleapis.com');
  89.   
  90.              foreach ($urls as $key => $url) {
  91.                   $post_body = new Google_Service_Indexing_UrlNotification();
  92.                   if($action  == 'status'){
  93.                     $request_part $service->urlNotifications->getMetadata( [ 'url' => $url ] );
  94.                   }else{
  95.                      $post_body->setType$action === 'update' 'URL_UPDATED' 'URL_DELETED' );
  96.                      $post_body->setUrl($url);
  97.                      $request_part $service->urlNotifications->publish($post_body);
  98.                   }
  99.                   $batch->add$request_part'url-' $key );
  100.              }
  101.              $results $batch->execute();
  102.              $res_count count$results );
  103.              foreach ( $results as $id => $response ) {
  104.                   $local_id substr($id,9);
  105.                   if(is_a($response,'Google_Service_Exception')){
  106.                      $data$local_id ] = json_decode$response->getMessage() );
  107.                   }else{
  108.                      $data$local_id ] = (array) $response->toSimpleObject();
  109.                   }
  110.                   if($res_count === 1){
  111.                      $data $data$local_id ];
  112.                   }
  113.              }
  114.              $this->log_request($action,$urls_count,$config_indexing);
  115.            }
  116.            return new JsonResponse($data);
  117.       }
  118.       public function log_request$type$number 1,$config_indexing ) {
  119.         if($config_indexing->getApiRequests() != null){
  120.             $requests_log unserialize($config_indexing->getApiRequests());
  121.         }else{
  122.             $requests_log = [
  123.                 'update'      => [],
  124.                 'delete'      => [],
  125.                 'status'   => [],
  126.                 'bing_submit' => [],
  127.             ];
  128.         }
  129.         if (!isset( $requests_log[$type] ) ) {
  130.             $requests_log[$type] = [];
  131.         }
  132.         $add array_fill(0$numbertime() );
  133.         $requests_log[$type] = array_merge$requests_log[$type], $add );
  134.         if(count($requests_log[$type]) > 600 ) {
  135.             $requests_log[$type] = array_slice$requests_log$type ], -600600true );
  136.         }
  137.         $config_indexing->setApiRequests(serialize($requests_log));
  138.         $this->em->flush();
  139.       }
  140.       public function get_limits($api_requests) {
  141.         $requests_log unserialize($api_requests);
  142.         $current_limits = [
  143.             'publishperday' => 0,
  144.             'permin'        => 0,
  145.             'metapermin'    => 0,
  146.             'bing_submitperday' => 0,
  147.         ];
  148.         $limit_publishperday 200;
  149.         $limit_permin        600;
  150.         $limit_metapermin    180;
  151.         $limit_bingsubmitperday 10;
  152.         // $requests_log = [
  153.         //     'update'      => [],
  154.         //     'delete'      => [],
  155.         //     'status'   => [],
  156.         //     'bing_submit' => [],
  157.         // ];
  158.         $timestamp_1day_ago strtotime'-1 day' );
  159.         $timestamp_1min_ago strtotime'-1 minute' );
  160.         $publish_1day 0;
  161.         $all_1min     0;
  162.         $meta_1min    0;
  163.         foreach ( $requests_log['update'] as $time ) {
  164.             if ( $time $timestamp_1day_ago ) {
  165.                 $publish_1day++;
  166.             }
  167.             if ( $time $timestamp_1min_ago ) {
  168.                 $all_1min++;
  169.             }
  170.         }
  171.         foreach ( $requests_log['delete'] as $time ) {
  172.             if ( $time $timestamp_1min_ago ) {
  173.                 $all_1min++;
  174.             }
  175.         }
  176.         foreach ( $requests_log['status'] as $time ) {
  177.             if ( $time $timestamp_1min_ago ) {
  178.                 $all_1min++;
  179.                 $meta_1min++;
  180.             }
  181.         }
  182.         $bing_submit_1day 0;
  183.         if ( ! isset( $requests_log['bing_submit'] ) ) {
  184.             $requests_log['bing_submit'] = [];
  185.         }
  186.         foreach ( $requests_log['bing_submit'] as $time ) {
  187.             if ( $time $timestamp_1day_ago ) {
  188.                 $bing_submit_1day++;
  189.             }
  190.         }
  191.         $current_limits['publishperday'] = $limit_publishperday $publish_1day;
  192.         $current_limits['permin']        = $limit_permin $all_1min;
  193.         $current_limits['metapermin']    = $limit_metapermin $meta_1min;
  194.         $current_limits['bing_submitperday'] = $limit_bingsubmitperday $bing_submit_1day;
  195.         $current_limits['publishperday_max'] = $limit_publishperday;
  196.         $current_limits['permin_max']        = $limit_permin;
  197.         $current_limits['metapermin_max']    = $limit_metapermin;
  198.         $current_limits['bing_submitperday_max'] = $limit_bingsubmitperday;
  199.         return $current_limits;
  200.     }
  201. }