Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / specials / SpecialPagesWithProp.php
blob3af95460d329df57d0f3289c640bdb4305eb6dd9
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Specials;
23 use MediaWiki\Html\Html;
24 use MediaWiki\HTMLForm\HTMLForm;
25 use MediaWiki\SpecialPage\QueryPage;
26 use MediaWiki\Title\Title;
27 use Skin;
28 use stdClass;
29 use Wikimedia\Rdbms\IConnectionProvider;
31 /**
32 * Special:PagesWithProp to search the page_props table
34 * @ingroup SpecialPage
35 * @since 1.21
37 class SpecialPagesWithProp extends QueryPage {
39 /**
40 * @var string|null
42 private $propName = null;
44 /**
45 * @var string[]|null
47 private $existingPropNames = null;
49 /**
50 * @var int|null
52 private $ns;
54 /**
55 * @var bool
57 private $reverse = false;
59 /**
60 * @var bool
62 private $sortByValue = false;
64 /**
65 * @param IConnectionProvider $dbProvider
67 public function __construct( IConnectionProvider $dbProvider ) {
68 parent::__construct( 'PagesWithProp' );
69 $this->setDatabaseProvider( $dbProvider );
72 public function isCacheable() {
73 return false;
76 public function execute( $par ) {
77 $this->setHeaders();
78 $this->outputHeader();
79 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
81 $request = $this->getRequest();
82 $propname = $request->getVal( 'propname', $par );
83 $this->ns = $request->getIntOrNull( 'namespace' );
84 $this->reverse = $request->getBool( 'reverse' );
85 $this->sortByValue = $request->getBool( 'sortbyvalue' );
87 $propnames = $this->getExistingPropNames();
89 $fields = [
90 'propname' => [
91 'type' => 'combobox',
92 'name' => 'propname',
93 'options' => $propnames,
94 'default' => $propname,
95 'label-message' => 'pageswithprop-prop',
96 'required' => true,
98 'namespace' => [
99 'type' => 'namespaceselect',
100 'name' => 'namespace',
101 'label-message' => 'namespace',
102 'all' => '',
103 'default' => $this->ns,
105 'reverse' => [
106 'type' => 'check',
107 'name' => 'reverse',
108 'default' => $this->reverse,
109 'label-message' => 'pageswithprop-reverse',
110 'required' => false,
112 'sortbyvalue' => [
113 'type' => 'check',
114 'name' => 'sortbyvalue',
115 'default' => $this->sortByValue,
116 'label-message' => 'pageswithprop-sortbyvalue',
117 'required' => false,
121 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() )
122 ->setMethod( 'get' )
123 ->setTitle( $this->getPageTitle() ) // Remove subpage
124 ->setSubmitCallback( [ $this, 'onSubmit' ] )
125 ->setWrapperLegendMsg( 'pageswithprop-legend' )
126 ->addHeaderHtml( $this->msg( 'pageswithprop-text' )->parseAsBlock() )
127 ->setSubmitTextMsg( 'pageswithprop-submit' )
128 ->prepareForm();
129 $form->displayForm( false );
130 if ( $propname !== '' && $propname !== null ) {
131 $form->trySubmit();
135 public function onSubmit( $data, $form ) {
136 $this->propName = $data['propname'];
137 parent::execute( $data['propname'] );
141 * Return an array of subpages beginning with $search that this special page will accept.
143 * @param string $search Prefix to search for
144 * @param int $limit Maximum number of results to return
145 * @param int $offset Number of pages to skip
146 * @return string[] Matching subpages
148 public function prefixSearchSubpages( $search, $limit, $offset ) {
149 $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
150 // We've already limited and offsetted, set to N and 0 respectively.
151 return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
155 * Disable RSS/Atom feeds
156 * @return bool
158 public function isSyndicated() {
159 return false;
163 * @inheritDoc
165 protected function linkParameters() {
166 $params = [
167 'reverse' => $this->reverse,
168 'sortbyvalue' => $this->sortByValue,
170 if ( $this->ns !== null ) {
171 $params['namespace'] = $this->ns;
173 return $params;
176 public function getQueryInfo() {
177 $query = [
178 'tables' => [ 'page_props', 'page' ],
179 'fields' => [
180 'page_id' => 'pp_page',
181 'page_namespace',
182 'page_title',
183 'page_len',
184 'page_is_redirect',
185 'page_latest',
186 'pp_value',
188 'conds' => [
189 'pp_propname' => $this->propName,
191 'join_conds' => [
192 'page' => [ 'JOIN', 'page_id = pp_page' ]
194 'options' => []
197 if ( $this->ns !== null ) {
198 $query['conds']['page_namespace'] = $this->ns;
201 return $query;
204 protected function getOrderFields() {
205 $sort = [ 'page_id' ];
206 if ( $this->sortByValue ) {
207 array_unshift( $sort, 'pp_sortkey' );
209 return $sort;
213 * @return bool
215 public function sortDescending() {
216 return !$this->reverse;
220 * @param Skin $skin
221 * @param stdClass $result Result row
222 * @return string
224 public function formatResult( $skin, $result ) {
225 $title = Title::newFromRow( $result );
226 $ret = $this->getLinkRenderer()->makeKnownLink( $title );
227 if ( $result->pp_value !== '' ) {
228 // Do not show very long or binary values on the special page
229 $valueLength = strlen( $result->pp_value );
230 $isBinary = str_contains( $result->pp_value, "\0" );
231 $isTooLong = $valueLength > 1024;
233 if ( $isBinary || $isTooLong ) {
234 $message = $this
235 ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
236 ->sizeParams( $valueLength );
238 $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
239 } else {
240 $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
243 $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
246 return $ret;
249 public function getExistingPropNames() {
250 if ( $this->existingPropNames === null ) {
251 $this->existingPropNames = $this->queryExistingProps();
253 return $this->existingPropNames;
256 protected function queryExistingProps( $limit = null, $offset = 0 ) {
257 $queryBuilder = $this->getDatabaseProvider()
258 ->getReplicaDatabase()
259 ->newSelectQueryBuilder()
260 ->select( 'pp_propname' )
261 ->distinct()
262 ->from( 'page_props' )
263 ->orderBy( 'pp_propname' );
265 if ( $limit ) {
266 $queryBuilder->limit( $limit );
268 if ( $offset ) {
269 $queryBuilder->offset( $offset );
271 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
273 $propnames = [];
274 foreach ( $res as $row ) {
275 $propnames[$row->pp_propname] = $row->pp_propname;
278 return $propnames;
281 protected function getGroupName() {
282 return 'pages';
287 * Retain the old class name for backwards compatibility.
288 * @deprecated since 1.41
290 class_alias( SpecialPagesWithProp::class, 'SpecialPagesWithProp' );