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