Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
SetCargoFileData | |
0.00% |
0 / 38 |
|
0.00% |
0 / 2 |
90 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
72 |
1 | <?php |
2 | |
3 | /** |
4 | * This script populates the Cargo _fileData DB table (and possibly other |
5 | * auxiliary tables) for all pages in the wiki. |
6 | * |
7 | * Usage: |
8 | * php setCargoFileData.php --delete --replacement |
9 | * |
10 | * This program is free software; you can redistribute it and/or modify |
11 | * it under the terms of the GNU General Public License as published by |
12 | * the Free Software Foundation; either version 2 of the License, or |
13 | * (at your option) any later version. |
14 | * |
15 | * This program is distributed in the hope that it will be useful, |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
18 | * GNU General Public License for more details. |
19 | * |
20 | * You should have received a copy of the GNU General Public License along |
21 | * with this program; if not, write to the Free Software Foundation, Inc., |
22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
23 | * http://www.gnu.org/copyleft/gpl.html |
24 | * |
25 | * @author Yaron Koren |
26 | * @ingroup Maintenance |
27 | */ |
28 | |
29 | if ( getenv( 'MW_INSTALL_PATH' ) ) { |
30 | require_once getenv( 'MW_INSTALL_PATH' ) . '/maintenance/Maintenance.php'; |
31 | } else { |
32 | require_once __DIR__ . '/../../../maintenance/Maintenance.php'; |
33 | } |
34 | |
35 | $maintClass = SetCargoFileData::class; |
36 | |
37 | class SetCargoFileData extends Maintenance { |
38 | |
39 | public function __construct() { |
40 | parent::__construct(); |
41 | |
42 | $this->requireExtension( 'Cargo' ); |
43 | $this->addDescription( "Stores a set of data for each file in the wiki in one or more database tables, for use within Cargo queries." ); |
44 | $this->addOption( "delete", "Delete the file data DB table(s)", false, false ); |
45 | $this->addOption( 'replacement', 'Put all new data into a replacement table, to be switched in later' ); |
46 | } |
47 | |
48 | public function execute() { |
49 | $createReplacement = $this->hasOption( 'replacement' ); |
50 | $fileDataTable = $createReplacement ? '_fileData__NEXT' : '_fileData'; |
51 | |
52 | $dbr = CargoUtils::getMainDBForRead(); |
53 | $res = $dbr->select( 'cargo_tables', [ 'field_tables', 'field_helper_tables' ], |
54 | [ 'main_table' => $fileDataTable ], __METHOD__ ); |
55 | |
56 | $numRows = $res->numRows(); |
57 | if ( $numRows >= 0 ) { |
58 | $row = $res->fetchRow(); |
59 | $fieldTables = unserialize( $row['field_tables'] ); |
60 | $fieldHelperTables = unserialize( $row['field_helper_tables'] ); |
61 | SpecialDeleteCargoTable::deleteTable( $fileDataTable, $fieldTables, $fieldHelperTables ); |
62 | } |
63 | |
64 | if ( $this->getOption( "delete" ) ) { |
65 | if ( $numRows > 0 ) { |
66 | $this->output( "\n Deleted file data table(s).\n" ); |
67 | } else { |
68 | $this->output( "\n No file data tables found; exiting.\n" ); |
69 | } |
70 | return; |
71 | } |
72 | |
73 | $tableSchema = CargoFileData::getTableSchema(); |
74 | $tableSchemaString = $tableSchema->toDBString(); |
75 | |
76 | $cdb = CargoUtils::getDB(); |
77 | $dbw = CargoUtils::getMainDBForWrite(); |
78 | CargoUtils::createCargoTableOrTables( $cdb, $dbw, $fileDataTable, $tableSchema, $tableSchemaString, -1 ); |
79 | |
80 | $pages = $dbr->select( 'page', [ 'page_id' ], 'page_namespace = ' . NS_FILE, __METHOD__ ); |
81 | |
82 | foreach ( $pages as $page ) { |
83 | $title = Title::newFromID( $page->page_id ); |
84 | if ( $title == null ) { |
85 | continue; |
86 | } |
87 | CargoFileData::storeValuesForFile( $title, $createReplacement ); |
88 | $this->output( wfTimestamp( TS_DB ) . ' Stored file data for page "' . $title->getFullText() . "\".\n" ); |
89 | } |
90 | |
91 | $this->output( "\n Finished populating file data table(s).\n" ); |
92 | |
93 | $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] ); |
94 | if ( $numRows >= 0 ) { |
95 | CargoUtils::logTableAction( 'recreatetable', $fileDataTable, $user ); |
96 | } else { |
97 | CargoUtils::logTableAction( 'createtable', $fileDataTable, $user ); |
98 | } |
99 | } |
100 | |
101 | } |
102 | |
103 | require_once RUN_MAINTENANCE_IF_MAIN; |