Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialDeleteCargoTable
0.00% covered (danger)
0.00%
0 / 77
0.00% covered (danger)
0.00%
0 / 4
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 doesWrites
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 deleteTable
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 execute
0.00% covered (danger)
0.00%
0 / 60
0.00% covered (danger)
0.00%
0 / 1
132
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
10class 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( __METHOD__ );
31            foreach ( $fieldTables as $fieldTable ) {
32                $cdb->dropTable( $fieldTable, __METHOD__ );
33            }
34            if ( is_array( $fieldHelperTables ) ) {
35                foreach ( $fieldHelperTables as $fieldHelperTable ) {
36                    $cdb->dropTable( $fieldHelperTable, __METHOD__ );
37                }
38            }
39            $cdb->dropTable( $mainTable, __METHOD__ );
40            $cdb->commit( __METHOD__ );
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 ], __METHOD__ );
48        $dbw->delete( 'cargo_pages', [ 'table_name' => $mainTable ], __METHOD__ );
49    }
50
51    public function execute( $subpage = false ) {
52        $this->checkPermissions();
53
54        $out = $this->getOutput();
55        $req = $this->getRequest();
56        $csrfTokenSet = $this->getContext()->getCsrfTokenSet();
57
58        $out->enableOOUI();
59
60        $this->setHeaders();
61        if ( $subpage == '' ) {
62            CargoUtils::displayErrorMessage( $out, $this->msg( "cargo-notable" ) );
63            return true;
64        }
65
66        $replacementTable = $req->getCheck( '_replacement' );
67        $origTableName = $subpage;
68        if ( $replacementTable ) {
69            $tableName = $subpage . '__NEXT';
70        } else {
71            $tableName = $subpage;
72        }
73
74        // Make sure that this table exists.
75        $dbr = CargoUtils::getMainDBForRead();
76        $res = $dbr->select( 'cargo_tables', [ 'main_table', 'field_tables', 'field_helper_tables' ],
77            [ 'main_table' => $tableName ], __METHOD__ );
78        if ( $res->numRows() == 0 ) {
79            CargoUtils::displayErrorMessage( $out, $this->msg( "cargo-unknowntable", $tableName ) );
80            return true;
81        }
82
83        $ctPage = CargoUtils::getSpecialPage( 'CargoTables' );
84        $row = $res->fetchRow();
85        $fieldTables = unserialize( $row['field_tables'] );
86        $fieldHelperTables = unserialize( $row['field_helper_tables'] );
87
88        if ( $req->wasPosted() && $req->getCheck( 'delete' ) && $csrfTokenSet->matchToken( $req->getText( 'wpEditToken' ) ) ) {
89            self::deleteTable( $tableName, $fieldTables, $fieldHelperTables );
90            $text = Html::rawElement( 'p', null, $this->msg( 'cargo-deletetable-success', $tableName )->escaped() ) . "\n";
91            $tablesLink = CargoUtils::makeLink( $this->getLinkRenderer(),
92                $ctPage->getPageTitle(),
93                htmlspecialchars( $ctPage->getDescription() ) );
94            $text .= Html::rawElement( 'p', null, $this->msg( 'returnto' )->rawParams( $tablesLink )->escaped() );
95            $out->addHTML( $text );
96            if ( !$replacementTable ) {
97                CargoUtils::logTableAction( 'deletetable', $tableName, $this->getUser() );
98            }
99            return true;
100        }
101
102        $ctURL = $ctPage->getPageTitle()->getFullURL();
103        $tableLink = "[$ctURL/$origTableName $origTableName]";
104
105        if ( $replacementTable ) {
106            $replacementTableURL = "$ctURL/$origTableName";
107            $replacementTableURL .= ( strpos( $replacementTableURL, '?' ) ) ? '&' : '?';
108            $replacementTableURL .= '_replacement';
109            $text = Html::rawElement( 'p',
110                [ 'class' => 'plainlinks' ],
111                $this->msg( 'cargo-deletetable-replacementconfirm', $replacementTableURL, $tableLink )->parse()
112            );
113        } else {
114            $text = Html::rawElement( 'p',
115                [ 'class' => 'plainlinks' ],
116                $this->msg( 'cargo-deletetable-confirm', $tableLink )->parse()
117            );
118        }
119        $out->addHTML( $text );
120
121        $htmlForm = HTMLForm::factory( 'ooui', [], $this->getContext() );
122
123        if ( $replacementTable ) {
124            $htmlForm = $htmlForm->addHiddenField( '_replacement', '' );
125        }
126
127        $htmlForm
128            ->setSubmitName( 'delete' )
129            ->setSubmitTextMsg( 'delete' )
130            ->setSubmitDestructive()
131            ->prepareForm()
132            ->displayForm( false );
133
134        return true;
135    }
136}