Use local context to get messages
[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 wfMsgForContent( 'recentchangeslinked-title', $this->getTargetTitle()->getPrefixedText() ),
58 wfMsgForContent( 'recentchangeslinked-feed' ),
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><br style=\"clear: both\" />", '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 = array( $dbr->tableName( 'recentchanges' ) . '.*' );
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 ) {
103 $tables[] = 'watchlist';
104 $select[] = 'wl_user';
105 $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
106 }
107 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
108 $tables[] = 'page';
109 $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
110 $select[] = 'page_latest';
111 }
112 if ( !$this->including() ) { // bug 23293
113 ChangeTags::modifyDisplayQuery( $tables, $select, $conds, $join_conds,
114 $query_options, $opts['tagfilter'] );
115 }
116
117 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
118 return false;
119 }
120
121 if( $ns == NS_CATEGORY && !$showlinkedto ) {
122 // special handling for categories
123 // XXX: should try to make this less klugy
124 $link_tables = array( 'categorylinks' );
125 $showlinkedto = true;
126 } else {
127 // for now, always join on these tables; really should be configurable as in whatlinkshere
128 $link_tables = array( '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 = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
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, and have xx_to instead of xx_title
148 if( $link_table == 'imagelinks' ) {
149 $link_ns = NS_FILE;
150 } elseif( $link_table == 'categorylinks' ) {
151 $link_ns = NS_CATEGORY;
152 } else {
153 $link_ns = 0;
154 }
155
156 if( $showlinkedto ) {
157 // find changes to pages linking to this page
158 if( $link_ns ) {
159 if( $ns != $link_ns ) {
160 continue;
161 } // should never happen, but check anyway
162 $subconds = array( "{$pfx}_to" => $dbkey );
163 } else {
164 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
165 }
166 $subjoin = "rc_cur_id = {$pfx}_from";
167 } else {
168 // find changes to pages linked from this page
169 $subconds = array( "{$pfx}_from" => $id );
170 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
171 $subconds["rc_namespace"] = $link_ns;
172 $subjoin = "rc_title = {$pfx}_to";
173 } else {
174 $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
175 }
176 }
177
178 if( $dbr->unionSupportsOrderAndLimit()) {
179 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
180 } else {
181 $order = array();
182 }
183
184 $query = $dbr->selectSQLText(
185 array_merge( $tables, array( $link_table ) ),
186 $select,
187 $conds + $subconds,
188 __METHOD__,
189 $order + $query_options,
190 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
191 );
192
193 if( $dbr->unionSupportsOrderAndLimit())
194 $query = $dbr->limitResult( $query, $limit );
195
196 $subsql[] = $query;
197 }
198
199 if( count($subsql) == 0 ) {
200 return false; // should never happen
201 }
202 if( count($subsql) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
203 $sql = $subsql[0];
204 } else {
205 // need to resort and relimit after union
206 $sql = $dbr->unionQueries($subsql, false).' ORDER BY rc_timestamp DESC';
207 $sql = $dbr->limitResult($sql, $limit, false);
208 }
209
210 $res = $dbr->query( $sql, __METHOD__ );
211
212 if( $res->numRows() == 0 ) {
213 $this->mResultEmpty = true;
214 }
215
216 return $res;
217 }
218
219 /**
220 * @param $opts FormOptions
221 * @return array
222 */
223 function getExtraOptions( $opts ){
224 $opts->consumeValues( array( 'showlinkedto', 'target', 'tagfilter' ) );
225 $extraOpts = array();
226 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
227 $extraOpts['target'] = array( wfMsgHtml( 'recentchangeslinked-page' ),
228 Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
229 Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
230 Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) );
231 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
232 if ($tagFilter) {
233 $extraOpts['tagfilter'] = $tagFilter;
234 }
235 return $extraOpts;
236 }
237
238 /**
239 * @return Title
240 */
241 function getTargetTitle() {
242 if ( $this->rclTargetTitle === null ) {
243 $opts = $this->getOptions();
244 if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
245 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
246 } else {
247 $this->rclTargetTitle = false;
248 }
249 }
250 return $this->rclTargetTitle;
251 }
252
253 function setTopText( FormOptions $opts ) {
254 $target = $this->getTargetTitle();
255 if( $target ) {
256 $this->getOutput()->addBacklinkSubtitle( $target );
257 }
258 }
259
260 public function getFeedQuery() {
261 $target = $this->getTargetTitle();
262 if( $target ) {
263 return "target=" . urlencode( $target->getPrefixedDBkey() );
264 } else {
265 return false;
266 }
267 }
268
269 function setBottomText( FormOptions $opts ) {
270 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ) {
271 $this->getOutput()->addWikiMsg( 'recentchangeslinked-noresult' );
272 }
273 }
274 }