| 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\ChangeTags\ChangeTagsStore; |
| 24 |
use MediaWiki\Html\FormOptions; |
| 25 |
use MediaWiki\Html\Html; |
| 26 |
use MediaWiki\MainConfigNames; |
| 27 |
use MediaWiki\Title\Title; |
| 28 |
use MediaWiki\User\Options\UserOptionsLookup; |
| 29 |
use MediaWiki\User\TempUser\TempUserConfig; |
| 30 |
use MediaWiki\User\UserIdentityUtils; |
| 31 |
use MediaWiki\Watchlist\WatchedItemStoreInterface; |
| 32 |
use MediaWiki\Xml\Xml; |
| 33 |
use MessageCache; |
| 34 |
use RecentChange; |
| 35 |
use SearchEngineFactory; |
| 36 |
use Wikimedia\Rdbms\SelectQueryBuilder; |
| 37 |
use Wikimedia\Rdbms\Subquery; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
class SpecialRecentChangesLinked extends SpecialRecentChanges { |
| 46 |
|
| 47 |
protected $rclTargetTitle; |
| 48 |
|
| 49 |
private SearchEngineFactory $searchEngineFactory; |
| 50 |
private ChangeTagsStore $changeTagsStore; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function __construct( |
| 59 |
WatchedItemStoreInterface $watchedItemStore, |
| 60 |
MessageCache $messageCache, |
| 61 |
UserOptionsLookup $userOptionsLookup, |
| 62 |
SearchEngineFactory $searchEngineFactory, |
| 63 |
ChangeTagsStore $changeTagsStore, |
| 64 |
UserIdentityUtils $userIdentityUtils, |
| 65 |
TempUserConfig $tempUserConfig |
| 66 |
) { |
| 67 |
parent::__construct( |
| 68 |
$watchedItemStore, |
| 69 |
$messageCache, |
| 70 |
$userOptionsLookup, |
| 71 |
$changeTagsStore, |
| 72 |
$userIdentityUtils, |
| 73 |
$tempUserConfig |
| 74 |
); |
| 75 |
$this->mName = 'Recentchangeslinked'; |
| 76 |
$this->searchEngineFactory = $searchEngineFactory; |
| 77 |
$this->changeTagsStore = $changeTagsStore; |
| 78 |
} |
| 79 |
|
| 80 |
public function getDefaultOptions() { |
| 81 |
$opts = parent::getDefaultOptions(); |
| 82 |
$opts->add( 'target', '' ); |
| 83 |
$opts->add( 'showlinkedto', false ); |
| 84 |
|
| 85 |
return $opts; |
| 86 |
} |
| 87 |
|
| 88 |
public function parseParameters( $par, FormOptions $opts ) { |
| 89 |
$opts['target'] = $par; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
protected function doMainQuery( $tables, $select, $conds, $query_options, |
| 98 |
$join_conds, FormOptions $opts |
| 99 |
) { |
| 100 |
$target = $opts['target']; |
| 101 |
$showlinkedto = $opts['showlinkedto']; |
| 102 |
$limit = $opts['limit']; |
| 103 |
|
| 104 |
if ( $target === '' ) { |
| 105 |
return false; |
| 106 |
} |
| 107 |
$outputPage = $this->getOutput(); |
| 108 |
$title = Title::newFromText( $target ); |
| 109 |
if ( !$title || $title->isExternal() ) { |
| 110 |
$outputPage->addHTML( |
| 111 |
Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse(), '', 'mw-recentchangeslinked-errorbox' ) |
| 112 |
); |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
$outputPage->setPageTitleMsg( |
| 117 |
$this->msg( 'recentchangeslinked-title' )->plaintextParams( $title->getPrefixedText() ) |
| 118 |
); |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$dbr = $this->getDB(); |
| 129 |
$id = $title->getArticleID(); |
| 130 |
$ns = $title->getNamespace(); |
| 131 |
$dbkey = $title->getDBkey(); |
| 132 |
|
| 133 |
$rcQuery = RecentChange::getQueryInfo(); |
| 134 |
$tables = array_unique( array_merge( $rcQuery['tables'], $tables ) ); |
| 135 |
$select = array_unique( array_merge( $rcQuery['fields'], $select ) ); |
| 136 |
$join_conds = array_merge( $rcQuery['joins'], $join_conds ); |
| 137 |
|
| 138 |
|
| 139 |
$this->addWatchlistJoins( $dbr, $tables, $select, $join_conds, $conds ); |
| 140 |
|
| 141 |
|
| 142 |
$tables[] = 'page'; |
| 143 |
$join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ]; |
| 144 |
$select[] = 'page_latest'; |
| 145 |
|
| 146 |
$tagFilter = $opts['tagfilter'] !== '' ? explode( '|', $opts['tagfilter'] ) : []; |
| 147 |
$this->changeTagsStore->modifyDisplayQuery( |
| 148 |
$tables, |
| 149 |
$select, |
| 150 |
$conds, |
| 151 |
$join_conds, |
| 152 |
$query_options, |
| 153 |
$tagFilter, |
| 154 |
$opts['inverttags'] |
| 155 |
); |
| 156 |
|
| 157 |
if ( $dbr->unionSupportsOrderAndLimit() ) { |
| 158 |
if ( in_array( 'DISTINCT', $query_options ) ) { |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
$order = [ |
| 163 |
'GROUP BY' => [ 'rc_timestamp', 'rc_id' ], |
| 164 |
'ORDER BY' => [ 'rc_timestamp DESC', 'rc_id DESC' ] |
| 165 |
]; |
| 166 |
} else { |
| 167 |
$order = [ 'ORDER BY' => 'rc_timestamp DESC' ]; |
| 168 |
} |
| 169 |
} else { |
| 170 |
$order = []; |
| 171 |
} |
| 172 |
|
| 173 |
if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds, |
| 174 |
$opts ) |
| 175 |
) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
|
| 179 |
if ( $ns === NS_CATEGORY && !$showlinkedto ) { |
| 180 |
|
| 181 |
|
| 182 |
$link_tables = [ 'categorylinks' ]; |
| 183 |
$showlinkedto = true; |
| 184 |
} else { |
| 185 |
|
| 186 |
$link_tables = [ 'pagelinks', 'templatelinks' ]; |
| 187 |
|
| 188 |
if ( $ns === NS_FILE || !$showlinkedto ) { |
| 189 |
$link_tables[] = 'imagelinks'; |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if ( $id == 0 && !$showlinkedto ) { |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
$prefix = [ |
| 199 |
'pagelinks' => 'pl', |
| 200 |
'templatelinks' => 'tl', |
| 201 |
'categorylinks' => 'cl', |
| 202 |
'imagelinks' => 'il' |
| 203 |
]; |
| 204 |
|
| 205 |
$subsql = []; |
| 206 |
|
| 207 |
foreach ( $link_tables as $link_table ) { |
| 208 |
$queryBuilder = $dbr->newSelectQueryBuilder(); |
| 209 |
$linksMigration = \MediaWiki\MediaWikiServices::getInstance()->getLinksMigration(); |
| 210 |
$queryBuilder = $queryBuilder |
| 211 |
->tables( $tables ) |
| 212 |
->fields( $select ) |
| 213 |
->where( $conds ) |
| 214 |
->caller( __METHOD__ ) |
| 215 |
->options( $order + $query_options ) |
| 216 |
->joinConds( $join_conds ); |
| 217 |
$pfx = $prefix[$link_table]; |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if ( $link_table == 'imagelinks' ) { |
| 222 |
$link_ns = NS_FILE; |
| 223 |
} elseif ( $link_table == 'categorylinks' ) { |
| 224 |
$link_ns = NS_CATEGORY; |
| 225 |
} else { |
| 226 |
$link_ns = 0; |
| 227 |
} |
| 228 |
|
| 229 |
if ( $showlinkedto ) { |
| 230 |
|
| 231 |
if ( $link_ns ) { |
| 232 |
if ( $ns != $link_ns ) { |
| 233 |
continue; |
| 234 |
} |
| 235 |
$queryBuilder->where( [ "{$pfx}_to" => $dbkey ] ); |
| 236 |
} else { |
| 237 |
if ( isset( $linksMigration::$mapping[$link_table] ) ) { |
| 238 |
$queryBuilder->where( $linksMigration->getLinksConditions( $link_table, $title ) ); |
| 239 |
} else { |
| 240 |
$queryBuilder->where( [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ] ); |
| 241 |
} |
| 242 |
} |
| 243 |
$queryBuilder->join( $link_table, null, "rc_cur_id = {$pfx}_from" ); |
| 244 |
} else { |
| 245 |
|
| 246 |
$queryBuilder->where( [ "{$pfx}_from" => $id ] ); |
| 247 |
if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) { |
| 248 |
$queryBuilder->where( [ "rc_namespace" => $link_ns ] ); |
| 249 |
$queryBuilder->join( $link_table, null, "rc_title = {$pfx}_to" ); |
| 250 |
} else { |
| 251 |
|
| 252 |
if ( isset( $linksMigration::$mapping[$link_table] ) ) { |
| 253 |
$queryInfo = $linksMigration->getQueryInfo( $link_table, $link_table ); |
| 254 |
[ $nsField, $titleField ] = $linksMigration->getTitleFields( $link_table ); |
| 255 |
if ( in_array( 'linktarget', $queryInfo['tables'] ) ) { |
| 256 |
$joinTable = 'linktarget'; |
| 257 |
} else { |
| 258 |
$joinTable = $link_table; |
| 259 |
} |
| 260 |
$queryBuilder->join( |
| 261 |
$joinTable, |
| 262 |
null, |
| 263 |
[ "rc_namespace = {$nsField}", "rc_title = {$titleField}" ] |
| 264 |
); |
| 265 |
if ( in_array( 'linktarget', $queryInfo['tables'] ) ) { |
| 266 |
$queryBuilder->joinConds( $queryInfo['joins'] ); |
| 267 |
$queryBuilder->table( $link_table ); |
| 268 |
} |
| 269 |
} else { |
| 270 |
$queryBuilder->join( |
| 271 |
$link_table, |
| 272 |
null, |
| 273 |
[ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ] |
| 274 |
); |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
if ( $dbr->unionSupportsOrderAndLimit() ) { |
| 280 |
$queryBuilder->limit( $limit ); |
| 281 |
} |
| 282 |
|
| 283 |
$subsql[] = $queryBuilder; |
| 284 |
} |
| 285 |
|
| 286 |
if ( count( $subsql ) == 0 ) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) { |
| 290 |
return $subsql[0] |
| 291 |
->setMaxExecutionTime( $this->getConfig()->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ) ) |
| 292 |
->caller( __METHOD__ )->fetchResultSet(); |
| 293 |
} else { |
| 294 |
$unionQueryBuilder = $dbr->newUnionQueryBuilder()->caller( __METHOD__ ); |
| 295 |
foreach ( $subsql as $selectQueryBuilder ) { |
| 296 |
$unionQueryBuilder->add( $selectQueryBuilder ); |
| 297 |
} |
| 298 |
return $dbr->newSelectQueryBuilder() |
| 299 |
->select( '*' ) |
| 300 |
->from( |
| 301 |
new Subquery( $unionQueryBuilder->getSQL() ), |
| 302 |
'main' |
| 303 |
) |
| 304 |
->orderBy( 'rc_timestamp', SelectQueryBuilder::SORT_DESC ) |
| 305 |
->setMaxExecutionTime( $this->getConfig()->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ) ) |
| 306 |
->limit( $limit ) |
| 307 |
->caller( __METHOD__ )->fetchResultSet(); |
| 308 |
} |
| 309 |
} |
| 310 |
|
| 311 |
public function setTopText( FormOptions $opts ) { |
| 312 |
$target = $this->getTargetTitle(); |
| 313 |
if ( $target ) { |
| 314 |
$this->getOutput()->addBacklinkSubtitle( $target ); |
| 315 |
$this->getSkin()->setRelevantTitle( $target ); |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
public function getExtraOptions( $opts ) { |
| 326 |
$extraOpts = parent::getExtraOptions( $opts ); |
| 327 |
|
| 328 |
$opts->consumeValues( [ 'showlinkedto', 'target' ] ); |
| 329 |
|
| 330 |
$extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(), |
| 331 |
Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) . ' ' . |
| 332 |
Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' . |
| 333 |
Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ]; |
| 334 |
|
| 335 |
$this->addHelpLink( 'Help:Related changes' ); |
| 336 |
return $extraOpts; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
private function getTargetTitle() { |
| 343 |
if ( $this->rclTargetTitle === null ) { |
| 344 |
$opts = $this->getOptions(); |
| 345 |
if ( isset( $opts['target'] ) && $opts['target'] !== '' ) { |
| 346 |
$this->rclTargetTitle = Title::newFromText( $opts['target'] ); |
| 347 |
} else { |
| 348 |
$this->rclTargetTitle = false; |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
return $this->rclTargetTitle; |
| 353 |
} |
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
public function prefixSearchSubpages( $search, $limit, $offset ) { |
| 364 |
return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory ); |
| 365 |
} |
| 366 |
|
| 367 |
protected function outputNoResults() { |
| 368 |
$targetTitle = $this->getTargetTitle(); |
| 369 |
if ( $targetTitle === false ) { |
| 370 |
$this->getOutput()->addHTML( |
| 371 |
Html::rawElement( |
| 372 |
'div', |
| 373 |
[ 'class' => 'mw-changeslist-empty mw-changeslist-notargetpage' ], |
| 374 |
$this->msg( 'recentchanges-notargetpage' )->parse() |
| 375 |
) |
| 376 |
); |
| 377 |
} elseif ( !$targetTitle || $targetTitle->isExternal() ) { |
| 378 |
$this->getOutput()->addHTML( |
| 379 |
Html::rawElement( |
| 380 |
'div', |
| 381 |
[ 'class' => 'mw-changeslist-empty mw-changeslist-invalidtargetpage' ], |
| 382 |
$this->msg( 'allpagesbadtitle' )->parse() |
| 383 |
) |
| 384 |
); |
| 385 |
} else { |
| 386 |
parent::outputNoResults(); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
class_alias( SpecialRecentChangesLinked::class, 'SpecialRecentChangesLinked' ); |