Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 57
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
GenerateFeatureTable
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 populateSearchIndex
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
42
 doUpdate
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
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
21require_once __DIR__ . '/../../../maintenance/Maintenance.php';
22
23class GenerateFeatureTable extends Maintenance {
24
25    private const RTI_CHUNK_SIZE = 100000;
26
27    /** @var bool */
28    private $purge = false;
29    /** @var \Wikimedia\Rdbms\IDatabase */
30    private $dbw = null;
31
32    /**
33     * @var \Wikimedia\Rdbms\IDatabase
34     */
35    private $db;
36
37    public function __construct() {
38        parent::__construct();
39        $this->addDescription( 'Outputs page text to stdout' );
40        $this->addOption(
41            'purge', "If set all formulae are rendered again from strech. (Very time consuming!)",
42            false, false, "f"
43        );
44        $this->addArg( 'min', "If set processing is started at the page with rank(pageID)>min", false );
45        $this->addArg( 'max', "If set processing is stopped at the page with rank(pageID)<=max", false );
46        $this->requireExtension( 'MathSearch' );
47    }
48
49    /**
50     * Populates the search index with content from all pages
51     *
52     * @param int $n
53     * @param int $cmax
54     */
55    protected function populateSearchIndex( $n = 0, $cmax = -1 ) {
56        $s = $this->db->selectRow( 'page', 'MAX(page_id) AS count' );
57        $count = $s->count;
58        if ( $cmax > 0 && $count > $cmax ) {
59            $count = $cmax;
60        }
61        # $this->output( "Rebuilding index fields for {$count} pages with option {$this->purge}...\n" );
62        $fcount = 0;
63
64        while ( $n < $count ) {
65            if ( $n ) {
66                $this->output( $n . " of $count \n" );
67            }
68            $end = $n + self::RTI_CHUNK_SIZE - 1;
69
70            $res =
71                $this->db->select( [ 'page', 'revision', 'text' ], [ 'page_id' ], [
72                        "page_id BETWEEN $n AND $end",
73                        'page_latest = rev_id',
74                        'rev_text_id = old_id'
75                ], __METHOD__ );
76            $this->beginTransaction( $this->dbw, __METHOD__ );
77            // echo "before" +$this->dbw->selectField('mathindex', 'count(*)')."\n";
78            foreach ( $res as $s ) {
79                $fcount += $this->doUpdate( $s->page_id );
80            }
81            $this->commitTransaction( $this->dbw, __METHOD__ );
82            $n += self::RTI_CHUNK_SIZE;
83        }
84        // $this->output( "Updated {$fcount} formulae!\n" );
85    }
86
87    /**
88     * @param int $pid
89     *
90     * @return number
91     */
92    private function doUpdate( $pid ) {
93        // TODO: fix link id problem
94        $res =
95            $this->db->select( [ 'mathrevisionstat', 'mathvarstat' ], [
96                    'revstat_revid',
97                    'pagestat_featurename',
98                    'pagestat_featuretype',
99                    'revstat_featurecount',
100                    'varstat_id',
101                    'varstat_featurecount'
102            ], [
103                    'revstat_revid' => $pid,
104                    'pagestat_featurename = varstat_featurename',
105                    'pagestat_featuretype=varstat_featuretype'
106            ], __METHOD__ );
107        foreach ( $res as $row ) {
108            $this->output( $pid . ',' . $row->varstat_id . ',' . $row->pagestat_featurecount
109                           /// $row->varstat_featurecount
110                           .
111                           "\n" );// .';'.$row->pagestat_featuretype.$row->pagestat_featurename."\n");
112        }
113        return 0;
114    }
115
116    public function execute() {
117        $connectionProvider = $this->getServiceContainer()->getConnectionProvider();
118        $this->dbw = $connectionProvider->getPrimaryDatabase();
119        $this->purge = $this->getOption( "purge", false );
120        $this->db = $connectionProvider->getPrimaryDatabase();
121        $this->populateSearchIndex( $this->getArg( 0, 0 ), $this->getArg( 1, -1 ) );
122    }
123}
124
125$maintClass = GenerateFeatureTable::class;
126/** @noinspection PhpIncludeInspection */
127require_once RUN_MAINTENANCE_IF_MAIN;