90 protected function doMainQuery( $tables, $select, $conds, $query_options,
93 $target = $opts[
'target'];
94 $showlinkedto = $opts[
'showlinkedto'];
95 $limit = $opts[
'limit'];
97 if ( $target ===
'' ) {
101 $title = Title::newFromText( $target );
103 $outputPage->addHTML(
104 Html::errorBox( $this->
msg(
'allpagesbadtitle' )->parse(),
'',
'mw-recentchangeslinked-errorbox' )
109 $outputPage->setPageTitle( $this->
msg(
'recentchangeslinked-title',
$title->getPrefixedText() ) );
119 $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
120 $id =
$title->getArticleID();
121 $ns =
$title->getNamespace();
122 $dbkey =
$title->getDBkey();
124 $rcQuery = RecentChange::getQueryInfo();
125 $tables = array_unique( array_merge( $rcQuery[
'tables'], $tables ) );
126 $select = array_unique( array_merge( $rcQuery[
'fields'], $select ) );
127 $join_conds = array_merge( $rcQuery[
'joins'], $join_conds );
134 $join_conds[
'page'] = [
'LEFT JOIN',
'rc_cur_id=page_id' ];
135 $select[] =
'page_latest';
137 $tagFilter = $opts[
'tagfilter'] !==
'' ? explode(
'|', $opts[
'tagfilter'] ) : [];
148 if (
$dbr->unionSupportsOrderAndLimit() ) {
149 if ( in_array(
'DISTINCT', $query_options ) ) {
154 'GROUP BY' => [
'rc_timestamp',
'rc_id' ],
155 'ORDER BY' => [
'rc_timestamp DESC',
'rc_id DESC' ]
158 $order = [
'ORDER BY' =>
'rc_timestamp DESC' ];
164 if ( !$this->
runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
173 $link_tables = [
'categorylinks' ];
174 $showlinkedto =
true;
177 $link_tables = [
'pagelinks',
'templatelinks' ];
179 if ( $ns ===
NS_FILE || !$showlinkedto ) {
180 $link_tables[] =
'imagelinks';
184 if ( $id == 0 && !$showlinkedto ) {
191 'templatelinks' =>
'tl',
192 'categorylinks' =>
'cl',
198 foreach ( $link_tables as $link_table ) {
199 $queryBuilder =
$dbr->newSelectQueryBuilder();
200 $linksMigration = \MediaWiki\MediaWikiServices::getInstance()->getLinksMigration();
201 $queryBuilder = $queryBuilder
205 ->caller( __METHOD__ )
206 ->options( $order + $query_options )
207 ->joinConds( $join_conds );
208 $pfx = $prefix[$link_table];
212 if ( $link_table ==
'imagelinks' ) {
214 } elseif ( $link_table ==
'categorylinks' ) {
220 if ( $showlinkedto ) {
223 if ( $ns != $link_ns ) {
226 $queryBuilder->where( [
"{$pfx}_to" => $dbkey ] );
228 if ( isset( $linksMigration::$mapping[$link_table] ) ) {
229 $queryBuilder->where( $linksMigration->getLinksConditions( $link_table,
$title ) );
231 $queryBuilder->where( [
"{$pfx}_namespace" => $ns,
"{$pfx}_title" => $dbkey ] );
234 $queryBuilder->join( $link_table,
null,
"rc_cur_id = {$pfx}_from" );
237 $queryBuilder->where( [
"{$pfx}_from" => $id ] );
238 if ( $link_table ==
'imagelinks' || $link_table ==
'categorylinks' ) {
239 $queryBuilder->where( [
"rc_namespace" => $link_ns ] );
240 $queryBuilder->join( $link_table,
null,
"rc_title = {$pfx}_to" );
243 if ( isset( $linksMigration::$mapping[$link_table] ) ) {
244 $queryInfo = $linksMigration->getQueryInfo( $link_table, $link_table );
245 [ $nsField, $titleField ] = $linksMigration->getTitleFields( $link_table );
246 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
247 $joinTable =
'linktarget';
249 $joinTable = $link_table;
254 [
"rc_namespace = {$nsField}",
"rc_title = {$titleField}" ]
256 if ( in_array(
'linktarget', $queryInfo[
'tables'] ) ) {
257 $queryBuilder->joinConds( $queryInfo[
'joins'] );
258 $queryBuilder->table( $link_table );
264 [
"rc_namespace = {$pfx}_namespace",
"rc_title = {$pfx}_title" ]
270 if (
$dbr->unionSupportsOrderAndLimit() ) {
271 $queryBuilder->limit( $limit );
274 $subsql[] = $queryBuilder;
277 if ( count( $subsql ) == 0 ) {
280 if ( count( $subsql ) == 1 &&
$dbr->unionSupportsOrderAndLimit() ) {
282 ->setMaxExecutionTime( $this->
getConfig()->
get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ) )
283 ->caller( __METHOD__ )->fetchResultSet();
285 $sqls = array_map(
static function ( $queryBuilder ) {
286 return $queryBuilder->getSQL();
288 return $dbr->newSelectQueryBuilder()
291 new Subquery(
$dbr->unionQueries( $sqls, $dbr::UNION_DISTINCT ) ),
294 ->orderBy(
'rc_timestamp', SelectQueryBuilder::SORT_DESC )
295 ->setMaxExecutionTime( $this->
getConfig()->
get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ) )
297 ->caller( __METHOD__ )->fetchResultSet();