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;
33 function __construct() {
34 parent
::__construct( 'Recentchangeslinked' );
37 public function getDefaultOptions() {
38 $opts = parent
::getDefaultOptions();
39 $opts->add( 'target', '' );
40 $opts->add( 'showlinkedto', false );
45 public function parseParameters( $par, FormOptions
$opts ) {
46 $opts['target'] = $par;
52 protected function doMainQuery( $tables, $select, $conds, $query_options,
53 $join_conds, FormOptions
$opts
55 $target = $opts['target'];
56 $showlinkedto = $opts['showlinkedto'];
57 $limit = $opts['limit'];
59 if ( $target === '' ) {
62 $outputPage = $this->getOutput();
63 $title = Title
::newFromText( $target );
64 if ( !$title ||
$title->isExternal() ) {
65 $outputPage->addHTML( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )
66 ->parse() . '</div>' );
71 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
74 * Ordinary links are in the pagelinks table, while transclusions are
75 * in the templatelinks table, categorizations in categorylinks and
76 * image use in imagelinks. We need to somehow combine all these.
77 * Special:Whatlinkshere does this by firing multiple queries and
78 * merging the results, but the code we inherit from our parent class
79 * expects only one result set so we use UNION instead.
82 $dbr = wfGetDB( DB_REPLICA
, 'recentchangeslinked' );
83 $id = $title->getArticleID();
84 $ns = $title->getNamespace();
85 $dbkey = $title->getDBkey();
87 $tables[] = 'recentchanges';
88 $select = array_merge( RecentChange
::selectFields(), $select );
90 // left join with watchlist table to highlight watched rows
91 $uid = $this->getUser()->getId();
92 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
93 $tables[] = 'watchlist';
94 $select[] = 'wl_user';
95 $join_conds['watchlist'] = [ 'LEFT JOIN', [
98 'wl_namespace=rc_namespace'
101 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
103 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
104 $select[] = 'page_latest';
107 $tagFilter = explode( '|', $opts['tagfilter'] );
108 ChangeTags
::modifyDisplayQuery(
117 if ( $dbr->unionSupportsOrderAndLimit() ) {
118 if ( count( $tagFilter ) > 1 ) {
119 // ChangeTags::modifyDisplayQuery() will have added DISTINCT.
120 // To prevent this from causing query performance problems, we need to add
121 // a GROUP BY, and add rc_id to the ORDER BY.
123 'GROUP BY' => 'rc_timestamp, rc_id',
124 'ORDER BY' => 'rc_timestamp DESC, rc_id DESC'
127 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
133 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
139 if ( $ns == NS_CATEGORY
&& !$showlinkedto ) {
140 // special handling for categories
141 // XXX: should try to make this less kludgy
142 $link_tables = [ 'categorylinks' ];
143 $showlinkedto = true;
145 // for now, always join on these tables; really should be configurable as in whatlinkshere
146 $link_tables = [ 'pagelinks', 'templatelinks' ];
147 // imagelinks only contains links to pages in NS_FILE
148 if ( $ns == NS_FILE ||
!$showlinkedto ) {
149 $link_tables[] = 'imagelinks';
153 if ( $id == 0 && !$showlinkedto ) {
154 return false; // nonexistent pages can't link to any pages
157 // field name prefixes for all the various tables we might want to join with
160 'templatelinks' => 'tl',
161 'categorylinks' => 'cl',
165 $subsql = []; // SELECT statements to combine with UNION
167 foreach ( $link_tables as $link_table ) {
168 $pfx = $prefix[$link_table];
170 // imagelinks and categorylinks tables have no xx_namespace field,
171 // and have xx_to instead of xx_title
172 if ( $link_table == 'imagelinks' ) {
174 } elseif ( $link_table == 'categorylinks' ) {
175 $link_ns = NS_CATEGORY
;
180 if ( $showlinkedto ) {
181 // find changes to pages linking to this page
183 if ( $ns != $link_ns ) {
185 } // should never happen, but check anyway
186 $subconds = [ "{$pfx}_to" => $dbkey ];
188 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
190 $subjoin = "rc_cur_id = {$pfx}_from";
192 // find changes to pages linked from this page
193 $subconds = [ "{$pfx}_from" => $id ];
194 if ( $link_table == 'imagelinks' ||
$link_table == 'categorylinks' ) {
195 $subconds["rc_namespace"] = $link_ns;
196 $subjoin = "rc_title = {$pfx}_to";
198 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
202 $query = $dbr->selectSQLText(
203 array_merge( $tables, [ $link_table ] ),
207 $order +
$query_options,
208 $join_conds +
[ $link_table => [ 'INNER JOIN', $subjoin ] ]
211 if ( $dbr->unionSupportsOrderAndLimit() ) {
212 $query = $dbr->limitResult( $query, $limit );
218 if ( count( $subsql ) == 0 ) {
219 return false; // should never happen
221 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
224 // need to resort and relimit after union
225 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
226 $sql = $dbr->limitResult( $sql, $limit, false );
229 $res = $dbr->query( $sql, __METHOD__
);
231 if ( $res->numRows() == 0 ) {
232 $this->mResultEmpty
= true;
238 function setTopText( FormOptions
$opts ) {
239 $target = $this->getTargetTitle();
241 $this->getOutput()->addBacklinkSubtitle( $target );
242 $this->getSkin()->setRelevantTitle( $target );
247 * Get options to be displayed in a form
249 * @param FormOptions $opts
252 function getExtraOptions( $opts ) {
253 $extraOpts = parent
::getExtraOptions( $opts );
255 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
257 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
258 Xml
::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
259 Xml
::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
260 Xml
::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
262 $this->addHelpLink( 'Help:Related changes' );
269 function getTargetTitle() {
270 if ( $this->rclTargetTitle
=== null ) {
271 $opts = $this->getOptions();
272 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
273 $this->rclTargetTitle
= Title
::newFromText( $opts['target'] );
275 $this->rclTargetTitle
= false;
279 return $this->rclTargetTitle
;
283 * Return an array of subpages beginning with $search that this special page will accept.
285 * @param string $search Prefix to search for
286 * @param int $limit Maximum number of results to return (usually 10)
287 * @param int $offset Number of results to skip (usually 0)
288 * @return string[] Matching subpages
290 public function prefixSearchSubpages( $search, $limit, $offset ) {
291 return $this->prefixSearchString( $search, $limit, $offset );