| 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\Cache\LinkBatchFactory; |
| 24 |
use MediaWiki\Html\Html; |
| 25 |
use MediaWiki\Linker\Linker; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\SpecialPage\QueryPage; |
| 28 |
use MediaWiki\Title\NamespaceInfo; |
| 29 |
use MediaWiki\Title\Title; |
| 30 |
use Skin; |
| 31 |
use stdClass; |
| 32 |
use Wikimedia\Rdbms\IConnectionProvider; |
| 33 |
use Wikimedia\Rdbms\IDatabase; |
| 34 |
use Wikimedia\Rdbms\IResultWrapper; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class SpecialShortPages extends QueryPage { |
| 42 |
|
| 43 |
private NamespaceInfo $namespaceInfo; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public function __construct( |
| 51 |
NamespaceInfo $namespaceInfo, |
| 52 |
IConnectionProvider $dbProvider, |
| 53 |
LinkBatchFactory $linkBatchFactory |
| 54 |
) { |
| 55 |
parent::__construct( 'Shortpages' ); |
| 56 |
$this->namespaceInfo = $namespaceInfo; |
| 57 |
$this->setDatabaseProvider( $dbProvider ); |
| 58 |
$this->setLinkBatchFactory( $linkBatchFactory ); |
| 59 |
} |
| 60 |
|
| 61 |
public function isSyndicated() { |
| 62 |
return false; |
| 63 |
} |
| 64 |
|
| 65 |
public function getQueryInfo() { |
| 66 |
$config = $this->getConfig(); |
| 67 |
$tables = [ 'page' ]; |
| 68 |
$conds = [ |
| 69 |
'page_namespace' => array_diff( |
| 70 |
$this->namespaceInfo->getContentNamespaces(), |
| 71 |
$config->get( MainConfigNames::ShortPagesNamespaceExclusions ) |
| 72 |
), |
| 73 |
'page_is_redirect' => 0 |
| 74 |
]; |
| 75 |
$joinConds = []; |
| 76 |
$options = [ 'USE INDEX' => [ 'page' => 'page_redirect_namespace_len' ] ]; |
| 77 |
|
| 78 |
|
| 79 |
$this->getHookRunner()->onShortPagesQuery( $tables, $conds, $joinConds, $options ); |
| 80 |
|
| 81 |
return [ |
| 82 |
'tables' => $tables, |
| 83 |
'fields' => [ |
| 84 |
'namespace' => 'page_namespace', |
| 85 |
'title' => 'page_title', |
| 86 |
'value' => 'page_len' |
| 87 |
], |
| 88 |
'conds' => $conds, |
| 89 |
'join_conds' => $joinConds, |
| 90 |
'options' => $options |
| 91 |
]; |
| 92 |
} |
| 93 |
|
| 94 |
public function reallyDoQuery( $limit, $offset = false ) { |
| 95 |
$fname = static::class . '::reallyDoQuery'; |
| 96 |
$dbr = $this->getRecacheDB(); |
| 97 |
$query = $this->getQueryInfo(); |
| 98 |
$conds = isset( $query['conds'] ) ? (array)$query['conds'] : []; |
| 99 |
$namespaces = $conds['page_namespace']; |
| 100 |
unset( $conds['page_namespace'] ); |
| 101 |
|
| 102 |
if ( count( $namespaces ) === 1 || !$dbr->unionSupportsOrderAndLimit() ) { |
| 103 |
return parent::reallyDoQuery( $limit, $offset ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
$sqb = $dbr->newSelectQueryBuilder() |
| 109 |
->select( isset( $query['fields'] ) ? (array)$query['fields'] : [] ) |
| 110 |
->tables( isset( $query['tables'] ) ? (array)$query['tables'] : [] ) |
| 111 |
->where( $conds ) |
| 112 |
->options( isset( $query['options'] ) ? (array)$query['options'] : [] ) |
| 113 |
->joinConds( isset( $query['join_conds'] ) ? (array)$query['join_conds'] : [] ); |
| 114 |
if ( $limit !== false ) { |
| 115 |
if ( $offset !== false ) { |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
$sqb->limit( intval( $limit ) + intval( $offset ) ); |
| 120 |
} else { |
| 121 |
$sqb->limit( intval( $limit ) ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
$order = $this->getOrderFields(); |
| 126 |
if ( $this->sortDescending() ) { |
| 127 |
foreach ( $order as &$field ) { |
| 128 |
$field .= ' DESC'; |
| 129 |
} |
| 130 |
} |
| 131 |
|
| 132 |
$uqb = $dbr->newUnionQueryBuilder()->all(); |
| 133 |
foreach ( $namespaces as $namespace ) { |
| 134 |
$nsSqb = clone $sqb; |
| 135 |
$nsSqb->orderBy( $order ); |
| 136 |
$nsSqb->andWhere( [ 'page_namespace' => $namespace ] ); |
| 137 |
$uqb->add( $nsSqb ); |
| 138 |
} |
| 139 |
|
| 140 |
if ( $limit !== false ) { |
| 141 |
$uqb->limit( intval( $limit ) ); |
| 142 |
} |
| 143 |
if ( $offset !== false ) { |
| 144 |
$uqb->offset( intval( $offset ) ); |
| 145 |
} |
| 146 |
$orderBy = 'value'; |
| 147 |
if ( $this->sortDescending() ) { |
| 148 |
$orderBy .= ' DESC'; |
| 149 |
} |
| 150 |
$uqb->orderBy( $orderBy ); |
| 151 |
return $uqb->caller( $fname )->fetchResultSet(); |
| 152 |
} |
| 153 |
|
| 154 |
protected function getOrderFields() { |
| 155 |
return [ 'page_len' ]; |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function preprocessResults( $db, $res ) { |
| 163 |
$this->executeLBFromResultWrapper( $res ); |
| 164 |
} |
| 165 |
|
| 166 |
protected function sortDescending() { |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
public function formatResult( $skin, $result ) { |
| 176 |
$title = Title::makeTitleSafe( $result->namespace, $result->title ); |
| 177 |
if ( !$title ) { |
| 178 |
return Html::element( 'span', [ 'class' => 'mw-invalidtitle' ], |
| 179 |
Linker::getInvalidTitleDescription( $this->getContext(), $result->namespace, $result->title ) ); |
| 180 |
} |
| 181 |
|
| 182 |
$linkRenderer = $this->getLinkRenderer(); |
| 183 |
$hlink = $linkRenderer->makeKnownLink( |
| 184 |
$title, |
| 185 |
$this->msg( 'hist' )->text(), |
| 186 |
[], |
| 187 |
[ 'action' => 'history' ] |
| 188 |
); |
| 189 |
$hlinkInParentheses = $this->msg( 'parentheses' )->rawParams( $hlink )->escaped(); |
| 190 |
|
| 191 |
if ( $this->isCached() ) { |
| 192 |
$plink = $linkRenderer->makeLink( $title ); |
| 193 |
$exists = $title->exists(); |
| 194 |
} else { |
| 195 |
$plink = $linkRenderer->makeKnownLink( $title ); |
| 196 |
$exists = true; |
| 197 |
} |
| 198 |
$contentLanguage = $this->getContentLanguage(); |
| 199 |
$bdiAttrs = [ |
| 200 |
'dir' => $contentLanguage->getDir(), |
| 201 |
'lang' => $contentLanguage->getHtmlCode(), |
| 202 |
]; |
| 203 |
$plink = Html::rawElement( 'bdi', $bdiAttrs, $plink ); |
| 204 |
$size = $this->msg( 'nbytes' )->numParams( $result->value )->escaped(); |
| 205 |
$result = "{$hlinkInParentheses} {$plink} [{$size}]"; |
| 206 |
|
| 207 |
return $exists ? $result : Html::rawElement( 'del', [], $result ); |
| 208 |
} |
| 209 |
|
| 210 |
protected function getGroupName() { |
| 211 |
return 'maintenance'; |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
class_alias( SpecialShortPages::class, 'SpecialShortPages' ); |