Merge "Send the full title to the 'nogomatch' debug log group"
[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 bool|Title */
31 protected $rclTargetTitle;
32
33 function __construct() {
34 parent::__construct( 'Recentchangeslinked' );
35 }
36
37 public function getDefaultOptions() {
38 $opts = parent::getDefaultOptions();
39 $opts->add( 'target', '' );
40 $opts->add( 'showlinkedto', false );
41 return $opts;
42 }
43
44 public function parseParameters( $par, FormOptions $opts ) {
45 $opts['target'] = $par;
46 }
47
48 public function doMainQuery( $conds, $opts ) {
49 $target = $opts['target'];
50 $showlinkedto = $opts['showlinkedto'];
51 $limit = $opts['limit'];
52
53 if ( $target === '' ) {
54 return false;
55 }
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>' );
61 return false;
62 }
63
64 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
65
66 /*
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.
73 */
74
75 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
76 $id = $title->getArticleID();
77 $ns = $title->getNamespace();
78 $dbkey = $title->getDBkey();
79
80 $tables = array( 'recentchanges' );
81 $select = RecentChange::selectFields();
82 $join_conds = array();
83 $query_options = array();
84
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(
91 'wl_user' => $uid,
92 'wl_title=rc_title',
93 'wl_namespace=rc_namespace'
94 ));
95 }
96 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
97 $tables[] = 'page';
98 $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
99 $select[] = 'page_latest';
100 }
101 ChangeTags::modifyDisplayQuery(
102 $tables,
103 $select,
104 $conds,
105 $join_conds,
106 $query_options,
107 $opts['tagfilter']
108 );
109
110 if ( !wfRunHooks( 'SpecialRecentChangesQuery',
111 array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) )
112 ) {
113 return false;
114 }
115
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;
121 } else {
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';
127 }
128 }
129
130 if ( $id == 0 && !$showlinkedto ) {
131 return false; // nonexistent pages can't link to any pages
132 }
133
134 // field name prefixes for all the various tables we might want to join with
135 $prefix = array(
136 'pagelinks' => 'pl',
137 'templatelinks' => 'tl',
138 'categorylinks' => 'cl',
139 'imagelinks' => 'il'
140 );
141
142 $subsql = array(); // SELECT statements to combine with UNION
143
144 foreach ( $link_tables as $link_table ) {
145 $pfx = $prefix[$link_table];
146
147 // imagelinks and categorylinks tables have no xx_namespace field,
148 // and have xx_to instead of xx_title
149 if ( $link_table == 'imagelinks' ) {
150 $link_ns = NS_FILE;
151 } elseif ( $link_table == 'categorylinks' ) {
152 $link_ns = NS_CATEGORY;
153 } else {
154 $link_ns = 0;
155 }
156
157 if ( $showlinkedto ) {
158 // find changes to pages linking to this page
159 if ( $link_ns ) {
160 if ( $ns != $link_ns ) {
161 continue;
162 } // should never happen, but check anyway
163 $subconds = array( "{$pfx}_to" => $dbkey );
164 } else {
165 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
166 }
167 $subjoin = "rc_cur_id = {$pfx}_from";
168 } else {
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";
174 } else {
175 $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
176 }
177 }
178
179 if ( $dbr->unionSupportsOrderAndLimit() ) {
180 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
181 } else {
182 $order = array();
183 }
184
185 $query = $dbr->selectSQLText(
186 array_merge( $tables, array( $link_table ) ),
187 $select,
188 $conds + $subconds,
189 __METHOD__,
190 $order + $query_options,
191 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
192 );
193
194 if ( $dbr->unionSupportsOrderAndLimit() ) {
195 $query = $dbr->limitResult( $query, $limit );
196 }
197
198 $subsql[] = $query;
199 }
200
201 if ( count( $subsql ) == 0 ) {
202 return false; // should never happen
203 }
204 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
205 $sql = $subsql[0];
206 } else {
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 );
210 }
211
212 $res = $dbr->query( $sql, __METHOD__ );
213
214 if ( $res->numRows() == 0 ) {
215 $this->mResultEmpty = true;
216 }
217
218 return $res;
219 }
220
221 function setTopText( FormOptions $opts ) {
222 $target = $this->getTargetTitle();
223 if ( $target ) {
224 $this->getOutput()->addBacklinkSubtitle( $target );
225 }
226 }
227
228 /**
229 * Get options to be displayed in a form
230 *
231 * @param FormOptions $opts
232 * @return array
233 */
234 function getExtraOptions( $opts ) {
235 $extraOpts = parent::getExtraOptions( $opts );
236
237 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
238
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' ) );
243
244 return $extraOpts;
245 }
246
247 /**
248 * @return Title
249 */
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;
257 }
258 }
259 return $this->rclTargetTitle;
260 }
261 }