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 );
44 public function parseParameters( $par, FormOptions
$opts ) {
45 $opts['target'] = $par;
48 public function doMainQuery( $conds, $opts ) {
49 $target = $opts['target'];
50 $showlinkedto = $opts['showlinkedto'];
51 $limit = $opts['limit'];
53 if ( $target === '' ) {
56 $outputPage = $this->getOutput();
57 $title = Title
::newFromURL( $target );
58 if ( !$title ||
$title->isExternal() ) {
59 $outputPage->addHtml( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )
60 ->parse() . '</div>' );
64 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
67 * Ordinary links are in the pagelinks table, while transclusions are
68 * in the templatelinks table, categorizations in categorylinks and
69 * image use in imagelinks. We need to somehow combine all these.
70 * Special:Whatlinkshere does this by firing multiple queries and
71 * merging the results, but the code we inherit from our parent class
72 * expects only one result set so we use UNION instead.
75 $dbr = wfGetDB( DB_SLAVE
, 'recentchangeslinked' );
76 $id = $title->getArticleID();
77 $ns = $title->getNamespace();
78 $dbkey = $title->getDBkey();
80 $tables = array( 'recentchanges' );
81 $select = RecentChange
::selectFields();
82 $join_conds = array();
83 $query_options = array();
85 // left join with watchlist table to highlight watched rows
86 $uid = $this->getUser()->getId();
87 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
88 $tables[] = 'watchlist';
89 $select[] = 'wl_user';
90 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
93 'wl_namespace=rc_namespace'
96 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
98 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
99 $select[] = 'page_latest';
101 ChangeTags
::modifyDisplayQuery(
110 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
111 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) )
116 if ( $ns == NS_CATEGORY
&& !$showlinkedto ) {
117 // special handling for categories
118 // XXX: should try to make this less kludgy
119 $link_tables = array( 'categorylinks' );
120 $showlinkedto = true;
122 // for now, always join on these tables; really should be configurable as in whatlinkshere
123 $link_tables = array( 'pagelinks', 'templatelinks' );
124 // imagelinks only contains links to pages in NS_FILE
125 if ( $ns == NS_FILE ||
!$showlinkedto ) {
126 $link_tables[] = 'imagelinks';
130 if ( $id == 0 && !$showlinkedto ) {
131 return false; // nonexistent pages can't link to any pages
134 // field name prefixes for all the various tables we might want to join with
137 'templatelinks' => 'tl',
138 'categorylinks' => 'cl',
142 $subsql = array(); // SELECT statements to combine with UNION
144 foreach ( $link_tables as $link_table ) {
145 $pfx = $prefix[$link_table];
147 // imagelinks and categorylinks tables have no xx_namespace field,
148 // and have xx_to instead of xx_title
149 if ( $link_table == 'imagelinks' ) {
151 } elseif ( $link_table == 'categorylinks' ) {
152 $link_ns = NS_CATEGORY
;
157 if ( $showlinkedto ) {
158 // find changes to pages linking to this page
160 if ( $ns != $link_ns ) {
162 } // should never happen, but check anyway
163 $subconds = array( "{$pfx}_to" => $dbkey );
165 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
167 $subjoin = "rc_cur_id = {$pfx}_from";
169 // find changes to pages linked from this page
170 $subconds = array( "{$pfx}_from" => $id );
171 if ( $link_table == 'imagelinks' ||
$link_table == 'categorylinks' ) {
172 $subconds["rc_namespace"] = $link_ns;
173 $subjoin = "rc_title = {$pfx}_to";
175 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
179 if ( $dbr->unionSupportsOrderAndLimit() ) {
180 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
185 $query = $dbr->selectSQLText(
186 array_merge( $tables, array( $link_table ) ),
190 $order +
$query_options,
191 $join_conds +
array( $link_table => array( 'INNER JOIN', $subjoin ) )
194 if ( $dbr->unionSupportsOrderAndLimit() ) {
195 $query = $dbr->limitResult( $query, $limit );
201 if ( count( $subsql ) == 0 ) {
202 return false; // should never happen
204 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
207 // need to resort and relimit after union
208 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
209 $sql = $dbr->limitResult( $sql, $limit, false );
212 $res = $dbr->query( $sql, __METHOD__
);
214 if ( $res->numRows() == 0 ) {
215 $this->mResultEmpty
= true;
221 function setTopText( FormOptions
$opts ) {
222 $target = $this->getTargetTitle();
224 $this->getOutput()->addBacklinkSubtitle( $target );
229 * Get options to be displayed in a form
231 * @param FormOptions $opts
234 function getExtraOptions( $opts ) {
235 $extraOpts = parent
::getExtraOptions( $opts );
237 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
239 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
240 Xml
::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
241 Xml
::check( 'showlinkedto', $opts['showlinkedto'], array( 'id' => 'showlinkedto' ) ) . ' ' .
242 Xml
::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
250 function getTargetTitle() {
251 if ( $this->rclTargetTitle
=== null ) {
252 $opts = $this->getOptions();
253 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
254 $this->rclTargetTitle
= Title
::newFromText( $opts['target'] );
256 $this->rclTargetTitle
= false;
259 return $this->rclTargetTitle
;