web/wp-content/plugins/twitter-tools/upgrade/3.0.php
changeset 194 32102edaa81b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
       
     1 <?php
       
     2 
       
     3 // Upgrade data from Twitter Tools 2.x to 3.0
       
     4 
       
     5 global $wpdb;
       
     6 $wpdb->aktt = $wpdb->prefix.'ak_twitter';
       
     7 
       
     8 function aktt_upgrade_30() {
       
     9 	global $wpdb;
       
    10 	$body = $head = $foot = '';
       
    11 	$errors = array();
       
    12 // check for username in wp_options data
       
    13 	$username = get_option('aktt_twitter_username');
       
    14 $username = 'foo';
       
    15 	if (empty($username)) {
       
    16 		$errors[] = '<div class="error"><p>'.__('Sorry, unable to find the legacy Twitter Tools username. Check that the <code>aktt_twitter_username</code> is set in your Options table.', 'twitter-tools').'</p></div>';
       
    17 	}
       
    18 // add upgraded col if needed
       
    19 	$cols = $wpdb->get_results("
       
    20 		DESCRIBE $wpdb->aktt;
       
    21 	");
       
    22 	$upgrade_col = false;
       
    23 	foreach ($cols as $col) {
       
    24 		if ($col->Field == 'upgrade_30') {
       
    25 			$upgrade_col = true;
       
    26 			break;
       
    27 		}
       
    28 	}
       
    29 	if (!$upgrade_col) {
       
    30 		$result = $wpdb->query("
       
    31 			ALTER TABLE $wpdb->aktt
       
    32 			ADD `upgrade_30` tinyint(1) default 0
       
    33 			AFTER modified
       
    34 		");
       
    35 		if (!$result) {
       
    36 			$errors[] = '<div class="error"><p>'.__('Sorry, unable to alter the database table as needed for the upgrade. Please check your database user permissions.', 'twitter-tools').'</p></div>';
       
    37 		}
       
    38 	}
       
    39 // make sure we have some tweets to upgrade
       
    40 	$count = $wpdb->get_var("
       
    41 		SELECT count(id)
       
    42 		FROM $wpdb->aktt
       
    43 		WHERE upgrade_30 = 0
       
    44 	");
       
    45 	if (!$count) {
       
    46 		$body = '<div class="error"><p>'.__('Sorry, it doesn\'t look like any tweets need upgrading.', 'twitter-tools').'</p></div>';
       
    47 	}
       
    48 // prep output
       
    49 	if (count($errors)) {
       
    50 		$body = implode("\n", $errors);
       
    51 	}
       
    52 	else {
       
    53 		ob_start();
       
    54 ?>
       
    55 <style type="text/css">
       
    56 h3, p, p.step {
       
    57 	text-align: center;
       
    58 }
       
    59 .step {
       
    60 	margin-bottom: 35px;
       
    61 }
       
    62 .warning {
       
    63 	background: #ffc;
       
    64 	padding: 10px;
       
    65 }
       
    66 .dim {
       
    67 	opacity: .3;
       
    68 }
       
    69 .button {
       
    70 	margin: 0 10px;
       
    71 }
       
    72 .padded {
       
    73 	padding: 10px 0 30px;
       
    74 }
       
    75 .progress {
       
    76 	background: #eee;
       
    77 	border: 1px solid #ccc;
       
    78 	box-shadow: inset 0 0 2px #aaa;
       
    79 	height: 30px;
       
    80 	margin: auto;
       
    81 	width: 400px;
       
    82 }
       
    83 .progress .bar {
       
    84 	background: #aaa;
       
    85 	height: 30px;
       
    86 	width: 2px;
       
    87 }
       
    88 #process_complete {
       
    89 	display: none;
       
    90 }
       
    91 </style>
       
    92 <p class="warning"><?php _e('<b>WARNING!</b> Before you upgrade, please back up your data. Y\'know, just in case.', 'twitter-tools'); ?></p>
       
    93 <div id="process">
       
    94 	<div class="padded dim">
       
    95 		<p><?php printf(__('Found %s tweets to upgrade', 'twitter-tools'), $count); ?></p>
       
    96 		<div class="progress">
       
    97 			<div class="bar" data-total="<?php echo esc_attr($count); ?>"></div>
       
    98 		</div>
       
    99 	</div>
       
   100 	<p class="step">
       
   101 		<a href="#" class="button" id="aktt_run_upgrade"><?php _e('Run Upgrade', 'twitter-tools'); ?></a>
       
   102 		or <a href="javascript:history.go(-1);"><?php _e('Cancel', 'twitter-tools'); ?></a>
       
   103 	</p>
       
   104 </div>
       
   105 <div id="process_complete">
       
   106 	<div class="padded">
       
   107 		<h3><?php _e('Yay!', 'twitter-tools'); ?></h3>
       
   108 		<p><?php printf(__('Your tweets have been upgraded successfully.', 'twitter-tools'), esc_url(admin_url(''))); ?></p>
       
   109 		<p><?php printf(__('Head back to your <a href="%s">Twitter Tools settings</a>.', 'twitter-tools'), esc_url(admin_url('options-general.php?page=twitter-tools'))); ?></p>
       
   110 	</div>
       
   111 </div>
       
   112 <script type="text/javascript">
       
   113 jQuery(function($) {
       
   114 	$('#aktt_run_upgrade').click(function(e) {
       
   115 		e.preventDefault();
       
   116 		$button = $(this);
       
   117 		$('.padded.dim').removeClass('dim');
       
   118 		$('.warning').addClass('dim');
       
   119 		$button.attr('disabled', true).addClass('dim');
       
   120 		$.get(
       
   121 			'<?php echo wp_nonce_url('index.php'); ?>',
       
   122 			{
       
   123 				'aktt_action': 'upgrade-3.0-run',
       
   124 				'nonce': '<?php echo wp_create_nonce('upgrade-3.0-run'); ?>'
       
   125 			},
       
   126 			function(response) {
       
   127 				if (response.result == 'error') {
       
   128 					alert(response.message);
       
   129 					return;
       
   130 				}
       
   131 				if (response.result == 'success') {
       
   132 // update status bar
       
   133 					var $bar = $('.progress .bar');
       
   134 					var total = parseInt($bar.data('total'));
       
   135 					var remaining = parseInt(response.to_upgrade);
       
   136 					$bar.animate({ width: Math.ceil(((total - remaining) / total) * 400) + 'px' });
       
   137 					if (remaining > 0) {
       
   138 // request again
       
   139 						$button.click();
       
   140 					}
       
   141 					else {
       
   142 // complete?
       
   143 						$('p.warning, #process').fadeOut('fast', function() {
       
   144 							$('#process_complete').fadeIn('fast');
       
   145 						});
       
   146 					}
       
   147 					return;
       
   148 				}
       
   149 				alert("<?php _e('Sorry, something didn\'t go as planned. Please try again.', 'twitter-tools'); ?>");
       
   150 			},
       
   151 			'json'
       
   152 		);
       
   153 		$(this).html('<?php _e('Upgrade Running&hellip;', 'twitter-tools'); ?>').attr('disabled', true);
       
   154 	});
       
   155 });
       
   156 </script>
       
   157 <?php
       
   158 		$body = ob_get_clean();
       
   159 	}
       
   160 	echo aktt_upgrade_30_shell(__('Twitter Tools Upgrade', 'twitter-tools'), $body, $head, $foot);
       
   161 }
       
   162 
       
   163 function aktt_upgrade_30_run($count = 25) {
       
   164 	global $wpdb;
       
   165 // pull next tweet(s)
       
   166 	$count = intval($count);
       
   167 	$tweets = $wpdb->get_results("
       
   168 		SELECT *
       
   169 		FROM $wpdb->aktt
       
   170 		WHERE upgrade_30 = 0
       
   171 		LIMIT $count
       
   172 	");
       
   173 // upgrade
       
   174 	if (count($tweets)) {
       
   175 		$upgraded = array();
       
   176 		$username = get_option('aktt_twitter_username'); // already passed sanity check to make sure this exists
       
   177 		foreach ($tweets as $tweet) {
       
   178 			if (trim($tweet->tw_text) == '') {
       
   179 				continue;
       
   180 			}
       
   181 			$t = new AKTT_Tweet($tweet->tw_id);
       
   182 			
       
   183 			$t->data = new stdClass;
       
   184 			$t->data->id = $t->data->id_str = $tweet->tw_id;
       
   185 			$t->data->text = $tweet->tw_text;
       
   186 			$t->data->created_at = date('D M d H:i:s +0000 Y', strtotime($tweet->tw_created_at.' +0000'));
       
   187 			$t->data->in_reply_to_screen_name = $tweet->tw_reply_username;
       
   188 			$t->data->in_reply_to_status_id = $t->data->in_reply_to_status_id_str = $tweet->tw_reply_tweet;
       
   189 			
       
   190 			$t->data->user = new stdClass;
       
   191 			$t->data->user->screen_name = $username;
       
   192 			
       
   193 			$t->raw_data = json_encode($t->data);
       
   194 			
       
   195 			// skip if duplicate
       
   196 			if ($t->exists_by_guid()) {
       
   197 // already there, so mark as upgraded
       
   198 				$upgraded[] = intval($tweet->id);
       
   199 				continue;
       
   200 			}
       
   201 			
       
   202 			if ($t->add()) {
       
   203 // add meta - upgraded tweet
       
   204 				update_post_meta($t->post_id, '_aktt_upgraded_30', 1);
       
   205 				update_post_meta($t->post_id, '_aktt_30_backfill_needed', 1);
       
   206 				$upgraded[] = intval($tweet->id);
       
   207 			}
       
   208 		}
       
   209 		if (count($upgraded)) {
       
   210 			$wpdb->query("
       
   211 				UPDATE $wpdb->aktt
       
   212 				SET upgrade_30 = 1
       
   213 				WHERE id IN (".implode(',', $upgraded).")
       
   214 			");
       
   215 		}
       
   216 	}
       
   217 // return stats
       
   218 	$to_upgrade = $wpdb->get_var("
       
   219 		SELECT count(id)
       
   220 		FROM $wpdb->aktt
       
   221 		WHERE upgrade_30 = 0
       
   222 	");
       
   223 	return $to_upgrade;
       
   224 }
       
   225 
       
   226 function aktt_upgrade_30_shell($title = '', $body = '', $head = '', $foot = '') {
       
   227 	ob_start();
       
   228 ?>
       
   229 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
   230 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>
       
   231 <head>
       
   232 	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
       
   233 	<title><?php echo $title; ?></title>
       
   234 	<script type="text/javascript" src="<?php echo esc_url(includes_url('/js/jquery/jquery.js')); ?>"></script>
       
   235 <?php
       
   236 wp_admin_css('install', true);
       
   237 do_action('admin_enqueue_styles');
       
   238 do_action('admin_print_styles');
       
   239 
       
   240 echo $head;
       
   241 ?>
       
   242 </head>
       
   243 <body>
       
   244 <h1 id="logo"><?php echo $title; ?></h1>
       
   245 <?php
       
   246 
       
   247 echo $body.$foot;
       
   248 
       
   249 ?>
       
   250 </body>
       
   251 </html>
       
   252 <?php
       
   253 	return ob_get_clean();
       
   254 }