PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-content/plugins/redirection/database/schema/latest.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 257 lines | 184 code | 35 blank | 38 comment | 8 complexity | 475523f4f4abfd2ef90b5072cf1dda1a MD5 | raw file
  1. <?php
  2. /**
  3. * Latest database schema
  4. */
  5. class Red_Latest_Database extends Red_Database_Upgrader {
  6. public function get_stages() {
  7. return [
  8. /* translators: displayed when installing the plugin */
  9. 'create_tables' => __( 'Install Redirection tables', 'redirection' ),
  10. /* translators: displayed when installing the plugin */
  11. 'create_groups' => __( 'Create basic data', 'redirection' ),
  12. ];
  13. }
  14. /**
  15. * Install the latest database
  16. *
  17. * @return bool|WP_Error true if installed, WP_Error otherwise
  18. */
  19. public function install() {
  20. global $wpdb;
  21. foreach ( $this->get_stages() as $stage => $info ) {
  22. $result = $this->$stage( $wpdb );
  23. if ( is_wp_error( $result ) ) {
  24. if ( $wpdb->last_error ) {
  25. $result->add_data( $wpdb->last_error );
  26. }
  27. return $result;
  28. }
  29. }
  30. red_set_options( array( 'database' => REDIRECTION_DB_VERSION ) );
  31. return true;
  32. }
  33. /**
  34. * Remove the database and any options (including unused ones)
  35. */
  36. public function remove() {
  37. global $wpdb;
  38. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_items" );
  39. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_logs" );
  40. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_groups" );
  41. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_modules" );
  42. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}redirection_404" );
  43. delete_option( 'redirection_lookup' );
  44. delete_option( 'redirection_post' );
  45. delete_option( 'redirection_root' );
  46. delete_option( 'redirection_index' );
  47. delete_option( 'redirection_options' );
  48. delete_option( Red_Database_Status::OLD_DB_VERSION );
  49. delete_option( Red_Database_Status::DB_UPGRADE_STAGE );
  50. }
  51. /**
  52. * Return any tables that are missing from the database
  53. *
  54. * @return array Array of missing table names
  55. */
  56. public function get_missing_tables() {
  57. global $wpdb;
  58. $tables = array_keys( $this->get_all_tables() );
  59. $missing = [];
  60. foreach ( $tables as $table ) {
  61. $result = $wpdb->query( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) );
  62. if ( intval( $result, 10 ) !== 1 ) {
  63. $missing[] = $table;
  64. }
  65. }
  66. return $missing;
  67. }
  68. /**
  69. * Get table schema for latest database tables
  70. *
  71. * @return array Database schema array
  72. */
  73. public function get_table_schema() {
  74. global $wpdb;
  75. $tables = array_keys( $this->get_all_tables() );
  76. $show = array();
  77. foreach ( $tables as $table ) {
  78. // These are known queries without user input
  79. // phpcs:ignore
  80. $row = $wpdb->get_row( 'SHOW CREATE TABLE ' . $table, ARRAY_N );
  81. if ( $row ) {
  82. $show = array_merge( $show, explode( "\n", $row[1] ) );
  83. $show[] = '';
  84. } else {
  85. /* translators: 1: table name */
  86. $show[] = sprintf( __( 'Table "%s" is missing', 'redirection' ), $table );
  87. }
  88. }
  89. return $show;
  90. }
  91. /**
  92. * Return array of table names and table schema
  93. *
  94. * @return array
  95. */
  96. public function get_all_tables() {
  97. global $wpdb;
  98. $charset_collate = $this->get_charset();
  99. return array(
  100. "{$wpdb->prefix}redirection_items" => $this->create_items_sql( $wpdb->prefix, $charset_collate ),
  101. "{$wpdb->prefix}redirection_groups" => $this->create_groups_sql( $wpdb->prefix, $charset_collate ),
  102. "{$wpdb->prefix}redirection_logs" => $this->create_log_sql( $wpdb->prefix, $charset_collate ),
  103. "{$wpdb->prefix}redirection_404" => $this->create_404_sql( $wpdb->prefix, $charset_collate ),
  104. );
  105. }
  106. /**
  107. * Creates default group information
  108. */
  109. public function create_groups( $wpdb, $is_live = true ) {
  110. if ( ! $is_live ) {
  111. return true;
  112. }
  113. $defaults = [
  114. [
  115. 'name' => __( 'Redirections', 'redirection' ),
  116. 'module_id' => 1,
  117. 'position' => 0,
  118. ],
  119. [
  120. 'name' => __( 'Modified Posts', 'redirection' ),
  121. 'module_id' => 1,
  122. 'position' => 1,
  123. ],
  124. ];
  125. $existing_groups = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->prefix}redirection_groups" );
  126. // Default groups
  127. if ( intval( $existing_groups, 10 ) === 0 ) {
  128. $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[0] );
  129. $wpdb->insert( $wpdb->prefix . 'redirection_groups', $defaults[1] );
  130. }
  131. $group = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}redirection_groups LIMIT 1" );
  132. if ( $group ) {
  133. red_set_options( array( 'last_group_id' => $group->id ) );
  134. }
  135. return true;
  136. }
  137. /**
  138. * Creates all the tables
  139. */
  140. public function create_tables( $wpdb ) {
  141. global $wpdb;
  142. foreach ( $this->get_all_tables() as $table => $sql ) {
  143. $sql = preg_replace( '/[ \t]{2,}/', '', $sql );
  144. $this->do_query( $wpdb, $sql );
  145. }
  146. return true;
  147. }
  148. private function create_items_sql( $prefix, $charset_collate ) {
  149. return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_items` (
  150. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  151. `url` mediumtext NOT NULL,
  152. `match_url` varchar(2000) DEFAULT NULL,
  153. `match_data` text,
  154. `regex` int(11) unsigned NOT NULL DEFAULT '0',
  155. `position` int(11) unsigned NOT NULL DEFAULT '0',
  156. `last_count` int(10) unsigned NOT NULL DEFAULT '0',
  157. `last_access` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
  158. `group_id` int(11) NOT NULL DEFAULT '0',
  159. `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
  160. `action_type` varchar(20) NOT NULL,
  161. `action_code` int(11) unsigned NOT NULL,
  162. `action_data` mediumtext,
  163. `match_type` varchar(20) NOT NULL,
  164. `title` text,
  165. PRIMARY KEY (`id`),
  166. KEY `url` (`url`(191)),
  167. KEY `status` (`status`),
  168. KEY `regex` (`regex`),
  169. KEY `group_idpos` (`group_id`,`position`),
  170. KEY `group` (`group_id`),
  171. KEY `match_url` (`match_url`(191))
  172. ) $charset_collate";
  173. }
  174. private function create_groups_sql( $prefix, $charset_collate ) {
  175. return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_groups` (
  176. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  177. `name` varchar(50) NOT NULL,
  178. `tracking` int(11) NOT NULL DEFAULT '1',
  179. `module_id` int(11) unsigned NOT NULL DEFAULT '0',
  180. `status` enum('enabled','disabled') NOT NULL DEFAULT 'enabled',
  181. `position` int(11) unsigned NOT NULL DEFAULT '0',
  182. PRIMARY KEY (`id`),
  183. KEY `module_id` (`module_id`),
  184. KEY `status` (`status`)
  185. ) $charset_collate";
  186. }
  187. private function create_log_sql( $prefix, $charset_collate ) {
  188. return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_logs` (
  189. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  190. `created` datetime NOT NULL,
  191. `url` mediumtext NOT NULL,
  192. `sent_to` mediumtext,
  193. `agent` mediumtext NOT NULL,
  194. `referrer` mediumtext,
  195. `redirection_id` int(11) unsigned DEFAULT NULL,
  196. `ip` varchar(45) DEFAULT NULL,
  197. `module_id` int(11) unsigned NOT NULL,
  198. `group_id` int(11) unsigned DEFAULT NULL,
  199. PRIMARY KEY (`id`),
  200. KEY `created` (`created`),
  201. KEY `redirection_id` (`redirection_id`),
  202. KEY `ip` (`ip`),
  203. KEY `group_id` (`group_id`),
  204. KEY `module_id` (`module_id`)
  205. ) $charset_collate";
  206. }
  207. private function create_404_sql( $prefix, $charset_collate ) {
  208. return "CREATE TABLE IF NOT EXISTS `{$prefix}redirection_404` (
  209. `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  210. `created` datetime NOT NULL,
  211. `url` varchar(255) NOT NULL DEFAULT '',
  212. `agent` varchar(255) DEFAULT NULL,
  213. `referrer` varchar(255) DEFAULT NULL,
  214. `ip` varchar(45) DEFAULT NULL,
  215. PRIMARY KEY (`id`),
  216. KEY `created` (`created`),
  217. KEY `url` (`url`(191)),
  218. KEY `referrer` (`referrer`(191)),
  219. KEY `ip` (`ip`)
  220. ) $charset_collate";
  221. }
  222. }