Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 54
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExportMathCache
0.00% covered (danger)
0.00%
0 / 49
0.00% covered (danger)
0.00%
0 / 3
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 getMathTagsFromDatabase
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
20
 execute
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
12
1#!/usr/bin/env php
2<?php
3/**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @ingroup Maintenance
20 */
21
22use MediaWiki\MediaWikiServices;
23
24require_once __DIR__ . '/../../../maintenance/Maintenance.php';
25
26class ExportMathCache extends Maintenance {
27
28    private const ERROR_CODE_DB_ERROR = 2;
29    private const ERROR_CODE_JSON = 3;
30
31    public function __construct() {
32        parent::__construct();
33        $this->addDescription( 'Exports a json file that consists of the input hashes and ' .
34            'the texvc input from the database cache.' );
35        $this->addOption(
36            'offset', "If set the first n equations on the table are skipped", false, true, "o"
37        );
38        $this->addOption(
39            'length', "If set the only n equations are exported processed", false, true, "l"
40        );
41        $this->addOption(
42            'sort', 'If set the result is sorted according to the input', false, false, 's'
43        );
44        $this->requireExtension( 'MathSearch' );
45    }
46
47    /**
48     * @param int $offset
49     * @param int $length
50     * @param bool $sort
51     * @return array|false
52     */
53    private static function getMathTagsFromDatabase(
54        int $offset,
55        int $length,
56        bool $sort ) {
57        $out = [];
58        $dbr = MediaWikiServices::getInstance()
59            ->getConnectionProvider()
60            ->getReplicaDatabase();
61        $options = [
62            'OFFSET'   => $offset,
63            'LIMIT'    => $length
64        ];
65        if ( $sort === true ) {
66            $options['ORDER BY'] = 'math_input';
67        }
68        $res = $dbr->select(
69            'mathlog',
70            [ 'math_inputhash', 'math_input' ],
71            '',
72            __METHOD__,
73            $options );
74        if ( $res === false ) {
75            return false;
76        }
77        // Convert result wrapper to array
78        foreach ( $res as $row ) {
79            $out[] = [
80                // the binary encoded input-hash is no valid json output
81                'inputhash' => MathObject::hash2md5( $row->math_inputhash ),
82                'input'     => $row->math_input
83            ];
84        }
85        return $out;
86    }
87
88    public function execute() {
89        $offset = $this->getOption( 'offset', 0 );
90        $length = $this->getOption( 'length', PHP_INT_MAX );
91        $sort = $this->hasOption( 'sort' );
92        $allEquations = self::getMathTagsFromDatabase( $offset, $length, $sort );
93        if ( !is_array( $allEquations ) ) {
94            $this->error( "Could not get equations from table 'mathlog'",
95                self::ERROR_CODE_DB_ERROR );
96        }
97        $out = FormatJson::encode( $allEquations, true );
98        if ( $out === false ) {
99            $this->error( "Could not encode result as json string 'mathlog'",
100                self::ERROR_CODE_JSON );
101        }
102        $this->output( "$out\n" );
103    }
104}
105
106$maintClass = ExportMathCache::class;
107require_once RUN_MAINTENANCE_IF_MAIN;