Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DataTableFactory.php « Archive « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e0d516aeb60800178b58d8d7a1ea81f6bb7da493 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */

use Piwik\Archive_DataCollection;
use Piwik\Site;
const FIX_ME_OMG = 'this is a warning and reminder to fix this code ';

/**
 * Creates a Piwik_DataTable or Piwik_DataTable_Array instance based on an array
 * index created by Archive_DataCollection.
 * 
 * This class is only used by Archive_DataCollection.
 */
class Piwik_Archive_DataTableFactory
{
    /**
     * @see Archive_DataCollection::$dataNames.
     */
    private $dataNames;
    
    /**
     * @see Archive_DataCollection::$dataType.
     */
    private $dataType;
    
    /**
     * Whether to expand the DataTables that're created or not. Expanding a DataTable
     * means creating DataTables using subtable blobs and correctly setting the subtable
     * IDs of all DataTables.
     * 
     * @var bool
     */
    private $expandDataTable = false;
    
    /**
     * Whether to add the subtable ID used in the database to the in-memory DataTables
     * as metadata or not.
     * 
     * @var bool
     */
    private $addMetadataSubtableId = false;
    
    /**
     * @see Archive_DataCollection::$sitesId.
     */
    private $sitesId;
    
    /**
     * @see Archive_DataCollection::$periods.
     */
    private $periods;
    
    /**
     * The ID of the subtable to create a DataTable for. Only relevant for blob data.
     * 
     * @var int|null
     */
    private $idSubtable = null;
    
    /**
     * @see Archive_DataCollection::$defaultRow.
     */
    private $defaultRow;
    
    /**
     * Constructor.
     */
    public function __construct($dataNames, $dataType, $sitesId, $periods, $defaultRow)
    {
        $this->dataNames = $dataNames;
        $this->dataType = $dataType;
        $this->sitesId = $sitesId;

        //here index period by string only
        $this->periods = $periods;
        $this->defaultRow = $defaultRow;
    }
    
    /**
     * Tells the factory instance to expand the DataTables that are created by
     * creating subtables and setting the subtable IDs of rows w/ subtables correctly.
     * 
     * @param bool $addMetadataSubtableId Whether to add the subtable ID used in the
     *                                    database to the in-memory DataTables as
     *                                    metadata or not.
     */
    public function expandDataTable($addMetadataSubtableId = false)
    {
        $this->expandDataTable = true;
        $this->addMetadataSubtableId = $addMetadataSubtableId;
    }

    /**
     * Tells the factory instance to create a DataTable using a blob with the
     * supplied subtable ID.
     *
     * @param int $idSubtable An in-database subtable ID.
     * @throws Exception
     */
    public function useSubtable($idSubtable)
    {
        if (count($this->dataNames) !== 1) {
            throw new Exception("Piwik_Archive_DataTableFactory: Getting subtables for multiple records in one"
                               . " archive query is not currently supported.");
        }
        
        $this->idSubtable = $idSubtable;
    }
    
    /**
     * Creates a Piwik_DataTable|Piwik_DataTable_Array instance using an index of
     * archive data.
     * 
     * @param array $index @see Archive_DataCollection
     * @param array $resultIndices an array mapping metadata names with pretty metadata
     *                             labels.
     * @return Piwik_DataTable|Piwik_DataTable_Array
     */
    public function make($index, $resultIndices)
    {
        if (empty($resultIndices)) {
            // for numeric data, if there's no index (and thus only 1 site & period in the query),
            // we want to display every queried metric name
            if (empty($index)
                && $this->dataType == 'numeric'
            ) {
                $index = $this->defaultRow;
            }
            
            $dataTable = $this->createDataTable($index, $keyMetadata = array());
        } else {
            $dataTable = $this->createDataTableArrayFromIndex($index, $resultIndices);
        }
        
        $this->transformMetadata($dataTable);
        return $dataTable;
    }

    /**
     * Creates a Piwik_DataTable|Piwik_DataTable_Array instance using an array
     * of blobs.
     * 
     * If only one record is being queried, a single DataTable will
     * be returned. Otherwise, a DataTable_Array is returned that indexes
     * DataTables by record name.
     * 
     * If expandDataTable was called, and only one record is being queried,
     * the created DataTable's subtables will be expanded.
     * 
     * @param array $blobRow
     * @return Piwik_DataTable|Piwik_DataTable_Array
     */
    private function makeFromBlobRow($blobRow)
    {
        if ($blobRow === false) {
            return new Piwik_DataTable();
        }
        
        if (count($this->dataNames) === 1) {
            return $this->makeDataTableFromSingleBlob($blobRow);
        } else {
            return $this->makeIndexedByRecordNameDataTable($blobRow);
        }
    }
    
    /**
     * Creates a DataTable for one record from an archive data row.
     * 
     * @see makeFromBlobRow
     * 
     * @param array $blobRow
     * @return Piwik_DataTable
     */
    private function makeDataTableFromSingleBlob($blobRow)
    {
        $recordName = reset($this->dataNames);
        if ($this->idSubtable !== null) {
            $recordName .= '_' . $this->idSubtable;
        }
        
        if (!empty($blobRow[$recordName])) {
            $table = Piwik_DataTable::fromSerializedArray($blobRow[$recordName]);
        } else {
            $table = new Piwik_DataTable();
        }
        
        // set table metadata
        $table->metadata = Archive_DataCollection::getDataRowMetadata($blobRow);
        
        if ($this->expandDataTable) {
            $table->enableRecursiveFilters();
            $this->setSubtables($table, $blobRow);
        }
        
        return $table;
    }
    
    /**
     * Creates a DataTable for every record in an archive data row and puts them
     * in a DataTable_Array instance.
     * 
     * @param array $blobRow
     * @return Piwik_DataTable_Array
     */
    private function makeIndexedByRecordNameDataTable($blobRow)
    {
        $table = new Piwik_DataTable_Array();
        $table->setKeyName('recordName');
        
        $tableMetadata = Archive_DataCollection::getDataRowMetadata($blobRow);
        
        foreach ($blobRow as $name => $blob) {
            $newTable = Piwik_DataTable::fromSerializedArray($blob);
            $newTable->metadata = $tableMetadata;
            
            $table->addTable($newTable, $name);
        }
        
        return $table;
    }
    
    /**
     * Creates a Piwik_DataTable_Array from an array index.
     * 
     * @param array $index @see Archive_DataCollection
     * @param array $resultIndices @see make
     * @param array $keyMetadata The metadata to add to the table when it's created.
     * @return Piwik_DataTable_Array
     */
    private function createDataTableArrayFromIndex($index, $resultIndices, $keyMetadata = array())
    {
        $resultIndexLabel = reset($resultIndices);
        $resultIndex = key($resultIndices);
        
        array_shift($resultIndices);
        
        $result = new Piwik_DataTable_Array();
        $result->setKeyName($resultIndexLabel);
        
        foreach ($index as $label => $value) {
            $keyMetadata[$resultIndex] = $label;
            
            if (empty($resultIndices)) {
                $newTable = $this->createDataTable($value, $keyMetadata);
            } else {
                $newTable = $this->createDataTableArrayFromIndex($value, $resultIndices, $keyMetadata);
            }
            
            $result->addTable($newTable, $this->prettifyIndexLabel($resultIndex, $label));
        }
        
        return $result;
    }
    
    /**
     * Creates a Piwik_DataTable instance from an index row.
     * 
     * @param array|false $data An archive data row.
     * @param array $keyMetadata The metadata to add to the table(s) when created.
     * @return Piwik_DataTable|Piwik_DataTable_Array
     */
    private function createDataTable($data, $keyMetadata)
    {
        if ($this->dataType == 'blob') {
            $result = $this->makeFromBlobRow($data);
        } else {
            $table = new Piwik_DataTable_Simple();
            
            if (!empty($data)) {
                $table->metadata = Archive_DataCollection::getDataRowMetadata($data);
                
                Archive_DataCollection::removeMetadataFromDataRow($data);
                
                $table->addRow(new Piwik_DataTable_Row(array(Piwik_DataTable_Row::COLUMNS => $data)));
            } else {
                // if we're querying numeric data, we couldn't find any, and we're only
                // looking for one metric, add a row w/ one column w/ value 0. this is to
                // ensure that the PHP renderer outputs 0 when only one column is queried.
                // w/o this code, an empty array would be created, and other parts of Piwik
                // would break.
                if (count($this->dataNames) == 1) {
                    $name = reset($this->dataNames);
                    $table->addRow(new Piwik_DataTable_Row(array(
                        Piwik_DataTable_Row::COLUMNS => array($name => 0)
                    )));
                }
            }
            
            $result = $table;
        }
        
        if (!isset($keyMetadata['site'])) {
            $keyMetadata['site'] = reset($this->sitesId);
        }
        
        if (!isset($keyMetadata['period'])) {
            reset($this->periods);
            $keyMetadata['period'] = key($this->periods);
        }
        
        // Note: $result can be a DataTable_Array
        $result->filter(function ($table) use ($keyMetadata) {
            foreach ($keyMetadata as $name => $value) {
                $table->setMetadata($name, $value);
            }
        });
        
        return $result;
    }
    
    /**
     * Creates DataTables from $dataTable's subtable blobs (stored in $blobRow) and sets
     * the subtable IDs of each DataTable row.
     * 
     * @param Piwik_DataTable $dataTable
     * @param array $blobRow An array associating record names (w/ subtable if applicable)
     *                       with blob values. This should hold every subtable blob for
     *                       the loaded DataTable.
     */
    private function setSubtables($dataTable, $blobRow)
    {
        $dataName = reset($this->dataNames);
        
        foreach ($dataTable->getRows() as $row) {
            $sid = $row->getIdSubDataTable();
            if ($sid === null) {
                continue;
            }
            
            $blobName = $dataName."_".$sid;
            if (isset($blobRow[$blobName])) {
                $subtable = Piwik_DataTable::fromSerializedArray($blobRow[$blobName]);
                $this->setSubtables($subtable, $blobRow);
                
                // we edit the subtable ID so that it matches the newly table created in memory
                // NB: we dont overwrite the datatableid in the case we are displaying the table expanded.
                if ($this->addMetadataSubtableId) {
                    // this will be written back to the column 'idsubdatatable' just before rendering,
                    // see Renderer/Php.php
                    $row->addMetadata('idsubdatatable_in_db', $row->getIdSubDataTable());
                }
                
                $row->setSubtable($subtable);
            }
        }
    }
    
    /**
     * Converts site IDs and period string ranges into Site instances and
     * Period instances in DataTable metadata.
     */
    private function transformMetadata($table)
    {
        $periods = $this->periods;
        $table->filter(function ($table) use($periods) {
            $table->metadata['site'] = new Site($table->metadata['site']);
            $table->metadata['period'] = empty($periods[$table->metadata['period']])
                                            ? FIX_ME_OMG
                                            : $periods[$table->metadata['period']];
        });
    }
    
    /**
     * Returns the pretty version of an index label.
     * 
     * @param string $labelType eg, 'site', 'period', etc.
     * @param string $label eg, '0', '1', '2012-01-01,2012-01-31', etc.
     * @return string
     */
    private function prettifyIndexLabel($labelType, $label)
    {
        if(empty($this->periods[$label])) {
            return $label; // BAD BUG FIXME
        }
        if ($labelType == 'period') { // prettify period labels
            return $this->periods[$label]->getPrettyString();
        }
        return $label;
    }
}