Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApiQueryProjects
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 6
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getCacheMode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
20
 getAllowedParams
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getExamplesMessages
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 getHelpUrls
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\PageAssessments\Api;
4
5use MediaWiki\Api\ApiQuery;
6use MediaWiki\Api\ApiQueryBase;
7use Wikimedia\ParamValidator\ParamValidator;
8
9/*
10 * API module for retrieving all the projects on a wiki
11 */
12class ApiQueryProjects extends ApiQueryBase {
13
14    public function __construct( ApiQuery $query, string $moduleName ) {
15        parent::__construct( $query, $moduleName, 'pj' );
16    }
17
18    /**
19     * Get the cache mode for the data generated by this module.
20     * @param array $params
21     * @return string
22     */
23    public function getCacheMode( $params ) {
24        return 'public';
25    }
26
27    /**
28     * Evaluate the parameters, perform the requested query, and set up the result
29     */
30    public function execute() {
31        $params = $this->extractRequestParams();
32
33        // Set the database query parameters
34        $this->addTables( [ 'page_assessments_projects' ] );
35        $this->addFields( [ 'project_title' => 'pap_project_title' ] );
36        // If this wiki distinguishes between projects and subprojects, exclude
37        // subprojects (i.e. projects with parents) unless explicitly asked for.
38        if ( $this->getConfig()->get( 'PageAssessmentsSubprojects' ) && !$params['subprojects'] ) {
39            $this->addWhere( [ 'pap_parent_id' => null ] );
40        }
41        $this->addOption( 'ORDER BY', 'pap_project_title' );
42
43        // Execute the query and put the results in an array
44        $db_res = $this->select( __METHOD__ );
45        $projects = [];
46        foreach ( $db_res as $row ) {
47            $projects[] = $row->project_title;
48        }
49
50        // Build the API output
51        $result = $this->getResult();
52        $result->addValue( 'query', 'projects', $projects );
53    }
54
55    /** @inheritDoc */
56    public function getAllowedParams() {
57        $allowedParams = [];
58        if ( $this->getConfig()->get( 'PageAssessmentsSubprojects' ) ) {
59            $allowedParams[ 'subprojects' ] = [
60                ParamValidator::PARAM_DEFAULT => false,
61                ParamValidator::PARAM_TYPE => 'boolean',
62            ];
63        }
64        return $allowedParams;
65    }
66
67    /** @inheritDoc */
68    public function getExamplesMessages() {
69        $exampleMessages = [
70            'action=query&list=projects' => 'apihelp-query+projects-example',
71        ];
72        if ( $this->getConfig()->get( 'PageAssessmentsSubprojects' ) ) {
73            $exampleMessages[ 'action=query&list=projects&pjsubprojects=true' ] =
74                'apihelp-query+projects-example-subprojects';
75        }
76        return $exampleMessages;
77    }
78
79    /** @inheritDoc */
80    public function getHelpUrls() {
81        return 'https://www.mediawiki.org/wiki/Special:MyLanguage/Extension:PageAssessments';
82    }
83}