Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
SpecialDeleteCargoTable | |
0.00% |
0 / 76 |
|
0.00% |
0 / 4 |
272 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
doesWrites | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
deleteTable | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
30 | |||
execute | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
90 |
1 | <?php |
2 | /** |
3 | * An interface to delete a "Cargo table", which can be one or more real |
4 | * database tables. |
5 | * |
6 | * @author Yaron Koren |
7 | * @ingroup Cargo |
8 | */ |
9 | |
10 | class SpecialDeleteCargoTable extends UnlistedSpecialPage { |
11 | |
12 | public function __construct() { |
13 | parent::__construct( 'DeleteCargoTable', 'deletecargodata' ); |
14 | } |
15 | |
16 | public function doesWrites() { |
17 | return true; |
18 | } |
19 | |
20 | /** |
21 | * The table being deleted here is a Cargo table, not a DB table per |
22 | * se - a Cargo table corresponds to a main DB table, plus |
23 | * potentially one or more helper tables; all need to be deleted. |
24 | * Also, records need to be removed from the cargo_tables and |
25 | * cargo_pages tables. |
26 | */ |
27 | public static function deleteTable( $mainTable, $fieldTables, $fieldHelperTables ) { |
28 | $cdb = CargoUtils::getDB(); |
29 | try { |
30 | $cdb->begin(); |
31 | foreach ( $fieldTables as $fieldTable ) { |
32 | $cdb->dropTable( $fieldTable ); |
33 | } |
34 | if ( is_array( $fieldHelperTables ) ) { |
35 | foreach ( $fieldHelperTables as $fieldHelperTable ) { |
36 | $cdb->dropTable( $fieldHelperTable ); |
37 | } |
38 | } |
39 | $cdb->dropTable( $mainTable ); |
40 | $cdb->commit(); |
41 | } catch ( Exception $e ) { |
42 | throw new MWException( "Caught exception ($e) while trying to drop Cargo table. " |
43 | . "Please make sure that your database user account has the DROP permission." ); |
44 | } |
45 | |
46 | $dbw = CargoUtils::getMainDBForWrite(); |
47 | $dbw->delete( 'cargo_tables', [ 'main_table' => $mainTable ] ); |
48 | $dbw->delete( 'cargo_pages', [ 'table_name' => $mainTable ] ); |
49 | } |
50 | |
51 | public function execute( $subpage = false ) { |
52 | $this->checkPermissions(); |
53 | |
54 | $out = $this->getOutput(); |
55 | $req = $this->getRequest(); |
56 | |
57 | $out->enableOOUI(); |
58 | |
59 | $this->setHeaders(); |
60 | if ( $subpage == '' ) { |
61 | CargoUtils::displayErrorMessage( $out, $this->msg( "cargo-notable" ) ); |
62 | return true; |
63 | } |
64 | |
65 | $replacementTable = $req->getCheck( '_replacement' ); |
66 | $origTableName = $subpage; |
67 | if ( $replacementTable ) { |
68 | $tableName = $subpage . '__NEXT'; |
69 | } else { |
70 | $tableName = $subpage; |
71 | } |
72 | |
73 | // Make sure that this table exists. |
74 | $dbr = CargoUtils::getMainDBForRead(); |
75 | $res = $dbr->select( 'cargo_tables', [ 'main_table', 'field_tables', 'field_helper_tables' ], |
76 | [ 'main_table' => $tableName ] ); |
77 | if ( $res->numRows() == 0 ) { |
78 | CargoUtils::displayErrorMessage( $out, $this->msg( "cargo-unknowntable", $tableName ) ); |
79 | return true; |
80 | } |
81 | |
82 | $ctPage = CargoUtils::getSpecialPage( 'CargoTables' ); |
83 | $row = $res->fetchRow(); |
84 | $fieldTables = unserialize( $row['field_tables'] ); |
85 | $fieldHelperTables = unserialize( $row['field_helper_tables'] ); |
86 | |
87 | if ( $this->getRequest()->getCheck( 'delete' ) ) { |
88 | self::deleteTable( $tableName, $fieldTables, $fieldHelperTables ); |
89 | $text = Html::element( 'p', null, $this->msg( 'cargo-deletetable-success', $tableName )->parse() ) . "\n"; |
90 | $tablesLink = CargoUtils::makeLink( $this->getLinkRenderer(), |
91 | $ctPage->getPageTitle(), |
92 | htmlspecialchars( $ctPage->getDescription() ) ); |
93 | $text .= Html::rawElement( 'p', null, $this->msg( 'returnto', $tablesLink )->text() ); |
94 | $out->addHTML( $text ); |
95 | if ( !$replacementTable ) { |
96 | CargoUtils::logTableAction( 'deletetable', $tableName, $this->getUser() ); |
97 | } |
98 | return true; |
99 | } |
100 | |
101 | $ctURL = $ctPage->getPageTitle()->getFullURL(); |
102 | $tableLink = "[$ctURL/$origTableName $origTableName]"; |
103 | |
104 | if ( $replacementTable ) { |
105 | $replacementTableURL = "$ctURL/$origTableName"; |
106 | $replacementTableURL .= ( strpos( $replacementTableURL, '?' ) ) ? '&' : '?'; |
107 | $replacementTableURL .= '_replacement'; |
108 | $text = Html::rawElement( 'p', |
109 | [ 'class' => 'plainlinks' ], |
110 | $this->msg( 'cargo-deletetable-replacementconfirm', $replacementTableURL, $tableLink )->parse() |
111 | ); |
112 | } else { |
113 | $text = Html::rawElement( 'p', |
114 | [ 'class' => 'plainlinks' ], |
115 | $this->msg( 'cargo-deletetable-confirm', $tableLink )->parse() |
116 | ); |
117 | } |
118 | $out->addHTML( $text ); |
119 | |
120 | $htmlForm = HTMLForm::factory( 'ooui', [], $this->getContext() ); |
121 | |
122 | if ( $replacementTable ) { |
123 | $htmlForm = $htmlForm->addHiddenField( '_replacement', '' ); |
124 | } |
125 | |
126 | $htmlForm |
127 | ->setSubmitName( 'delete' ) |
128 | ->setSubmitTextMsg( 'delete' ) |
129 | ->setSubmitDestructive() |
130 | ->prepareForm() |
131 | ->displayForm( false ); |
132 | |
133 | return true; |
134 | } |
135 | } |