Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / specials / SpecialRecentchangeslinked.php
blob062e09dadb8010d1553f84f30639d65f26cbe77c
1 <?php
2 /**
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
20 * @file
21 * @ingroup SpecialPage
24 /**
25 * This is to display changes made to all articles linked in an article.
27 * @ingroup SpecialPage
29 class SpecialRecentchangeslinked extends SpecialRecentChanges {
30 var $rclTargetTitle;
32 function __construct() {
33 parent::__construct( 'Recentchangeslinked' );
36 public function getDefaultOptions() {
37 $opts = parent::getDefaultOptions();
38 $opts->add( 'target', '' );
39 $opts->add( 'showlinkedto', false );
40 return $opts;
43 public function parseParameters( $par, FormOptions $opts ) {
44 $opts['target'] = $par;
47 public function feedSetup() {
48 $opts = parent::feedSetup();
49 $opts['target'] = $this->getRequest()->getVal( 'target' );
50 return $opts;
53 public function getFeedObject( $feedFormat ) {
54 $feed = new ChangesFeed( $feedFormat, false );
55 $feedObj = $feed->getFeedObject(
56 $this->msg( 'recentchangeslinked-title', $this->getTargetTitle()->getPrefixedText() )
57 ->inContentLanguage()->text(),
58 $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
59 $this->getTitle()->getFullURL()
61 return array( $feed, $feedObj );
64 public function doMainQuery( $conds, $opts ) {
65 $target = $opts['target'];
66 $showlinkedto = $opts['showlinkedto'];
67 $limit = $opts['limit'];
69 if ( $target === '' ) {
70 return false;
72 $outputPage = $this->getOutput();
73 $title = Title::newFromURL( $target );
74 if ( !$title || $title->getInterwiki() != '' ) {
75 $outputPage->wrapWikiMsg( "<div class=\"errorbox\">\n$1\n</div><br style=\"clear: both\" />", 'allpagesbadtitle' );
76 return false;
79 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
82 * Ordinary links are in the pagelinks table, while transclusions are
83 * in the templatelinks table, categorizations in categorylinks and
84 * image use in imagelinks. We need to somehow combine all these.
85 * Special:Whatlinkshere does this by firing multiple queries and
86 * merging the results, but the code we inherit from our parent class
87 * expects only one result set so we use UNION instead.
90 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
91 $id = $title->getArticleID();
92 $ns = $title->getNamespace();
93 $dbkey = $title->getDBkey();
95 $tables = array( 'recentchanges' );
96 $select = RecentChange::selectFields();
97 $join_conds = array();
98 $query_options = array();
100 // left join with watchlist table to highlight watched rows
101 $uid = $this->getUser()->getId();
102 if ( $uid ) {
103 $tables[] = 'watchlist';
104 $select[] = 'wl_user';
105 $join_conds['watchlist'] = array( 'LEFT JOIN', array(
106 'wl_user' => $uid,
107 'wl_title=rc_title',
108 'wl_namespace=rc_namespace'
111 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
112 $tables[] = 'page';
113 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
114 $select[] = 'page_latest';
116 ChangeTags::modifyDisplayQuery(
117 $tables,
118 $select,
119 $conds,
120 $join_conds,
121 $query_options,
122 $opts['tagfilter']
125 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
126 return false;
129 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
130 // special handling for categories
131 // XXX: should try to make this less kludgy
132 $link_tables = array( 'categorylinks' );
133 $showlinkedto = true;
134 } else {
135 // for now, always join on these tables; really should be configurable as in whatlinkshere
136 $link_tables = array( 'pagelinks', 'templatelinks' );
137 // imagelinks only contains links to pages in NS_FILE
138 if ( $ns == NS_FILE || !$showlinkedto ) {
139 $link_tables[] = 'imagelinks';
143 if ( $id == 0 && !$showlinkedto ) {
144 return false; // nonexistent pages can't link to any pages
147 // field name prefixes for all the various tables we might want to join with
148 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
150 $subsql = array(); // SELECT statements to combine with UNION
152 foreach ( $link_tables as $link_table ) {
153 $pfx = $prefix[$link_table];
155 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
156 if ( $link_table == 'imagelinks' ) {
157 $link_ns = NS_FILE;
158 } elseif ( $link_table == 'categorylinks' ) {
159 $link_ns = NS_CATEGORY;
160 } else {
161 $link_ns = 0;
164 if ( $showlinkedto ) {
165 // find changes to pages linking to this page
166 if ( $link_ns ) {
167 if ( $ns != $link_ns ) {
168 continue;
169 } // should never happen, but check anyway
170 $subconds = array( "{$pfx}_to" => $dbkey );
171 } else {
172 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
174 $subjoin = "rc_cur_id = {$pfx}_from";
175 } else {
176 // find changes to pages linked from this page
177 $subconds = array( "{$pfx}_from" => $id );
178 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
179 $subconds["rc_namespace"] = $link_ns;
180 $subjoin = "rc_title = {$pfx}_to";
181 } else {
182 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
186 if ( $dbr->unionSupportsOrderAndLimit() ) {
187 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
188 } else {
189 $order = array();
192 $query = $dbr->selectSQLText(
193 array_merge( $tables, array( $link_table ) ),
194 $select,
195 $conds + $subconds,
196 __METHOD__,
197 $order + $query_options,
198 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
201 if ( $dbr->unionSupportsOrderAndLimit() ) {
202 $query = $dbr->limitResult( $query, $limit );
205 $subsql[] = $query;
208 if ( count( $subsql ) == 0 ) {
209 return false; // should never happen
211 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
212 $sql = $subsql[0];
213 } else {
214 // need to resort and relimit after union
215 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
216 $sql = $dbr->limitResult( $sql, $limit, false );
219 $res = $dbr->query( $sql, __METHOD__ );
221 if ( $res->numRows() == 0 ) {
222 $this->mResultEmpty = true;
225 return $res;
229 * @param $opts FormOptions
230 * @return array
232 function getExtraOptions( $opts ) {
233 $opts->consumeValues( array( 'showlinkedto', 'target', 'tagfilter' ) );
234 $extraOpts = array();
235 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
236 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
237 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
238 Xml::check( 'showlinkedto', $opts['showlinkedto'], array( 'id' => 'showlinkedto' ) ) . ' ' .
239 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
240 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
241 if ( $tagFilter ) {
242 $extraOpts['tagfilter'] = $tagFilter;
244 return $extraOpts;
248 * @return Title
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'] );
255 } else {
256 $this->rclTargetTitle = false;
259 return $this->rclTargetTitle;
262 function setTopText( FormOptions $opts ) {
263 $target = $this->getTargetTitle();
264 if ( $target ) {
265 $this->getOutput()->addBacklinkSubtitle( $target );
269 function setBottomText( FormOptions $opts ) {
270 if ( isset( $this->mResultEmpty ) && $this->mResultEmpty ) {
271 $this->getOutput()->addWikiMsg( 'recentchangeslinked-noresult' );