Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 43
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoPopulateTableJob
0.00% covered (danger)
0.00%
0 / 43
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 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 run
0.00% covered (danger)
0.00%
0 / 41
0.00% covered (danger)
0.00%
0 / 1
90
1<?php
2/**
3 * Background job to populate the database table for one template using the
4 * data from the call(s) to that template in one page.
5 *
6 * @ingroup Cargo
7 * @author Yaron Koren
8 */
9
10use MediaWiki\Logger\LoggerFactory;
11use MediaWiki\Title\Title;
12
13class CargoPopulateTableJob extends Job {
14
15    /**
16     * @param Title $title
17     * @param array $params
18     */
19    public function __construct( $title, array $params = [] ) {
20        parent::__construct( 'cargoPopulateTable', $title, $params );
21    }
22
23    /**
24     * Run a CargoPopulateTable job.
25     *
26     * @return bool success
27     */
28    public function run() {
29        if ( $this->title === null ) {
30            $this->error = "cargoPopulateTable: Invalid title";
31            return false;
32        }
33
34        $tableName = $this->params['dbTableName'];
35        if ( substr( $tableName, 0, 1 ) == '_' ) {
36            $cdb = CargoUtils::getDB();
37            $possibleReplacementTable = $tableName . '__NEXT';
38            $createReplacement = $cdb->tableExists( $possibleReplacementTable, __METHOD__ );
39            if ( $tableName == '_pageData' ) {
40                CargoPageData::storeValuesForPage( $this->title, $createReplacement );
41            } elseif ( $tableName == '_fileData' ) {
42                CargoFileData::storeValuesForFile( $this->title, $createReplacement );
43            } elseif ( $tableName == '_bpmnData' ) {
44                CargoBPMNData::storeBPMNValues( $this->title, $createReplacement );
45            } elseif ( $tableName == '_ganttData' ) {
46                CargoGanttData::storeGanttValues( $this->title, $createReplacement );
47            } else {
48                $this->error = "cargoPopulateTable: Invalid table " . $tableName;
49                return false;
50            }
51            return true;
52        }
53
54        $page = CargoUtils::makeWikiPage( $this->title );
55
56        if ( !$page->exists() ) {
57            $logger = LoggerFactory::getInstance( 'CargoPopulateTableJob' );
58            $logger->warning(
59                'Ignoring a non-existent page',
60                [
61                    'page_title' => $this->title->getPrefixedDBkey(),
62                    'job_params' => $this->getParams(),
63                    'job_metadata' => $this->getMetadata()
64                ]
65            );
66            return true;
67        }
68
69        // If it was requested, delete all the existing rows for
70        // this page in this Cargo table. This is only necessary
71        // if the table wasn't just dropped and recreated.
72        if ( $this->params['replaceOldRows'] ?? false ) {
73            $cdb = CargoUtils::getDB();
74            $cdb->begin( __METHOD__ );
75            $cdb->delete( $this->params['dbTableName'], [ '_pageID' => $page->getID() ], __METHOD__ );
76            $cdb->commit( __METHOD__ );
77        }
78
79        // All we need to do here is set some global variables based
80        // on the parameters of this job, then parse the page -
81        // the #cargo_store function will take care of the rest.
82        try {
83            CargoStore::$settings['origin'] = 'template';
84            CargoStore::$settings['dbTableName'] = $this->params['dbTableName'];
85            CargoUtils::parsePageForStorage( $this->title, CargoUtils::getContentText( $page->getContent() ) );
86        } finally {
87            // We need to unset this, if the job was called via runJobs.php, so that it doesn't affect other (non-Cargo)
88            // jobs, like page refreshes.
89            // This must be done in a 'finally' block so that if parsePageForStorage throws an exception, the flag is
90            // still cleared.
91            unset( CargoStore::$settings['origin'] );
92        }
93
94        return true;
95    }
96}