3 * Implements Special:Recentchangeslinked
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
21 * @ingroup SpecialPage
24 use MediaWiki\MediaWikiServices
;
27 * This is to display changes made to all articles linked in an article.
29 * @ingroup SpecialPage
31 class SpecialRecentChangesLinked
extends SpecialRecentChanges
{
32 /** @var bool|Title */
33 protected $rclTargetTitle;
35 function __construct() {
36 parent
::__construct( 'Recentchangeslinked' );
39 public function getDefaultOptions() {
40 $opts = parent
::getDefaultOptions();
41 $opts->add( 'target', '' );
42 $opts->add( 'showlinkedto', false );
47 public function parseParameters( $par, FormOptions
$opts ) {
48 $opts['target'] = $par;
54 protected function doMainQuery( $tables, $select, $conds, $query_options,
55 $join_conds, FormOptions
$opts
57 $target = $opts['target'];
58 $showlinkedto = $opts['showlinkedto'];
59 $limit = $opts['limit'];
61 if ( $target === '' ) {
64 $outputPage = $this->getOutput();
65 $title = Title
::newFromText( $target );
66 if ( !$title ||
$title->isExternal() ) {
68 Html
::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
73 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
76 * Ordinary links are in the pagelinks table, while transclusions are
77 * in the templatelinks table, categorizations in categorylinks and
78 * image use in imagelinks. We need to somehow combine all these.
79 * Special:Whatlinkshere does this by firing multiple queries and
80 * merging the results, but the code we inherit from our parent class
81 * expects only one result set so we use UNION instead.
84 $dbr = wfGetDB( DB_REPLICA
, 'recentchangeslinked' );
85 $id = $title->getArticleID();
86 $ns = $title->getNamespace();
87 $dbkey = $title->getDBkey();
89 $rcQuery = RecentChange
::getQueryInfo();
90 $tables = array_merge( $tables, $rcQuery['tables'] );
91 $select = array_merge( $rcQuery['fields'], $select );
92 $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
94 // left join with watchlist table to highlight watched rows
95 $uid = $this->getUser()->getId();
96 if ( $uid && MediaWikiServices
::getInstance()
97 ->getPermissionManager()
98 ->userHasRight( $this->getUser(), 'viewmywatchlist' )
100 $tables[] = 'watchlist';
101 $select[] = 'wl_user';
102 $join_conds['watchlist'] = [ 'LEFT JOIN', [
105 'wl_namespace=rc_namespace'
109 // JOIN on page, used for 'last revision' filter highlight
111 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
112 $select[] = 'page_latest';
114 $tagFilter = $opts['tagfilter'] ?
explode( '|', $opts['tagfilter'] ) : [];
115 ChangeTags
::modifyDisplayQuery(
124 if ( $dbr->unionSupportsOrderAndLimit() ) {
125 if ( count( $tagFilter ) > 1 ) {
126 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
127 // To prevent this from causing query performance problems, we need to add
128 // a GROUP BY, and add rc_id to the ORDER BY.
130 'GROUP BY' => 'rc_timestamp, rc_id',
131 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
134 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
140 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
146 if ( $ns == NS_CATEGORY
&& !$showlinkedto ) {
147 // special handling for categories
148 // XXX: should try to make this less kludgy
149 $link_tables = [ 'categorylinks' ];
150 $showlinkedto = true;
152 // for now, always join on these tables; really should be configurable as in whatlinkshere
153 $link_tables = [ 'pagelinks', 'templatelinks' ];
154 // imagelinks only contains links to pages in NS_FILE
155 if ( $ns == NS_FILE ||
!$showlinkedto ) {
156 $link_tables[] = 'imagelinks';
160 if ( $id == 0 && !$showlinkedto ) {
161 return false; // nonexistent pages can't link to any pages
164 // field name prefixes for all the various tables we might want to join with
167 'templatelinks' => 'tl',
168 'categorylinks' => 'cl',
172 $subsql = []; // SELECT statements to combine with UNION
174 foreach ( $link_tables as $link_table ) {
175 $pfx = $prefix[$link_table];
177 // imagelinks and categorylinks tables have no xx_namespace field,
178 // and have xx_to instead of xx_title
179 if ( $link_table == 'imagelinks' ) {
181 } elseif ( $link_table == 'categorylinks' ) {
182 $link_ns = NS_CATEGORY
;
187 if ( $showlinkedto ) {
188 // find changes to pages linking to this page
190 if ( $ns != $link_ns ) {
192 } // should never happen, but check anyway
193 $subconds = [ "{$pfx}_to" => $dbkey ];
195 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
197 $subjoin = "rc_cur_id = {$pfx}_from";
199 // find changes to pages linked from this page
200 $subconds = [ "{$pfx}_from" => $id ];
201 if ( $link_table == 'imagelinks' ||
$link_table == 'categorylinks' ) {
202 $subconds["rc_namespace"] = $link_ns;
203 $subjoin = "rc_title = {$pfx}_to";
205 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
209 $query = $dbr->selectSQLText(
210 array_merge( $tables, [ $link_table ] ),
214 $order +
$query_options,
215 $join_conds +
[ $link_table => [ 'JOIN', $subjoin ] ]
218 if ( $dbr->unionSupportsOrderAndLimit() ) {
219 $query = $dbr->limitResult( $query, $limit );
225 if ( count( $subsql ) == 0 ) {
226 return false; // should never happen
228 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
231 // need to resort and relimit after union
232 $sql = $dbr->unionQueries( $subsql, $dbr::UNION_DISTINCT
) .
233 ' ORDER BY rc_timestamp DESC';
234 $sql = $dbr->limitResult( $sql, $limit, false );
237 return $dbr->query( $sql, __METHOD__
);
240 function setTopText( FormOptions
$opts ) {
241 $target = $this->getTargetTitle();
243 $this->getOutput()->addBacklinkSubtitle( $target );
244 $this->getSkin()->setRelevantTitle( $target );
249 * Get options to be displayed in a form
251 * @param FormOptions $opts
254 function getExtraOptions( $opts ) {
255 $extraOpts = parent
::getExtraOptions( $opts );
257 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
259 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
260 Xml
::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
261 Xml
::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
262 Xml
::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
264 $this->addHelpLink( 'Help:Related changes' );
271 function getTargetTitle() {
272 if ( $this->rclTargetTitle
=== null ) {
273 $opts = $this->getOptions();
274 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
275 $this->rclTargetTitle
= Title
::newFromText( $opts['target'] );
277 $this->rclTargetTitle
= false;
281 return $this->rclTargetTitle
;
285 * Return an array of subpages beginning with $search that this special page will accept.
287 * @param string $search Prefix to search for
288 * @param int $limit Maximum number of results to return (usually 10)
289 * @param int $offset Number of results to skip (usually 0)
290 * @return string[] Matching subpages
292 public function prefixSearchSubpages( $search, $limit, $offset ) {
293 return $this->prefixSearchString( $search, $limit, $offset );
296 protected function outputNoResults() {
297 $targetTitle = $this->getTargetTitle();
298 if ( $targetTitle === false ) {
299 $this->getOutput()->addHTML(
302 [ 'class' => 'mw-changeslist-empty mw-changeslist-notargetpage' ],
303 $this->msg( 'recentchanges-notargetpage' )->parse()
306 } elseif ( !$targetTitle ||
$targetTitle->isExternal() ) {
307 $this->getOutput()->addHTML(
310 [ 'class' => 'mw-changeslist-empty mw-changeslist-invalidtargetpage' ],
311 $this->msg( 'allpagesbadtitle' )->parse()
315 parent
::outputNoResults();