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