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 getFeedObject( $feedFormat ) {
48 $feed = new ChangesFeed( $feedFormat, false );
49 $feedObj = $feed->getFeedObject(
50 $this->msg( 'recentchangeslinked-title', $this->getTargetTitle()->getPrefixedText() )
51 ->inContentLanguage()->text(),
52 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
53 $this->getPageTitle()->getFullURL()
55 return array( $feed, $feedObj );
58 public function doMainQuery( $conds, $opts ) {
59 $target = $opts['target'];
60 $showlinkedto = $opts['showlinkedto'];
61 $limit = $opts['limit'];
63 if ( $target === '' ) {
66 $outputPage = $this->getOutput();
67 $title = Title
::newFromURL( $target );
68 if ( !$title ||
$title->isExternal() ) {
69 $outputPage->addHtml( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )->parse() . '</div>' );
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_SLAVE
, 'recentchangeslinked' );
85 $id = $title->getArticleID();
86 $ns = $title->getNamespace();
87 $dbkey = $title->getDBkey();
89 $tables = array( 'recentchanges' );
90 $select = RecentChange
::selectFields();
91 $join_conds = array();
92 $query_options = array();
94 // left join with watchlist table to highlight watched rows
95 $uid = $this->getUser()->getId();
96 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
97 $tables[] = 'watchlist';
98 $select[] = 'wl_user';
99 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
102 'wl_namespace=rc_namespace'
105 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
107 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
108 $select[] = 'page_latest';
110 ChangeTags
::modifyDisplayQuery(
119 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
123 if ( $ns == NS_CATEGORY
&& !$showlinkedto ) {
124 // special handling for categories
125 // XXX: should try to make this less kludgy
126 $link_tables = array( 'categorylinks' );
127 $showlinkedto = true;
129 // for now, always join on these tables; really should be configurable as in whatlinkshere
130 $link_tables = array( 'pagelinks', 'templatelinks' );
131 // imagelinks only contains links to pages in NS_FILE
132 if ( $ns == NS_FILE ||
!$showlinkedto ) {
133 $link_tables[] = 'imagelinks';
137 if ( $id == 0 && !$showlinkedto ) {
138 return false; // nonexistent pages can't link to any pages
141 // field name prefixes for all the various tables we might want to join with
142 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
144 $subsql = array(); // SELECT statements to combine with UNION
146 foreach ( $link_tables as $link_table ) {
147 $pfx = $prefix[$link_table];
149 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
150 if ( $link_table == 'imagelinks' ) {
152 } elseif ( $link_table == 'categorylinks' ) {
153 $link_ns = NS_CATEGORY
;
158 if ( $showlinkedto ) {
159 // find changes to pages linking to this page
161 if ( $ns != $link_ns ) {
163 } // should never happen, but check anyway
164 $subconds = array( "{$pfx}_to" => $dbkey );
166 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
168 $subjoin = "rc_cur_id = {$pfx}_from";
170 // find changes to pages linked from this page
171 $subconds = array( "{$pfx}_from" => $id );
172 if ( $link_table == 'imagelinks' ||
$link_table == 'categorylinks' ) {
173 $subconds["rc_namespace"] = $link_ns;
174 $subjoin = "rc_title = {$pfx}_to";
176 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
180 if ( $dbr->unionSupportsOrderAndLimit() ) {
181 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
186 $query = $dbr->selectSQLText(
187 array_merge( $tables, array( $link_table ) ),
191 $order +
$query_options,
192 $join_conds +
array( $link_table => array( 'INNER JOIN', $subjoin ) )
195 if ( $dbr->unionSupportsOrderAndLimit() ) {
196 $query = $dbr->limitResult( $query, $limit );
202 if ( count( $subsql ) == 0 ) {
203 return false; // should never happen
205 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
208 // need to resort and relimit after union
209 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
210 $sql = $dbr->limitResult( $sql, $limit, false );
213 $res = $dbr->query( $sql, __METHOD__
);
215 if ( $res->numRows() == 0 ) {
216 $this->mResultEmpty
= true;
223 * Get options to be displayed in a form
225 * @param FormOptions $opts
228 function getExtraOptions( $opts ) {
229 $extraOpts = parent
::getExtraOptions( $opts );
231 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
233 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
234 Xml
::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
235 Xml
::check( 'showlinkedto', $opts['showlinkedto'], array( 'id' => 'showlinkedto' ) ) . ' ' .
236 Xml
::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
244 function getTargetTitle() {
245 if ( $this->rclTargetTitle
=== null ) {
246 $opts = $this->getOptions();
247 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
248 $this->rclTargetTitle
= Title
::newFromText( $opts['target'] );
250 $this->rclTargetTitle
= false;
253 return $this->rclTargetTitle
;
256 function setTopText( FormOptions
$opts ) {
257 $target = $this->getTargetTitle();
259 $this->getOutput()->addBacklinkSubtitle( $target );