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