More to the timeout process and added json returns
Jeremy D

Jeremy D commited on 2011-10-17 06:56:01
Showing 1 changed files, with 108 additions and 11 deletions.

... ...
@@ -52,12 +52,15 @@ class tbg_coverter
52 52
 
53 53
 	// Is this CLI?
54 54
 	protected $is_cli = false;
55
+	protected $is_json = false;
55 56
 
56 57
 	// The database connection resource.
57 58
 	protected $db;
58 59
 
59 60
 	function __construct()
60 61
 	{
62
+		global $theme;
63
+
61 64
 		// Start your engines.
62 65
 		$this->start_time = time();
63 66
 
... ...
@@ -70,6 +73,9 @@ class tbg_coverter
70 73
 		// Try to find some settings.
71 74
 		$this->loadSettings();
72 75
 
76
+		// Open a new theme.
77
+		$theme = new tbg_converter_wrapper();
78
+
73 79
 		// We can't process anymore until this exists.
74 80
 		if (empty($this->db_user))
75 81
 		{
... ...
@@ -78,6 +84,16 @@ class tbg_coverter
78 84
 		{
79 85
 			$this->setDatabasePrefix();
80 86
 			$this->getDatabaseConnection();
87
+
88
+			// Fire this thing off.
89
+			$this->loadSettings();
90
+
91
+			// Now restart.
92
+			$this->updateSubStep($this->substep);
93
+			$this->updateStep('doStep' . $this->step);
94
+
95
+			$function = 'doStep' . $this->step;
96
+			$function();
81 97
 		}
82 98
 	}
83 99
 
... ...
@@ -88,7 +104,6 @@ class tbg_coverter
88 104
 	private function converterSettings()
89 105
 	{
90 106
 		return array(
91
-			'mantis_loc' => array('type' => 'text', 'required' => true, 'validate' => 'return file_exists($data);'),
92 107
 			'tbg_loc' => array('type' => 'text', 'required' => true, 'default' => dirname(__FILE__), 'validate' => 'return file_exists($data);'),
93 108
 			// @TODO: Make this validate the password.
94 109
 			'tbg_db_pass' => array('type' => 'password', 'required' => true, 'validate' => true,),
... ...
@@ -144,14 +159,18 @@ class tbg_coverter
144 159
 	private function setTBGDatabasePrefix()
145 160
 	{
146 161
 		$this->tbg_db_prefix = $this->tbg_db_name . '.' . $this->tbg_db_table_prefix;
147
-	}
148 162
 
163
+		return true;
164
+	}
149 165
 	/**
166
+
150 167
 	 * Establish a database connection
151 168
 	 */
152 169
 	function getDatabaseConnection()
153 170
 	{
154 171
 		$this->db = new PDO ($this->db_driver . ':host=' . $this->db_host, $this->db_user, $this->db_pass);
172
+
173
+		return true;
155 174
 	}
156 175
 
157 176
 	/**
... ...
@@ -178,6 +197,19 @@ class tbg_coverter
178 197
 		return $random_string;
179 198
 	}
180 199
 
200
+	/**
201
+	*
202
+	* Updates the current step
203
+	*
204
+	* @param init $substep new value for substep.
205
+	*/
206
+	function updateStep($substep)
207
+	{
208
+		$_GET['step'] = (int) $step;
209
+
210
+		return true;
211
+	}
212
+
181 213
 	/**
182 214
 	*
183 215
 	* Gets the current substep
... ...
@@ -201,6 +233,8 @@ class tbg_coverter
201 233
 	function updateSubStep($substep)
202 234
 	{
203 235
 		$_GET['substep'] = (int) $substep;
236
+
237
+		return true;
204 238
 	}
205 239
 
206 240
 	/**
... ...
@@ -211,6 +245,8 @@ class tbg_coverter
211 245
 	*/
212 246
 	function checkTimeout($function)
213 247
 	{
248
+		global $theme;
249
+
214 250
 		// CLI conversions can just continue.
215 251
 		if ($this->is_cli)
216 252
 		{
... ...
@@ -231,6 +267,23 @@ class tbg_coverter
231 267
 		// @ TODO: Add in timeout stuff here.
232 268
 		// @ !!! If this is all done via ajax, it should be a json or xml return.
233 269
 		// @ !!! Will need to strip doStep from the function name and cast as a int for security.
270
+		$real_step = str_replace('doStep', '', $function);
271
+
272
+		if (!is_int($real_step) || (int) $real_step == 0)
273
+		{
274
+			debug_print_backtrace();
275
+			exit;
276
+		}
277
+
278
+		// If ajax, return some data.
279
+		$data = array(
280
+			'step' => $function,
281
+			'time' => time(),
282
+			'substep' => getSubStep($function),
283
+		);
284
+
285
+		if ($this->is_json)
286
+			$theme->return_json($data);
234 287
 	}
235 288
 }
236 289
 
... ...
@@ -247,6 +300,20 @@ class mbt_to_tbg extends tbg_converter
247 300
 		$this->mbt_db_prefix = $this->mbt_db_name . '.' . $this->mbt_db_table_prefix;
248 301
 	}
249 302
 
303
+	/**
304
+	* Request these settings during setup.
305
+	*
306
+	*/
307
+	private function converterSettings()
308
+	{
309
+		$settings = parent::converterSettings();
310
+
311
+		// As long as we don't overwrite, this should work.
312
+		return $settings + array(
313
+			'mantis_loc' => array('type' => 'text', 'required' => true, 'validate' => 'return file_exists($data);'),
314
+		);
315
+	}
316
+
250 317
 	/**
251 318
 	 * Sets the list types in TBG to be like MBT
252 319
 	 * 
... ...
@@ -326,6 +393,7 @@ class mbt_to_tbg extends tbg_converter
326 393
 
327 394
 		$this->updateSubStep($substep + $step_size);
328 395
 		$this->checkTimeout(__FUNCTION__);
396
+		$this->updateStep(__FUNCTION__);
329 397
 	}
330 398
 
331 399
 	/**
... ...
@@ -351,6 +419,7 @@ class mbt_to_tbg extends tbg_converter
351 419
 
352 420
 		$this->updateSubStep($substep + $step_size);
353 421
 		$this->checkTimeout(__FUNCTION__);
422
+		$this->updateStep(__FUNCTION__);
354 423
 	}
355 424
 
356 425
 	/**
... ...
@@ -375,6 +444,7 @@ class mbt_to_tbg extends tbg_converter
375 444
 
376 445
 		$this->updateSubStep($substep + $step_size);
377 446
 		$this->checkTimeout(__FUNCTION__);
447
+		$this->updateStep(__FUNCTION__);
378 448
 	}
379 449
 
380 450
 	/**
... ...
@@ -415,6 +485,7 @@ class mbt_to_tbg extends tbg_converter
415 485
 
416 486
 		$this->updateSubStep($substep + $step_size);
417 487
 		$this->checkTimeout(__FUNCTION__);
488
+		$this->updateStep(__FUNCTION__);
418 489
 	}
419 490
 
420 491
 
... ...
@@ -477,6 +548,7 @@ class mbt_to_tbg extends tbg_converter
477 548
 
478 549
 		$this->updateSubStep($substep + $step_size);
479 550
 		$this->checkTimeout(__FUNCTION__);
551
+		$this->updateStep(__FUNCTION__);
480 552
 	}
481 553
 
482 554
 	/**
... ...
@@ -503,6 +575,7 @@ class mbt_to_tbg extends tbg_converter
503 575
 
504 576
 		$this->updateSubStep($substep + $step_size);
505 577
 		$this->checkTimeout(__FUNCTION__);
578
+		$this->updateStep(__FUNCTION__);
506 579
 	}
507 580
 
508 581
 	/**
... ...
@@ -528,6 +601,7 @@ class mbt_to_tbg extends tbg_converter
528 601
 
529 602
 		$this->updateSubStep($substep + $step_size);
530 603
 		$this->checkTimeout(__FUNCTION__);
604
+		$this->updateStep(__FUNCTION__);
531 605
 	}
532 606
 
533 607
 	/**
... ...
@@ -560,7 +634,7 @@ class mbt_to_tbg extends tbg_converter
560 634
 
561 635
 		$this->updateSubStep($substep + $step_size);
562 636
 		$this->checkTimeout(__FUNCTION__);
563
-
637
+		$this->updateStep(__FUNCTION__);
564 638
 	}
565 639
 
566 640
 	private function getIssues()
... ...
@@ -650,7 +724,7 @@ class mbt_to_tbg extends tbg_converter
650 724
 			INSERT INTO ' . $this->tbg_prefix . 'projects
651 725
 				id, name, locked, description	
652 726
 			SELECT id, name, enabled AS locked, description
653
-			FROM ' . $this->mtb_prefix . 'project_table
727
+			FROM ' . $this->mtb_prefix . 'project_table'
654 728
 		);
655 729
 	}
656 730
 }
... ...
@@ -661,36 +735,59 @@ class mbt_to_tbg extends tbg_converter
661 735
 class tbg_converter_wrapper
662 736
 {
663 737
 	protected $page_title = 'Mantis to The Bug Genie converter';
738
+	protected $headers = array();
664 739
 
665 740
 	/*
666 741
 	* Set the title.
667 742
 	* @param $title: The page title to set
668 743
 	*/
669
-	public static function setHeader($title)
744
+	public function setTitle($title)
670 745
 	{
671
-		self:$page_title = $title;
746
+		$this->page_title = $title;
672 747
 	}
673 748
 
674 749
 	/*
675 750
 	* Any custom header()s
676 751
 	*
677 752
 	*/
678
-	public static function header()
753
+	public function printHeader()
754
+	{
755
+		foreach ($this->headers as $type => $data)
756
+			header($type . ': ' . $data);
757
+	}
758
+
759
+	/*
760
+	* Set headers.
761
+	*
762
+	*/
763
+	public function setHeader($type, $data)
764
+	{
765
+		$this->headers[$type] = $data;
766
+	}
767
+
768
+	/*
769
+	* Return a json array.
770
+	* @param $data: The json data.
771
+	*/
772
+	public function return_json($data)
679 773
 	{
774
+		$this->setHeader('Content-Type', 'text/javascript; charset=utf8');
680 775
 
776
+		echo json_encode($data);
777
+		exit;
681 778
 	}
682 779
 
683 780
 	/*
684 781
 	* The upper part of the theme.
685 782
 	*
686 783
 	*/
687
-	public static function upper()
784
+	public function upper()
688 785
 	{
689 786
 		echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
690 787
 <html xmlns="http://www.w3.org/1999/html">
691 788
 <head>
692 789
 	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
693
-	<title>', self::$page_title, '</title>
790
+	<title>', $this->page_title, '</title>
694 791
 	<style type="text/css">
695 792
 	<!--
696 793
 	-->
... ...
@@ -700,7 +797,7 @@ class tbg_converter_wrapper
700 797
 	<div>
701 798
 		<div style="padding: 10px; padding-right: 0px; padding-left: 0px; width:98% ">
702 799
 			<div style="padding-left: 200px; padding-right: 0px;">
703
-				<h1>', self::$page_title, '</h1>
800
+				<h1>', $this->page_title, '</h1>
704 801
 				<div class="panel" style="padding-right: 0px;  white-space: normal; overflow: hidden;">';
705 802
 	}
706 803
 
... ...
@@ -708,7 +805,7 @@ class tbg_converter_wrapper
708 805
 	* The lower part of the theme.
709 806
 	*
710 807
 	*/
711
-	public static function lower()
808
+	public function lower()
712 809
 	{
713 810
 		echo '
714 811
 				</div>
715 812