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
25 * This is to display changes made to all articles linked in an article.
27 * @ingroup SpecialPage
29 class SpecialRecentChangesLinked
extends SpecialRecentChanges
{
30 /** @var bool|Title */
31 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;
49 $this->rclTarget
= $par;
55 protected function doMainQuery( $tables, $select, $conds, $query_options,
56 $join_conds, FormOptions
$opts
58 $target = $opts['target'];
59 $showlinkedto = $opts['showlinkedto'];
60 $limit = $opts['limit'];
62 if ( $target === '' ) {
65 $outputPage = $this->getOutput();
66 $title = Title
::newFromText( $target );
67 if ( !$title ||
$title->isExternal() ) {
69 Html
::errorBox( $this->msg( 'allpagesbadtitle' )->parse() )
75 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
78 * Ordinary links are in the pagelinks table, while transclusions are
79 * in the templatelinks table, categorizations in categorylinks and
80 * image use in imagelinks. We need to somehow combine all these.
81 * Special:Whatlinkshere does this by firing multiple queries and
82 * merging the results, but the code we inherit from our parent class
83 * expects only one result set so we use UNION instead.
86 $dbr = wfGetDB( DB_REPLICA
, 'recentchangeslinked' );
87 $id = $title->getArticleID();
88 $ns = $title->getNamespace();
89 $dbkey = $title->getDBkey();
91 $rcQuery = RecentChange
::getQueryInfo();
92 $tables = array_merge( $tables, $rcQuery['tables'] );
93 $select = array_merge( $rcQuery['fields'], $select );
94 $join_conds = array_merge( $join_conds, $rcQuery['joins'] );
96 // left join with watchlist table to highlight watched rows
97 $uid = $this->getUser()->getId();
98 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
99 $tables[] = 'watchlist';
100 $select[] = 'wl_user';
101 $join_conds['watchlist'] = [ 'LEFT JOIN', [
104 'wl_namespace=rc_namespace'
108 // JOIN on page, used for 'last revision' filter highlight
110 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
111 $select[] = 'page_latest';
113 $tagFilter = $opts['tagfilter'] ?
explode( '|', $opts['tagfilter'] ) : [];
114 ChangeTags
::modifyDisplayQuery(
123 if ( $dbr->unionSupportsOrderAndLimit() ) {
124 if ( count( $tagFilter ) > 1 ) {
125 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
126 // To prevent this from causing query performance problems, we need to add
127 // a GROUP BY, and add rc_id to the ORDER BY.
129 'GROUP BY' => 'rc_timestamp, rc_id',
130 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
133 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
139 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
145 if ( $ns == NS_CATEGORY
&& !$showlinkedto ) {
146 // special handling for categories
147 // XXX: should try to make this less kludgy
148 $link_tables = [ 'categorylinks' ];
149 $showlinkedto = true;
151 // for now, always join on these tables; really should be configurable as in whatlinkshere
152 $link_tables = [ 'pagelinks', 'templatelinks' ];
153 // imagelinks only contains links to pages in NS_FILE
154 if ( $ns == NS_FILE ||
!$showlinkedto ) {
155 $link_tables[] = 'imagelinks';
159 if ( $id == 0 && !$showlinkedto ) {
160 return false; // nonexistent pages can't link to any pages
163 // field name prefixes for all the various tables we might want to join with
166 'templatelinks' => 'tl',
167 'categorylinks' => 'cl',
171 $subsql = []; // SELECT statements to combine with UNION
173 foreach ( $link_tables as $link_table ) {
174 $pfx = $prefix[$link_table];
176 // imagelinks and categorylinks tables have no xx_namespace field,
177 // and have xx_to instead of xx_title
178 if ( $link_table == 'imagelinks' ) {
180 } elseif ( $link_table == 'categorylinks' ) {
181 $link_ns = NS_CATEGORY
;
186 if ( $showlinkedto ) {
187 // find changes to pages linking to this page
189 if ( $ns != $link_ns ) {
191 } // should never happen, but check anyway
192 $subconds = [ "{$pfx}_to" => $dbkey ];
194 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
196 $subjoin = "rc_cur_id = {$pfx}_from";
198 // find changes to pages linked from this page
199 $subconds = [ "{$pfx}_from" => $id ];
200 if ( $link_table == 'imagelinks' ||
$link_table == 'categorylinks' ) {
201 $subconds["rc_namespace"] = $link_ns;
202 $subjoin = "rc_title = {$pfx}_to";
204 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
208 $query = $dbr->selectSQLText(
209 array_merge( $tables, [ $link_table ] ),
213 $order +
$query_options,
214 $join_conds +
[ $link_table => [ 'INNER JOIN', $subjoin ] ]
217 if ( $dbr->unionSupportsOrderAndLimit() ) {
218 $query = $dbr->limitResult( $query, $limit );
224 if ( count( $subsql ) == 0 ) {
225 return false; // should never happen
227 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
230 // need to resort and relimit after union
231 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
232 $sql = $dbr->limitResult( $sql, $limit, false );
235 $res = $dbr->query( $sql, __METHOD__
);
237 if ( $res->numRows() == 0 ) {
238 $this->mResultEmpty
= true;
244 function setTopText( FormOptions
$opts ) {
245 $target = $this->getTargetTitle();
247 $this->getOutput()->addBacklinkSubtitle( $target );
248 $this->getSkin()->setRelevantTitle( $target );
253 * Get options to be displayed in a form
255 * @param FormOptions $opts
258 function getExtraOptions( $opts ) {
259 $extraOpts = parent
::getExtraOptions( $opts );
261 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
263 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
264 Xml
::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
265 Xml
::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
266 Xml
::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
268 $this->addHelpLink( 'Help:Related changes' );
275 function getTargetTitle() {
276 if ( $this->rclTargetTitle
=== null ) {
277 $opts = $this->getOptions();
278 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
279 $this->rclTargetTitle
= Title
::newFromText( $opts['target'] );
281 $this->rclTargetTitle
= false;
285 return $this->rclTargetTitle
;
289 * Return an array of subpages beginning with $search that this special page will accept.
291 * @param string $search Prefix to search for
292 * @param int $limit Maximum number of results to return (usually 10)
293 * @param int $offset Number of results to skip (usually 0)
294 * @return string[] Matching subpages
296 public function prefixSearchSubpages( $search, $limit, $offset ) {
297 return $this->prefixSearchString( $search, $limit, $offset );
301 * Get a self-referential title object
302 * with consideration to the given subpage.
304 * @param string|bool $subpage
308 public function getPageTitle( $subpage = false ) {
309 $subpage = $subpage ?
$subpage : $this->rclTarget
;
311 return parent
::getPageTitle( $subpage );