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
{
32 function __construct() {
33 parent
::__construct( 'Recentchangeslinked' );
36 public function getDefaultOptions() {
37 $opts = parent
::getDefaultOptions();
38 $opts->add( 'target', '' );
39 $opts->add( 'showlinkedto', false );
43 public function parseParameters( $par, FormOptions
$opts ) {
44 $opts['target'] = $par;
47 public function doMainQuery( $conds, $opts ) {
48 $target = $opts['target'];
49 $showlinkedto = $opts['showlinkedto'];
50 $limit = $opts['limit'];
52 if ( $target === '' ) {
55 $outputPage = $this->getOutput();
56 $title = Title
::newFromURL( $target );
57 if ( !$title ||
$title->isExternal() ) {
58 $outputPage->addHtml( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )->parse() . '</div>' );
62 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
65 * Ordinary links are in the pagelinks table, while transclusions are
66 * in the templatelinks table, categorizations in categorylinks and
67 * image use in imagelinks. We need to somehow combine all these.
68 * Special:Whatlinkshere does this by firing multiple queries and
69 * merging the results, but the code we inherit from our parent class
70 * expects only one result set so we use UNION instead.
73 $dbr = wfGetDB( DB_SLAVE
, 'recentchangeslinked' );
74 $id = $title->getArticleID();
75 $ns = $title->getNamespace();
76 $dbkey = $title->getDBkey();
78 $tables = array( 'recentchanges' );
79 $select = RecentChange
::selectFields();
80 $join_conds = array();
81 $query_options = array();
83 // left join with watchlist table to highlight watched rows
84 $uid = $this->getUser()->getId();
85 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
86 $tables[] = 'watchlist';
87 $select[] = 'wl_user';
88 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
91 'wl_namespace=rc_namespace'
94 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
96 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
97 $select[] = 'page_latest';
99 ChangeTags
::modifyDisplayQuery(
108 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
112 if ( $ns == NS_CATEGORY
&& !$showlinkedto ) {
113 // special handling for categories
114 // XXX: should try to make this less kludgy
115 $link_tables = array( 'categorylinks' );
116 $showlinkedto = true;
118 // for now, always join on these tables; really should be configurable as in whatlinkshere
119 $link_tables = array( 'pagelinks', 'templatelinks' );
120 // imagelinks only contains links to pages in NS_FILE
121 if ( $ns == NS_FILE ||
!$showlinkedto ) {
122 $link_tables[] = 'imagelinks';
126 if ( $id == 0 && !$showlinkedto ) {
127 return false; // nonexistent pages can't link to any pages
130 // field name prefixes for all the various tables we might want to join with
131 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
133 $subsql = array(); // SELECT statements to combine with UNION
135 foreach ( $link_tables as $link_table ) {
136 $pfx = $prefix[$link_table];
138 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
139 if ( $link_table == 'imagelinks' ) {
141 } elseif ( $link_table == 'categorylinks' ) {
142 $link_ns = NS_CATEGORY
;
147 if ( $showlinkedto ) {
148 // find changes to pages linking to this page
150 if ( $ns != $link_ns ) {
152 } // should never happen, but check anyway
153 $subconds = array( "{$pfx}_to" => $dbkey );
155 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
157 $subjoin = "rc_cur_id = {$pfx}_from";
159 // find changes to pages linked from this page
160 $subconds = array( "{$pfx}_from" => $id );
161 if ( $link_table == 'imagelinks' ||
$link_table == 'categorylinks' ) {
162 $subconds["rc_namespace"] = $link_ns;
163 $subjoin = "rc_title = {$pfx}_to";
165 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
169 if ( $dbr->unionSupportsOrderAndLimit() ) {
170 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
175 $query = $dbr->selectSQLText(
176 array_merge( $tables, array( $link_table ) ),
180 $order +
$query_options,
181 $join_conds +
array( $link_table => array( 'INNER JOIN', $subjoin ) )
184 if ( $dbr->unionSupportsOrderAndLimit() ) {
185 $query = $dbr->limitResult( $query, $limit );
191 if ( count( $subsql ) == 0 ) {
192 return false; // should never happen
194 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
197 // need to resort and relimit after union
198 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
199 $sql = $dbr->limitResult( $sql, $limit, false );
202 $res = $dbr->query( $sql, __METHOD__
);
204 if ( $res->numRows() == 0 ) {
205 $this->mResultEmpty
= true;
211 function setTopText( FormOptions
$opts ) {
212 $target = $this->getTargetTitle();
214 $this->getOutput()->addBacklinkSubtitle( $target );
219 * Get options to be displayed in a form
221 * @param FormOptions $opts
224 function getExtraOptions( $opts ) {
225 $extraOpts = parent
::getExtraOptions( $opts );
227 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
229 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
230 Xml
::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
231 Xml
::check( 'showlinkedto', $opts['showlinkedto'], array( 'id' => 'showlinkedto' ) ) . ' ' .
232 Xml
::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
237 public function getFeedObject( $feedFormat ) {
238 $feed = new ChangesFeed( $feedFormat, false );
239 $feedObj = $feed->getFeedObject(
240 $this->msg( 'recentchangeslinked-title', $this->getTargetTitle()->getPrefixedText() )
241 ->inContentLanguage()->text(),
242 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
243 $this->getPageTitle()->getFullURL()
245 return array( $feed, $feedObj );
251 function getTargetTitle() {
252 if ( $this->rclTargetTitle
=== null ) {
253 $opts = $this->getOptions();
254 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
255 $this->rclTargetTitle
= Title
::newFromText( $opts['target'] );
257 $this->rclTargetTitle
= false;
260 return $this->rclTargetTitle
;