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