Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialDisplayTopics
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 7
272
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
 execute
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 displayOverview
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
6
 displayTopic
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 printMostFrequentRuns
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
12
 printIndividualResults
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
12
 getGroupName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5/**
6 * Lets the user import a CSV file with the results
7 *
8 * @author Moritz Schubotz
9 */
10class SpecialDisplayTopics extends SpecialPage {
11
12    /**
13     * @param string $name
14     */
15    public function __construct( $name = 'DisplayTopics' ) {
16        $listed = (bool)$this->getConfig()->get( 'MathWmcServer' );
17        parent::__construct( $name, 'mathwmcsubmit', $listed );
18    }
19
20    /**
21     * @param null|string $query
22     *
23     * @throws PermissionsError
24     */
25    function execute( $query ) {
26        $this->setHeaders();
27        if ( !( $this->getUser()->isAllowed( 'mathwmcsubmit' ) ) ) {
28            throw new PermissionsError( 'mathwmcsubmit' );
29        }
30        $this->getOutput()->addWikiTextAsInterface( $this->msg( 'math-wmc-Queries' )->text() );
31        if ( $query ) {
32            $this->displayTopic( $query );
33        } else {
34            $this->displayOverview();
35        }
36    }
37
38    private function displayOverview( $filter = 1 ) {
39        // If $wgMathWmcServer is unset there's no math_wmc_ref table to query
40        if ( !$this->getConfig()->get( 'MathWmcServer' ) ) {
41            return;
42        }
43
44        $dbw = MediaWikiServices::getInstance()
45            ->getConnectionProvider()
46            ->getPrimaryDatabase();
47        $cols = [ '#', 'fId', '#Var', '#matches', 'query', 'reference' ];
48        $res = $dbw->query( <<<SQL
49SELECT
50  concat( '[[{{FULLPAGENAME}}/',qId,'|',qId,']]'),
51  concat('[[Special:Permalink/', oldId, '#math.', oldId, '.', fId, '|', p.page_title, '-',fId, ']]'),
52  qVarCount,
53  count( mathindex_revision_id ),
54  concat('<mquery>', texQuery, '</mquery>' ),
55  concat('<math>', math_input, '</math>')
56FROM math_wmc_ref ref
57  NATURAL JOIN mathlog L
58  JOIN revision rev ON ref.oldId = rev.rev_id
59  JOIN page p on rev.rev_page = p.page_id
60  JOIN mathindex ON mathindex_inputhash = math_inputhash
61  WHERE $filter
62  GROUP BY qId
63SQL
64        );
65        $this->getOutput()->addWikiTextAsInterface( MathSearchUtils::dbRowToWikiTable( $res, $cols ) );
66    }
67
68    private function displayTopic( $query ) {
69        // If $wgMathWmcServer is unset there's no math_wmc_ref table to query
70        if ( !$this->getConfig()->get( 'MathWmcServer' ) ) {
71            return;
72        }
73
74        $out = $this->getOutput();
75        $dbr = MediaWikiServices::getInstance()
76            ->getConnectionProvider()
77            ->getReplicaDatabase();
78        $qId = $dbr->selectField( 'math_wmc_ref', 'qId', [ 'qID' => $query ] );
79        if ( !$qId ) {
80            $out->addWikiTextAsInterface( "Topic $query does not exist." );
81            return;
82        }
83        $this->displayOverview( "qID = $qId" );
84        $this->printMostFrequentRuns( $qId );
85        $this->printIndividualResults( $qId );
86    }
87
88    /**
89     * @param int $qId
90     */
91    private function printMostFrequentRuns( $qId ) {
92        $out = $this->getOutput();
93        $dbr = MediaWikiServices::getInstance()
94            ->getConnectionProvider()
95            ->getReplicaDatabase();
96        $res = $dbr->query( "select
97              math_input as            rendering,
98              count(distinct runs.userId) cntUser,
99              count(distinct runs.runId)  cntRun,
100              min(`rank`)                 minRank
101            from
102              math_wmc_results r
103              join mathlog l ON r.math_inputhash = l.math_inputhash
104              join math_wmc_runs runs ON r.runId = runs.runId
105            where
106              r.qId = $qId
107              and r.runId in (select runId from math_wmc_runs WHERE isDraft <> 1 and pageOnly <> 1)
108            group by r.math_inputhash
109            having min(rank) < 50
110            order by count(distinct runs.userId) desc, min(rank) asc
111            Limit 15"
112        );
113        $out->addWikiTextAsInterface( "== Most frequent results ==" );
114        foreach ( $res as $hit ) {
115            $out->addWikiTextAsInterface(
116                "*<math>{$hit->rendering}</math>  was found by {$hit->cntUser} users in " .
117                " {$hit->cntRun} runs with minimal rank of {$hit->minRank} \n"
118            );
119            $mo = new MathObject( $hit->rendering );
120            $all = $mo->getAllOccurrences();
121            foreach ( $all as  $occ ) {
122                $out->addWikiTextAsInterface( '*' . $occ->printLink2Page( false ) );
123            }
124        }
125    }
126
127    private function printIndividualResults( $qId ) {
128        $out = $this->getOutput();
129        $out->addWikiTextAsInterface( "== Individual results ==" );
130        $dbr = MediaWikiServices::getInstance()
131            ->getConnectionProvider()
132            ->getReplicaDatabase();
133        if ( !$dbr->tableExists( 'math_wmc_page_ranks' ) ) {
134            MathSearchUtils::createEvaluationTables();
135        }
136        $res = $dbr->select( 'math_wmc_page_ranks', '*', [ 'qId' => $qId ] );
137        foreach ( $res as $rank ) {
138            $out->addWikiTextAsInterface( $rank->runId . ': ' . $rank->rank );
139        }
140    }
141
142    protected function getGroupName() {
143        return 'mathsearch';
144    }
145}