Completed
Branch master (939199)
by
unknown
39:35
created

includes/specials/SpecialActiveusers.php (1 issue)

a variable is defined regardless of execution path.

Bug Major

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Implements Special:Activeusers
4
 *
5
 * Copyright © 2008 Aaron Schulz
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 2 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License along
18
 * with this program; if not, write to the Free Software Foundation, Inc.,
19
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
 * http://www.gnu.org/copyleft/gpl.html
21
 *
22
 * @file
23
 * @ingroup SpecialPage
24
 */
25
26
/**
27
 * Implements Special:Activeusers
28
 *
29
 * @ingroup SpecialPage
30
 */
31
class SpecialActiveUsers extends SpecialPage {
32
33
	/**
34
	 * Constructor
35
	 */
36
	public function __construct() {
37
		parent::__construct( 'Activeusers' );
38
	}
39
40
	/**
41
	 * Show the special page
42
	 *
43
	 * @param string $par Parameter passed to the page or null
44
	 */
45
	public function execute( $par ) {
46
		$out = $this->getOutput();
47
48
		$this->setHeaders();
49
		$this->outputHeader();
50
51
		$opts = new FormOptions();
52
53
		$opts->add( 'username', '' );
54
		$opts->add( 'groups', [] );
55
56
		$opts->fetchValuesFromRequest( $this->getRequest() );
57
58
		if ( $par !== null ) {
59
			$opts->setValue( 'username', $par );
60
		}
61
62
		$pager = new ActiveUsersPager( $this->getContext(), $opts );
63
		$usersBody = $pager->getBody();
64
65
		$this->buildForm();
66
67
		if ( $usersBody ) {
68
			$out->addHTML(
69
				$pager->getNavigationBar() .
70
				Html::rawElement( 'ul', [], $usersBody ) .
71
				$pager->getNavigationBar()
72
			);
73
		} else {
74
			$out->addWikiMsg( 'activeusers-noresult' );
75
		}
76
	}
77
78
	/**
79
	 * Generate and output the form
80
	 */
81
	protected function buildForm() {
82
		$groups = User::getAllGroups();
83
84
		foreach ( $groups as $group ) {
85
			$msg = User::getGroupName( $group );
86
			$options[$msg] = $group;
87
		}
88
89
		$formDescriptor = [
90
			'username' => [
91
				'type' => 'user',
92
				'name' => 'username',
93
				'label-message' => 'activeusers-from',
94
			],
95
96
			'groups' => [
97
				'type' => 'multiselect',
98
				'dropdown' => true,
99
				'flatlist' => true,
100
				'name' => 'groups',
101
				'label-message' => 'activeusers-groups',
102
				'options' => $options,
0 ignored issues
show
The variable $options does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
103
			],
104
		];
105
106
		HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
107
			// For the 'multiselect' field values to be preserved on submit
108
			->setFormIdentifier( 'specialactiveusers' )
109
			->setIntro( $this->getIntroText() )
110
			->setWrapperLegendMsg( 'activeusers' )
111
			->setSubmitTextMsg( 'activeusers-submit' )
112
			// prevent setting subpage and 'username' parameter at the same time
113
			->setAction( $this->getPageTitle()->getLocalURL() )
114
			->setMethod( 'get' )
115
			->prepareForm()
116
			->displayForm( false );
117
	}
118
119
	/**
120
	 * Return introductory message.
121
	 * @return string
122
	 */
123
	protected function getIntroText() {
124
		$days = $this->getConfig()->get( 'ActiveUserDays' );
125
126
		$intro = $this->msg( 'activeusers-intro' )->numParams( $days )->parse();
127
128
		// Mention the level of cache staleness...
129
		$dbr = wfGetDB( DB_REPLICA, 'recentchanges' );
130
		$rcMax = $dbr->selectField( 'recentchanges', 'MAX(rc_timestamp)', '', __METHOD__ );
131
		if ( $rcMax ) {
132
			$cTime = $dbr->selectField( 'querycache_info',
133
				'qci_timestamp',
134
				[ 'qci_type' => 'activeusers' ],
135
				__METHOD__
136
			);
137
			if ( $cTime ) {
138
				$secondsOld = wfTimestamp( TS_UNIX, $rcMax ) - wfTimestamp( TS_UNIX, $cTime );
139
			} else {
140
				$rcMin = $dbr->selectField( 'recentchanges', 'MIN(rc_timestamp)' );
141
				$secondsOld = time() - wfTimestamp( TS_UNIX, $rcMin );
142
			}
143
			if ( $secondsOld > 0 ) {
144
				$intro .= $this->msg( 'cachedspecial-viewing-cached-ttl' )
145
					->durationParams( $secondsOld )->parseAsBlock();
146
			}
147
		}
148
149
		return $intro;
150
	}
151
152
	protected function getGroupName() {
153
		return 'users';
154
	}
155
}
156