Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 85
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
createMySqlFunctionDropperClass
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
MySqlFunctionDropper
n/a
0 / 0
n/a
0 / 0
2
n/a
0 / 0
 newInstance
n/a
0 / 0
n/a
0 / 0
1
 dropFunction
n/a
0 / 0
n/a
0 / 0
1
QueryEval
0.00% covered (danger)
0.00%
0 / 80
0.00% covered (danger)
0.00%
0 / 5
156
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
 addExtensionTable
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 createTopicTex
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 1
20
 dropUDFs
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
6
 execute
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
12
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
21use MediaWiki\MediaWikiServices;
22
23require_once __DIR__ . '/../../../maintenance/Maintenance.php';
24
25/**
26 * TODO: Get rid of the workaround
27 */
28// phpcs:ignore MediaWiki.NamingConventions.PrefixedGlobalFunctions
29function createMySqlFunctionDropperClass() {
30    class MySqlFunctionDropper extends MysqlUpdater {
31        /**
32         * @param \Wikimedia\Rdbms\IDatabase $db
33         *
34         * @return MySqlFunctionDropper
35         */
36        public static function newInstance( $db ) {
37            return new MySqlFunctionDropper( $db, false, null );
38        }
39
40        /**
41         * Applies a SQL patch
42         *
43         * @param string $path Path to the patch file
44         * @param bool $isFullPath Whether to treat $path as a relative or not
45         * @param string|null $msg Description of the patch
46         *
47         * @return bool False if patch is skipped.
48         */
49        public function dropFunction( $path, $isFullPath = false, $msg = null ) {
50            return parent::applyPatch( $path, $isFullPath, $msg );
51        }
52    }
53}
54
55class QueryEval extends Maintenance {
56
57    /** @var DatabaseUpdater */
58    private $dbu = null;
59
60    public function __construct() {
61        parent::__construct();
62        $this->addDescription( "Exports submissions to a folder. \n Each run is named after the " .
63            "following convention: \n \$userName-\$runName-\$runId.csv" );
64        $this->addArg( "dir", "The output directory", true );
65        $this->requireExtension( 'MathSearch' );
66    }
67
68    private function addExtensionTable( $name, $folder = '' ) {
69        if ( $this->dbu === null ) {
70            $dbw = MediaWikiServices::getInstance()
71                ->getConnectionProvider()
72                ->getPrimaryDatabase();
73            $this->dbu = DatabaseUpdater::newForDB( $dbw );
74        }
75        $this->dbu->addExtensionTable( $name, __DIR__ . "/../db/wmc/{$folder}{$name}.sql" );
76    }
77
78    /**
79     * @param stdClass $row
80     *
81     * @return string
82     */
83    private function createTopicTex( $row ) {
84        $qId = $row->qId;
85        $row->title = str_replace( [ 'π', 'ő' ], [ '$\\pi$', 'ö' ], $row->title );
86        $tName = $row->qId .
87            ': {\\wikiLink{' . $row->title . '}{' . $row->oldId . '}{' . $row->fId . '}}';
88        $dbr = MediaWikiServices::getInstance()
89            ->getConnectionProvider()
90            ->getReplicaDatabase();
91        $res = $dbr->select( 'math_wmc_freq_hits',
92            [ 'cntRun', 'cntUser', 'links', 'minRank', 'rendering' ],
93            [ 'qId' => $qId ] );
94        $mostFrequent = "\\subsection*{Most frequent results}\n \\begin{enumerate}\n";
95        foreach ( $res as $hit ) {
96            $hit->rendering = str_replace( // TODO: preg_match replaces for by f\lor
97                [ '\\or ', '%', '$\\begin{align}', '\\end{align}$' ],
98                [ '\\lor ', '\\%', '\\begin{align}', '\\end{align}' ], $hit->rendering );
99            $mostFrequent .= "\\item {$hit->rendering} was found by {$hit->cntUser} users in " .
100                " {$hit->cntRun} runs with minimal rank of {$hit->minRank}. \n" .
101                "For example in the context of the following pages: {$hit->links}\n";
102        }
103        $mostFrequent .= "\\end{enumerate}";
104        if ( $row->qVarCount > 0 ) {
105            $relevance = "  \\begin{relevance}[{$row->qVarCount}]
106  $row->rendering
107  \\end{relevance}";
108        } else {
109            $relevance = '';
110        }
111        $individualResults = '';
112        $res = $dbr->select( 'math_wmc_page_ranks', '*', [ 'qId' => $row->qId ] );
113        foreach ( $res as $rank ) {
114            $individualResults .= $rank->runId . ': ' . $rank->rank . '; ';
115        }
116        $out = <<<TEX
117\\begin{topic}{{$tName}}
118\\begin{fquery}
119  \${$row->texQuery}\$
120  \\end{fquery}
121
122\\begin{private}
123  $relevance
124  The reference entry occurs {$row->exactMatches} time(s) in the dataset.
125  In the top 25 results the page {$row->title} occurred {$row->count} times.
126  The rank varied between {$row->min} and {$row->max} with an average of {$row->avg}.
127  In detail the run results are the following: $individualResults
128  $mostFrequent
129\\end{private}
130\\end{topic}
131TEX;
132        return $out;
133    }
134
135    /**
136     * TODO: Replace with DBU call
137     */
138    private function dropUDFs() {
139        $dbw = MediaWikiServices::getInstance()
140            ->getConnectionProvider()
141            ->getPrimaryDatabase();
142        if ( !class_exists( "MySqlFunctionDropper" ) ) {
143            createMySqlFunctionDropperClass();
144        }
145        $dbu = MySqlFunctionDropper::newInstance( $dbw );
146        $dbu->dropFunction( __DIR__ . '/../db/wmc/math_wmc_udf_drop.sql', true,
147            'drop math_wmc udfs' );
148    }
149
150    public function execute() {
151        $dir = $this->getArg( 0 );
152        if ( !is_dir( $dir ) ) {
153            $this->output( "{$dir} is not a directory.\n" );
154            exit( 1 );
155        }
156        MathSearchUtils::createEvaluationTables();
157        $this->addExtensionTable( 'math_wmc_udf_create' );
158        $this->dbu->doUpdates( [ "extensions" ] );
159        $dbr = MediaWikiServices::getInstance()
160            ->getConnectionProvider()
161            ->getReplicaDatabase();
162        // runId INT PRIMARY KEY AUTO_INCREMENT NOT NULL,
163        // runName VARCHAR(45),
164        // userId INT UNSIGNED,
165        // isDraft TINYINT NOT NULL,
166        $res = $dbr->select( "math_wmc_query_summary", '*' );
167        $all = "";
168        mb_internal_encoding( 'UTF-8' );
169        mb_regex_encoding( "UTF-8" );
170        foreach ( $res as $row ) {
171            $all .= $this->createTopicTex( $row ) . "\n\n";
172        }
173        $fh = fopen( $dir . '/all.tex', 'w' );
174        fwrite( $fh, $all );
175        fclose( $fh );
176        echo "all done";
177        $this->dropUDFs();
178        exit( 0 );
179    }
180}
181
182$maintClass = QueryEval::class;
183/** @noinspection PhpIncludeInspection */
184require_once RUN_MAINTENANCE_IF_MAIN;