MediaWiki REL1_40
SpecialRecentChangesLinked.php
Go to the documentation of this file.
1<?php
32
40 protected $rclTargetTitle;
41
43 private $loadBalancer;
44
46 private $searchEngineFactory;
47
55 public function __construct(
56 WatchedItemStoreInterface $watchedItemStore,
57 MessageCache $messageCache,
58 ILoadBalancer $loadBalancer,
59 UserOptionsLookup $userOptionsLookup,
60 SearchEngineFactory $searchEngineFactory
61 ) {
62 parent::__construct(
63 $watchedItemStore,
64 $messageCache,
65 $loadBalancer,
66 $userOptionsLookup
67 );
68 $this->mName = 'Recentchangeslinked';
69 $this->loadBalancer = $loadBalancer;
70 $this->searchEngineFactory = $searchEngineFactory;
71 }
72
73 public function getDefaultOptions() {
74 $opts = parent::getDefaultOptions();
75 $opts->add( 'target', '' );
76 $opts->add( 'showlinkedto', false );
77
78 return $opts;
79 }
80
81 public function parseParameters( $par, FormOptions $opts ) {
82 $opts['target'] = $par;
83 }
84
90 protected function doMainQuery( $tables, $select, $conds, $query_options,
91 $join_conds, FormOptions $opts
92 ) {
93 $target = $opts['target'];
94 $showlinkedto = $opts['showlinkedto'];
95 $limit = $opts['limit'];
96
97 if ( $target === '' ) {
98 return false;
99 }
100 $outputPage = $this->getOutput();
101 $title = Title::newFromText( $target );
102 if ( !$title || $title->isExternal() ) {
103 $outputPage->addHTML(
104 Html::errorBox( $this->msg( 'allpagesbadtitle' )->parse(), '', 'mw-recentchangeslinked-errorbox' )
105 );
106 return false;
107 }
108
109 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
110
111 /*
112 * Ordinary links are in the pagelinks table, while transclusions are
113 * in the templatelinks table, categorizations in categorylinks and
114 * image use in imagelinks. We need to somehow combine all these.
115 * Special:Whatlinkshere does this by firing multiple queries and
116 * merging the results, but the code we inherit from our parent class
117 * expects only one result set so we use UNION instead.
118 */
119 $dbr = $this->loadBalancer->getConnectionRef( ILoadBalancer::DB_REPLICA );
120 $id = $title->getArticleID();
121 $ns = $title->getNamespace();
122 $dbkey = $title->getDBkey();
123
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 );
128
129 // Join with watchlist and watchlist_expiry tables to highlight watched rows.
130 $this->addWatchlistJoins( $dbr, $tables, $select, $join_conds, $conds );
131
132 // JOIN on page, used for 'last revision' filter highlight
133 $tables[] = 'page';
134 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
135 $select[] = 'page_latest';
136
137 $tagFilter = $opts['tagfilter'] !== '' ? explode( '|', $opts['tagfilter'] ) : [];
139 $tables,
140 $select,
141 $conds,
142 $join_conds,
143 $query_options,
144 $tagFilter,
145 $opts['inverttags']
146 );
147
148 if ( $dbr->unionSupportsOrderAndLimit() ) {
149 if ( in_array( 'DISTINCT', $query_options ) ) {
150 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
151 // To prevent this from causing query performance problems, we need to add
152 // a GROUP BY, and add rc_id to the ORDER BY.
153 $order = [
154 'GROUP BY' => [ 'rc_timestamp', 'rc_id' ],
155 'ORDER BY' => [ 'rc_timestamp DESC', 'rc_id DESC' ]
156 ];
157 } else {
158 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
159 }
160 } else {
161 $order = [];
162 }
163
164 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
165 $opts )
166 ) {
167 return false;
168 }
169
170 if ( $ns === NS_CATEGORY && !$showlinkedto ) {
171 // special handling for categories
172 // XXX: should try to make this less kludgy
173 $link_tables = [ 'categorylinks' ];
174 $showlinkedto = true;
175 } else {
176 // for now, always join on these tables; really should be configurable as in whatlinkshere
177 $link_tables = [ 'pagelinks', 'templatelinks' ];
178 // imagelinks only contains links to pages in NS_FILE
179 if ( $ns === NS_FILE || !$showlinkedto ) {
180 $link_tables[] = 'imagelinks';
181 }
182 }
183
184 if ( $id == 0 && !$showlinkedto ) {
185 return false; // nonexistent pages can't link to any pages
186 }
187
188 // field name prefixes for all the various tables we might want to join with
189 $prefix = [
190 'pagelinks' => 'pl',
191 'templatelinks' => 'tl',
192 'categorylinks' => 'cl',
193 'imagelinks' => 'il'
194 ];
195
196 $subsql = []; // SELECT statements to combine with UNION
197
198 foreach ( $link_tables as $link_table ) {
199 $queryBuilder = $dbr->newSelectQueryBuilder();
200 $linksMigration = \MediaWiki\MediaWikiServices::getInstance()->getLinksMigration();
201 $queryBuilder = $queryBuilder
202 ->tables( $tables )
203 ->fields( $select )
204 ->where( $conds )
205 ->caller( __METHOD__ )
206 ->options( $order + $query_options )
207 ->joinConds( $join_conds );
208 $pfx = $prefix[$link_table];
209
210 // imagelinks and categorylinks tables have no xx_namespace field,
211 // and have xx_to instead of xx_title
212 if ( $link_table == 'imagelinks' ) {
213 $link_ns = NS_FILE;
214 } elseif ( $link_table == 'categorylinks' ) {
215 $link_ns = NS_CATEGORY;
216 } else {
217 $link_ns = 0;
218 }
219
220 if ( $showlinkedto ) {
221 // find changes to pages linking to this page
222 if ( $link_ns ) {
223 if ( $ns != $link_ns ) {
224 continue;
225 } // should never happen, but check anyway
226 $queryBuilder->where( [ "{$pfx}_to" => $dbkey ] );
227 } else {
228 if ( isset( $linksMigration::$mapping[$link_table] ) ) {
229 $queryBuilder->where( $linksMigration->getLinksConditions( $link_table, $title ) );
230 } else {
231 $queryBuilder->where( [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ] );
232 }
233 }
234 $queryBuilder->join( $link_table, null, "rc_cur_id = {$pfx}_from" );
235 } else {
236 // find changes to pages linked from this page
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" );
241 } else {
242 // TODO: Move this to LinksMigration
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';
248 } else {
249 $joinTable = $link_table;
250 }
251 $queryBuilder->join(
252 $joinTable,
253 null,
254 [ "rc_namespace = {$nsField}", "rc_title = {$titleField}" ]
255 );
256 if ( in_array( 'linktarget', $queryInfo['tables'] ) ) {
257 $queryBuilder->joinConds( $queryInfo['joins'] );
258 $queryBuilder->table( $link_table );
259 }
260 } else {
261 $queryBuilder->join(
262 $link_table,
263 null,
264 [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ]
265 );
266 }
267 }
268 }
269
270 if ( $dbr->unionSupportsOrderAndLimit() ) {
271 $queryBuilder->limit( $limit );
272 }
273
274 $subsql[] = $queryBuilder;
275 }
276
277 if ( count( $subsql ) == 0 ) {
278 return false; // should never happen
279 }
280 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
281 return $subsql[0]
282 ->setMaxExecutionTime( $this->getConfig()->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ) )
283 ->caller( __METHOD__ )->fetchResultSet();
284 } else {
285 $sqls = array_map( static function ( $queryBuilder ) {
286 return $queryBuilder->getSQL();
287 }, $subsql );
288 return $dbr->newSelectQueryBuilder()
289 ->select( '*' )
290 ->from(
291 new Subquery( $dbr->unionQueries( $sqls, $dbr::UNION_DISTINCT ) ),
292 'main'
293 )
294 ->orderBy( 'rc_timestamp', SelectQueryBuilder::SORT_DESC )
295 ->setMaxExecutionTime( $this->getConfig()->get( MainConfigNames::MaxExecutionTimeForExpensiveQueries ) )
296 ->limit( $limit )
297 ->caller( __METHOD__ )->fetchResultSet();
298 }
299 }
300
301 public function setTopText( FormOptions $opts ) {
302 $target = $this->getTargetTitle();
303 if ( $target ) {
304 $this->getOutput()->addBacklinkSubtitle( $target );
305 $this->getSkin()->setRelevantTitle( $target );
306 }
307 }
308
315 public function getExtraOptions( $opts ) {
316 $extraOpts = parent::getExtraOptions( $opts );
317
318 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
319
320 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
321 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) . ' ' .
322 Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
323 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
324
325 $this->addHelpLink( 'Help:Related changes' );
326 return $extraOpts;
327 }
328
332 private function getTargetTitle() {
333 if ( $this->rclTargetTitle === null ) {
334 $opts = $this->getOptions();
335 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
336 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
337 } else {
338 $this->rclTargetTitle = false;
339 }
340 }
341
343 }
344
353 public function prefixSearchSubpages( $search, $limit, $offset ) {
354 return $this->prefixSearchString( $search, $limit, $offset, $this->searchEngineFactory );
355 }
356
357 protected function outputNoResults() {
358 $targetTitle = $this->getTargetTitle();
359 if ( $targetTitle === false ) {
360 $this->getOutput()->addHTML(
361 Html::rawElement(
362 'div',
363 [ 'class' => 'mw-changeslist-empty mw-changeslist-notargetpage' ],
364 $this->msg( 'recentchanges-notargetpage' )->parse()
365 )
366 );
367 } elseif ( !$targetTitle || $targetTitle->isExternal() ) {
368 $this->getOutput()->addHTML(
369 Html::rawElement(
370 'div',
371 [ 'class' => 'mw-changeslist-empty mw-changeslist-invalidtargetpage' ],
372 $this->msg( 'allpagesbadtitle' )->parse()
373 )
374 );
375 } else {
376 parent::outputNoResults();
377 }
378 }
379}
const NS_FILE
Definition Defines.php:70
const NS_CATEGORY
Definition Defines.php:78
static modifyDisplayQuery(&$tables, &$fields, &$conds, &$join_conds, &$options, $filter_tag='', bool $exclude=false)
Applies all tags-related changes to a query.
runMainQueryHook(&$tables, &$fields, &$conds, &$query_options, &$join_conds, $opts)
getOptions()
Get the current FormOptions for this request.
Helper class to keep track of options when mixing links and form elements.
This class is a collection of static functions that serve two purposes:
Definition Html.php:55
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition Title.php:82
Provides access to user options.
Cache messages that are defined by MediaWiki-namespace pages or by hooks.
Factory class for SearchEngine.
getOutput()
Get the OutputPage being used for this instance.
getSkin()
Shortcut to get the skin being used for this instance.
msg( $key,... $params)
Wrapper around wfMessage that sets the current context.
getConfig()
Shortcut to get main config object.
prefixSearchString( $search, $limit, $offset, SearchEngineFactory $searchEngineFactory=null)
Perform a regular substring search for prefixSearchSubpages.
addHelpLink( $to, $overrideBaseUrl=false)
Adds help link with an icon via page indicators.
This is to display changes made to all articles linked in an article.
setTopText(FormOptions $opts)
Send the text to be displayed above the options.
doMainQuery( $tables, $select, $conds, $query_options, $join_conds, FormOptions $opts)
FIXME: Port useful changes from SpecialRecentChanges.
parseParameters( $par, FormOptions $opts)
Process $par and put options found in $opts.
getDefaultOptions()
Get a FormOptions object containing the default options.
prefixSearchSubpages( $search, $limit, $offset)
Return an array of subpages beginning with $search that this special page will accept.
outputNoResults()
Add the "no results" message to the output.
__construct(WatchedItemStoreInterface $watchedItemStore, MessageCache $messageCache, ILoadBalancer $loadBalancer, UserOptionsLookup $userOptionsLookup, SearchEngineFactory $searchEngineFactory)
getExtraOptions( $opts)
Get options to be displayed in a form.
A special page that lists last changes made to the wiki.
addWatchlistJoins(IDatabase $dbr, &$tables, &$fields, &$joinConds, &$conds)
Add required values to a query's $tables, $fields, $joinConds, and $conds arrays to join to the watch...
A query builder for SELECT queries with a fluent interface.
This class is a delegate to ILBFactory for a given database cluster.