Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
ImportDefinitions | |
0.00% |
0 / 40 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @ingroup Maintenance |
19 | */ |
20 | |
21 | use MediaWiki\Title\Title; |
22 | |
23 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
24 | |
25 | class ImportDefinitions extends Maintenance { |
26 | |
27 | /** @var string */ |
28 | private $dir; |
29 | /** @var bool */ |
30 | private $overwrite; |
31 | |
32 | public function __construct() { |
33 | parent::__construct(); |
34 | $this->addDescription( 'Batch imports definitions from a folder.' . |
35 | " \n Processes mathosphere files that follow the naming convention: \n *.json" ); |
36 | $this->addArg( 'dir', 'The directory to be read', true ); |
37 | $this->addOption( |
38 | 'overwrite', 'Overwrite existing definitions with the same name.', false, false, "o" |
39 | ); |
40 | $this->requireExtension( 'MathSearch' ); |
41 | } |
42 | |
43 | public function execute() { |
44 | $dbw = $this->getServiceContainer() |
45 | ->getConnectionProvider() |
46 | ->getPrimaryDatabase(); |
47 | $this->dir = $this->getArg( 0 ); |
48 | $this->overwrite = $this->getOption( 'overwrite' ); |
49 | if ( $this->overwrite ) { |
50 | $this->output( "Loaded with option overwrite enabled .\n" ); |
51 | } |
52 | if ( !is_dir( $this->dir ) ) { |
53 | $this->output( "{$this->dir} is not a directory.\n" ); |
54 | exit( 1 ); |
55 | } |
56 | $files = new GlobIterator( $this->dir . '/*.json' ); |
57 | foreach ( $files as $file ) { |
58 | $handle = fopen( $file, 'r' ); |
59 | while ( !feof( $handle ) ) { |
60 | $line = fgets( $handle ); |
61 | if ( preg_match( '/^[\s\[]*(?P<content>\{.*?\})[\s,\]]$/', $line, $matches ) ) { |
62 | $oJson = json_decode( $matches['content'] ); |
63 | $title = Title::newFromText( $oJson->title ); |
64 | $this->beginTransaction( $dbw, __METHOD__ ); |
65 | if ( $title->exists() ) { |
66 | $revId = $title->getLatestRevID(); |
67 | foreach ( $oJson->relations as $relation ) { |
68 | $dbw->insert( 'mathsemantics', [ |
69 | 'revision_id' => $revId, |
70 | 'identifier' => $relation->identifier, |
71 | 'noun' => $relation->definition, |
72 | 'evidence' => $relation->score |
73 | ] ); |
74 | $this->output( "{$title->getText()}: $relation->identifier is " . |
75 | "$relation->definition certainty $relation->score\n" ); |
76 | } |
77 | } else { |
78 | $this->output( $title->getText() . " does not exist\n" ); |
79 | } |
80 | $this->commitTransaction( $dbw, __METHOD__ ); |
81 | } |
82 | } |
83 | } |
84 | } |
85 | } |
86 | |
87 | $maintClass = ImportDefinitions::class; |
88 | /** @noinspection PhpIncludeInspection */ |
89 | require_once RUN_MAINTENANCE_IF_MAIN; |