src/Controller/BackEnd/IndexingGoogleController.php line 134

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