Merge "Make it show email as required if you choose to email a random password."
[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 feedSetup() {
48 $opts = parent::feedSetup();
49 $opts['target'] = $this->getRequest()->getVal( 'target' );
50 return $opts;
51 }
52
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()
60 );
61 return array( $feed, $feedObj );
62 }
63
64 public function doMainQuery( $conds, $opts ) {
65 $target = $opts['target'];
66 $showlinkedto = $opts['showlinkedto'];
67 $limit = $opts['limit'];
68
69 if ( $target === '' ) {
70 return false;
71 }
72 $outputPage = $this->getOutput();
73 $title = Title::newFromURL( $target );
74 if ( !$title || $title->getInterwiki() != '' ) {
75 $outputPage->wrapWikiMsg( "<div class=\"errorbox\">\n$1\n</div>", 'allpagesbadtitle' );
76 return false;
77 }
78
79 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
80
81 /*
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.
88 */
89
90 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
91 $id = $title->getArticleID();
92 $ns = $title->getNamespace();
93 $dbkey = $title->getDBkey();
94
95 $tables = array( 'recentchanges' );
96 $select = RecentChange::selectFields();
97 $join_conds = array();
98 $query_options = array();
99
100 // left join with watchlist table to highlight watched rows
101 $uid = $this->getUser()->getId();
102 if ( $uid && $this->getUser()->isAllowed( 'viewmywatchlist' ) ) {
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'
109 ));
110 }
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';
115 }
116 ChangeTags::modifyDisplayQuery(
117 $tables,
118 $select,
119 $conds,
120 $join_conds,
121 $query_options,
122 $opts['tagfilter']
123 );
124
125 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
126 return false;
127 }
128
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';
140 }
141 }
142
143 if ( $id == 0 && !$showlinkedto ) {
144 return false; // nonexistent pages can't link to any pages
145 }
146
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' );
149
150 $subsql = array(); // SELECT statements to combine with UNION
151
152 foreach ( $link_tables as $link_table ) {
153 $pfx = $prefix[$link_table];
154
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;
162 }
163
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 );
173 }
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" );
183 }
184 }
185
186 if ( $dbr->unionSupportsOrderAndLimit() ) {
187 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
188 } else {
189 $order = array();
190 }
191
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 ) )
199 );
200
201 if ( $dbr->unionSupportsOrderAndLimit() ) {
202 $query = $dbr->limitResult( $query, $limit );
203 }
204
205 $subsql[] = $query;
206 }
207
208 if ( count( $subsql ) == 0 ) {
209 return false; // should never happen
210 }
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 );
217 }
218
219 $res = $dbr->query( $sql, __METHOD__ );
220
221 if ( $res->numRows() == 0 ) {
222 $this->mResultEmpty = true;
223 }
224
225 return $res;
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
262 function setTopText( FormOptions $opts ) {
263 $target = $this->getTargetTitle();
264 if ( $target ) {
265 $this->getOutput()->addBacklinkSubtitle( $target );
266 }
267 }
268 }