piwik.install 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. /**
  3. * @file
  4. * Installation file for Piwik - Web analytics module.
  5. */
  6. function piwik_install() {
  7. // Remove tracking from all administrative pages, see http://drupal.org/node/34970.
  8. variable_set('piwik_visibility_roles', 0);
  9. variable_set('piwik_visibility_pages', 0);
  10. $pages = array(
  11. 'admin',
  12. 'admin/*',
  13. 'batch',
  14. 'node/add*',
  15. 'node/*/*',
  16. 'user/*/*',
  17. );
  18. variable_set('piwik_pages', implode("\n", $pages));
  19. }
  20. function piwik_uninstall() {
  21. variable_del('piwik_cache');
  22. variable_del('piwik_codesnippet_before');
  23. variable_del('piwik_codesnippet_after');
  24. variable_del('piwik_custom');
  25. variable_del('piwik_custom_var');
  26. variable_del('piwik_domain_mode');
  27. variable_del('piwik_js_scope');
  28. variable_del('piwik_last_cache');
  29. variable_del('piwik_page_title_hierarchy');
  30. variable_del('piwik_page_title_hierarchy_exclude_home');
  31. variable_del('piwik_pages');
  32. variable_del('piwik_privacy_donottrack');
  33. variable_del('piwik_roles');
  34. variable_del('piwik_site_id');
  35. variable_del('piwik_site_search');
  36. variable_del('piwik_trackcolorbox');
  37. variable_del('piwik_trackmailto');
  38. variable_del('piwik_track'); // interrims solution
  39. variable_del('piwik_trackfiles_extensions');
  40. variable_del('piwik_trackmessages');
  41. variable_del('piwik_trackuserid');
  42. variable_del('piwik_translation_set');
  43. variable_del('piwik_url_http');
  44. variable_del('piwik_url_https');
  45. variable_del('piwik_visibility_pages');
  46. variable_del('piwik_visibility_roles');
  47. // Remove backup variables if exits. Remove this code in D8.
  48. variable_del('piwik_codesnippet_before_backup_7202');
  49. variable_del('piwik_codesnippet_after_backup_7202');
  50. }
  51. /**
  52. * Remove cache directory if module is disabled (or uninstalled).
  53. */
  54. function piwik_disable() {
  55. piwik_clear_js_cache();
  56. }
  57. /**
  58. * Implementation of hook_requirements().
  59. */
  60. function piwik_requirements($phase) {
  61. $requirements = array();
  62. $t = get_t();
  63. switch ($phase) {
  64. case 'runtime':
  65. // Module cannot validate piwik URL without external HTTP requests.
  66. if (variable_get('drupal_http_request_fails', TRUE) && !system_check_http_request()) {
  67. $requirements['piwik_http_requests'] = array(
  68. 'title' => $t('HTTP request status'),
  69. 'value' => $t('Fails'),
  70. 'severity' => REQUIREMENT_ERROR,
  71. 'description' => $t('Your system or network configuration does not allow Drupal to access web pages, resulting in reduced functionality. This could be due to your webserver configuration or PHP settings, and should be resolved in order to download information about available updates, fetch aggregator feeds, sign in via OpenID, or use other network-dependent services.'),
  72. );
  73. }
  74. // Raise warning if Piwik user account has not been set yet.
  75. if (!preg_match('/^\d{1,}$/', variable_get('piwik_site_id', ''))) {
  76. $requirements['piwik'] = array(
  77. 'title' => $t('Piwik module'),
  78. 'description' => $t('Piwik module has not been configured yet. Please configure its settings from the <a href="@url">Piwik settings page</a>.', array('@url' => url('admin/config/system/piwik'))),
  79. 'severity' => REQUIREMENT_WARNING,
  80. 'value' => $t('Not configured'),
  81. );
  82. }
  83. break;
  84. }
  85. return $requirements;
  86. }
  87. /**
  88. * Change visibility setting for paths.
  89. */
  90. function piwik_update_6000() {
  91. // Orginal pages setting.
  92. $pages = array(
  93. 'admin*',
  94. 'user*',
  95. 'node/add*',
  96. 'node/*/*',
  97. );
  98. $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('piwik_pages', implode("\n", $pages))));
  99. if (empty($diff)) {
  100. // No diff to original settings found. Update with new settings.
  101. $pages = array(
  102. 'admin',
  103. 'admin/*',
  104. 'user/*/*',
  105. 'node/add*',
  106. 'node/*/*',
  107. );
  108. variable_set('piwik_pages', implode("\n", $pages));
  109. return t('Path visibility filter setting changed from "admin*" to "admin, admin/*" and "user*" changed to "user/*/*".');
  110. }
  111. else {
  112. return t('Custom path visibility filter setting found. Update skipped!');
  113. }
  114. }
  115. /**
  116. * Upgrade custom javascript settings.
  117. */
  118. function piwik_update_6001() {
  119. variable_set('piwik_codesnippet_before', variable_get('piwik_codesnippet', ''));
  120. variable_del('piwik_codesnippet');
  121. return t('Upgraded custom javascript codesnippet setting.');
  122. }
  123. /**
  124. * Remove obsolte token auth.
  125. */
  126. function piwik_update_6002() {
  127. variable_del('piwik_auth');
  128. return t('The global token_auth setting has been deleted. Users need to configure their personal token_auth.');
  129. }
  130. /**
  131. * Update list of default extensions
  132. */
  133. function piwik_update_6003() {
  134. if (variable_get('piwik_trackfiles_extensions', '') == '7z|aac|avi|csv|doc|exe|flv|gif|gz|jpe?g|js|mp(3|4|e?g)|mov|pdf|phps|png|ppt|rar|sit|tar|torrent|txt|wma|wmv|xls|xml|zip') {
  135. variable_set('piwik_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip');
  136. }
  137. return t('The default extensions for download tracking have been updated to piwik defaults.');
  138. }
  139. /**
  140. * Add trailing slash to piwik URLs.
  141. */
  142. function piwik_update_7200() {
  143. $url_http = variable_get('piwik_url_http', '');
  144. $url_https = variable_get('piwik_url_https', '');
  145. if (!empty($url_http)) {
  146. variable_set('piwik_url_http', $url_http . '/');
  147. }
  148. if (!empty($url_https)) {
  149. variable_set('piwik_url_https', $url_https . '/');
  150. }
  151. return t('Added trailing slash to piwik URLs.');
  152. }
  153. /**
  154. * Move async tracking code to header.
  155. */
  156. function piwik_update_7201() {
  157. variable_set('piwik_js_scope', 'header');
  158. return t('Async tracking code has been moved to header.');
  159. }
  160. /**
  161. * Backup synchronous code snippets.
  162. */
  163. function piwik_update_7202() {
  164. $messages = array();
  165. // TODO: Backup synchronous code snippets. Remove variables in D8.
  166. variable_set('piwik_codesnippet_before_backup_7202', variable_get('piwik_codesnippet_before', ''));
  167. variable_set('piwik_codesnippet_after_backup_7202', variable_get('piwik_codesnippet_after', ''));
  168. // Upgrade of BEFORE code snippet.
  169. $code_before = variable_get('piwik_codesnippet_before', '');
  170. if (!empty($code_before)) {
  171. // No value, e.g. piwikTracker.enableLinkTracking()
  172. $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\(\);(.*)/i', '$1_paq.push(["$2"]);$3', $code_before);
  173. // One value, e.g. piwikTracker.addDownloadExtensions("mp5|mp6"), piwikTracker.trackGoal(1)
  174. $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\(("|\'?)(\w+)("|\'?)\);(.*)/i', '$1_paq.push(["$2", $3$4$5]);$6', $code_before);
  175. // Multiple values e.g. piwikTracker.trackLink('/store', 'addTransaction', trans)
  176. $code_before = preg_replace('/(.*)piwikTracker\.(\w+)\((.*)\);(.*)/i', '$1_paq.push(["$2", $3]);$4', $code_before);
  177. variable_set('piwik_codesnippet_before', $code_before);
  178. drupal_set_message(Database::getConnection()->prefixTables("<strong>Attempted</strong> to upgrade Piwik custom 'before' code snippet. Backup of previous code snippet has been saved in database table '{variable}' as 'piwik_codesnippet_before_backup_7202'. Please consult Piwik's <a href='http://piwik.org/docs/javascript-tracking/#toc-asynchronous-tracking'>Asynchronous tracking Guide</a> if the upgrade was successfully."), 'warning');
  179. $messages[] = t('Upgraded custom "before" code snippet.');
  180. }
  181. // Upgrade of AFTER code snippet.
  182. // We cannot update this code snippet automatically. Show message that the upgrade has been skipped.
  183. $code_after = variable_get('piwik_codesnippet_after', '');
  184. if (!empty($code_after)) {
  185. drupal_set_message(Database::getConnection()->prefixTables("Automatic upgrade of Google Analytics custom 'after' code snippet has been skipped. Backup of previous code snippet has been saved in database table '{variable}' as 'piwik_codesnippet_after_backup_7202'. You need to manually upgrade the custom 'after' code snippet."), 'error');
  186. $messages[] = t('Skipped custom "after" code snippet.');
  187. }
  188. return empty($messages) ? t('No custom code snipped found. Nothing to do.') : implode(' ', $messages);
  189. }
  190. /**
  191. * Path visibility filter setting should hide "batch" path.
  192. */
  193. function piwik_update_7203() {
  194. // Latest pages setting.
  195. $pages = array(
  196. 'admin',
  197. 'admin/*',
  198. 'user/*/*',
  199. 'node/add*',
  200. 'node/*/*',
  201. );
  202. $diff = array_diff($pages, preg_split('/(\r\n?|\n)/', variable_get('piwik_pages', implode("\n", $pages))));
  203. if (empty($diff)) {
  204. // No diff to latest settings found. Update with new settings.
  205. $pages = array(
  206. 'admin',
  207. 'admin/*',
  208. 'batch',
  209. 'node/add*',
  210. 'node/*/*',
  211. 'user/*/*',
  212. );
  213. variable_set('piwik_pages', implode("\n", $pages));
  214. return t('Added "batch" to path visibility filter setting.');
  215. }
  216. else {
  217. return t('Custom path visibility filter setting found. Update skipped!');
  218. }
  219. }
  220. /**
  221. * Rename piwik_visibility variable to piwik_visibility_pages for consistency.
  222. */
  223. function piwik_update_7204() {
  224. variable_set('piwik_visibility_pages', variable_get('piwik_visibility', 1));
  225. variable_del('piwik_visibility');
  226. return t('Renamed "piwik_visibility" settings variable to piwik_visibility_pages.');
  227. }
  228. /**
  229. * Update list of default file extensions.
  230. */
  231. function piwik_update_7205() {
  232. if (variable_get('piwik_trackfiles_extensions', '') == '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls|xml|z|zip') {
  233. variable_set('piwik_trackfiles_extensions', '7z|aac|arc|arj|asf|asx|avi|bin|csv|doc(x|m)?|dot(x|m)?|exe|flv|gif|gz|gzip|hqx|jar|jpe?g|js|mp(2|3|4|e?g)|mov(ie)?|msi|msp|pdf|phps|png|ppt(x|m)?|pot(x|m)?|pps(x|m)?|ppam|sld(x|m)?|thmx|qtm?|ra(m|r)?|sea|sit|tar|tgz|torrent|txt|wav|wma|wmv|wpd|xls(x|m|b)?|xlt(x|m)|xlam|xml|z|zip');
  234. return t('The default extensions for download tracking have been updated.');
  235. }
  236. else {
  237. return t('Custom extensions for download tracking setting found. Update skipped!');
  238. }
  239. }