1<?php
2/**
3 * Implements Special:Protectedpages
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24use MediaWiki\Cache\LinkBatchFactory;
25use Wikimedia\Rdbms\ILoadBalancer;
26
27/**
28 * A special page that lists protected pages
29 *
30 * @ingroup SpecialPage
31 */
32class SpecialProtectedpages extends SpecialPage {
33	protected $IdLevel = 'level';
34	protected $IdType = 'type';
35
36	/** @var LinkBatchFactory */
37	private $linkBatchFactory;
38
39	/** @var ILoadBalancer */
40	private $loadBalancer;
41
42	/** @var CommentStore */
43	private $commentStore;
44
45	/** @var ActorMigration */
46	private $actorMigration;
47
48	/** @var UserCache */
49	private $userCache;
50
51	/**
52	 * @param LinkBatchFactory $linkBatchFactory
53	 * @param ILoadBalancer $loadBalancer
54	 * @param CommentStore $commentStore
55	 * @param ActorMigration $actorMigration
56	 * @param UserCache $userCache
57	 */
58	public function __construct(
59		LinkBatchFactory $linkBatchFactory,
60		ILoadBalancer $loadBalancer,
61		CommentStore $commentStore,
62		ActorMigration $actorMigration,
63		UserCache $userCache
64	) {
65		parent::__construct( 'Protectedpages' );
66		$this->linkBatchFactory = $linkBatchFactory;
67		$this->loadBalancer = $loadBalancer;
68		$this->commentStore = $commentStore;
69		$this->actorMigration = $actorMigration;
70		$this->userCache = $userCache;
71	}
72
73	public function execute( $par ) {
74		$this->setHeaders();
75		$this->outputHeader();
76		$this->getOutput()->addModuleStyles( 'mediawiki.special' );
77		$this->addHelpLink( 'Help:Protected_pages' );
78
79		$request = $this->getRequest();
80		$type = $request->getVal( $this->IdType );
81		$level = $request->getVal( $this->IdLevel );
82		$sizetype = $request->getVal( 'size-mode' );
83		$size = $request->getIntOrNull( 'size' );
84		$ns = $request->getIntOrNull( 'namespace' );
85
86		$filters = $request->getArray( 'wpfilters', [] );
87		$indefOnly = in_array( 'indefonly', $filters );
88		$cascadeOnly = in_array( 'cascadeonly', $filters );
89		$noRedirect = in_array( 'noredirect', $filters );
90
91		$pager = new ProtectedPagesPager(
92			$this,
93			[],
94			$type,
95			$level,
96			$ns,
97			$sizetype,
98			$size,
99			$indefOnly,
100			$cascadeOnly,
101			$noRedirect,
102			$this->getLinkRenderer(),
103			$this->linkBatchFactory,
104			$this->loadBalancer,
105			$this->commentStore,
106			$this->actorMigration,
107			$this->userCache
108		);
109
110		$this->getOutput()->addHTML( $this->showOptions(
111			$ns,
112			$type,
113			$level,
114			$sizetype,
115			$size,
116			$filters
117		) );
118
119		if ( $pager->getNumRows() ) {
120			$this->getOutput()->addParserOutputContent( $pager->getFullOutput() );
121		} else {
122			$this->getOutput()->addWikiMsg( 'protectedpagesempty' );
123		}
124	}
125
126	/**
127	 * @param int $namespace
128	 * @param string $type Restriction type
129	 * @param string $level Restriction level
130	 * @param string $sizetype "min" or "max"
131	 * @param int $size
132	 * @param array $filters Filters set for the pager: indefOnly,
133	 *   cascadeOnly, noRedirect
134	 * @return string Input form
135	 */
136	protected function showOptions( $namespace, $type, $level, $sizetype,
137		$size, $filters
138	) {
139		$formDescriptor = [
140			'namespace' => [
141				'class' => HTMLSelectNamespace::class,
142				'name' => 'namespace',
143				'id' => 'namespace',
144				'cssclass' => 'namespaceselector',
145				'all' => '',
146				'label' => $this->msg( 'namespace' )->text(),
147			],
148			'typemenu' => $this->getTypeMenu( $type ),
149			'levelmenu' => $this->getLevelMenu( $level ),
150			'filters' => [
151				'class' => HTMLMultiSelectField::class,
152				'label' => $this->msg( 'protectedpages-filters' )->text(),
153				'flatlist' => true,
154				'options-messages' => [
155					'protectedpages-indef' => 'indefonly',
156					'protectedpages-cascade' => 'cascadeonly',
157					'protectedpages-noredirect' => 'noredirect',
158				],
159				'default' => $filters,
160			],
161			'sizelimit' => [
162				'class' => HTMLSizeFilterField::class,
163				'name' => 'size',
164			]
165		];
166		$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() )
167			->setMethod( 'get' )
168			->setWrapperLegendMsg( 'protectedpages' )
169			->setSubmitText( $this->msg( 'protectedpages-submit' )->text() );
170
171		return $htmlForm->prepareForm()->getHTML( false );
172	}
173
174	/**
175	 * Creates the input label of the restriction type
176	 * @param string $pr_type Protection type
177	 * @return array
178	 */
179	protected function getTypeMenu( $pr_type ) {
180		$m = []; // Temporary array
181		$options = [];
182
183		// First pass to load the log names
184		foreach ( Title::getFilteredRestrictionTypes( true ) as $type ) {
185			// Messages: restriction-edit, restriction-move, restriction-create, restriction-upload
186			$text = $this->msg( "restriction-$type" )->text();
187			$m[$text] = $type;
188		}
189
190		// Third pass generates sorted XHTML content
191		foreach ( $m as $text => $type ) {
192			$options[$text] = $type;
193		}
194
195		return [
196			'type' => 'select',
197			'options' => $options,
198			'label' => $this->msg( 'restriction-type' )->text(),
199			'name' => $this->IdType,
200			'id' => $this->IdType,
201		];
202	}
203
204	/**
205	 * Creates the input label of the restriction level
206	 * @param string $pr_level Protection level
207	 * @return array
208	 */
209	protected function getLevelMenu( $pr_level ) {
210		// Temporary array
211		$m = [ $this->msg( 'restriction-level-all' )->text() => 0 ];
212		$options = [];
213
214		// First pass to load the log names
215		foreach ( $this->getConfig()->get( 'RestrictionLevels' ) as $type ) {
216			// Messages used can be 'restriction-level-sysop' and 'restriction-level-autoconfirmed'
217			if ( $type != '' && $type != '*' ) {
218				$text = $this->msg( "restriction-level-$type" )->text();
219				$m[$text] = $type;
220			}
221		}
222
223		// Third pass generates sorted XHTML content
224		foreach ( $m as $text => $type ) {
225			$options[$text] = $type;
226		}
227
228		return [
229			'type' => 'select',
230			'options' => $options,
231			'label' => $this->msg( 'restriction-level' )->text(),
232			'name' => $this->IdLevel,
233			'id' => $this->IdLevel
234		];
235	}
236
237	protected function getGroupName() {
238		return 'maintenance';
239	}
240}
241