1<?php
2/**
3 * Matomo - 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 */
9namespace Piwik;
10use Piwik\Updater\Migration;
11
12/**
13 * Base class for update scripts.
14 *
15 * Update scripts perform version updates for Piwik core or individual plugins. They can run
16 * SQL queries and/or PHP code to update an environment to a newer version.
17 *
18 * To create a new update script, create a class that extends `Updates`. Name the class and file
19 * after the version, eg, `class Updates_3_0_0` and `3.0.0.php`. Override the {@link getMigrationQueries()}
20 * method if you need to run SQL queries. Override the {@link doUpdate()} method to do other types
21 * of updating, eg, to activate/deactivate plugins or create files.
22 *
23 * @example core/Updates/0.4.2.php
24 */
25abstract class Updates
26{
27
28    /**
29     * Return migrations to be executed in this update.
30     *
31     * Migrations should be defined here, instead of in `doUpdate()`, since this method is used to display a preview
32     * of which migrations and database queries an update will run. If you execute migrations directly in `doUpdate()`,
33     * they won't be displayed to the user.
34     *
35     * @param Updater $updater
36     * @return Migration[]
37     * @api
38     */
39    public function getMigrations(Updater $updater)
40    {
41        return array();
42    }
43
44    /**
45     * Perform the incremental version update.
46     *
47     * This method should perform all updating logic. If you define migrations in an overridden `getMigrations()`
48     * method, you must call {@link Updater::executeMigrations()} here.
49     *
50     * See {@link \Piwik\Plugins\ExamplePlugin\Updates\Updates_0_0_2} for an example.
51     *
52     * @param Updater $updater
53     * @api
54     */
55    public function doUpdate(Updater $updater)
56    {
57    }
58
59    /**
60     * Tell the updater that this is a major update.
61     * Leads to a more visible notice.
62     *
63     * NOTE to release manager: Remember to mention in the Changelog
64     * that this update contains major DB upgrades and will take some time!
65     *
66     * @return bool
67     */
68    public static function isMajorUpdate()
69    {
70        return false;
71    }
72
73    /**
74     * Enables maintenance mode. Should be used for updates where Piwik will be unavailable
75     * for a large amount of time.
76     */
77    public static function enableMaintenanceMode()
78    {
79        $config = Config::getInstance();
80
81        $tracker = $config->Tracker;
82        $tracker['record_statistics'] = 0;
83        $config->Tracker = $tracker;
84
85        $general = $config->General;
86        $general['maintenance_mode'] = 1;
87        $config->General = $general;
88
89        $config->forceSave();
90    }
91
92    /**
93     * Helper method to disable maintenance mode after large updates.
94     */
95    public static function disableMaintenanceMode()
96    {
97        $config = Config::getInstance();
98
99        $tracker = $config->Tracker;
100        $tracker['record_statistics'] = 1;
101        $config->Tracker = $tracker;
102
103        $general = $config->General;
104        $general['maintenance_mode'] = 0;
105        $config->General = $general;
106
107        $config->forceSave();
108    }
109
110    public static function deletePluginFromConfigFile($pluginToDelete)
111    {
112        $config = Config::getInstance();
113        if (isset($config->Plugins['Plugins'])) {
114            $plugins = $config->Plugins['Plugins'];
115            if (($key = array_search($pluginToDelete, $plugins)) !== false) {
116                unset($plugins[$key]);
117            }
118            $config->Plugins['Plugins'] = $plugins;
119
120            $pluginsInstalled = $config->PluginsInstalled['PluginsInstalled'];
121            if (($key = array_search($pluginToDelete, $pluginsInstalled)) !== false) {
122                unset($pluginsInstalled[$key]);
123            }
124            $config->PluginsInstalled = array('PluginsInstalled' => $pluginsInstalled);
125
126            $config->forceSave();
127        }
128    }
129}
130