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