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