Revisions: Style action links in old revision notices as links
[mediawiki.git] / includes / specials / SpecialPagesWithProp.php
blob7bb529fe20830d4dc824f3e265f803fcf7c06e81
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 public function __construct( IConnectionProvider $dbProvider ) {
65 parent::__construct( 'PagesWithProp' );
66 $this->setDatabaseProvider( $dbProvider );
69 public function isCacheable() {
70 return false;
73 public function execute( $par ) {
74 $this->setHeaders();
75 $this->outputHeader();
76 $this->getOutput()->addModuleStyles( 'mediawiki.special' );
78 $request = $this->getRequest();
79 $propname = $request->getVal( 'propname', $par );
80 $this->ns = $request->getIntOrNull( 'namespace' );
81 $this->reverse = $request->getBool( 'reverse' );
82 $this->sortByValue = $request->getBool( 'sortbyvalue' );
84 $propnames = $this->getExistingPropNames();
86 $fields = [
87 'propname' => [
88 'type' => 'combobox',
89 'name' => 'propname',
90 'options' => $propnames,
91 'default' => $propname,
92 'label-message' => 'pageswithprop-prop',
93 'required' => true,
95 'namespace' => [
96 'type' => 'namespaceselect',
97 'name' => 'namespace',
98 'label-message' => 'namespace',
99 'all' => '',
100 'default' => $this->ns,
102 'reverse' => [
103 'type' => 'check',
104 'name' => 'reverse',
105 'default' => $this->reverse,
106 'label-message' => 'pageswithprop-reverse',
107 'required' => false,
109 'sortbyvalue' => [
110 'type' => 'check',
111 'name' => 'sortbyvalue',
112 'default' => $this->sortByValue,
113 'label-message' => 'pageswithprop-sortbyvalue',
114 'required' => false,
118 $form = HTMLForm::factory( 'ooui', $fields, $this->getContext() )
119 ->setMethod( 'get' )
120 ->setTitle( $this->getPageTitle() ) // Remove subpage
121 ->setSubmitCallback( [ $this, 'onSubmit' ] )
122 ->setWrapperLegendMsg( 'pageswithprop-legend' )
123 ->addHeaderHtml( $this->msg( 'pageswithprop-text' )->parseAsBlock() )
124 ->setSubmitTextMsg( 'pageswithprop-submit' )
125 ->prepareForm();
126 $form->displayForm( false );
127 if ( $propname !== '' && $propname !== null ) {
128 $form->trySubmit();
132 public function onSubmit( $data, $form ) {
133 $this->propName = $data['propname'];
134 parent::execute( $data['propname'] );
138 * Return an array of subpages beginning with $search that this special page will accept.
140 * @param string $search Prefix to search for
141 * @param int $limit Maximum number of results to return
142 * @param int $offset Number of pages to skip
143 * @return string[] Matching subpages
145 public function prefixSearchSubpages( $search, $limit, $offset ) {
146 $subpages = array_keys( $this->queryExistingProps( $limit, $offset ) );
147 // We've already limited and offsetted, set to N and 0 respectively.
148 return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 );
152 * Disable RSS/Atom feeds
153 * @return bool
155 public function isSyndicated() {
156 return false;
160 * @inheritDoc
162 protected function linkParameters() {
163 $params = [
164 'reverse' => $this->reverse,
165 'sortbyvalue' => $this->sortByValue,
167 if ( $this->ns !== null ) {
168 $params['namespace'] = $this->ns;
170 return $params;
173 public function getQueryInfo() {
174 $query = [
175 'tables' => [ 'page_props', 'page' ],
176 'fields' => [
177 'page_id' => 'pp_page',
178 'page_namespace',
179 'page_title',
180 'page_len',
181 'page_is_redirect',
182 'page_latest',
183 'pp_value',
185 'conds' => [
186 'pp_propname' => $this->propName,
188 'join_conds' => [
189 'page' => [ 'JOIN', 'page_id = pp_page' ]
191 'options' => []
194 if ( $this->ns !== null ) {
195 $query['conds']['page_namespace'] = $this->ns;
198 return $query;
201 protected function getOrderFields() {
202 $sort = [ 'page_id' ];
203 if ( $this->sortByValue ) {
204 array_unshift( $sort, 'pp_sortkey' );
206 return $sort;
210 * @return bool
212 public function sortDescending() {
213 return !$this->reverse;
217 * @param Skin $skin
218 * @param stdClass $result Result row
219 * @return string
221 public function formatResult( $skin, $result ) {
222 $title = Title::newFromRow( $result );
223 $ret = $this->getLinkRenderer()->makeKnownLink( $title );
224 if ( $result->pp_value !== '' ) {
225 // Do not show very long or binary values on the special page
226 $valueLength = strlen( $result->pp_value );
227 $isBinary = str_contains( $result->pp_value, "\0" );
228 $isTooLong = $valueLength > 1024;
230 if ( $isBinary || $isTooLong ) {
231 $message = $this
232 ->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' )
233 ->sizeParams( $valueLength );
235 $propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() );
236 } else {
237 $propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value );
240 $ret .= $this->msg( 'colon-separator' )->escaped() . $propValue;
243 return $ret;
246 public function getExistingPropNames() {
247 if ( $this->existingPropNames === null ) {
248 $this->existingPropNames = $this->queryExistingProps();
250 return $this->existingPropNames;
253 protected function queryExistingProps( $limit = null, $offset = 0 ) {
254 $queryBuilder = $this->getDatabaseProvider()
255 ->getReplicaDatabase()
256 ->newSelectQueryBuilder()
257 ->select( 'pp_propname' )
258 ->distinct()
259 ->from( 'page_props' )
260 ->orderBy( 'pp_propname' );
262 if ( $limit ) {
263 $queryBuilder->limit( $limit );
265 if ( $offset ) {
266 $queryBuilder->offset( $offset );
268 $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet();
270 $propnames = [];
271 foreach ( $res as $row ) {
272 $propnames[$row->pp_propname] = $row->pp_propname;
275 return $propnames;
278 protected function getGroupName() {
279 return 'pages';
284 * Retain the old class name for backwards compatibility.
285 * @deprecated since 1.41
287 class_alias( SpecialPagesWithProp::class, 'SpecialPagesWithProp' );