Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
70.97% covered (warning)
70.97%
44 / 62
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
UpdateTable
78.57% covered (warning)
78.57%
44 / 56
50.00% covered (danger)
50.00%
2 / 4
12.19
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 execute
80.00% covered (warning)
80.00%
20 / 25
0.00% covered (danger)
0.00%
0 / 1
5.20
 createImportContext
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 clearTable
61.11% covered (warning)
61.11%
11 / 18
0.00% covered (danger)
0.00%
0 / 1
4.94
1<?php
2
3namespace PropertySuggester\Maintenance;
4
5use Maintenance;
6use MediaWiki\MediaWikiServices;
7use PropertySuggester\UpdateTable\ImportContext;
8use PropertySuggester\UpdateTable\Importer\BasicImporter;
9use UnexpectedValueException;
10use Wikimedia\Rdbms\IDatabase;
11use Wikimedia\Rdbms\ILBFactory;
12
13$basePath = getenv( 'MW_INSTALL_PATH' ) !== false
14    ? getenv( 'MW_INSTALL_PATH' )
15    : __DIR__ . '/../../..';
16require_once $basePath . '/maintenance/Maintenance.php';
17
18/**
19 * Maintenance script to load property pair occurrence probability table from given csv file
20 *
21 * @author BP2013N2
22 * @license GPL-2.0-or-later
23 */
24class UpdateTable extends Maintenance {
25
26    public function __construct() {
27        parent::__construct();
28
29        $this->addDescription( "Read CSV Dump and refill probability table" );
30        $this->addOption( 'file', 'CSV table to be loaded (relative path)', true, true );
31        $this->setBatchSize( 10000 );
32        $this->requireExtension( 'PropertySuggester' );
33    }
34
35    /**
36     * loads property pair occurrence probability table from given csv file
37     */
38    public function execute() {
39        if ( substr( $this->getOption( 'file' ), 0, 2 ) === "--" ) {
40            $this->fatalError( "The --file option requires a file as an argument.\n" );
41        }
42        $path = $this->getOption( 'file' );
43        $fullPath = realpath( $path );
44        $fullPath = str_replace( '\\', '/', $fullPath );
45
46        if ( !file_exists( $fullPath ) ) {
47            $this->fatalError( "Cant find $path \n" );
48        }
49
50        $tableName = 'wbs_propertypairs';
51
52        $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
53        $lbFactory->waitForReplication();
54
55        $this->clearTable( $lbFactory, $tableName );
56
57        $this->output( "loading new entries from file\n" );
58
59        $importContext = $this->createImportContext(
60            $lbFactory,
61            $tableName,
62            $fullPath,
63            $this->isQuiet()
64        );
65        $importStrategy = new BasicImporter();
66
67        try {
68            $success = $importStrategy->importFromCsvFileToDb( $importContext );
69        } catch ( UnexpectedValueException $e ) {
70            $this->fatalError( "Import failed: " . $e->getMessage() );
71        }
72
73        if ( !$success ) {
74            $this->fatalError( "Failed to run import to db" );
75        }
76        $this->output( "... Done loading\n" );
77    }
78
79    /**
80     * @param ILBFactory $lbFactory
81     * @param string $tableName
82     * @param string $wholePath
83     * @param bool $quiet
84     * @return ImportContext
85     */
86    private function createImportContext( ILBFactory $lbFactory, $tableName, $wholePath, $quiet ) {
87        $importContext = new ImportContext();
88        $importContext->setLbFactory( $lbFactory );
89        $importContext->setTargetTableName( $tableName );
90        $importContext->setCsvFilePath( $wholePath );
91        $importContext->setCsvDelimiter( ',' );
92        $importContext->setBatchSize( $this->mBatchSize );
93        $importContext->setQuiet( $quiet );
94
95        return $importContext;
96    }
97
98    /**
99     * @param ILBFactory $lbFactory
100     * @param string $tableName
101     */
102    private function clearTable( ILBFactory $lbFactory, $tableName ) {
103        global $wgDBtype;
104
105        $lb = $lbFactory->getMainLB();
106        $db = $lb->getMaintenanceConnectionRef( DB_PRIMARY );
107        if ( !$db->tableExists( $tableName, __METHOD__ ) ) {
108            $this->fatalError( "$tableName table does not exist.\n" .
109                "Executing core/maintenance/update.php may help.\n" );
110        }
111        $this->output( "Removing old entries\n" );
112        if ( $wgDBtype === 'sqlite' || $wgDBtype === 'postgres' ) {
113            $db->newDeleteQueryBuilder()
114                ->deleteFrom( $tableName )
115                ->where( IDatabase::ALL_ROWS )
116                ->caller( __METHOD__ )
117                ->execute();
118        } else {
119            do {
120                $db->commit( __METHOD__, 'flush' );
121                $lbFactory->waitForReplication();
122                $this->output( "Deleting a batch\n" );
123                $table = $db->tableName( $tableName );
124                $db->query( "DELETE FROM $table LIMIT $this->mBatchSize", __METHOD__ );
125            } while ( $db->affectedRows() > 0 );
126        }
127    }
128
129}
130
131$maintClass = UpdateTable::class;
132require_once RUN_MAINTENANCE_IF_MAIN;