[BUGFIX] Fix several typos in php comments
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Classes / Search / LiveSearch / QueryParser.php
1 <?php
2 namespace TYPO3\CMS\Backend\Search\LiveSearch;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class for parsing query parameters in backend live search.
19 * Detects searches for #pages:23 or #content:mycontent
20 * @internal This class is a specific Backend controller implementation and is not considered part of the Public TYPO3 API.
21 */
22 class QueryParser
23 {
24 /**
25 * @var string
26 */
27 protected $commandKey = '';
28
29 /**
30 * @var string
31 */
32 protected $tableName = '';
33
34 /**
35 * @var string
36 */
37 const COMMAND_KEY_INDICATOR = '#';
38
39 /**
40 * @var string
41 */
42 const COMMAND_SPLIT_INDICATOR = ':';
43
44 /**
45 * Retrieve the validated command key
46 *
47 * @param string $query
48 * @return string Command name
49 */
50 protected function extractKeyFromQuery($query)
51 {
52 list($this->commandKey) = explode(':', substr($query, 1));
53 }
54
55 /**
56 * Extract the search value from the full search query which contains also the command part.
57 *
58 * @param string $query For example #news:weather
59 * @return string The extracted search value
60 */
61 public function getSearchQueryValue($query)
62 {
63 $this->extractKeyFromQuery($query);
64 return str_replace(self::COMMAND_KEY_INDICATOR . $this->commandKey . self::COMMAND_SPLIT_INDICATOR, '', $query);
65 }
66
67 /**
68 * Find the registered table command and retrieve the matching table name.
69 *
70 * @param string $query
71 * @return string Database Table name
72 */
73 public function getTableNameFromCommand($query)
74 {
75 $tableName = '';
76 $this->extractKeyFromQuery($query);
77 if (array_key_exists($this->commandKey, $GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'])) {
78 $tableName = $GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'][$this->commandKey];
79 }
80 return $tableName;
81 }
82
83 /**
84 * Verify if a given query contains a page jump command.
85 *
86 * @param string $query A valid value looks like '#14'
87 * @return int
88 */
89 public function getId($query)
90 {
91 return str_replace(self::COMMAND_KEY_INDICATOR, '', $query);
92 }
93
94 /**
95 * Verify if a given query contains a page jump command.
96 *
97 * @param string $query A valid value looks like '#14'
98 * @return bool
99 */
100 public function isValidPageJump($query)
101 {
102 $isValid = false;
103 if (preg_match('~^#(\\d)+$~', $query)) {
104 $isValid = true;
105 }
106 return $isValid;
107 }
108
109 /**
110 * Verify if a given query contains a registered command key.
111 *
112 * @param string $query
113 * @return bool
114 */
115 public function isValidCommand($query)
116 {
117 $isValid = false;
118 if (strpos($query, self::COMMAND_KEY_INDICATOR) === 0 && strpos($query, self::COMMAND_SPLIT_INDICATOR) > 1 && $this->getTableNameFromCommand($query)) {
119 $isValid = true;
120 }
121 return $isValid;
122 }
123
124 /**
125 * Gets the command for the given table.
126 *
127 * @param string $tableName The table to find a command for.
128 * @return string
129 */
130 public function getCommandForTable($tableName)
131 {
132 $commandArray = array_keys($GLOBALS['TYPO3_CONF_VARS']['SYS']['livesearch'], $tableName);
133 if (is_array($commandArray)) {
134 $command = $commandArray[0];
135 } else {
136 $command = false;
137 }
138 return $command;
139 }
140
141 /**
142 * Gets the page jump command for a given query.
143 *
144 * @param string $query
145 * @return string
146 */
147 public function getCommandForPageJump($query)
148 {
149 if ($this->isValidPageJump($query)) {
150 $command = $this->getCommandForTable('pages');
151 $id = $this->getId($query);
152 $resultQuery = self::COMMAND_KEY_INDICATOR . $command . self::COMMAND_SPLIT_INDICATOR . $id;
153 } else {
154 $resultQuery = false;
155 }
156 return $resultQuery;
157 }
158 }