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

includes/specials/SpecialActiveusers.php (1 issue)

lazy-initialization of arrays.

Coding Style Comprehensibility Informational

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;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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,
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