Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SetCargoPageData
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2
3/**
4 * This script populates the Cargo _pageData DB table (and possibly other
5 * auxiliary tables) for all pages in the wiki.
6 *
7 * Usage:
8 *  php setCargoPageData.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
29if ( 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
35use MediaWiki\MediaWikiServices;
36
37$maintClass = SetCargoPageData::class;
38
39class SetCargoPageData extends Maintenance {
40
41    public function __construct() {
42        parent::__construct();
43
44        $this->requireExtension( 'Cargo' );
45        $this->addDescription( "Stores a set of data for each page in the wiki in one or more database tables, for use within Cargo queries." );
46        $this->addOption( "delete", "Delete the page data DB table(s)", false, false );
47        $this->addOption( 'replacement', 'Put all new data into a replacement table, to be switched in later' );
48    }
49
50    public function execute() {
51        $createReplacement = $this->hasOption( 'replacement' );
52        $pageDataTable = $createReplacement ? '_pageData__NEXT' : '_pageData';
53
54        $lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
55        $dbr = $lb->getConnectionRef( DB_REPLICA );
56        $res = $dbr->select( 'cargo_tables', [ 'field_tables', 'field_helper_tables' ],
57            [ 'main_table' => $pageDataTable ] );
58
59        $numRows = $res->numRows();
60        if ( $numRows > 0 ) {
61            $row = $res->fetchRow();
62            $fieldTables = unserialize( $row['field_tables'] );
63            $fieldHelperTables = unserialize( $row['field_helper_tables'] );
64            SpecialDeleteCargoTable::deleteTable( $pageDataTable, $fieldTables, $fieldHelperTables );
65        }
66
67        if ( $this->getOption( "delete" ) ) {
68            if ( $numRows > 0 ) {
69                $this->output( "\n Deleted page data table(s).\n" );
70            } else {
71                $this->output( "\n No page data tables found; exiting.\n" );
72            }
73            return;
74        }
75
76        $tableSchema = CargoPageData::getTableSchema();
77        $tableSchemaString = $tableSchema->toDBString();
78
79        $cdb = CargoUtils::getDB();
80        $dbw = CargoUtils::getMainDBForWrite();
81        CargoUtils::createCargoTableOrTables( $cdb, $dbw, $pageDataTable, $tableSchema, $tableSchemaString, 0 );
82
83        $pages = $dbr->select( 'page', [ 'page_id' ] );
84
85        foreach ( $pages as $page ) {
86            $title = Title::newFromID( $page->page_id );
87            if ( $title == null ) {
88                continue;
89            }
90            try {
91                CargoPageData::storeValuesForPage( $title, $createReplacement );
92            } catch ( MWException $e ) {
93                // This can happen if, for example, there's a
94                // page with a content type that is no longer
95                // supported.
96                continue;
97            }
98            $this->output( wfTimestamp( TS_DB ) . ' Stored page data for page "' . $title->getFullText() . "\".\n" );
99        }
100
101        $this->output( "\n Finished populating page data table(s).\n" );
102
103        $user = User::newSystemUser( 'Maintenance script', [ 'steal' => true ] );
104        if ( $numRows >= 0 ) {
105            CargoUtils::logTableAction( 'recreatetable', $pageDataTable, $user );
106        } else {
107            CargoUtils::logTableAction( 'createtable', $pageDataTable, $user );
108        }
109    }
110
111}
112
113require_once RUN_MAINTENANCE_IF_MAIN;