PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.12.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.12.1
5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / classes / WpMatomo / WpStatistics / Importers / Actions / PagesImporter.php
matomo / classes / WpMatomo / WpStatistics / Importers / Actions Last commit date
ActionsInterface.php 4 years ago DeviceDetectionImporter.php 2 weeks ago PagesImporter.php 2 weeks ago RecordImporter.php 2 weeks ago ReferrersImporter.php 8 months ago UserCountryImporter.php 8 months ago VisitorsImporter.php 2 weeks ago VisitsTimeImporter.php 1 year ago
PagesImporter.php
109 lines
1 <?php
2
3 namespace WpMatomo\WpStatistics\Importers\Actions;
4
5 use Piwik\Common;
6 use Piwik\Config as PiwikConfig;
7 use Piwik\Metrics;
8 use Piwik\Plugins\Actions\Archiver;
9 use Psr\Log\LoggerInterface;
10 use WP_STATISTICS\MetaBox\pages;
11 use Piwik\Date;
12 use WP_Statistics\Service\Admin\Metabox\MetaboxDataProvider;
13 use WpMatomo\WpStatistics\Config;
14 use WpMatomo\WpStatistics\DataConverters\PagesUrlConverter;
15 use WpMatomo\WpStatistics\DataConverters\PagesTitleConverter;
16 use WpMatomo\WpStatistics\DataConverters\SearchQueryConverter;
17
18 /**
19 * @package WpMatomo
20 * @subpackage WpStatisticsImport
21 */
22 class PagesImporter extends RecordImporter implements ActionsInterface {
23
24 const PLUGIN_NAME = 'Actions';
25
26 public function __construct( LoggerInterface $logger ) {
27 parent::__construct( $logger );
28 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
29 $this->maximum_rows_in_data_table_level_zero = @PiwikConfig::getInstance()->General['datatable_archiving_maximum_rows_actions'];
30 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
31 $this->maximum_rows_in_sub_data_table = @PiwikConfig::getInstance()->General['datatable_archiving_maximum_rows_subtable_actions'];
32 }
33
34 public function import_records( Date $date ) {
35 $limit = 100;
36 $pages = [];
37 $page = 0;
38 do {
39 ++$page;
40 if ( class_exists( '\WP_STATISTICS\MetaBox\pages' ) ) {
41 $pages_found = pages::get(
42 [
43 'from' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ),
44 'to' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ),
45 'per_page' => $limit,
46 'paged' => $page,
47 ]
48 );
49
50 $has_no_data_prop = ( array_key_exists( 'no_data', $pages_found ) && ( 1 === $pages_found['no_data'] ) );
51 $has_empty_pages_prop = ( array_key_exists( 'pages', $pages_found ) && empty( $pages_found['pages'] ) );
52 $no_data = $has_no_data_prop || $has_empty_pages_prop;
53 } else {
54 $pages_found = $this->get_metabox_data_provider()->getTopPages(
55 [
56 'date' => [
57 'from' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ),
58 'to' => $date->toString( Config::WP_STATISTICS_DATE_FORMAT ),
59 ],
60 'per_page' => $limit,
61 'page' => $page,
62 'fields' => [ 'id', 'uri', 'type', 'SUM(count) as views', 'page_id' ],
63 ]
64 );
65
66 $pages_found = array_map(
67 function ( $row_std_class ) {
68 return (array) $row_std_class;
69 },
70 $pages_found
71 );
72
73 $no_data = count( $pages_found ) < 1;
74 }
75
76 if ( ! $no_data ) {
77 $pages = array_merge( $pages, array_key_exists( 'pages', $pages_found ) ? $pages_found['pages'] : $pages_found );
78 }
79 } while ( true !== $no_data );
80 $search_keywords = SearchQueryConverter::convert( $pages );
81 $this->logger->debug( 'Import {nb_keywords} search keywords...', [ 'nb_keywords' => $search_keywords->getRowsCount() ] );
82 $this->insert_record( Archiver::SITE_SEARCH_RECORD_NAME, $search_keywords, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table );
83 Common::destroy( $search_keywords );
84
85 foreach ( $pages as $id => $page ) {
86 $key = isset( $page['uri'] ) ? 'uri' : 'str_url';
87 $uri = $page[ $key ];
88
89 $pos = strpos( $uri, '?' );
90 if ( false !== $pos ) {
91 $pages[ $id ][ $key ] = substr( $uri, 0, $pos );
92 }
93 }
94 $pages_url = PagesUrlConverter::convert( $pages );
95 $this->logger->debug( 'Import {nb_pages} global pages...', [ 'nb_pages' => $pages_url->getRowsCount() ] );
96 $this->insert_record( Archiver::PAGE_URLS_RECORD_NAME, $pages_url, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table, Metrics::INDEX_NB_VISITS );
97 Common::destroy( $pages_url );
98
99 $pages_title = PagesTitleConverter::convert( $pages );
100 $this->logger->debug( 'Import {nb_pages} page titles...', [ 'nb_pages' => $pages_title->getRowsCount() ] );
101 $this->insert_record( Archiver::PAGE_TITLES_RECORD_NAME, $pages_title, $this->maximum_rows_in_data_table_level_zero, $this->maximum_rows_in_sub_data_table, Metrics::INDEX_NB_VISITS );
102 Common::destroy( $pages_title );
103 }
104
105 private function get_metabox_data_provider() {
106 return new MetaboxDataProvider();
107 }
108 }
109