Remove this ucfirst() crap. $filtered is already a DB key, so it should've been norma...
[lhc/web/wiklou.git] / includes / specials / SpecialRecentchangeslinked.php
1 <?php
2
3 /**
4 * This is to display changes made to all articles linked in an article.
5 * @ingroup SpecialPage
6 */
7 class SpecialRecentchangeslinked extends SpecialRecentchanges {
8
9 function __construct(){
10 SpecialPage::SpecialPage( 'Recentchangeslinked' );
11 $this->includable( true );
12 }
13
14 public function getDefaultOptions() {
15 $opts = parent::getDefaultOptions();
16 $opts->add( 'target', '' );
17 $opts->add( 'showlinkedto', false );
18 return $opts;
19 }
20
21 public function parseParameters( $par, FormOptions $opts ) {
22 $opts['target'] = $par;
23 }
24
25 public function feedSetup(){
26 global $wgRequest;
27 $opts = parent::feedSetup();
28 $opts['target'] = $wgRequest->getVal( 'target' );
29 return $opts;
30 }
31
32 public function getFeedObject( $feedFormat ){
33 $feed = new ChangesFeed( $feedFormat, false );
34 $feedObj = $feed->getFeedObject(
35 wfMsgForContent( 'recentchangeslinked-title', $this->mTargetTitle->getPrefixedText() ),
36 wfMsgForContent( 'recentchangeslinked' )
37 );
38 return array( $feed, $feedObj );
39 }
40
41 public function doMainQuery( $conds, $opts ) {
42 global $wgUser, $wgOut;
43
44 $target = $opts['target'];
45 $showlinkedto = $opts['showlinkedto'];
46 $limit = $opts['limit'];
47
48 if ( $target === '' ) {
49 return false;
50 }
51 $title = Title::newFromURL( $target );
52 if( !$title || $title->getInterwiki() != '' ){
53 $wgOut->wrapWikiMsg( '<div class="errorbox">$1</div><br clear="both" />', 'allpagesbadtitle' );
54 return false;
55 }
56 $this->mTargetTitle = $title;
57
58 $wgOut->setPageTitle( wfMsg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
59
60 /*
61 * Ordinary links are in the pagelinks table, while transclusions are
62 * in the templatelinks table, categorizations in categorylinks and
63 * image use in imagelinks. We need to somehow combine all these.
64 * Special:Whatlinkshere does this by firing multiple queries and
65 * merging the results, but the code we inherit from our parent class
66 * expects only one result set so we use UNION instead.
67 */
68
69 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
70 $id = $title->getArticleId();
71 $ns = $title->getNamespace();
72 $dbkey = $title->getDBkey();
73
74 $tables = array( 'recentchanges' );
75 $select = array( $dbr->tableName( 'recentchanges' ) . '.*' );
76 $join_conds = array();
77
78 // left join with watchlist table to highlight watched rows
79 if( $uid = $wgUser->getId() ) {
80 $tables[] = 'watchlist';
81 $select[] = 'wl_user';
82 $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
83 }
84
85 // XXX: parent class does this, should we too?
86 // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
87
88 if( $ns == NS_CATEGORY && !$showlinkedto ) {
89 // special handling for categories
90 // XXX: should try to make this less klugy
91 $link_tables = array( 'categorylinks' );
92 $showlinkedto = true;
93 } else {
94 // for now, always join on these tables; really should be configurable as in whatlinkshere
95 $link_tables = array( 'pagelinks', 'templatelinks' );
96 // imagelinks only contains links to pages in NS_FILE
97 if( $ns == NS_FILE || !$showlinkedto ) $link_tables[] = 'imagelinks';
98 }
99
100 if( $id == 0 && !$showlinkedto )
101 return false; // nonexistent pages can't link to any pages
102
103 // field name prefixes for all the various tables we might want to join with
104 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
105
106 $subsql = array(); // SELECT statements to combine with UNION
107
108 foreach( $link_tables as $link_table ) {
109 $pfx = $prefix[$link_table];
110
111 // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
112 if( $link_table == 'imagelinks' ) $link_ns = NS_FILE;
113 else if( $link_table == 'categorylinks' ) $link_ns = NS_CATEGORY;
114 else $link_ns = 0;
115
116 if( $showlinkedto ) {
117 // find changes to pages linking to this page
118 if( $link_ns ) {
119 if( $ns != $link_ns ) continue; // should never happen, but check anyway
120 $subconds = array( "{$pfx}_to" => $dbkey );
121 } else {
122 $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
123 }
124 $subjoin = "rc_cur_id = {$pfx}_from";
125 } else {
126 // find changes to pages linked from this page
127 $subconds = array( "{$pfx}_from" => $id );
128 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
129 $subconds["rc_namespace"] = $link_ns;
130 $subjoin = "rc_title = {$pfx}_to";
131 } else {
132 $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
133 }
134 }
135
136 $subsql[] = $dbr->selectSQLText( array_merge( $tables, array( $link_table ) ), $select, $conds + $subconds,
137 __METHOD__, array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ),
138 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) ) );
139 }
140
141 if( count($subsql) == 0 )
142 return false; // should never happen
143 if( count($subsql) == 1 )
144 $sql = $subsql[0];
145 else {
146 // need to resort and relimit after union
147 $sql = "(" . implode( ") UNION (", $subsql ) . ") ORDER BY rc_timestamp DESC LIMIT {$limit}";
148 }
149
150 $res = $dbr->query( $sql, __METHOD__ );
151
152 if( $res->numRows() == 0 )
153 $this->mResultEmpty = true;
154
155 return $res;
156 }
157
158 function getExtraOptions( $opts ){
159 $opts->consumeValues( array( 'showlinkedto', 'target' ) );
160 $extraOpts = array();
161 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
162 $extraOpts['target'] = array( wfMsg( 'recentchangeslinked-page' ),
163 Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
164 Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
165 Xml::label( wfMsg("recentchangeslinked-to"), 'showlinkedto' ) );
166 return $extraOpts;
167 }
168
169 function setTopText( OutputPage $out, FormOptions $opts ) {
170 global $wgUser;
171 $skin = $wgUser->getSkin();
172 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) )
173 $out->setSubtitle( wfMsg( 'recentchangeslinked-backlink', $skin->link( $this->mTargetTitle,
174 $this->mTargetTitle->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
175 }
176
177 function setBottomText( OutputPage $out, FormOptions $opts ){
178 if( isset( $this->mTargetTitle ) && is_object( $this->mTargetTitle ) ){
179 global $wgUser;
180 $out->setFeedAppendQuery( "target=" . urlencode( $this->mTargetTitle->getPrefixedDBkey() ) );
181 }
182 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ){
183 $out->addWikiMsg( 'recentchangeslinked-noresult' );
184 }
185 }
186 }