Added initial support for CLI to actually work.
Jeremy D

Jeremy D commited on 2011-10-29 19:57:23
Showing 1 changed files, with 123 additions and 10 deletions.

... ...
@@ -23,7 +23,7 @@
23 23
 * Test
24 24
 */
25 25
 
26
-class tbg_coverter
26
+class tbg_converter
27 27
 {
28 28
 	// Where your config_inc.php is located
29 29
 	//const MBT_PATH = '';
... ...
@@ -69,6 +69,10 @@ class tbg_coverter
69 69
 		// Start your engines.
70 70
 		$this->start_time = time();
71 71
 
72
+		// We might be running this from terminal
73
+		if ((php_sapi_name() == 'cli' || (isset($_SERVER['TERM']) && strpos($_SERVER['TERM'], 'xterm') !== false)) && empty($_SERVER['REMOTE_ADDR']))
74
+			$this->processCLI();
75
+
72 76
 		// Don't timeout!
73 77
 		set_time_limit(0);
74 78
 
... ...
@@ -88,6 +92,68 @@ class tbg_coverter
88 92
 			$this->doConversion();
89 93
 	}
90 94
 
95
+	/**
96
+	* Actually does the conversion process
97
+	*
98
+	*/
99
+	private function processCLI()
100
+	{
101
+		$this->is_cli = true;
102
+
103
+		// First off, clean the buffers and buy us some time.
104
+		@ob_end_clean();
105
+		ob_implicit_flush(true);
106
+		@set_time_limit(600);
107
+
108
+		// This isn't good.
109
+		if (!isset($_SERVER['argv']))
110
+			$_SERVER['argv'] = array();
111
+
112
+		// If its empty, force help.
113
+		if (empty($_SERVER['argv'][1]))
114
+			$_SERVER['argv'][1] = '--help';
115
+
116
+		// Lets load up our possible arguments.
117
+		$settings = $this->converterSettings();
118
+
119
+		// We need help!.
120
+		if (in_array('--help', $_SERVER['argv']))
121
+		{
122
+			// This is a special case, where echo is fine here.
123
+			echo "
124
+			Usage: " . (isset($_ENV['_']) ? $_ENV['_'] : '/path/to/php') . " -f " . str_replace(getcwd() . '/', '', __FILE__) . " -- [OPTION]...\n";
125
+
126
+			foreach ($settings as $key => $setting)
127
+				echo '
128
+			--' . $key . '			' . $setting['name'];
129
+
130
+			// The debug option.
131
+			exit("
132
+		   	--debug				Output debugging information.\n");
133
+		}
134
+
135
+		// Lets get the settings.
136
+		foreach ($settings as $key => $setting)
137
+		{
138
+			// No special regex?
139
+			if (!isset($setting['cli_regex']))
140
+				$setting['cli_regex'] = '~^--' . $key . '=([^--]+)~i';
141
+
142
+			foreach ($_SERVER['argv'] as $i => $arg)
143
+			{
144
+				// Trim spaces.
145
+				$arg = trim($arg);
146
+
147
+				if (preg_match($setting['cli_regex'], $arg, $match) != 0)
148
+					$_POST[$key] = substr($match[1], -1) == '/' ? substr($match[1], 0, -1) : trim($match[1]);
149
+			}
150
+
151
+			// Oh noes, we can't find it!
152
+			if ($setting['required'] && empty($_POST[$key]))
153
+				exit('The argument, ' . $key . ', is required to continue.');
154
+		}
155
+	}
156
+
91 157
 	/**
92 158
 	* Actually does the conversion process
93 159
 	*
... ...
@@ -103,18 +169,17 @@ class tbg_coverter
103 169
 		// Now restart.
104 170
 		do
105 171
 		{
106
-			$data = $steps[$this->step];
172
+			$data = $this->steps[$this->step];
107 173
 
108 174
 			$this->updateSubStep($this->substep);
109
-			$this->updateStep('doStep' . $this->step);
175
+			$this->updateStep($data[1]);
110 176
 			$this->step_size = $data[2];
111
-		}
112
-		while
113
-		{
177
+
114 178
 			$count = $data[1]();
115 179
 
116 180
 			$this->checkTimeout($data[1], $this->substep, $data[2], $count);
117 181
 		}
182
+		while ($this->step < count($this->steps));
118 183
 	}
119 184
 
120 185
 	/**
... ...
@@ -139,13 +204,15 @@ class tbg_coverter
139 204
 
140 205
 		// Prompt for some settings.
141 206
 		$theme->showSettings($this->converterSettings());
207
+
208
+		$theme->done($this->is_cli);
142 209
 	}
143 210
 
144 211
 	/**
145 212
 	* Request these settings during setup.
146 213
 	*
147 214
 	*/
148
-	private function converterSettings()
215
+	public function converterSettings()
149 216
 	{
150 217
 		return array(
151 218
 			'tbg_loc' => array('name' => 'The Bug Genie location', 'type' => 'text', 'required' => true, 'default' => dirname(__FILE__), 'validate' => 'return file_exists($data);'),
... ...
@@ -376,13 +444,13 @@ class mbt_to_tbg extends tbg_converter
376 444
 	* Request these settings during setup.
377 445
 	*
378 446
 	*/
379
-	private function converterSettings()
447
+	public function converterSettings()
380 448
 	{
381 449
 		$settings = parent::converterSettings();
382 450
 
383 451
 		// As long as we don't overwrite, this should work.
384 452
 		return $settings + array(
385
-			'mantis_loc' => array('type' => 'text', 'required' => true, 'validate' => 'return file_exists($data);'),
453
+			'mantis_loc' => array('name' => 'Mantis Location', 'type' => 'text', 'required' => true, 'validate' => 'return file_exists($data);'),
386 454
 		);
387 455
 	}
388 456
 
... ...
@@ -830,6 +898,34 @@ class tbg_converter_wrapper
830 898
 	protected $page_title = 'Mantis to The Bug Genie converter';
831 899
 	protected $headers = array();
832 900
 
901
+	/** 
902
+	 * Start the HTML.
903
+	*/
904
+	public function __construct($no_html = false)
905
+	{
906
+		// Do nothing!
907
+		if (!empty($no_html))
908
+			return;
909
+
910
+		// Some headers.
911
+		$this->printHeader();
912
+
913
+		// Upper part of theme.
914
+		$this->upper();
915
+	}
916
+
917
+	/** 
918
+	 * End of output.
919
+	*/
920
+	public function done($is_cli = false)
921
+	{
922
+		if ($is_cli)
923
+			exit("\nConversion completed");
924
+
925
+		$this->lower();
926
+		exit;
927
+	}
928
+
833 929
 	/*
834 930
 	* Set the title.
835 931
 	* @param $title: The page title to set
... ...
@@ -864,12 +960,28 @@ class tbg_converter_wrapper
864 960
 	*/
865 961
 	public function return_json($data)
866 962
 	{
867
-		$this->setHeader('Content-Type', 'text/javascript; charset=utf8');
963
+		header('Content-Type', 'text/javascript; charset=utf8');
868 964
 		
869 965
 		echo json_encode($data);
870 966
 		exit;
871 967
 	}
872 968
 
969
+	/*
970
+	* We have some new data!.
971
+	* @param $data: The json data.
972
+	*/
973
+	public function steps($steps)
974
+	{
975
+		echo '<ol>';
976
+
977
+		foreach ($steps as $id_step => $step)
978
+			echo '
979
+				<li id="step', $id_step, '"><span class="name">', $step[0], '</span><span class="progress"></span><span class="progress_percent" style="display: none;">0%</span></li>';
980
+
981
+		echo '</ol>';
982
+	}
983
+
984
+
873 985
 	/*
874 986
 	* The upper part of the theme.
875 987
 	*
... ...
@@ -952,3 +1064,5 @@ class tbg_converter_wrapper
952 1064
 
953 1065
 
954 1066
 }
1067
+
1068
+$convert = new mbt_to_tbg();
955 1069
\ No newline at end of file
956 1070