Added some timeout protection and substep fetching and storing
Jeremy D

Jeremy D commited on 2011-09-27 22:13:06
Showing 1 changed files, with 54 additions and 1 deletions.

... ...
@@ -41,11 +41,20 @@ class mbt_to_tbg
41 41
 	protected $mbt_db_prefix;
42 42
 	protected $tbg_db_prefix;
43 43
 
44
+	// The start timer.
45
+	protected $start_time = 0;
46
+
47
+	// Is this CLI?
48
+	protected $is_cli = false;
49
+
44 50
 	// The database connection resource.
45 51
 	protected $db;
46 52
 
47 53
 	function __construct()
48 54
 	{
55
+		// Start your engines.
56
+		$this->start_time = time();
57
+
49 58
 		// Don't timeout!
50 59
 		set_time_limit(0);
51 60
 
... ...
@@ -151,6 +160,47 @@ class mbt_to_tbg
151 160
 			return (int) $_GET['substep'];
152 161
 	}
153 162
 
163
+	/**
164
+	*
165
+	* Updates the current substep
166
+	*
167
+	* @param init $substep new value for substep.
168
+	*/
169
+	function updateSubStep($substep)
170
+	{
171
+		$_GET['substep'] = (int) $substep;
172
+	}
173
+
174
+	/**
175
+	*
176
+	* Checks our timeout status and attempts to allow us to work longer.
177
+	*
178
+	* @param $function The function name we should return to if we can continue.
179
+	*/
180
+	function checkTimeout($function)
181
+	{
182
+		// CLI conversions can just continue.
183
+		if ($this->is_cli)
184
+		{
185
+			if (time() - $this->start_time > 1)
186
+				print (".\r");
187
+			$this->$function();
188
+		}
189
+
190
+		// Try to buy us more time.
191
+		@set_time_limit(300);
192
+		if (function_exists('apache_reset_timeout'))
193
+			@apache_reset_timeout();
194
+
195
+		// if we can pass go, collect $200.
196
+		if (time() - $this->start_time < 10)
197
+			$this->$function();
198
+
199
+		// @ TODO: Add in timeout stuff here.
200
+		// @ !!! If this is all done via ajax, it should be a json or xml return.
201
+		// @ !!! Will need to strip doStep from the function name and cast as a int for security.
202
+	}
203
+
154 204
 	/**
155 205
 	* Empty the tables prior to the conversion.
156 206
 	*
... ...
@@ -182,10 +232,13 @@ class mbt_to_tbg
182 232
 		{
183 233
 			$password = $this->getRandomString();
184 234
 
185
-			$his->db->query('
235
+			$this->db->query('
186 236
 				INSERT INTO ' . $this->tbg_db_prefix . 'users (id, username, buddyname, realname, email, password, enabled, lastseen, joined)
187 237
 				VALUES (' . $row['id'] . ', "' . $row['username'] . '", "' . $row['buddyname'] . '", "' . $row['realname'] . '", "' . $row['email'] . '", "' . $password . '", ' . $row['enabled'] . ', ' . $row['username'] . ', ' . $row['joined'] . ')');
188 238
 		}
239
+
240
+		$this->updateSubStep($substep + 500);
241
+		$this->checkTimeout(__FUNCTION__);
189 242
 	}
190 243
 }
191 244
 
192 245