| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Specials; |
| 22 |
|
| 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; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class SpecialPagesWithProp extends QueryPage { |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private $propName = null; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
private $existingPropNames = null; |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
private $ns; |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
private $reverse = false; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
private $sortByValue = false; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function __construct( IConnectionProvider $dbProvider ) { |
| 68 |
parent::__construct( 'PagesWithProp' ); |
| 69 |
$this->setDatabaseProvider( $dbProvider ); |
| 70 |
} |
| 71 |
|
| 72 |
public function isCacheable() { |
| 73 |
return false; |
| 74 |
} |
| 75 |
|
| 76 |
public function execute( $par ) { |
| 77 |
$this->setHeaders(); |
| 78 |
$this->outputHeader(); |
| 79 |
$this->getOutput()->addModuleStyles( 'mediawiki.special' ); |
| 80 |
|
| 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' ); |
| 86 |
|
| 87 |
$propnames = $this->getExistingPropNames(); |
| 88 |
|
| 89 |
$fields = [ |
| 90 |
'propname' => [ |
| 91 |
'type' => 'combobox', |
| 92 |
'name' => 'propname', |
| 93 |
'options' => $propnames, |
| 94 |
'default' => $propname, |
| 95 |
'label-message' => 'pageswithprop-prop', |
| 96 |
'required' => true, |
| 97 |
], |
| 98 |
'namespace' => [ |
| 99 |
'type' => 'namespaceselect', |
| 100 |
'name' => 'namespace', |
| 101 |
'label-message' => 'namespace', |
| 102 |
'all' => '', |
| 103 |
'default' => $this->ns, |
| 104 |
], |
| 105 |
'reverse' => [ |
| 106 |
'type' => 'check', |
| 107 |
'name' => 'reverse', |
| 108 |
'default' => $this->reverse, |
| 109 |
'label-message' => 'pageswithprop-reverse', |
| 110 |
'required' => false, |
| 111 |
], |
| 112 |
'sortbyvalue' => [ |
| 113 |
'type' => 'check', |
| 114 |
'name' => 'sortbyvalue', |
| 115 |
'default' => $this->sortByValue, |
| 116 |
'label-message' => 'pageswithprop-sortbyvalue', |
| 117 |
'required' => false, |
| 118 |
] |
| 119 |
]; |
| 120 |
|
| 121 |
$form = HTMLForm::factory( 'ooui', $fields, $this->getContext() ) |
| 122 |
->setMethod( 'get' ) |
| 123 |
->setTitle( $this->getPageTitle() ) |
| 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(); |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
public function onSubmit( $data, $form ) { |
| 136 |
$this->propName = $data['propname']; |
| 137 |
parent::execute( $data['propname'] ); |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 149 |
$subpages = array_keys( $this->queryExistingProps( $limit, $offset ) ); |
| 150 |
|
| 151 |
return self::prefixSearchArray( $search, count( $subpages ), $subpages, 0 ); |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public function isSyndicated() { |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
protected function linkParameters() { |
| 166 |
$params = [ |
| 167 |
'reverse' => $this->reverse, |
| 168 |
'sortbyvalue' => $this->sortByValue, |
| 169 |
]; |
| 170 |
if ( $this->ns !== null ) { |
| 171 |
$params['namespace'] = $this->ns; |
| 172 |
} |
| 173 |
return $params; |
| 174 |
} |
| 175 |
|
| 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', |
| 187 |
], |
| 188 |
'conds' => [ |
| 189 |
'pp_propname' => $this->propName, |
| 190 |
], |
| 191 |
'join_conds' => [ |
| 192 |
'page' => [ 'JOIN', 'page_id = pp_page' ] |
| 193 |
], |
| 194 |
'options' => [] |
| 195 |
]; |
| 196 |
|
| 197 |
if ( $this->ns !== null ) { |
| 198 |
$query['conds']['page_namespace'] = $this->ns; |
| 199 |
} |
| 200 |
|
| 201 |
return $query; |
| 202 |
} |
| 203 |
|
| 204 |
protected function getOrderFields() { |
| 205 |
$sort = [ 'page_id' ]; |
| 206 |
if ( $this->sortByValue ) { |
| 207 |
array_unshift( $sort, 'pp_sortkey' ); |
| 208 |
} |
| 209 |
return $sort; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
public function sortDescending() { |
| 216 |
return !$this->reverse; |
| 217 |
} |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
public function formatResult( $skin, $result ) { |
| 225 |
$title = Title::newFromRow( $result ); |
| 226 |
$ret = $this->getLinkRenderer()->makeKnownLink( $title ); |
| 227 |
if ( $result->pp_value !== '' ) { |
| 228 |
|
| 229 |
$valueLength = strlen( $result->pp_value ); |
| 230 |
$isBinary = str_contains( $result->pp_value, "\0" ); |
| 231 |
$isTooLong = $valueLength > 1024; |
| 232 |
|
| 233 |
if ( $isBinary || $isTooLong ) { |
| 234 |
$message = $this |
| 235 |
->msg( $isBinary ? 'pageswithprop-prophidden-binary' : 'pageswithprop-prophidden-long' ) |
| 236 |
->sizeParams( $valueLength ); |
| 237 |
|
| 238 |
$propValue = Html::element( 'span', [ 'class' => 'prop-value-hidden' ], $message->text() ); |
| 239 |
} else { |
| 240 |
$propValue = Html::element( 'span', [ 'class' => 'prop-value' ], $result->pp_value ); |
| 241 |
} |
| 242 |
|
| 243 |
$ret .= $this->msg( 'colon-separator' )->escaped() . $propValue; |
| 244 |
} |
| 245 |
|
| 246 |
return $ret; |
| 247 |
} |
| 248 |
|
| 249 |
public function getExistingPropNames() { |
| 250 |
if ( $this->existingPropNames === null ) { |
| 251 |
$this->existingPropNames = $this->queryExistingProps(); |
| 252 |
} |
| 253 |
return $this->existingPropNames; |
| 254 |
} |
| 255 |
|
| 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' ); |
| 264 |
|
| 265 |
if ( $limit ) { |
| 266 |
$queryBuilder->limit( $limit ); |
| 267 |
} |
| 268 |
if ( $offset ) { |
| 269 |
$queryBuilder->offset( $offset ); |
| 270 |
} |
| 271 |
$res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 272 |
|
| 273 |
$propnames = []; |
| 274 |
foreach ( $res as $row ) { |
| 275 |
$propnames[$row->pp_propname] = $row->pp_propname; |
| 276 |
} |
| 277 |
|
| 278 |
return $propnames; |
| 279 |
} |
| 280 |
|
| 281 |
protected function getGroupName() { |
| 282 |
return 'pages'; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
class_alias( SpecialPagesWithProp::class, 'SpecialPagesWithProp' ); |