Use HTML::hidden to create input fields
[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
42 return $opts;
43 }
44
45 public function parseParameters( $par, FormOptions $opts ) {
46 $opts['target'] = $par;
47 }
48
49 /**
50 * @inheritdoc
51 */
52 protected function doMainQuery( $tables, $select, $conds, $query_options,
53 $join_conds, FormOptions $opts
54 ) {
55 $target = $opts['target'];
56 $showlinkedto = $opts['showlinkedto'];
57 $limit = $opts['limit'];
58
59 if ( $target === '' ) {
60 return false;
61 }
62 $outputPage = $this->getOutput();
63 $title = Title::newFromText( $target );
64 if ( !$title || $title->isExternal() ) {
65 $outputPage->addHTML( '<div class="errorbox">' . $this->msg( 'allpagesbadtitle' )
66 ->parse() . '</div>' );
67
68 return false;
69 }
70
71 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
72
73 /*
74 * Ordinary links are in the pagelinks table, while transclusions are
75 * in the templatelinks table, categorizations in categorylinks and
76 * image use in imagelinks. We need to somehow combine all these.
77 * Special:Whatlinkshere does this by firing multiple queries and
78 * merging the results, but the code we inherit from our parent class
79 * expects only one result set so we use UNION instead.
80 */
81
82 $dbr = wfGetDB( DB_REPLICA, 'recentchangeslinked' );
83 $id = $title->getArticleID();
84 $ns = $title->getNamespace();
85 $dbkey = $title->getDBkey();
86
87 $tables[] = 'recentchanges';
88 $select = array_merge( RecentChange::selectFields(), $select );
89
90 // left join with watchlist table to highlight watched rows
91 $uid = $this->getUser()->getId();
92 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
93 $tables[] = 'watchlist';
94 $select[] = 'wl_user';
95 $join_conds['watchlist'] = [ 'LEFT JOIN', [
96 'wl_user' => $uid,
97 'wl_title=rc_title',
98 'wl_namespace=rc_namespace'
99 ] ];
100 }
101 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
102 $tables[] = 'page';
103 $join_conds['page'] = [ 'LEFT JOIN', 'rc_cur_id=page_id' ];
104 $select[] = 'page_latest';
105 }
106 ChangeTags::modifyDisplayQuery(
107 $tables,
108 $select,
109 $conds,
110 $join_conds,
111 $query_options,
112 $opts['tagfilter']
113 );
114
115 if ( !$this->runMainQueryHook( $tables, $select, $conds, $query_options, $join_conds,
116 $opts )
117 ) {
118 return false;
119 }
120
121 if ( $ns == NS_CATEGORY && !$showlinkedto ) {
122 // special handling for categories
123 // XXX: should try to make this less kludgy
124 $link_tables = [ 'categorylinks' ];
125 $showlinkedto = true;
126 } else {
127 // for now, always join on these tables; really should be configurable as in whatlinkshere
128 $link_tables = [ 'pagelinks', 'templatelinks' ];
129 // imagelinks only contains links to pages in NS_FILE
130 if ( $ns == NS_FILE || !$showlinkedto ) {
131 $link_tables[] = 'imagelinks';
132 }
133 }
134
135 if ( $id == 0 && !$showlinkedto ) {
136 return false; // nonexistent pages can't link to any pages
137 }
138
139 // field name prefixes for all the various tables we might want to join with
140 $prefix = [
141 'pagelinks' => 'pl',
142 'templatelinks' => 'tl',
143 'categorylinks' => 'cl',
144 'imagelinks' => 'il'
145 ];
146
147 $subsql = []; // SELECT statements to combine with UNION
148
149 foreach ( $link_tables as $link_table ) {
150 $pfx = $prefix[$link_table];
151
152 // imagelinks and categorylinks tables have no xx_namespace field,
153 // and have xx_to instead of xx_title
154 if ( $link_table == 'imagelinks' ) {
155 $link_ns = NS_FILE;
156 } elseif ( $link_table == 'categorylinks' ) {
157 $link_ns = NS_CATEGORY;
158 } else {
159 $link_ns = 0;
160 }
161
162 if ( $showlinkedto ) {
163 // find changes to pages linking to this page
164 if ( $link_ns ) {
165 if ( $ns != $link_ns ) {
166 continue;
167 } // should never happen, but check anyway
168 $subconds = [ "{$pfx}_to" => $dbkey ];
169 } else {
170 $subconds = [ "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey ];
171 }
172 $subjoin = "rc_cur_id = {$pfx}_from";
173 } else {
174 // find changes to pages linked from this page
175 $subconds = [ "{$pfx}_from" => $id ];
176 if ( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
177 $subconds["rc_namespace"] = $link_ns;
178 $subjoin = "rc_title = {$pfx}_to";
179 } else {
180 $subjoin = [ "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" ];
181 }
182 }
183
184 if ( $dbr->unionSupportsOrderAndLimit() ) {
185 $order = [ 'ORDER BY' => 'rc_timestamp DESC' ];
186 } else {
187 $order = [];
188 }
189
190 $query = $dbr->selectSQLText(
191 array_merge( $tables, [ $link_table ] ),
192 $select,
193 $conds + $subconds,
194 __METHOD__,
195 $order + $query_options,
196 $join_conds + [ $link_table => [ 'INNER JOIN', $subjoin ] ]
197 );
198
199 if ( $dbr->unionSupportsOrderAndLimit() ) {
200 $query = $dbr->limitResult( $query, $limit );
201 }
202
203 $subsql[] = $query;
204 }
205
206 if ( count( $subsql ) == 0 ) {
207 return false; // should never happen
208 }
209 if ( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
210 $sql = $subsql[0];
211 } else {
212 // need to resort and relimit after union
213 $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
214 $sql = $dbr->limitResult( $sql, $limit, false );
215 }
216
217 $res = $dbr->query( $sql, __METHOD__ );
218
219 if ( $res->numRows() == 0 ) {
220 $this->mResultEmpty = true;
221 }
222
223 return $res;
224 }
225
226 function setTopText( FormOptions $opts ) {
227 $target = $this->getTargetTitle();
228 if ( $target ) {
229 $this->getOutput()->addBacklinkSubtitle( $target );
230 $this->getSkin()->setRelevantTitle( $target );
231 }
232 }
233
234 /**
235 * Get options to be displayed in a form
236 *
237 * @param FormOptions $opts
238 * @return array
239 */
240 function getExtraOptions( $opts ) {
241 $extraOpts = parent::getExtraOptions( $opts );
242
243 $opts->consumeValues( [ 'showlinkedto', 'target' ] );
244
245 $extraOpts['target'] = [ $this->msg( 'recentchangeslinked-page' )->escaped(),
246 Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
247 Xml::check( 'showlinkedto', $opts['showlinkedto'], [ 'id' => 'showlinkedto' ] ) . ' ' .
248 Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) ];
249
250 $this->addHelpLink( 'Help:Related changes' );
251 return $extraOpts;
252 }
253
254 /**
255 * @return Title
256 */
257 function getTargetTitle() {
258 if ( $this->rclTargetTitle === null ) {
259 $opts = $this->getOptions();
260 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
261 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
262 } else {
263 $this->rclTargetTitle = false;
264 }
265 }
266
267 return $this->rclTargetTitle;
268 }
269
270 /**
271 * Return an array of subpages beginning with $search that this special page will accept.
272 *
273 * @param string $search Prefix to search for
274 * @param int $limit Maximum number of results to return (usually 10)
275 * @param int $offset Number of results to skip (usually 0)
276 * @return string[] Matching subpages
277 */
278 public function prefixSearchSubpages( $search, $limit, $offset ) {
279 return $this->prefixSearchString( $search, $limit, $offset );
280 }
281 }