2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Set of functions used to build MediaWiki dumps of tables
6 * @package PhpMyAdmin-Export
7 * @subpackage MediaWiki
9 if (! defined('PHPMYADMIN')) {
13 /* Get the export interface */
14 require_once 'libraries/plugins/ExportPlugin.class.php';
17 * Handles the export for the MediaWiki class
19 * @package PhpMyAdmin-Export
20 * @subpackage MediaWiki
22 class ExportMediawiki
extends ExportPlugin
27 public function __construct()
29 $this->setProperties();
33 * Sets the export MediaWiki properties
37 protected function setProperties()
39 $props = 'libraries/properties/';
40 include_once "$props/plugins/ExportPluginProperties.class.php";
41 include_once "$props/options/groups/OptionsPropertyRootGroup.class.php";
42 include_once "$props/options/groups/OptionsPropertyMainGroup.class.php";
43 include_once "$props/options/groups/OptionsPropertySubgroup.class.php";
44 include_once "$props/options/items/MessageOnlyPropertyItem.class.php";
45 include_once "$props/options/items/RadioPropertyItem.class.php";
46 include_once "$props/options/items/BoolPropertyItem.class.php";
48 $exportPluginProperties = new ExportPluginProperties();
49 $exportPluginProperties->setText('MediaWiki Table');
50 $exportPluginProperties->setExtension('mediawiki');
51 $exportPluginProperties->setMimeType('text/plain');
52 $exportPluginProperties->setOptionsText(__('Options'));
54 // create the root group that will be the options field for
55 // $exportPluginProperties
56 // this will be shown as "Format specific options"
57 $exportSpecificOptions = new OptionsPropertyRootGroup();
58 $exportSpecificOptions->setName("Format Specific Options");
60 // general options main group
61 $generalOptions = new OptionsPropertyMainGroup();
62 $generalOptions->setName("general_opts");
63 $generalOptions->setText(__('Dump table'));
65 // what to dump (structure/data/both)
66 $subgroup = new OptionsPropertySubgroup();
67 $subgroup->setName("dump_table");
68 $subgroup->setText("Dump table");
69 $leaf = new RadioPropertyItem();
70 $leaf->setName('structure_or_data');
73 'structure' => __('structure'),
75 'structure_and_data' => __('structure and data')
78 $subgroup->setSubgroupHeader($leaf);
79 $generalOptions->addProperty($subgroup);
82 $leaf = new BoolPropertyItem();
83 $leaf->setName("caption");
84 $leaf->setText(__('Export table names'));
85 $generalOptions->addProperty($leaf);
87 // export table headers
88 $leaf = new BoolPropertyItem();
89 $leaf->setName("headers");
90 $leaf->setText(__('Export table headers'));
91 $generalOptions->addProperty($leaf);
92 //add the main group to the root group
93 $exportSpecificOptions->addProperty($generalOptions);
95 // set the options for the export plugin property item
96 $exportPluginProperties->setOptions($exportSpecificOptions);
97 $this->properties
= $exportPluginProperties;
101 * This method is called when any PluginManager to which the observer
102 * is attached calls PluginManager::notify()
104 * @param SplSubject $subject The PluginManager notifying the observer
109 public function update (SplSubject
$subject)
114 * Outputs export header
116 * @return bool Whether it succeeded
118 public function exportHeader ()
124 * Outputs export footer
126 * @return bool Whether it succeeded
128 public function exportFooter ()
134 * Outputs database header
136 * @param string $db Database name
138 * @return bool Whether it succeeded
140 public function exportDBHeader ($db)
146 * Outputs database footer
148 * @param string $db Database name
150 * @return bool Whether it succeeded
152 public function exportDBFooter ($db)
158 * Outputs CREATE DATABASE statement
160 * @param string $db Database name
162 * @return bool Whether it succeeded
164 public function exportDBCreate($db)
170 * Outputs table's structure
172 * @param string $db database name
173 * @param string $table table name
174 * @param string $crlf the end of line sequence
175 * @param string $error_url the url to go back in case of error
176 * @param string $export_mode 'create_table','triggers','create_view',
178 * @param string $export_type 'server', 'database', 'table'
179 * @param bool $do_relation whether to include relation comments
180 * @param bool $do_comments whether to include the pmadb-style column
181 * comments as comments in the structure; this is
182 * deprecated but the parameter is left here
183 * because export.php calls exportStructure()
184 * also for other export types which use this
186 * @param bool $do_mime whether to include mime comments
187 * @param bool $dates whether to include creation/update/check dates
189 * @return bool Whether it succeeded
191 public function exportStructure(
198 $do_relation = false,
199 $do_comments = false,
203 switch($export_mode) {
205 $columns = PMA_DBI_get_columns($db, $table);
206 $columns = array_values($columns);
207 $row_cnt = count($columns);
209 // Print structure comment
210 $output = $this->_exportComment(
211 "Table structure for "
212 . PMA_Util
::backquote($table)
215 // Begin the table construction
216 $output .= "{| class=\"wikitable\" style=\"text-align:center;\""
217 . $this->_exportCRLF();
219 // Add the table name
220 if ($GLOBALS['mediawiki_caption']) {
221 $output .= "|+'''" . $table . "'''" . $this->_exportCRLF();
224 // Add the table headers
225 if ($GLOBALS['mediawiki_headers']) {
226 $output .= "|- style=\"background:#ffdead;\"" . $this->_exportCRLF();
227 $output .= "! style=\"background:#ffffff\" | "
228 . $this->_exportCRLF();
229 for ($i = 0; $i < $row_cnt; ++
$i) {
230 $output .= " | " . $columns[$i]['Field']. $this->_exportCRLF();
234 // Add the table structure
235 $output .= "|-" . $this->_exportCRLF();
236 $output .= "! Type" . $this->_exportCRLF();
237 for ($i = 0; $i < $row_cnt; ++
$i) {
238 $output .= " | " . $columns[$i]['Type'] . $this->_exportCRLF();
241 $output .= "|-" . $this->_exportCRLF();
242 $output .= "! Null" . $this->_exportCRLF();
243 for ($i = 0; $i < $row_cnt; ++
$i) {
244 $output .= " | " . $columns[$i]['Null'] . $this->_exportCRLF();
247 $output .= "|-" . $this->_exportCRLF();
248 $output .= "! Default" . $this->_exportCRLF();
249 for ($i = 0; $i < $row_cnt; ++
$i) {
250 $output .= " | " . $columns[$i]['Default'] . $this->_exportCRLF();
253 $output .= "|-" . $this->_exportCRLF();
254 $output .= "! Extra" . $this->_exportCRLF();
255 for ($i = 0; $i < $row_cnt; ++
$i) {
256 $output .= " | " . $columns[$i]['Extra'] . $this->_exportCRLF();
259 $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
263 return PMA_exportOutputHandler($output);
267 * Outputs the content of a table in MediaWiki format
269 * @param string $db database name
270 * @param string $table table name
271 * @param string $crlf the end of line sequence
272 * @param string $error_url the url to go back in case of error
273 * @param string $sql_query SQL query for obtaining data
275 * @return bool Whether it succeeded
277 public function exportData(
284 // Print data comment
285 $output = $this->_exportComment(
286 "Table data for ". PMA_Util
::backquote($table)
289 // Begin the table construction
290 // Use the "wikitable" class for style
291 // Use the "sortable" class for allowing tables to be sorted by column
292 $output .= "{| class=\"wikitable sortable\" style=\"text-align:center;\""
293 . $this->_exportCRLF();
295 // Add the table name
296 if ($GLOBALS['mediawiki_caption']) {
297 $output .= "|+'''" . $table . "'''" . $this->_exportCRLF();
300 // Add the table headers
301 if ($GLOBALS['mediawiki_headers']) {
303 $column_names = PMA_DBI_get_column_names($db, $table);
305 // Add column names as table headers
306 if ( ! is_null($column_names) ) {
307 // Use '|-' for separating rows
308 $output .= "|-" . $this->_exportCRLF();
310 // Use '!' for separating table headers
311 foreach ($column_names as $column) {
312 $output .= " ! " . $column . "" . $this->_exportCRLF();
317 // Get the table data from the database
318 $result = PMA_DBI_query($sql_query, null, PMA_DBI_QUERY_UNBUFFERED
);
319 $fields_cnt = PMA_DBI_num_fields($result);
321 while ($row = PMA_DBI_fetch_row($result)) {
322 $output .= "|-" . $this->_exportCRLF();
324 // Use '|' for separating table columns
325 for ($i = 0; $i < $fields_cnt; ++
$i) {
326 $output .= " | " . $row[$i] . "" . $this->_exportCRLF();
330 // End table construction
331 $output .= "|}" . str_repeat($this->_exportCRLF(), 2);
332 return PMA_exportOutputHandler($output);
336 * Outputs comments containing info about the exported tables
338 * @param string $text Text of comment
340 * @return string The formatted comment
342 private function _exportComment($text = '')
344 // see http://www.mediawiki.org/wiki/Help:Formatting
345 $comment = $this->_exportCRLF();
346 $comment .= '<!--' . $this->_exportCRLF();
347 $comment .= $text . $this->_exportCRLF();
348 $comment .= '-->' . str_repeat($this->_exportCRLF(), 2);
356 * @return string CRLF
358 private function _exportCRLF()
360 // The CRLF expected by the mediawiki format is "\n"