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 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik;
use Exception;
use Piwik\DataTable\BaseFilter;
use Piwik\DataTable\DataTableInterface;
use Piwik\DataTable\Manager;
use Piwik\DataTable\Renderer\Html;
use Piwik\DataTable\Row;
use Piwik\DataTable\Row\DataTableSummaryRow;
use Piwik\DataTable\Simple;
use ReflectionClass;
/**
* @see Common::destroy()
*/
require_once PIWIK_INCLUDE_PATH . '/core/Common.php';
require_once PIWIK_INCLUDE_PATH . "/core/DataTable/Bridges.php";
/**
* The primary data structure used to store analytics data in Piwik.
*
* <a name="class-desc-the-basics"></a>
* ### The Basics
*
* DataTables consist of rows and each row consists of columns. A column value can be
* a numeric, a string or an array.
*
* Every row has an ID. The ID is either the index of the row or {@link ID_SUMMARY_ROW}.
*
* DataTables are hierarchical data structures. Each row can also contain an additional
* nested sub-DataTable (commonly referred to as a 'subtable').
*
* Both DataTables and DataTable rows can hold **metadata**. _DataTable metadata_ is information
* regarding all the data, such as the site or period that the data is for. _Row metadata_
* is information regarding that row, such as a browser logo or website URL.
*
* Finally, all DataTables contain a special _summary_ row. This row, if it exists, is
* always at the end of the DataTable.
*
* ### Populating DataTables
*
* Data can be added to DataTables in three different ways. You can either:
*
* 1. create rows one by one and add them through {@link addRow()} then truncate if desired,
* 2. create an array of DataTable\Row instances or an array of arrays and add them using
* {@link addRowsFromArray()} or {@link addRowsFromSimpleArray()}
* then truncate if desired,
* 3. or set the maximum number of allowed rows (with {@link setMaximumAllowedRows()})
* and add rows one by one.
*
* If you want to eventually truncate your data (standard practice for all Piwik plugins),
* the third method is the most memory efficient. It is, unfortunately, not always possible
* to use since it requires that the data be sorted before adding.
*
* ### Manipulating DataTables
*
* There are two ways to manipulate a DataTable. You can either:
*
* 1. manually iterate through each row and manipulate the data,
* 2. or you can use predefined filters.
*
* A filter is a class that has a 'filter' method which will manipulate a DataTable in
* some way. There are several predefined Filters that allow you to do common things,
* such as,
*
* - add a new column to each row,
* - add new metadata to each row,
* - modify an existing column value for each row,
* - sort an entire DataTable,
* - and more.
*
* Using these filters instead of writing your own code will increase code clarity and
* reduce code redundancy. Additionally, filters have the advantage that they can be
* applied to DataTable\Map instances. So you can visit every DataTable in a {@link DataTable\Map}
* without having to write a recursive visiting function.
*
* All predefined filters exist in the **Piwik\DataTable\BaseFilter** namespace.
*
* _Note: For convenience, [anonymous functions](https://www.php.net/manual/en/functions.anonymous.php)
* can be used as DataTable filters._
*
* ### Applying Filters
*
* Filters can be applied now (via {@link filter()}), or they can be applied later (via
* {@link queueFilter()}).
*
* Filters that sort rows or manipulate the number of rows should be applied right away.
* Non-essential, presentation filters should be queued.
*
* ### Learn more
*
* - See **{@link ArchiveProcessor}** to learn how DataTables are persisted.
*
* ### Examples
*
* **Populating a DataTable**
*
* // adding one row at a time
* $dataTable = new DataTable();
* $dataTable->addRow(new Row(array(
* Row::COLUMNS => array('label' => 'thing1', 'nb_visits' => 1, 'nb_actions' => 1),
* Row::METADATA => array('url' => 'http://thing1.com')
* )));
* $dataTable->addRow(new Row(array(
* Row::COLUMNS => array('label' => 'thing2', 'nb_visits' => 2, 'nb_actions' => 2),
* Row::METADATA => array('url' => 'http://thing2.com')
* )));
*
* // using an array of rows
* $dataTable = new DataTable();
* $dataTable->addRowsFromArray(array(
* array(
* Row::COLUMNS => array('label' => 'thing1', 'nb_visits' => 1, 'nb_actions' => 1),
* Row::METADATA => array('url' => 'http://thing1.com')
* ),
* array(
* Row::COLUMNS => array('label' => 'thing2', 'nb_visits' => 2, 'nb_actions' => 2),
* Row::METADATA => array('url' => 'http://thing2.com')
* )
* ));
*
* // using a "simple" array
* $dataTable->addRowsFromSimpleArray(array(
* array('label' => 'thing1', 'nb_visits' => 1, 'nb_actions' => 1),
* array('label' => 'thing2', 'nb_visits' => 2, 'nb_actions' => 2)
* ));
*
* **Getting & setting metadata**
*
* $dataTable = \Piwik\Plugins\Referrers\API::getInstance()->getSearchEngines($idSite = 1, $period = 'day', $date = '2007-07-24');
* $oldPeriod = $dataTable->metadata['period'];
* $dataTable->metadata['period'] = Period\Factory::build('week', Date::factory('2013-10-18'));
*
* **Serializing & unserializing**
*
* $maxRowsInTable = Config::getInstance()->General['datatable_archiving_maximum_rows_standard'];j
*
* $dataTable = // ... build by aggregating visits ...
* $serializedData = $dataTable->getSerialized($maxRowsInTable, $maxRowsInSubtable = $maxRowsInTable,
* $columnToSortBy = Metrics::INDEX_NB_VISITS);
*
* $serializedDataTable = $serializedData[0];
* $serailizedSubTable = $serializedData[$idSubtable];
*
* **Filtering for an API method**
*
* public function getMyReport($idSite, $period, $date, $segment = false, $expanded = false)
* {
* $dataTable = Archive::createDataTableFromArchive('MyPlugin_MyReport', $idSite, $period, $date, $segment, $expanded);
* $dataTable->filter('Sort', array(Metrics::INDEX_NB_VISITS, 'desc', $naturalSort = false, $expanded));
* $dataTable->queueFilter('ColumnCallbackAddMetadata', array('label', 'url', __NAMESPACE__ . '\getUrlFromLabelForMyReport'));
* return $dataTable;
* }
*
* @implements \IteratorAggregate<int, Row>
* @implements \ArrayAccess<int, Row>
* @api
*/
class DataTable implements DataTableInterface, \IteratorAggregate, \ArrayAccess
{
public const MAX_DEPTH_DEFAULT = 15;
/** Name for metadata that describes the archiving state of a report */
public const ARCHIVE_STATE_METADATA_NAME = 'archive_state';
/** Name for metadata that describes when a report was archived. */
public const ARCHIVED_DATE_METADATA_NAME = 'ts_archived';
/** Name for metadata that describes which columns are empty and should not be shown. */
public const EMPTY_COLUMNS_METADATA_NAME = 'empty_column';
/** Name for metadata that describes the number of rows that existed before the Limit filter was applied. */
public const TOTAL_ROWS_BEFORE_LIMIT_METADATA_NAME = 'total_rows_before_limit';
/**
* Name for metadata that describes how individual columns should be aggregated when {@link addDataTable()}
* or {@link Piwik\DataTable\Row::sumRow()} is called.
*
* This metadata value must be an array that maps column names with valid operations. Valid aggregation operations are:
*
* - `'skip'`: do nothing
* - `'max'`: does `max($column1, $column2)`
* - `'min'`: does `min($column1, $column2)`
* - `'sum'`: does `$column1 + $column2`
*
* See {@link addDataTable()} and {@link DataTable\Row::sumRow()} for more information.
*/
public const COLUMN_AGGREGATION_OPS_METADATA_NAME = 'column_aggregation_ops';
/**
* Name for metadata that stores array of generic filters that should not be run on the table.
*/
public const GENERIC_FILTERS_TO_DISABLE_METADATA_NAME = 'generic_filters_to_disable';
/** The ID of the Summary Row. */
public const ID_SUMMARY_ROW = -1;
/**
* The ID of the special metadata row. This row only exists in the serialized row data and stores the datatable metadata.
*
* This allows us to save datatable metadata in archive data.
*/
public const ID_ARCHIVED_METADATA_ROW = -3;
/** The original label of the Summary Row. */
public const LABEL_SUMMARY_ROW = -1;
public const LABEL_TOTALS_ROW = -2;
public const LABEL_ARCHIVED_METADATA_ROW = '__datatable_metadata__';
/**
* Name for metadata that contains extra {@link Piwik\Plugin\ProcessedMetric}s for a DataTable.
* These metrics will be added in addition to the ones specified in the table's associated
* {@link Piwik\Plugin\Report} class.
*/
public const EXTRA_PROCESSED_METRICS_METADATA_NAME = 'extra_processed_metrics';
public const ROW_IDENTIFIER_METADATA_NAME = 'rowIdentifier';
/**
* Maximum nesting level.
* @var int
*/
private static $maximumDepthLevelAllowed = self::MAX_DEPTH_DEFAULT;
/**
* Array of Row
*
* @var Row[]|null
*/
protected $rows = [];
/**
* Id assigned to the DataTable, used to lookup the table using the DataTable_Manager
*
* @var int
*/
protected $currentId;
/**
* This flag is set to false once we modify the table in a way that outdates the index
*
* @var bool
*/
protected $indexNotUpToDate = true;
/**
* This flag sets the index to be rebuild whenever a new row is added,
* as opposed to re-building the full index when getRowFromLabel is called.
* This is to optimize and not rebuild the full Index in the case where we
* add row, getRowFromLabel, addRow, getRowFromLabel thousands of times.
*
* @var bool
*/
protected $rebuildIndexContinuously = false;
/**
* Column name of last time the table was sorted
*
* @var string|false
*/
protected $tableSortedBy = false;
/**
* List of BaseFilter queued to this table
*
* @var array<array{className: string|callable, parameters: array<scalar, mixed>}>
*/
protected $queuedFilters = array();
/**
* List of disabled filter names eg 'Limit' or 'Sort'
*
* @var String[]
*/
protected $disabledFilters = array();
/**
* Defaults to false for performance reasons (most of the time we don't need recursive sorting so we save a looping over the dataTable)
*
* @var bool
*/
protected $enableRecursiveSort = false;
/**
* When the table and all subtables are loaded, this flag will be set to true to ensure filters are applied to all subtables
*
* @var bool
*/
protected $enableRecursiveFilters = false;
/**
* @var array<string, int>
*/
protected $rowsIndexByLabel = array();
/**
* @var Row|null
*/
protected $summaryRow = null;
/**
* @var Row|null
*/
protected $totalsRow = null;
/**
* Table metadata. Read [this](#class-desc-the-basics) to learn more.
*
* Any data that describes the data held in the table's rows should go here.
*
* Note: this field is protected so derived classes will serialize it.
*
* @var array<string, mixed>
*/
protected $metadata = array();
/**
* Maximum number of rows allowed in this datatable (including the summary row).
* If adding more rows is attempted, the extra rows get summed to the summary row.
*
* @var int
*/
protected $maximumAllowedRows = 0;
/** @var bool */
protected $isBuiltWithoutArchives = true;
/**
* Constructor. Creates an empty DataTable.
*/
public function __construct()
{
// registers this instance to the manager
$this->currentId = Manager::getInstance()->addTable($this);
}
/**
* Destructor. Makes sure DataTable memory will be cleaned up.
*/
public function __destruct()
{
static $depth = 0;
// destruct can be called several times
if (
$depth < self::$maximumDepthLevelAllowed
&& isset($this->rows)
) {
$depth++;
foreach ($this->rows as $row) {
Common::destroy($row);
}
if (isset($this->summaryRow)) {
Common::destroy($this->summaryRow);
}
unset($this->rows);
Manager::getInstance()->setTableDeleted($this->currentId);
$depth--;
}
}
/**
* Clone. Called when cloning the datatable. We need to make sure to create a new datatableId.
* If we do not increase tableId it can result in segmentation faults when destructing a datatable.
*/
public function __clone()
{
// registers this instance to the manager
$this->currentId = Manager::getInstance()->addTable($this);
}
/**
* @return void
*/
public function setLabelsHaveChanged()
{
$this->indexNotUpToDate = true;
}
/**
* does not update the summary row!
* @param Row[]|null $rows
* @return void
* @ignore
*/
public function setRows($rows)
{
unset($this->rows);
$this->rows = (is_array($rows) ? $rows : []);
$this->indexNotUpToDate = true;
}
/**
* Sorts the DataTable rows using the supplied callback function.
*
* @param callable $functionCallback A comparison callback compatible with {@link usort}.
* @param string $columnSortedBy The column name `$functionCallback` sorts by. This is stored
* so we can determine how the DataTable was sorted in the future.
* @return void
*/
public function sort($functionCallback, $columnSortedBy)
{
$this->setTableSortedBy($columnSortedBy);
usort($this->rows, $functionCallback);
if ($this->isSortRecursiveEnabled()) {
foreach ($this->getRowsWithoutSummaryRow() as $row) {
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->enableRecursiveSort();
$subTable->sort($functionCallback, $columnSortedBy);
}
}
}
}
/**
* @return void
*/
public function setTotalsRow(Row $totalsRow)
{
$this->totalsRow = $totalsRow;
}
/**
* @return Row|null
*/
public function getTotalsRow()
{
return $this->totalsRow;
}
/**
* @return Row|null
*/
public function getSummaryRow()
{
return $this->summaryRow;
}
/**
* Returns the name of the column this table was sorted by (if any).
*
* See {@link sort()}.
*
* @return false|string The sorted column name or false if none.
*/
public function getSortedByColumnName()
{
return $this->tableSortedBy;
}
/**
* Enables recursive sorting. If this method is called {@link sort()} will also sort all
* subtables.
* @return void
*/
public function enableRecursiveSort()
{
$this->enableRecursiveSort = true;
}
/**
* @return bool
* @ignore
*/
public function isSortRecursiveEnabled()
{
return $this->enableRecursiveSort === true;
}
/**
* @param string $column
* @return void
* @ignore
*/
public function setTableSortedBy($column)
{
$this->indexNotUpToDate = true;
$this->tableSortedBy = $column;
}
/**
* Enables recursive filtering. If this method is called then the {@link filter()} method
* will apply filters to every subtable in addition to this instance.
* @return void
*/
public function enableRecursiveFilters()
{
$this->enableRecursiveFilters = true;
}
/**
* @return void
* @ignore
*/
public function disableRecursiveFilters()
{
$this->enableRecursiveFilters = false;
}
/**
* Applies a filter to this datatable.
*
* If {@link enableRecursiveFilters()} was called, the filter will be applied
* to all subtables as well.
*
* @param string|callable $className Class name, eg. `"Sort"` or "Piwik\DataTable\Filters\Sort"`. If no
* namespace is supplied, `Piwik\DataTable\Filter` is assumed. This parameter
* can also be a closure that takes a DataTable as its first parameter.
* @param array $parameters Array of extra parameters to pass to the filter.
* @return void
*/
public function filter($className, $parameters = array())
{
if (
$className instanceof \Closure
|| is_array($className)
) {
array_unshift($parameters, $this);
call_user_func_array($className, $parameters);
return;
}
if (!is_string($className)) {
throw new Exception('Unsupported filter provided');
}
if (in_array($className, $this->disabledFilters)) {
return;
}
if (!class_exists($className, true)) {
$className = 'Piwik\DataTable\Filter\\' . $className;
}
$reflectionObj = new ReflectionClass($className);
// the first parameter of a filter is the DataTable
// we add the current datatable as the parameter
$parameters = array_merge(array($this), $parameters);
/** @var BaseFilter $filter */
$filter = $reflectionObj->newInstanceArgs($parameters);
$filter->enableRecursive($this->enableRecursiveFilters);
$filter->filter($this);
}
/**
* Invokes `$filter` with this table and every table in `$otherTables`. The result of `$filter()` is returned.
*
* This method is used to iterate over multiple DataTable\Map's concurrently.
*
* See {@link Map::multiFilter()} for more information.
*
* @param DataTable[] $otherTables
* @param callable $filter A function like `function (DataTable $thisTable, $otherTable1, $otherTable2) {}`.
* @return mixed The result of $filter.
*/
public function multiFilter($otherTables, $filter)
{
return $filter(...array_merge([$this], $otherTables));
}
/**
* Applies a filter to all subtables but not to this datatable.
*
* @param string|callable $className Class name, eg. `"Sort"` or "Piwik\DataTable\Filters\Sort"`. If no
* namespace is supplied, `Piwik\DataTable\BaseFilter` is assumed. This parameter
* can also be a closure that takes a DataTable as its first parameter.
* @param array $parameters Array of extra parameters to pass to the filter.
* @return void
*/
public function filterSubtables($className, $parameters = array())
{
foreach ($this->getRowsWithoutSummaryRow() as $row) {
$subtable = $row->getSubtable();
if ($subtable) {
$subtable->filter($className, $parameters);
$subtable->filterSubtables($className, $parameters);
}
}
}
/**
* Adds a filter and a list of parameters to the list of queued filters of all subtables. These filters will be
* executed when {@link applyQueuedFilters()} is called.
*
* Filters that prettify the column values or don't need the full set of rows should be queued. This
* way they will be run after the table is truncated which will result in better performance.
*
* @param string|callable $className The class name of the filter, eg. `'Limit'`.
* @param array $parameters The parameters to give to the filter, eg. `array($offset, $limit)` for the Limit filter.
* @return void
*/
public function queueFilterSubtables($className, $parameters = array())
{
foreach ($this->getRowsWithoutSummaryRow() as $row) {
$subtable = $row->getSubtable();
if ($subtable) {
$subtable->queueFilter($className, $parameters);
$subtable->queueFilterSubtables($className, $parameters);
}
}
}
/**
* Adds a filter and a list of parameters to the list of queued filters. These filters will be
* executed when {@link applyQueuedFilters()} is called.
*
* Filters that prettify the column values or don't need the full set of rows should be queued. This
* way they will be run after the table is truncated which will result in better performance.
*
* @param string|callable $className The class name of the filter, eg. `'Limit'`.
* @param array $parameters The parameters to give to the filter, eg. `array($offset, $limit)` for the Limit filter.
* @return void
*/
public function queueFilter($className, $parameters = array())
{
if (!is_array($parameters)) {
$parameters = array($parameters);
}
$this->queuedFilters[] = ['className' => $className, 'parameters' => $parameters];
}
/**
* Disable a specific filter to run on this DataTable in case you have already applied this filter or if you will
* handle this filter manually by using a custom filter. Be aware if you disable a given filter, that filter won't
* be ever executed. Even if another filter calls this filter on the DataTable.
*
* @param string $className eg 'Limit' or 'Sort'. Passing a `Closure` or an `array($class, $methodName)` is not
* supported yet. We check for exact match. So if you disable 'Limit' and
* call `->filter('Limit')` this filter won't be executed. If you call
* `->filter('Piwik\DataTable\Filter\Limit')` that filter will be executed. See it as a
* feature.
* @return void
* @ignore
*/
public function disableFilter($className)
{
$this->disabledFilters[] = $className;
}
/**
* Applies all filters that were previously queued to the table. See {@link queueFilter()}
* for more information.
* @return void
*/
public function applyQueuedFilters()
{
foreach ($this->queuedFilters as $filter) {
$this->filter($filter['className'], $filter['parameters']);
}
$this->clearQueuedFilters();
}
/**
* Sums a DataTable to this one.
*
* This method will sum rows that have the same label. If a row is found in `$tableToSum` whose
* label is not found in `$this`, the row will be added to `$this`.
*
* If the subtables for this table are loaded, they will be summed as well.
*
* Rows are summed together by summing individual columns. By default columns are summed by
* adding one column value to another. Some columns cannot be aggregated this way. In these
* cases, the {@link COLUMN_AGGREGATION_OPS_METADATA_NAME}
* metadata can be used to specify a different type of operation.
*
* @return void
* @throws Exception
*/
public function addDataTable(DataTable $tableToSum)
{
if ($tableToSum instanceof Simple) {
if ($tableToSum->getRowsCount() > 1) {
throw new Exception("Did not expect a Simple table with more than one row in addDataTable()");
}
$row = $tableToSum->getFirstRow();
$this->aggregateRowFromSimpleTable($row);
} else {
$columnAggregationOps = $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME);
foreach ($tableToSum->getRowsWithoutSummaryRow() as $row) {
$this->aggregateRowWithLabel($row, $columnAggregationOps);
}
// we do not use getRows() as this method might get called 100k times when aggregating many datatables and
// this takes a lot of time.
$row = $tableToSum->getRowFromId(DataTable::ID_SUMMARY_ROW);
if ($row) {
$this->aggregateRow($this->summaryRow, $row, $columnAggregationOps, true);
}
}
}
/**
* Returns the Row whose `'label'` column is equal to `$label`.
*
* This method executes in constant time except for the first call which caches row
* label => row ID mappings.
*
* @param string $label `'label'` column value to look for.
* @return Row|false The row if found, `false` if otherwise.
*/
public function getRowFromLabel($label)
{
$rowId = $this->getRowIdFromLabel($label);
if (is_int($rowId) && isset($this->rows[$rowId])) {
return $this->rows[$rowId];
}
if (
$rowId == self::ID_SUMMARY_ROW
&& !empty($this->summaryRow)
) {
return $this->summaryRow;
}
if (
empty($rowId)
&& !empty($this->totalsRow)
&& $label == $this->totalsRow->getColumn('label')
) {
return $this->totalsRow;
}
return false;
}
/**
* Returns the row id for the row whose `'label'` column is equal to `$label`.
*
* This method executes in constant time except for the first call which caches row
* label => row ID mappings.
*
* @param string $label `'label'` column value to look for.
* @return int|false The row ID.
*/
public function getRowIdFromLabel($label)
{
if ($this->indexNotUpToDate) {
$this->rebuildIndex();
}
$label = (string) $label;
if (!isset($this->rowsIndexByLabel[$label])) {
// in case label is '-1' and there is no normal row w/ that label. Note: this is for BC since
// in the past, it was possible to get the summary row by searching for the label '-1'
if (
$label == self::LABEL_SUMMARY_ROW
&& !is_null($this->summaryRow)
) {
return self::ID_SUMMARY_ROW;
}
return false;
}
return $this->rowsIndexByLabel[$label];
}
/**
* Returns an empty DataTable with the same metadata and queued filters as `$this` one.
*
* @param bool $keepFilters Whether to pass the queued filter list to the new DataTable or not.
* @return DataTable
*/
public function getEmptyClone($keepFilters = true)
{
$clone = new DataTable();
if ($keepFilters) {
$clone->queuedFilters = $this->queuedFilters;
}
$clone->metadata = $this->metadata;
return $clone;
}
/**
* Rebuilds the index used to lookup a row by label
* @return void
* @internal
*/
public function rebuildIndex()
{
$this->rowsIndexByLabel = [];
$this->rebuildIndexContinuously = true;
foreach ($this->rows as $id => $row) {
$label = $row->getColumn('label');
if ($label !== false) {
$this->rowsIndexByLabel[(string) $label] = $id;
}
}
$this->indexNotUpToDate = false;
}
/**
* Returns a row by ID. The ID is either the index of the row or {@link ID_SUMMARY_ROW}.
*
* @param int $id The row ID.
* @return Row|false The Row or false if not found.
*/
public function getRowFromId($id)
{
if (
$id == self::ID_SUMMARY_ROW
&& !is_null($this->summaryRow)
) {
return $this->summaryRow;
}
if (!isset($this->rows[$id])) {
return false;
}
return $this->rows[$id];
}
/**
* Returns the row that has a subtable with ID matching `$idSubtable`.
*
* @param int $idSubTable The subtable ID.
* @return Row|false The row or false if not found
*/
public function getRowFromIdSubDataTable($idSubTable)
{
$idSubTable = (int)$idSubTable;
foreach ($this->rows as $row) {
if ($row->getIdSubDataTable() === $idSubTable) {
return $row;
}
}
return false;
}
/**
* Adds a row to this table.
*
* If {@link setMaximumAllowedRows()} was called and the current row count is
* at the maximum, the new row will be summed to the summary row. If there is no summary row,
* this row is set as the summary row.
*
* @return Row `$row` or the summary row if we're at the maximum number of rows.
*/
public function addRow(Row $row)
{
// if there is a upper limit on the number of allowed rows and the table is full,
// add the new row to the summary row
if (
$this->maximumAllowedRows > 0
&& $this->getRowsCount() >= $this->maximumAllowedRows - 1
) {
if ($this->summaryRow === null) {
// create the summary row if necessary
$columns = array('label' => self::LABEL_SUMMARY_ROW) + $row->getColumns();
$this->addSummaryRow(new Row(array(Row::COLUMNS => $columns)));
} else {
$this->summaryRow->sumRow(
$row,
$enableCopyMetadata = false,
$this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME)
);
}
return $this->summaryRow;
}
$this->rows[] = $row;
if (
!$this->indexNotUpToDate
&& $this->rebuildIndexContinuously
) {
$label = $row->getColumn('label');
if ($label !== false) {
$this->rowsIndexByLabel[(string) $label] = count($this->rows) - 1;
}
}
return $row;
}
/**
* Sets the summary row.
*
* _Note: A DataTable can have only one summary row._
*
* @return Row Returns `$row`.
*/
public function addSummaryRow(Row $row)
{
$this->summaryRow = $row;
$row->setIsSummaryRow();
// NOTE: the summary row does not go in the index, since it will overwrite rows w/ label == -1
return $row;
}
/**
* Returns the DataTable ID.
*
* @return int
*/
public function getId()
{
return $this->currentId;
}
/**
* Adds a new row from an array.
*
* You can add row metadata with this method.
*
* @param array $row eg. `array(Row::COLUMNS => array('visits' => 13, 'test' => 'toto'),
* Row::METADATA => array('mymetadata' => 'myvalue'))`
* @return void
*/
public function addRowFromArray($row)
{
$this->addRowsFromArray(array($row));
}
/**
* Adds a new row a from an array of column values.
*
* Row metadata cannot be added with this method.
*
* @param array $row eg. `array('name' => 'google analytics', 'license' => 'commercial')`
* @return void
*/
public function addRowFromSimpleArray($row)
{
$this->addRowsFromSimpleArray(array($row));
}
/**
* Returns the array of Rows.
* Internal logic in Matomo core should avoid using this method as it is time and memory consuming when being
* executed thousands of times. The alternative is to use {@link getRowsWithoutSummaryRow()} + get the summary
* row manually.
*
* @return Row[]
*/
public function getRows()
{
if (is_null($this->summaryRow)) {
return $this->rows;
} else {
return $this->rows + array(self::ID_SUMMARY_ROW => $this->summaryRow);
}
}
/**
* @return Row[]
* @ignore
*/
public function getRowsWithoutSummaryRow()
{
return $this->rows;
}
/**
* @return int
* @ignore
*/
public function getRowsCountWithoutSummaryRow()
{
return count($this->rows);
}
/**
* Returns an array containing all column values for the requested column.
*
* @param string|int $name The column name.
* @return array The array of column values.
*/
public function getColumn($name)
{
$columnValues = array();
foreach ($this->getRows() as $row) {
$columnValues[] = $row->getColumn($name);
}
return $columnValues;
}
/**
* Returns an array containing all column values of columns whose name starts with `$name`.
*
* @param string $namePrefix The column name prefix.
* @return array The array of column values.
*/
public function getColumnsStartingWith($namePrefix)
{
$columnValues = array();
foreach ($this->getRows() as $row) {
$columns = $row->getColumns();
foreach ($columns as $column => $value) {
if (strpos($column, $namePrefix) === 0) {
$columnValues[] = $row->getColumn($column);
}
}
}
return $columnValues;
}
/**
* Returns the names of every column this DataTable contains. This method will return the
* columns of the first row with data and will assume they occur in every other row as well.
*
*_ Note: If column names still use their in-database INDEX values (@see Metrics), they
* will be converted to their string name in the array result._
*
* @return array Array of string column names.
*/
public function getColumns()
{
$result = array();
foreach ($this->getRows() as $row) {
$columns = $row->getColumns();
if (!empty($columns)) {
$result = array_keys($columns);
break;
}
}
// make sure column names are not DB index values
foreach ($result as &$column) {
if (isset(Metrics::$mappingFromIdToName[$column])) {
$column = Metrics::$mappingFromIdToName[$column];
}
}
return $result;
}
/**
* Returns an array containing the requested metadata value of each row.
*
* @param string $name The metadata column to return.
* @return array
*/
public function getRowsMetadata($name)
{
$metadataValues = array();
foreach ($this->getRows() as $row) {
$metadataValues[] = $row->getMetadata($name);
}
return $metadataValues;
}
/**
* Delete row metadata by name in every row.
*
* @param string $name
* @param bool $deleteRecursiveInSubtables
* @return void
*/
public function deleteRowsMetadata($name, $deleteRecursiveInSubtables = false)
{
foreach ($this->rows as $row) {
$row->deleteMetadata($name);
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->deleteRowsMetadata($name, $deleteRecursiveInSubtables);
}
}
if (!is_null($this->summaryRow)) {
$this->summaryRow->deleteMetadata($name);
}
if (!is_null($this->totalsRow)) {
$this->totalsRow->deleteMetadata($name);
}
}
/**
* Returns the number of rows in the table including the summary row.
*
* @return int
*/
public function getRowsCount()
{
if (is_null($this->summaryRow)) {
return count($this->rows);
} else {
return count($this->rows) + 1;
}
}
/**
* Returns the first row of the DataTable.
*
* @return Row|false The first row or `false` if it cannot be found.
*/
public function getFirstRow()
{
if (count($this->rows) == 0) {
if (!is_null($this->summaryRow)) {
return $this->summaryRow;
}
return false;
}
return reset($this->rows);
}
/**
* Returns the last row of the DataTable. If there is a summary row, it
* will always be considered the last row.
*
* @return Row|false The last row or `false` if it cannot be found.
*/
public function getLastRow()
{
if (!is_null($this->summaryRow)) {
return $this->summaryRow;
}
if (count($this->rows) == 0) {
return false;
}
return end($this->rows);
}
/**
* Returns the number of rows in the entire DataTable hierarchy. This is the number of rows in this DataTable
* summed with the row count of each descendant subtable.
*
* @return int
*/
public function getRowsCountRecursive()
{
$totalCount = 0;
foreach ($this->rows as $row) {
$subTable = $row->getSubtable();
if ($subTable) {
$count = $subTable->getRowsCountRecursive();
$totalCount += $count;
}
}
$totalCount += $this->getRowsCount();
return $totalCount;
}
/**
* Returns the number of leaf rows in the entire DataTable hierarchy. Only rows that do not contain a subtables are counted
*
* @return int
*/
public function getLeafRowsCount()
{
$totalCount = 0;
foreach ($this->rows as $row) {
$subTable = $row->getSubtable();
if ($subTable) {
$totalCount += $subTable->getLeafRowsCount();
} else {
$totalCount++;
}
}
return $totalCount;
}
/**
* Delete a column by name in every row. This change is NOT applied recursively to all
* subtables.
*
* @param string $name Column name to delete.
* @return void
*/
public function deleteColumn($name)
{
$this->deleteColumns(array($name));
}
public function __sleep()
{
return array('rows', 'summaryRow', 'metadata', 'totalsRow');
}
/**
* Rename a column in every row. This change is applied recursively to all subtables.
*
* @param string $oldName Old column name.
* @param string $newName New column name.
* @return void
*/
public function renameColumn($oldName, $newName)
{
foreach ($this->rows as $row) {
$row->renameColumn($oldName, $newName);
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->renameColumn($oldName, $newName);
}
}
if (!is_null($this->summaryRow)) {
$this->summaryRow->renameColumn($oldName, $newName);
}
if (!is_null($this->totalsRow)) {
$this->totalsRow->renameColumn($oldName, $newName);
}
}
/**
* Deletes several columns by name in every row.
*
* @param array $names List of column names to delete.
* @param bool $deleteRecursiveInSubtables Whether to apply this change to all subtables or not.
* @return void
*/
public function deleteColumns($names, $deleteRecursiveInSubtables = false)
{
foreach ($this->rows as $row) {
foreach ($names as $name) {
$row->deleteColumn($name);
}
$subTable = $row->getSubtable();
if ($subTable) {
$subTable->deleteColumns($names, $deleteRecursiveInSubtables);
}
}
if (!is_null($this->summaryRow)) {
foreach ($names as $name) {
$this->summaryRow->deleteColumn($name);
}
}
if (!is_null($this->totalsRow)) {
foreach ($names as $name) {
$this->totalsRow->deleteColumn($name);
}
}
}
/**
* Deletes a row by ID.
*
* @param int $id The row ID.
* @return void
* @throws Exception If the row `$id` cannot be found.
*/
public function deleteRow($id)
{
if ($id === self::ID_SUMMARY_ROW) {
$this->summaryRow = null;
return;
}
if (!isset($this->rows[$id])) {
throw new Exception("Trying to delete unknown row with idkey = $id");
}
unset($this->rows[$id]);
}
/**
* Deletes rows from `$offset` to `$offset + $limit`.
*
* @param int $offset The offset to start deleting rows from.
* @param int|null $limit The number of rows to delete. If `null` all rows after the offset
* will be removed.
* @return int The number of rows deleted.
*/
public function deleteRowsOffset($offset, $limit = null)
{
if ($limit === 0) {
return 0;
}
$count = $this->getRowsCount();
if ($offset >= $count) {
return 0;
}
// if we delete until the end, we delete the summary row as well
if (
is_null($limit)
|| $limit >= $count
) {
$this->summaryRow = null;
}
if (is_null($limit)) {
array_splice($this->rows, $offset);
} else {
array_splice($this->rows, $offset, $limit);
}
return $count - $this->getRowsCount();
}
/**
* Deletes a set of rows by ID.
*
* @param array $rowIds The list of row IDs to delete.
* @return void
* @throws Exception If a row ID cannot be found.
*/
public function deleteRows(array $rowIds)
{
foreach ($rowIds as $key) {
$this->deleteRow($key);
}
}
/**
* Returns a string representation of this DataTable for convenient viewing.
*
* _Note: This uses the **html** DataTable renderer._
*
* @return string
*/
public function __toString()
{
$renderer = new Html();
$renderer->setTable($this);
return (string)$renderer;
}
/**
* Returns true if both DataTable instances are exactly the same.
*
* DataTables are equal if they have the same number of rows, if
* each row has a label that exists in the other table, and if each row
* is equal to the row in the other table with the same label. The order
* of rows is not important.
*
* @return bool
*/
public static function isEqual(DataTable $table1, DataTable $table2)
{
$table1->rebuildIndex();
$table2->rebuildIndex();
if ($table1->getRowsCount() != $table2->getRowsCount()) {
return false;
}
$rows1 = $table1->getRows();
foreach ($rows1 as $row1) {
$row2 = $table2->getRowFromLabel($row1->getColumn('label'));
if (
$row2 === false
|| !Row::isEqual($row1, $row2)
) {
return false;
}
}
return true;
}
/**
* Serializes an entire DataTable hierarchy and returns the array of serialized DataTables.
*
* The first element in the returned array will be the serialized representation of this DataTable.
* Every subsequent element will be a serialized subtable.
*
* This DataTable and subtables can optionally be truncated before being serialized. In most
* cases where DataTables can become quite large, they should be truncated before being persisted
* in an archive.
*
* The result of this method is intended for use with the {@link ArchiveProcessor::insertBlobRecord()} method.
*
* @throws Exception If infinite recursion detected. This will occur if a table's subtable is one of its parent tables.
* @param int $maximumRowsInDataTable If not null, defines the maximum number of rows allowed in the serialized DataTable.
* @param int $maximumRowsInSubDataTable If not null, defines the maximum number of rows allowed in serialized subtables.
* @param string $columnToSortByBeforeTruncation The column to sort by before truncating, eg, `Metrics::INDEX_NB_VISITS`.
* @param array $aSerializedDataTable Will contain all the output arrays
* @return array The array of serialized DataTables:
*
* array(
* // this DataTable (the root)
* 0 => 'eghuighahgaueytae78yaet7yaetae',
*
* // a subtable
* 1 => 'gaegae gh gwrh guiwh uigwhuige',
*
* // another subtable
* 2 => 'gqegJHUIGHEQjkgneqjgnqeugUGEQHGUHQE',
*
* // etc.
* );
*/
public function getSerialized(
$maximumRowsInDataTable = null,
$maximumRowsInSubDataTable = null,
$columnToSortByBeforeTruncation = null,
&$aSerializedDataTable = array()
) {
static $depth = 0;
// make sure subtableIds are consecutive from 1 to N
static $subtableId = 0;
if ($depth > self::$maximumDepthLevelAllowed) {
$depth = 0;
$subtableId = 0;
throw new Exception("Maximum recursion level of " . self::$maximumDepthLevelAllowed . " reached. Maybe you have set a DataTable\Row with an associated DataTable belonging already to one of its parent tables?");
}
// gather metadata before filters are called, so their metadata is not stored in serialized form
$metadata = $this->getAllTableMetadata();
foreach ($metadata as $key => $value) {
if (!is_scalar($value)) {
unset($metadata[$key]);
}
}
if (!is_null($maximumRowsInDataTable)) {
$this->filter(
'Truncate',
array($maximumRowsInDataTable - 1,
DataTable::LABEL_SUMMARY_ROW,
$columnToSortByBeforeTruncation,
$filterRecursive = false)
);
}
$consecutiveSubtableIds = array();
$forcedId = $subtableId;
// For each row (including the summary row), get the serialized row
// If it is associated to a sub table, get the serialized table recursively ;
// but returns all serialized tables and subtable in an array of 1 dimension
foreach ($this->getRows() as $id => $row) {
$subTable = $row->getSubtable();
if ($subTable) {
$consecutiveSubtableIds[$id] = ++$subtableId;
$depth++;
$subTable->getSerialized($maximumRowsInSubDataTable, $maximumRowsInSubDataTable, $columnToSortByBeforeTruncation, $aSerializedDataTable);
$depth--;
} else {
$row->removeSubtable();
}
}
// if the datatable is the parent we force the Id at 0 (this is part of the specification)
if ($depth == 0) {
$forcedId = 0;
$subtableId = 0;
}
// we then serialize the rows and store them in the serialized dataTable
$rows = array();
foreach ($this->rows as $id => $row) {
if (isset($consecutiveSubtableIds[$id])) {
$backup = $row->subtableId;
$row->subtableId = $consecutiveSubtableIds[$id];
$rows[$id] = $row->export();
$row->subtableId = $backup;
} else {
$rows[$id] = $row->export();
}
}
if (isset($this->summaryRow)) {
$id = self::ID_SUMMARY_ROW;
$row = $this->summaryRow;
// duplicating code above so we don't create a new array w/ getRows() above in this function which is
// used heavily in matomo.
if (isset($consecutiveSubtableIds[$id])) {
$backup = $row->subtableId;
$row->subtableId = $consecutiveSubtableIds[$id];
$rows[$id] = $row->export();
$row->subtableId = $backup;
} else {
$rows[$id] = $row->export();
}
}
if (!empty($metadata)) {
$metadataRow = new Row();
$metadataRow->setColumns($metadata);
// set the label so the row will be indexed correctly internally
$metadataRow->setColumn('label', self::LABEL_ARCHIVED_METADATA_ROW);
$rows[self::ID_ARCHIVED_METADATA_ROW] = $metadataRow->export();
}
$aSerializedDataTable[$forcedId] = serialize($rows);
unset($rows);
return $aSerializedDataTable;
}
/** @var string[] */
private static $previousRowClasses = [
'O:39:"Piwik\DataTable\Row\DataTableSummaryRow"',
'O:19:"Piwik\DataTable\Row"',
'O:36:"Piwik_DataTable_Row_DataTableSummary"',
'O:19:"Piwik_DataTable_Row"',
];
/** @var string */
private static $rowClassToUseForUnserialize = 'O:29:"Piwik_DataTable_SerializedRow"';
/**
* It is faster to unserialize existing serialized Row instances to "Piwik_DataTable_SerializedRow" and access the
* `$row->c` property than implementing a "__wakeup" method in the Row instance to map the "$row->c" to $row->columns
* etc. We're talking here about 15% faster reports aggregation in some cases. To be concrete: We have a test where
* Archiving a year takes 1700 seconds with "__wakeup" and 1400 seconds with this method. Yes, it takes 300 seconds
* to wake up millions of rows. We should be able to remove this code here end 2015 and use the "__wakeup" way by then.
* Why? By then most new archives will have only arrays serialized anyway and therefore this mapping is rather an overhead.
*
* @param string $serialized
* @return array
* @throws Exception In case the unserialize fails
*/
private function unserializeRows($serialized)
{
// Current archives only persist row arrays, so do not allow objects in the default path.
$rows = Common::safe_unserialize($serialized, []);
if (!$this->isValidRowsPayload($rows, $allowLegacySerializedRowObjects = false)) {
$rows = false;
}
if ($rows === false) {
// Legacy object payloads are attempted as a fallback for BC.
$legacySerialized = str_replace(
array_map(function ($class) {
return $class . ':';
}, self::$previousRowClasses),
self::$rowClassToUseForUnserialize . ':',
$serialized
);
$rows = Common::safe_unserialize($legacySerialized, [
\Piwik_DataTable_SerializedRow::class,
]);
}
if (!$this->isValidRowsPayload($rows, $allowLegacySerializedRowObjects = true)) {
throw new Exception("The unserialization has failed!");
}
return $rows;
}
private function isValidRowsPayload($rows, bool $allowLegacySerializedRowObjects): bool
{
if (!is_array($rows)) {
return false;
}
foreach ($rows as $row) {
if ($allowLegacySerializedRowObjects && $this->isValidLegacySerializedRowObject($row)) {
continue;
}
if ($this->containsObject($row)) {
return false;
}
}
return true;
}
private function isValidLegacySerializedRowObject($row): bool
{
if (!$row instanceof \Piwik_DataTable_SerializedRow) {
return false;
}
return isset($row->c) && is_array($row->c) && !$this->containsObject($row->c);
}
private function containsObject($value): bool
{
if (is_object($value)) {
return true;
}
if (!is_array($value)) {
return false;
}
$containsObject = false;
try {
array_walk_recursive($value, function ($entry) use (&$containsObject): void {
if (is_object($entry)) {
$containsObject = true;
}
});
} catch (\Throwable $error) {
throw new Exception('The unserialization has failed! Array payload cannot be safely traversed.', 0, $error);
}
return $containsObject;
}
/**
* Adds a set of rows from a serialized DataTable string.
*
* See {@link serialize()}.
*
* _Note: This function will successfully load DataTables serialized by Piwik 1.X._
*
* @param string $serialized A string with the format of a string in the array returned by
* {@link serialize()}.
* @return void
* @throws Exception if `$serialized` is invalid.
*/
public function addRowsFromSerializedArray($serialized)
{
$rows = $this->unserializeRows($serialized);
if (array_key_exists(self::ID_SUMMARY_ROW, $rows)) {
if (is_array($rows[self::ID_SUMMARY_ROW])) {
$this->summaryRow = new Row($rows[self::ID_SUMMARY_ROW]);
$this->summaryRow->setIsSummaryRow();
} elseif (isset($rows[self::ID_SUMMARY_ROW]->c)) {
$this->summaryRow = new Row($rows[self::ID_SUMMARY_ROW]->c); // Pre Piwik 2.13
$this->summaryRow->setIsSummaryRow();
}
unset($rows[self::ID_SUMMARY_ROW]);
}
if (array_key_exists(self::ID_ARCHIVED_METADATA_ROW, $rows)) {
$metadata = $rows[self::ID_ARCHIVED_METADATA_ROW][Row::COLUMNS];
unset($metadata['label']);
$this->setAllTableMetadata($metadata);
unset($rows[self::ID_ARCHIVED_METADATA_ROW]);
}
foreach ($rows as $id => $row) {
if (isset($row->c)) {
$this->addRow(new Row($row->c)); // Pre Piwik 2.13
} else {
$this->addRow(new Row($row));
}
}
}
/**
* Adds multiple rows from an array.
*
* You can add row metadata with this method.
*
* @param array $array Array with the following structure
*
* array(
* // row1
* array(
* Row::COLUMNS => array( col1_name => value1, col2_name => value2, ...),
* Row::METADATA => array( metadata1_name => value1, ...), // see Row
* ),
* // row2
* array( ... ),
* )
* @return void
*/
public function addRowsFromArray($array)
{
foreach ($array as $id => $row) {
if (is_array($row)) {
$row = new Row($row);
}
if ($id == self::ID_SUMMARY_ROW) {
$this->summaryRow = $row;
$this->summaryRow->setIsSummaryRow();
} else {
$this->addRow($row);
}
}
}
/**
* Adds multiple rows from an array containing arrays of column values.
*
* Row metadata cannot be added with this method.
*
* @param array $array Array with the following structure:
*
* array(
* array( col1_name => valueA, col2_name => valueC, ...),
* array( col1_name => valueB, col2_name => valueD, ...),
* )
* @return void
* @throws Exception if `$array` is in an incorrect format.
*/
public function addRowsFromSimpleArray($array)
{
if (count($array) === 0) {
return;
}
$exceptionText = "Data structure returned is not convertible in the requested format: %s" .
" Try to call this method with the parameters '&format=original&serialize=1'" .
"; you will get the original php data structure serialized.";
// first pass to see if the array has the structure
// array(col1_name => val1, col2_name => val2, etc.)
// with val* that are never arrays (only strings/numbers/bool/etc.)
// if we detect such a "simple" data structure we convert it to a row with the correct columns' names
$thisIsNotThatSimple = false;
foreach ($array as $columnValue) {
if (is_array($columnValue) || is_object($columnValue)) {
$thisIsNotThatSimple = true;
break;
}
}
if ($thisIsNotThatSimple === false) {
// case when the array is indexed by the default numeric index
if (array_keys($array) === array_keys(array_fill(0, count($array), true))) {
foreach ($array as $row) {
$this->addRow(new Row(array(Row::COLUMNS => array($row))));
}
} else {
$this->addRow(new Row(array(Row::COLUMNS => $array)));
}
// we have converted our simple array to one single row
// => we exit the method as the job is now finished
return;
}
foreach ($array as $key => $row) {
// stuff that looks like a line
if (is_array($row)) {
/**
* We make sure we can convert this PHP array without losing information.
* We are able to convert only simple php array (no strings keys, no sub arrays, etc.)
*
*/
// if the key is a string it means that some information was contained in this key.
// it cannot be lost during the conversion. Because we are not able to handle properly
// this key, we throw an explicit exception.
if (is_string($key)) {
// we define an exception we may throw if at one point we notice that we cannot handle the data structure
throw new Exception(
sprintf(
$exceptionText,
sprintf(
"Only integer keys supported for array columns on base level. Unsupported string '%s' found for row '%s'.",
$key,
substr(var_export($row, true), 0, 500)
)
)
);
}
// if any of the sub elements of row is an array we cannot handle this data structure...
foreach ($row as $name => $subRow) {
if (is_array($subRow)) {
throw new Exception(
sprintf(
$exceptionText,
sprintf(
"Multidimensional column values not supported. Found unexpected array value for column '%s' in row '%s': '%s'.",
$name,
$key,
substr(var_export($subRow, true), 0, 500)
)
)
);
}
}
$row = new Row(array(Row::COLUMNS => $row));
} else {
// other (string, numbers...) => we build a line from this value
$row = new Row(array(Row::COLUMNS => array($key => $row)));
}
$this->addRow($row);
}
}
/**
* Rewrites the input `$array`
*
* array (
* LABEL => array(col1 => X, col2 => Y),
* LABEL2 => array(col1 => X, col2 => Y),
* )
*
* to a DataTable with rows that look like:
*
* array (
* array( Row::COLUMNS => array('label' => LABEL, col1 => X, col2 => Y)),
* array( Row::COLUMNS => array('label' => LABEL2, col1 => X, col2 => Y)),
* )
*
* Will also convert arrays like:
*
* array (
* LABEL => X,
* LABEL2 => Y,
* )
*
* to:
*
* array (
* array( Row::COLUMNS => array('label' => LABEL, 'value' => X)),
* array( Row::COLUMNS => array('label' => LABEL2, 'value' => Y)),
* )
*
* @param array $array Indexed array, two formats supported, see above.
* @param array|null $subtablePerLabel An array mapping label values with DataTable instances to associate as a subtable.
* @return DataTable
*/
public static function makeFromIndexedArray($array, $subtablePerLabel = null)
{
$table = new DataTable();
foreach ($array as $label => $row) {
$cleanRow = array();
// Support the case of an $array of single values
if (!is_array($row)) {
$row = array('value' => $row);
}
// Put the 'label' column first
$cleanRow[Row::COLUMNS] = array('label' => $label) + $row;
// Assign subtable if specified
if (isset($subtablePerLabel[$label])) {
$cleanRow[Row::DATATABLE_ASSOCIATED] = $subtablePerLabel[$label];
}
if ($label === RankingQuery::LABEL_SUMMARY_ROW) {
$table->addSummaryRow(new Row($cleanRow));
} else {
$table->addRow(new Row($cleanRow));
}
}
return $table;
}
/**
* Sets the maximum depth level to at least a certain value. If the current value is
* greater than `$atLeastLevel`, the maximum nesting level is not changed.
*
* The maximum depth level determines the maximum number of subtable levels in the
* DataTable tree. For example, if it is set to `2`, this DataTable is allowed to
* have subtables, but the subtables are not.
*
* @param int $atLeastLevel
* @return void
*/
public static function setMaximumDepthLevelAllowedAtLeast($atLeastLevel)
{
self::$maximumDepthLevelAllowed = max($atLeastLevel, self::$maximumDepthLevelAllowed);
if (self::$maximumDepthLevelAllowed < 1) {
self::$maximumDepthLevelAllowed = 1;
}
}
/**
* Returns metadata by name.
*
* @param string $name The metadata name.
* @return mixed|false The metadata value or `false` if it cannot be found.
*/
public function getMetadata($name)
{
if (!isset($this->metadata[$name])) {
return false;
}
return $this->metadata[$name];
}
/**
* Sets a metadata value by name.
*
* @param string $name The metadata name.
* @param mixed $value
* @return void
*/
public function setMetadata($name, $value)
{
$this->metadata[$name] = $value;
}
/**
* Deletes a metadata property by name.
*
* @param bool|string $name The metadata name (omit to delete all metadata)
* @return bool True if the requested metadata was deleted
*/
public function deleteMetadata($name = false): bool
{
if ($name === false) {
$this->metadata = [];
return true;
}
if (!isset($this->metadata[$name])) {
return false;
}
unset($this->metadata[$name]);
return true;
}
/**
* Returns all table metadata.
*
* @return array<string, mixed>
*/
public function getAllTableMetadata()
{
return $this->metadata;
}
/**
* Sets several metadata values by name.
*
* @param array<string, mixed> $values Array mapping metadata names with metadata values.
* @return void
*/
public function setMetadataValues($values)
{
foreach ($values as $name => $value) {
$this->metadata[$name] = $value;
}
}
/**
* Sets metadata, erasing existing values.
*
* @param array $metadata Array mapping metadata names with metadata values.
* @return void
*/
public function setAllTableMetadata($metadata)
{
$this->metadata = $metadata;
}
/**
* Sets the maximum number of rows allowed in this datatable (including the summary
* row). If adding more then the allowed number of rows is attempted, the extra
* rows are summed to the summary row.
*
* @param int $maximumAllowedRows If `0`, the maximum number of rows is unset.
* @return void
*/
public function setMaximumAllowedRows($maximumAllowedRows)
{
$this->maximumAllowedRows = $maximumAllowedRows;
}
/**
* Traverses a DataTable tree using an array of labels and returns the row
* it finds or `false` if it cannot find one. The number of path segments that
* were successfully walked is also returned.
*
* If `$missingRowColumns` is supplied, the specified path is created. When
* a subtable is encountered w/o the required label, a new row is created
* with the label, and a new subtable is added to the row.
*
* Read [https://en.wikipedia.org/wiki/Tree_(data_structure)#Traversal_methods](https://en.wikipedia.org/wiki/Tree_(data_structure)#Traversal_methods)
* for more information about tree walking.
*
* @param array $path The path to walk. An array of label values. The first element
* refers to a row in this DataTable, the second in a subtable of
* the first row, the third a subtable of the second row, etc.
* @param array|false $missingRowColumns The default columns to use when creating new rows.
* If this parameter is supplied, new rows will be
* created for path labels that cannot be found.
* @param int $maxSubtableRows The maximum number of allowed rows in new subtables. New
* subtables are only created if `$missingRowColumns` is provided.
* @return array{0: false|Row, 1: int} First element is the found row or `false`. Second element is
* the number of path segments walked. If a row is found, this
* will be == to `count($path)`. Otherwise, it will be the index
* of the path segment that we could not find.
*/
public function walkPath($path, $missingRowColumns = false, $maxSubtableRows = 0)
{
$pathLength = count($path);
$table = $this;
$next = false;
for ($i = 0; $i < $pathLength; ++$i) {
$segment = $path[$i];
$next = $table->getRowFromLabel($segment);
if ($next === false) {
// if there is no table to advance to, and we're not adding missing rows, return false
if ($missingRowColumns === false) {
return [false, $i];
} else {
// if we're adding missing rows, add a new row
$row = new DataTableSummaryRow();
$row->setColumns(array('label' => $segment) + $missingRowColumns);
$next = $table->addRow($row);
if ($next !== $row) {
// if the row wasn't added, the table is full
// Summary row, has no metadata
$next->deleteMetadata();
return [$next, $i];
}
}
}
$table = $next->getSubtable();
if ($table === false) {
// if the row has no table (and thus no child rows), and we're not adding
// missing rows, return false
if ($missingRowColumns === false) {
return [false, $i];
} elseif ($i != $pathLength - 1) {
// create subtable if missing, but only if not on the last segment
$table = new DataTable();
$table->setMaximumAllowedRows($maxSubtableRows);
$table->metadata[self::COLUMN_AGGREGATION_OPS_METADATA_NAME]
= $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME);
$next->setSubtable($table);
// Summary row, has no metadata
$next->deleteMetadata();
}
}
}
return [$next, $i];
}
/**
* Returns a new DataTable in which the rows of this table are replaced with the aggregatated rows of all its subtables.
*
* @param string|false $labelColumn If supplied the label of the parent row will be added to
* a new column in each subtable row.
*
* If set to, `'label'` each subtable row's label will be prepended
* w/ the parent row's label. So `'child_label'` becomes
* `'parent_label - child_label'`.
* @param bool $useMetadataColumn If true and if `$labelColumn` is supplied, the parent row's
* label will be added as metadata and not a new column.
* @return DataTable
*/
public function mergeSubtables($labelColumn = false, $useMetadataColumn = false)
{
$result = new DataTable();
$result->setAllTableMetadata($this->getAllTableMetadata());
foreach ($this->getRowsWithoutSummaryRow() as $row) {
$subtable = $row->getSubtable();
if ($subtable !== false) {
$parentLabel = $row->getColumn('label');
// add a copy of each subtable row to the new datatable
foreach ($subtable->getRows() as $id => $subRow) {
$copy = clone $subRow;
// if the summary row, add it to the existing summary row (or add a new one)
if ($id == self::ID_SUMMARY_ROW) {
$existing = $result->getRowFromId(self::ID_SUMMARY_ROW);
if ($existing === false) {
$result->addSummaryRow($copy);
} else {
$existing->sumRow($copy, $copyMeta = true, $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME));
}
} else {
if ($labelColumn !== false) {
// if we're modifying the subtable's rows' label column, then we make
// sure to prepend the existing label w/ the parent row's label. otherwise
// we're just adding the parent row's label as a new column/metadata.
$newLabel = $parentLabel;
if ($labelColumn == 'label') {
$newLabel .= ' - ' . $copy->getColumn('label');
}
// modify the child row's label or add new column/metadata
if ($useMetadataColumn) {
$copy->setMetadata($labelColumn, $newLabel);
} else {
$copy->setColumn($labelColumn, $newLabel);
}
}
$result->addRow($copy);
}
}
}
}
return $result;
}
/**
* Returns a new DataTable created with data from a 'simple' array.
*
* See {@link addRowsFromSimpleArray()}.
*
* @param array $array
* @return DataTable
*/
public static function makeFromSimpleArray($array)
{
$dataTable = new DataTable();
$dataTable->addRowsFromSimpleArray($array);
return $dataTable;
}
/**
* Creates a new DataTable instance from a serialized DataTable string.
*
* See {@link getSerialized()} and {@link addRowsFromSerializedArray()}
* for more information on DataTable serialization.
*
* @param string $data
* @return DataTable
*/
public static function fromSerializedArray($data)
{
$result = new DataTable();
$result->addRowsFromSerializedArray($data);
return $result;
}
/**
* Aggregates the $row columns to this table.
*
* $row must have a column "label". The $row will be summed to this table's row with the same label.
*
* @param null|array<string|int, string> $columnAggregationOps
* @return void
* @throws \Exception
*/
protected function aggregateRowWithLabel(Row $row, $columnAggregationOps)
{
$labelToLookFor = $row->getColumn('label');
if ($labelToLookFor === false) {
$message = sprintf(
"Label column not found in the table to add in addDataTable(). Row: %s",
var_export($row->getColumns(), true)
);
throw new Exception($message);
}
$rowFound = $this->getRowFromLabel($labelToLookFor);
// if we find the summary row in the other table, ignore it, since we're aggregating normal rows in this method.
// the summary row is aggregated explicitly after this method is called.
if (
!empty($rowFound)
&& $rowFound->isSummaryRow()
) {
$rowFound = false;
}
$this->aggregateRow($rowFound, $row, $columnAggregationOps, false);
}
/**
* @param Row|false|null $thisRow
* @param array<string|int, string>|false|null $columnAggregationOps
*/
private function aggregateRow($thisRow, Row $otherRow, $columnAggregationOps, bool $isSummaryRow): void
{
if (empty($thisRow)) {
$thisRow = new Row();
$otherRowLabel = $otherRow->getColumn('label');
if ($otherRowLabel !== false) {
$thisRow->addColumn('label', $otherRowLabel);
}
$thisRow->setAllMetadata($otherRow->getMetadata());
if ($isSummaryRow) {
$this->addSummaryRow($thisRow);
} else {
$this->addRow($thisRow);
}
}
$thisRow->sumRow($otherRow, $copyMeta = true, $columnAggregationOps);
// if the row to add has a subtable whereas the current row doesn't
// we simply add it (cloning the subtable)
// if the row has the subtable already
// then we have to recursively sum the subtables
$subTable = $otherRow->getSubtable();
if ($subTable) {
$subTable->metadata[self::COLUMN_AGGREGATION_OPS_METADATA_NAME] = $columnAggregationOps;
$thisRow->sumSubtable($subTable);
}
}
/**
* @param Row|false $row
* @return void
*/
protected function aggregateRowFromSimpleTable($row)
{
if ($row === false) {
return;
}
$thisRow = $this->getFirstRow();
if ($thisRow === false) {
$thisRow = new Row();
$this->addRow($thisRow);
}
$thisRow->sumRow($row, true, $this->getMetadata(self::COLUMN_AGGREGATION_OPS_METADATA_NAME));
}
/**
* Unsets all queued filters.
* @return void
*/
public function clearQueuedFilters()
{
$this->queuedFilters = [];
}
/**
* @return array
*/
public function getQueuedFilters()
{
return $this->queuedFilters;
}
/**
* @return \ArrayIterator<int, Row>
*/
public function getIterator(): \ArrayIterator
{
return new \ArrayIterator($this->getRows());
}
/**
* @param int $offset
*/
public function offsetExists($offset): bool
{
$row = $this->getRowFromId($offset);
return false !== $row;
}
/**
* @param int $offset
*/
public function offsetGet($offset): Row
{
return $this->getRowFromId($offset);
}
/**
* @param int $offset
* @param Row $value
*/
public function offsetSet($offset, $value): void
{
$this->rows[$offset] = $value;
}
/**
* @param int $offset
* @throws Exception
*/
public function offsetUnset($offset): void
{
$this->deleteRow($offset);
}
/**
* @param string|int|null $label
* @param array $columns
* @param array<string, string>|null $aggregationOps
* @throws Exception
*/
public function sumRowWithLabel($label, array $columns, ?array $aggregationOps = null): Row
{
$label = $label ?? '';
$tableRow = new DataTable\Row([DataTable\Row::COLUMNS => ['label' => $label] + $columns]);
if ($label === RankingQuery::LABEL_SUMMARY_ROW) {
$existingRow = $this->getSummaryRow();
} else {
$existingRow = $this->getRowFromLabel($label);
}
if (empty($existingRow)) {
if ($label === RankingQuery::LABEL_SUMMARY_ROW) {
$this->addSummaryRow($tableRow);
} else {
$this->addRow($tableRow);
}
$existingRow = $tableRow;
} else {
$existingRow->sumRow($tableRow, true, $aggregationOps);
}
return $existingRow;
}
public function setAsBuiltWithoutArchives(bool $flag): void
{
$this->isBuiltWithoutArchives = $flag;
}
public function wasBuiltWithoutArchives(): bool
{
return $this->isBuiltWithoutArchives;
}
}
|