Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
QueryHelper
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
2 / 2
5
100.00% covered (success)
100.00%
1 / 1
 matchPage
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
3
 matchCategory
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace CirrusSearch\Query;
4
5use MediaWiki\Category\Category;
6use MediaWiki\Title\Title;
7
8/**
9 * helpers for building queries
10 */
11class QueryHelper {
12    /**
13     * Builds a match query against $field for $title. $title is munged to make
14     * title matching better more intuitive for users.
15     *
16     * @param string $field field containing the title
17     * @param string $title title query text to match against
18     * @param bool $underscores If the field contains underscores instead of
19     *  spaces. Defaults to false.
20     * @return \Elastica\Query\MatchQuery For matching $title to $field
21     */
22    public static function matchPage( $field, $title, $underscores = false ) {
23        $t = Title::newFromText( $title );
24        if ( $t ) {
25            $title = $t->getPrefixedText();
26        }
27        if ( $underscores ) {
28            $title = str_replace( ' ', '_', $title );
29        }
30        $match = new \Elastica\Query\MatchQuery();
31        $match->setFieldQuery( $field, $title );
32
33        return $match;
34    }
35
36    /**
37     * Builds a match query against $field for $name. $name is munged to make
38     * category matching better more intuitive for users.
39     *
40     * @param string $field field containing the title
41     * @param string $name title query text to match against
42     *  spaces. Defaults to false.
43     * @return \Elastica\Query\MatchQuery For matching $title to $field
44     */
45    public static function matchCategory( $field, $name ): \Elastica\Query\MatchQuery {
46        $c = Category::newFromName( $name );
47        if ( $c ) {
48            $name = $c->getName();
49        }
50
51        $name = str_replace( '_', ' ', $name );
52        $match = new \Elastica\Query\MatchQuery();
53        $match->setFieldQuery( $field, $name );
54
55        return $match;
56    }
57}