Merge "Propagate the favicon information to getInfo()"
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchangeslinked.php
1 <?php
2 /**
3 * Implements Special:Recentchangeslinked
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup SpecialPage
22 */
23
24 /**
25 * This is to display changes made to all articles linked in an article.
26 *
27 * @ingroup SpecialPage
28 */
29 class SpecialRecentChangesLinked extends SpecialRecentChanges {
30 var $rclTargetTitle;
31
32 function __construct() {
33 parent::__construct( 'Recentchangeslinked' );
34 }
35
36 public function getDefaultOptions() {
37 $opts = parent::getDefaultOptions();
38 $opts->add( 'target', '' );
39 $opts->add( 'showlinkedto', false );
40 return $opts;
41 }
42
43 public function parseParameters( $par, FormOptions $opts ) {
44 $opts['target'] = $par;
45 }
46
47 public function doMainQuery( $conds, $opts ) {
48 $target = $opts['target'];
49 $showlinkedto = $opts['showlinkedto'];
50 $limit = $opts['limit'];
51
52 if ( $target === '' ) {
53 return false;
54 }
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>' );
59 return false;
60 }
61
62 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
63
64 /*
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.
71 */
72
73 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
74 $id = $title->getArticleID();
75 $ns = $title->getNamespace();
76 $dbkey = $title->getDBkey();
77
78 $tables = array( 'recentchanges' );
79 $select = RecentChange::selectFields();
80 $join_conds = array();
81 $query_options = array();
82
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(
89 'wl_user' => $uid,
90 'wl_title=rc_title',
91 'wl_namespace=rc_namespace'
92 ));
93 }
94 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
95 $tables[] = 'page';
96 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
97 $select[] = 'page_latest';
98 }
99 ChangeTags::modifyDisplayQuery(
100 $tables,
101 $select,
102 $conds,
103 $join_conds,
104 $query_options,
105 $opts['tagfilter']
106 );
107
108 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
109 return false;
110 }
111
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;
117 } else {
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';
123 }
124 }
125
126 if ( $id == 0 && !$showlinkedto ) {
127 return false; // nonexistent pages can't link to any pages
128 }
129
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' );
132
133 $subsql = array(); // SELECT statements to combine with UNION
134
135 foreach ( $link_tables as $link_table ) {
136 $pfx = $prefix[$link_table];
137
138 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
139 if ( $link_table == 'imagelinks' ) {
140 $link_ns = NS_FILE;
141 } elseif ( $link_table == 'categorylinks' ) {
142 $link_ns = NS_CATEGORY;
143 } else {
144 $link_ns = 0;
145 }
146
147 if ( $showlinkedto ) {
148 // find changes to pages linking to this page
149 if ( $link_ns ) {
150 if ( $ns != $link_ns ) {
151 continue;
152 } // should never happen, but check anyway
153 $subconds = array( "{$pfx}_to" => $dbkey );
154 } else {
155 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
156 }
157 $subjoin = "rc_cur_id = {$pfx}_from";
158 } else {
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";
164 } else {
165 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
166 }
167 }
168
169 if ( $dbr->unionSupportsOrderAndLimit() ) {
170 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
171 } else {
172 $order = array();
173 }
174
175 $query = $dbr->selectSQLText(
176 array_merge( $tables, array( $link_table ) ),
177 $select,
178 $conds + $subconds,
179 __METHOD__,
180 $order + $query_options,
181 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
182 );
183
184 if ( $dbr->unionSupportsOrderAndLimit() ) {
185 $query = $dbr->limitResult( $query, $limit );
186 }
187
188 $subsql[] = $query;
189 }
190
191 if ( count( $subsql ) == 0 ) {
192 return false; // should never happen
193 }
194 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
195 $sql = $subsql[0];
196 } else {
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 );
200 }
201
202 $res = $dbr->query( $sql, __METHOD__ );
203
204 if ( $res->numRows() == 0 ) {
205 $this->mResultEmpty = true;
206 }
207
208 return $res;
209 }
210
211 function setTopText( FormOptions $opts ) {
212 $target = $this->getTargetTitle();
213 if ( $target ) {
214 $this->getOutput()->addBacklinkSubtitle( $target );
215 }
216 }
217
218 /**
219 * Get options to be displayed in a form
220 *
221 * @param FormOptions $opts
222 * @return array
223 */
224 function getExtraOptions( $opts ) {
225 $extraOpts = parent::getExtraOptions( $opts );
226
227 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
228
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' ) );
233
234 return $extraOpts;
235 }
236
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()
244 );
245 return array( $feed, $feedObj );
246 }
247
248 /**
249 * @return Title
250 */
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'] );
256 } else {
257 $this->rclTargetTitle = false;
258 }
259 }
260 return $this->rclTargetTitle;
261 }
262 }