PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 1.3.1
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v1.3.1
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / DbHelper.php
matomo / app / core Last commit date
API 6 years ago Access 6 years ago Application 6 years ago Archive 6 years ago ArchiveProcessor 6 years ago Archiver 6 years ago AssetManager 6 years ago Auth 6 years ago Category 6 years ago CliMulti 6 years ago Columns 6 years ago Composer 6 years ago Concurrency 6 years ago Config 6 years ago Container 6 years ago CronArchive 6 years ago DataAccess 5 years ago DataFiles 6 years ago DataTable 6 years ago Db 6 years ago DeviceDetector 5 years ago Email 6 years ago Exception 6 years ago Http 6 years ago Intl 6 years ago Mail 6 years ago Measurable 6 years ago Menu 6 years ago Metrics 6 years ago Notification 6 years ago Period 6 years ago Plugin 6 years ago ProfessionalServices 6 years ago Report 6 years ago ReportRenderer 6 years ago Scheduler 6 years ago Segment 6 years ago Session 6 years ago Settings 6 years ago Tracker 5 years ago Translation 6 years ago UpdateCheck 6 years ago Updater 6 years ago Updates 6 years ago Validators 6 years ago View 6 years ago ViewDataTable 6 years ago Visualization 6 years ago Widget 6 years ago .htaccess 6 years ago Access.php 6 years ago Archive.php 6 years ago ArchiveProcessor.php 6 years ago AssetManager.php 6 years ago Auth.php 6 years ago BaseFactory.php 6 years ago Cache.php 6 years ago CacheId.php 6 years ago CliMulti.php 6 years ago Common.php 6 years ago Config.php 6 years ago Console.php 6 years ago Context.php 6 years ago Cookie.php 5 years ago CronArchive.php 5 years ago DataArray.php 6 years ago DataTable.php 6 years ago Date.php 6 years ago Db.php 6 years ago DbHelper.php 6 years ago Development.php 6 years ago DeviceDetectorFactory.php 6 years ago ErrorHandler.php 6 years ago EventDispatcher.php 6 years ago ExceptionHandler.php 6 years ago FileIntegrity.php 6 years ago Filechecks.php 6 years ago Filesystem.php 6 years ago FrontController.php 6 years ago Http.php 6 years ago IP.php 6 years ago Log.php 6 years ago LogDeleter.php 6 years ago Mail.php 6 years ago Metrics.php 6 years ago MetricsFormatter.php 6 years ago Nonce.php 5 years ago Notification.php 6 years ago NumberFormatter.php 6 years ago Option.php 5 years ago Period.php 6 years ago Piwik.php 6 years ago Plugin.php 6 years ago Profiler.php 6 years ago ProxyHeaders.php 6 years ago ProxyHttp.php 6 years ago QuickForm2.php 6 years ago RankingQuery.php 6 years ago Registry.php 6 years ago ReportRenderer.php 6 years ago ScheduledTask.php 6 years ago Segment.php 6 years ago Sequence.php 6 years ago Session.php 6 years ago SettingsPiwik.php 6 years ago SettingsServer.php 6 years ago Singleton.php 6 years ago Site.php 6 years ago TCPDF.php 6 years ago TaskScheduler.php 6 years ago Theme.php 6 years ago Timer.php 6 years ago Tracker.php 6 years ago Translate.php 6 years ago Twig.php 6 years ago Unzip.php 6 years ago UpdateCheck.php 6 years ago Updater.php 6 years ago Updates.php 6 years ago Url.php 6 years ago UrlHelper.php 6 years ago Version.php 5 years ago View.php 6 years ago bootstrap.php 6 years ago dispatch.php 6 years ago testMinimumPhpVersion.php 6 years ago
DbHelper.php
281 lines
1 <?php
2 /**
3 * Piwik - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik;
10
11 use Exception;
12 use Piwik\Db\Schema;
13 use Piwik\DataAccess\ArchiveTableCreator;
14
15 /**
16 * Contains database related helper functions.
17 */
18 class DbHelper
19 {
20 /**
21 * Get list of tables installed
22 *
23 * @param bool $forceReload Invalidate cache
24 * @return array Tables installed
25 */
26 public static function getTablesInstalled($forceReload = true)
27 {
28 return Schema::getInstance()->getTablesInstalled($forceReload);
29 }
30
31 /**
32 * Get list of installed columns in a table
33 *
34 * @param string $tableName The name of a table.
35 *
36 * @return array Installed columns indexed by the column name.
37 */
38 public static function getTableColumns($tableName)
39 {
40 return Schema::getInstance()->getTableColumns($tableName);
41 }
42
43 /**
44 * Creates a new table in the database.
45 *
46 * Example:
47 * ```
48 * $tableDefinition = "`age` INT(11) NOT NULL AUTO_INCREMENT,
49 * `name` VARCHAR(255) NOT NULL";
50 *
51 * DbHelper::createTable('tablename', $tableDefinition);
52 * ``
53 *
54 * @param string $nameWithoutPrefix The name of the table without any piwik prefix.
55 * @param string $createDefinition The table create definition
56 *
57 * @api
58 */
59 public static function createTable($nameWithoutPrefix, $createDefinition)
60 {
61 Schema::getInstance()->createTable($nameWithoutPrefix, $createDefinition);
62 }
63
64 public static function getUsedCharset()
65 {
66 if (Config::getInstance()->database['charset']) {
67 return strtolower(Config::getInstance()->database['charset']);
68 }
69
70 return 'utf8';
71 }
72
73 /**
74 * Returns true if Piwik is installed
75 *
76 * @since 0.6.3
77 *
78 * @return bool True if installed; false otherwise
79 */
80 public static function isInstalled()
81 {
82 try {
83 return Schema::getInstance()->hasTables();
84 } catch (Exception $e) {
85 return false;
86 }
87 }
88
89 /**
90 * Truncate all tables
91 */
92 public static function truncateAllTables()
93 {
94 Schema::getInstance()->truncateAllTables();
95 }
96
97 /**
98 * Creates an entry in the User table for the "anonymous" user.
99 */
100 public static function createAnonymousUser()
101 {
102 Schema::getInstance()->createAnonymousUser();
103 }
104
105 /**
106 * Records the Matomo version a user used when installing this Matomo for the first time
107 */
108 public static function recordInstallVersion()
109 {
110 Schema::getInstance()->recordInstallVersion();
111 }
112
113 /**
114 * Returns which Matomo version was used to install this Matomo for the first time.
115 */
116 public static function getInstallVersion()
117 {
118 return Schema::getInstance()->getInstallVersion();
119 }
120
121 public static function wasMatomoInstalledBeforeVersion($version)
122 {
123 $installVersion = self::getInstallVersion();
124 if (empty($installVersion)) {
125 return true; // we assume yes it was installed
126 }
127 return true === version_compare($version, $installVersion, '>');
128 }
129
130 /**
131 * Create all tables
132 */
133 public static function createTables()
134 {
135 Schema::getInstance()->createTables();
136 }
137
138 /**
139 * Drop database, used in tests
140 */
141 public static function dropDatabase($dbName = null)
142 {
143 if (defined('PIWIK_TEST_MODE') && PIWIK_TEST_MODE) {
144 Schema::getInstance()->dropDatabase($dbName);
145 }
146 }
147
148 /**
149 * Check database connection character set is utf8.
150 *
151 * @return bool True if it is (or doesn't matter); false otherwise
152 */
153 public static function isDatabaseConnectionUTF8()
154 {
155 return Db::get()->isConnectionUTF8();
156 }
157
158 /**
159 * Checks the database server version against the required minimum
160 * version.
161 *
162 * @see config/global.ini.php
163 * @since 0.4.4
164 * @throws Exception if server version is less than the required version
165 */
166 public static function checkDatabaseVersion()
167 {
168 Db::get()->checkServerVersion();
169 }
170
171 /**
172 * Disconnect from database
173 */
174 public static function disconnectDatabase()
175 {
176 Db::get()->closeConnection();
177 }
178
179 /**
180 * Create database
181 *
182 * @param string|null $dbName
183 */
184 public static function createDatabase($dbName = null)
185 {
186 Schema::getInstance()->createDatabase($dbName);
187 }
188
189 /**
190 * Returns if the given table has an index with the given name
191 *
192 * @param string $table
193 * @param string $indexName
194 *
195 * @return bool
196 * @throws Exception
197 */
198 public static function tableHasIndex($table, $indexName)
199 {
200 $result = Db::get()->fetchOne('SHOW INDEX FROM '.$table.' WHERE Key_name = ?', [$indexName]);
201 return !empty($result);
202 }
203
204 /**
205 * Get the SQL to create Piwik tables
206 *
207 * @return array array of strings containing SQL
208 */
209 public static function getTablesCreateSql()
210 {
211 return Schema::getInstance()->getTablesCreateSql();
212 }
213
214 /**
215 * Get the SQL to create a specific Piwik table
216 *
217 * @param string $tableName Unprefixed table name.
218 * @return string SQL
219 */
220 public static function getTableCreateSql($tableName)
221 {
222 return Schema::getInstance()->getTableCreateSql($tableName);
223 }
224
225 /**
226 * Deletes archive tables. For use in tests.
227 */
228 public static function deleteArchiveTables()
229 {
230 foreach (ArchiveTableCreator::getTablesArchivesInstalled() as $table) {
231 Log::debug("Dropping table $table");
232
233 Db::query("DROP TABLE IF EXISTS `$table`");
234 }
235
236 ArchiveTableCreator::refreshTableList($forceReload = true);
237 }
238
239 /**
240 * Adds a MAX_EXECUTION_TIME hint into a SELECT query if $limit is bigger than 1
241 *
242 * @param string $sql query to add hint to
243 * @param int $limit time limit in seconds
244 * @return string
245 */
246 public static function addMaxExecutionTimeHintToQuery($sql, $limit)
247 {
248 if ($limit <= 0) {
249 return $sql;
250 }
251
252 $sql = trim($sql);
253 $pos = stripos($sql, 'SELECT');
254 if ($pos !== false) {
255
256 $timeInMs = $limit * 1000;
257 $timeInMs = (int) $timeInMs;
258 $maxExecutionTimeHint = ' /*+ MAX_EXECUTION_TIME('.$timeInMs.') */ ';
259
260 $sql = substr_replace($sql, 'SELECT ' . $maxExecutionTimeHint, $pos, strlen('SELECT'));
261 }
262
263 return $sql;
264 }
265
266 /**
267 * Returns true if the string is a valid database name for MySQL. MySQL allows + in the database names.
268 * Database names that start with a-Z or 0-9 and contain a-Z, 0-9, underscore(_), dash(-), plus(+), and dot(.) will be accepted.
269 * File names beginning with anything but a-Z or 0-9 will be rejected (including .htaccess for example).
270 * File names containing anything other than above mentioned will also be rejected (file names with spaces won't be accepted).
271 *
272 * @param string $dbname
273 * @return bool
274 */
275 public static function isValidDbname($dbname)
276 {
277 return (0 !== preg_match('/(^[a-zA-Z0-9]+([a-zA-Z0-9\_\.\-\+]*))$/D', $dbname));
278 }
279
280 }
281