Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LabelFilter.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\API\DataTableManipulator;
  10. use Piwik\API\DataTableManipulator;
  11. use Piwik\Common;
  12. use Piwik\DataTable;
  13. use Piwik\DataTable\Row;
  14. /**
  15. * This class is responsible for handling the label parameter that can be
  16. * added to every API call. If the parameter is set, only the row with the matching
  17. * label is returned.
  18. *
  19. * The labels passed to this class should be urlencoded.
  20. * Some reports use recursive labels (e.g. action reports). Use > to join them.
  21. */
  22. class LabelFilter extends DataTableManipulator
  23. {
  24. const SEPARATOR_RECURSIVE_LABEL = '>';
  25. const TERMINAL_OPERATOR = '@';
  26. private $labels;
  27. private $addLabelIndex;
  28. const FLAG_IS_ROW_EVOLUTION = 'label_index';
  29. /**
  30. * Filter a data table by label.
  31. * The filtered table is returned, which might be a new instance.
  32. *
  33. * $apiModule, $apiMethod and $request are needed load sub-datatables
  34. * for the recursive search. If the label is not recursive, these parameters
  35. * are not needed.
  36. *
  37. * @param string $labels the labels to search for
  38. * @param DataTable $dataTable the data table to be filtered
  39. * @param bool $addLabelIndex Whether to add label_index metadata describing which
  40. * label a row corresponds to.
  41. * @return DataTable
  42. */
  43. public function filter($labels, $dataTable, $addLabelIndex = false)
  44. {
  45. if (!is_array($labels)) {
  46. $labels = array($labels);
  47. }
  48. $this->labels = $labels;
  49. $this->addLabelIndex = (bool)$addLabelIndex;
  50. return $this->manipulate($dataTable);
  51. }
  52. /**
  53. * Method for the recursive descend
  54. *
  55. * @param array $labelParts
  56. * @param DataTable $dataTable
  57. * @return Row|bool
  58. */
  59. private function doFilterRecursiveDescend($labelParts, $dataTable)
  60. {
  61. // search for the first part of the tree search
  62. $labelPart = array_shift($labelParts);
  63. $row = false;
  64. foreach ($this->getLabelVariations($labelPart) as $labelPart) {
  65. $row = $dataTable->getRowFromLabel($labelPart);
  66. if ($row !== false) {
  67. break;
  68. }
  69. }
  70. if ($row === false) {
  71. // not found
  72. return false;
  73. }
  74. // end of tree search reached
  75. if (count($labelParts) == 0) {
  76. return $row;
  77. }
  78. $subTable = $this->loadSubtable($dataTable, $row);
  79. if ($subTable === null) {
  80. // no more subtables but label parts left => no match found
  81. return false;
  82. }
  83. return $this->doFilterRecursiveDescend($labelParts, $subTable);
  84. }
  85. /**
  86. * Clean up request for ResponseBuilder to behave correctly
  87. *
  88. * @param $request
  89. */
  90. protected function manipulateSubtableRequest($request)
  91. {
  92. unset($request['label']);
  93. return $request;
  94. }
  95. /**
  96. * Use variations of the label to make it easier to specify the desired label
  97. *
  98. * Note: The HTML Encoded version must be tried first, since in ResponseBuilder the $label is unsanitized
  99. * via Common::unsanitizeLabelParameter.
  100. *
  101. * @param string $originalLabel
  102. * @return array
  103. */
  104. private function getLabelVariations($originalLabel)
  105. {
  106. static $pageTitleReports = array('getPageTitles', 'getEntryPageTitles', 'getExitPageTitles');
  107. $originalLabel = trim($originalLabel);
  108. $isTerminal = substr($originalLabel, 0, 1) == self::TERMINAL_OPERATOR;
  109. if ($isTerminal) {
  110. $originalLabel = substr($originalLabel, 1);
  111. }
  112. $variations = array();
  113. $label = trim(urldecode($originalLabel));
  114. $sanitizedLabel = Common::sanitizeInputValue($label);
  115. $variations[] = $sanitizedLabel;
  116. if ($this->apiModule == 'Actions'
  117. && in_array($this->apiMethod, $pageTitleReports)
  118. ) {
  119. if ($isTerminal) {
  120. array_unshift($variations, ' ' . $sanitizedLabel);
  121. array_unshift($variations, ' ' . $label);
  122. } else {
  123. // special case: the Actions.getPageTitles report prefixes some labels with a blank.
  124. // the blank might be passed by the user but is removed in Request::getRequestArrayFromString.
  125. $variations[] = ' ' . $sanitizedLabel;
  126. $variations[] = ' ' . $label;
  127. }
  128. }
  129. $variations[] = $label;
  130. return $variations;
  131. }
  132. /**
  133. * Filter a DataTable instance. See @filter for more info.
  134. *
  135. * @param DataTable\Simple|DataTable\Map $dataTable
  136. * @return mixed
  137. */
  138. protected function manipulateDataTable($dataTable)
  139. {
  140. $result = $dataTable->getEmptyClone();
  141. foreach ($this->labels as $labelIndex => $label) {
  142. $row = null;
  143. foreach ($this->getLabelVariations($label) as $labelVariation) {
  144. $labelVariation = explode(self::SEPARATOR_RECURSIVE_LABEL, $labelVariation);
  145. $row = $this->doFilterRecursiveDescend($labelVariation, $dataTable);
  146. if ($row) {
  147. if ($this->addLabelIndex) {
  148. $row->setMetadata(self::FLAG_IS_ROW_EVOLUTION, $labelIndex);
  149. }
  150. $result->addRow($row);
  151. break;
  152. }
  153. }
  154. }
  155. return $result;
  156. }
  157. }