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
21 namespace MediaWiki\Specials
;
23 use MediaWiki\Html\Html
;
24 use MediaWiki\HTMLForm\HTMLForm
;
25 use MediaWiki\SpecialPage\QueryPage
;
26 use MediaWiki\Title\Title
;
29 use Wikimedia\Rdbms\IConnectionProvider
;
32 * Special:PagesWithProp to search the page_props table
34 * @ingroup SpecialPage
37 class SpecialPagesWithProp
extends QueryPage
{
42 private $propName = null;
47 private $existingPropNames = null;
57 private $reverse = false;
62 private $sortByValue = false;
65 * @param IConnectionProvider $dbProvider
67 public function __construct( IConnectionProvider
$dbProvider ) {
68 parent
::__construct( 'PagesWithProp' );
69 $this->setDatabaseProvider( $dbProvider );
72 public function isCacheable() {
76 public function execute( $par ) {
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();
93 'options' => $propnames,
94 'default' => $propname,
95 'label-message' => 'pageswithprop-prop',
99 'type' => 'namespaceselect',
100 'name' => 'namespace',
101 'label-message' => 'namespace',
103 'default' => $this->ns
,
108 'default' => $this->reverse
,
109 'label-message' => 'pageswithprop-reverse',
114 'name' => 'sortbyvalue',
115 'default' => $this->sortByValue
,
116 'label-message' => 'pageswithprop-sortbyvalue',
121 $form = HTMLForm
::factory( 'ooui', $fields, $this->getContext() )
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' )
129 $form->displayForm( false );
130 if ( $propname !== '' && $propname !== null ) {
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
158 public function isSyndicated() {
165 protected function linkParameters() {
167 'reverse' => $this->reverse
,
168 'sortbyvalue' => $this->sortByValue
,
170 if ( $this->ns
!== null ) {
171 $params['namespace'] = $this->ns
;
176 public function getQueryInfo() {
178 'tables' => [ 'page_props', 'page' ],
180 'page_id' => 'pp_page',
189 'pp_propname' => $this->propName
,
192 'page' => [ 'JOIN', 'page_id = pp_page' ]
197 if ( $this->ns
!== null ) {
198 $query['conds']['page_namespace'] = $this->ns
;
204 protected function getOrderFields() {
205 $sort = [ 'page_id' ];
206 if ( $this->sortByValue
) {
207 array_unshift( $sort, 'pp_sortkey' );
215 public function sortDescending() {
216 return !$this->reverse
;
221 * @param stdClass $result Result row
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 ) {
235 ->msg( $isBinary ?
'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
236 ->sizeParams( $valueLength );
238 $propValue = Html
::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
240 $propValue = Html
::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value
);
243 $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
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' )
262 ->from( 'page_props' )
263 ->orderBy( 'pp_propname' );
266 $queryBuilder->limit( $limit );
269 $queryBuilder->offset( $offset );
271 $res = $queryBuilder->caller( __METHOD__
)->fetchResultSet();
274 foreach ( $res as $row ) {
275 $propnames[$row->pp_propname
] = $row->pp_propname
;
281 protected function getGroupName() {
287 * Retain the old class name for backwards compatibility.
288 * @deprecated since 1.41
290 class_alias( SpecialPagesWithProp
::class, 'SpecialPagesWithProp' );