Make Special:Recentchangeslinked display changes to transcluded pages (templatelinks...
authorIlmari Karonen <vyznev@users.mediawiki.org>
Tue, 22 Jul 2008 22:37:55 +0000 (22:37 +0000)
committerIlmari Karonen <vyznev@users.mediawiki.org>
Tue, 22 Jul 2008 22:37:55 +0000 (22:37 +0000)
Also make the "Show changes to pages linked" button work for categories.

Remaining issues / things to do:
 - Test performance of the UNION query on a larger dataset than my test wiki.
 - Add some checkboxes to allow filtering the results like on Special:Whatlinkshere.
 - Try to think of some way to make the behavior for categories less klugy.

RELEASE-NOTES
includes/specials/SpecialRecentchangeslinked.php

index bb7ea66..fa69fa8 100644 (file)
@@ -191,7 +191,10 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 9736) Redirects on Special:Fewestrevisions are now marked as such.
 * New date/time formats in Cs localization according to ČSN and PČP.
 * (bug 14883) Create hook AlternateSkinPreferences to alternate skin section in user preferences
+* Special:Recentchangeslinked now includes changes to transcluded pages and
+  displayed images; also, the "Show changes to pages linked" checkbox now works on
+  category pages too, showing all links that are not categorizations
+
 === Bug fixes in 1.13 ===
 
 * (bug 10677) Add link to the file description page on the shared repository
index dd893bf..d773fb7 100644 (file)
@@ -56,45 +56,94 @@ class SpecialRecentchangeslinked extends SpecialRecentchanges {
 
                $wgOut->setPageTitle( wfMsg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
 
+               /*
+                * Ordinary links are in the pagelinks table, while transclusions are
+                * in the templatelinks table, categorizations in categorylinks and
+                * image use in imagelinks.  We need to somehow combine all these.
+                * Special:Whatlinkshere does this by firing multiple queries and
+                * merging the results, but the code we inherit from our parent class
+                * expects only one result set so we use UNION instead.
+                */
+
                $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
                $id = $title->getArticleId();
+               $ns = $title->getNamespace();
+               $dbkey = $title->getDBkey();
 
                $tables = array( 'recentchanges' );
                $select = array( $dbr->tableName( 'recentchanges' ) . '.*' );
                $join_conds = array();
 
-               if( $title->getNamespace() == NS_CATEGORY ) {
-                       $tables[] = 'categorylinks';
-                       $conds['cl_to'] = $title->getDBkey();
-                       $join_conds['categorylinks'] = array( 'LEFT JOIN', 'cl_from=rc_cur_id' );
+               // left join with watchlist table to highlight watched rows
+               if( $uid = $wgUser->getId() ) {
+                       $tables[] = 'watchlist';
+                       $select[] = 'wl_user';
+                       $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
+               }
+
+               // XXX: parent class does this, should we too?
+               // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
+
+               if( $ns == NS_CATEGORY && !$showlinkedto ) {
+                       // special handling for categories
+                       // XXX: should try to make this less klugy
+                       $link_tables = array( 'categorylinks' );
+                       $showlinkedto = true;
                } else {
+                       // for now, always join on these tables; really should be configurable as in whatlinkshere
+                       $link_tables = array( 'pagelinks', 'templatelinks' );
+                       // imagelinks only contains links to pages in NS_IMAGE
+                       if( $ns == NS_IMAGE || !$showlinkedto ) $link_tables[] = 'imagelinks';
+               }
+
+               // field name prefixes for all the various tables we might want to join with
+               $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
+
+               $subsql = array(); // SELECT statements to combine with UNION
+
+               foreach( $link_tables as $link_table ) {
+                       $pfx = $prefix[$link_table];
+
+                       // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
+                       if( $link_table == 'imagelinks' ) $link_ns = NS_IMAGE;
+                       else if( $link_table == 'categorylinks' ) $link_ns = NS_CATEGORY;
+                       else $link_ns = 0;
+
                        if( $showlinkedto ) {
-                               if( $title->getNamespace() == NS_TEMPLATE ){
-                                       $tables[] = 'templatelinks';
-                                       $conds['tl_namespace'] = $title->getNamespace();
-                                       $conds['tl_title'] = $title->getDBkey();
-                                       $join_conds['templatelinks'] = array( 'LEFT JOIN', 'tl_from=rc_cur_id' );
+                               // find changes to pages linking to this page
+                               if( $link_ns ) {
+                                       if( $ns != $link_ns ) continue; // should never happen, but check anyway
+                                       $subconds = array( "{$pfx}_to" => $dbkey );
                                } else {
-                                       $tables[] = 'pagelinks';
-                                       $conds['pl_namespace'] = $title->getNamespace();
-                                       $conds['pl_title'] = $title->getDBkey();
-                                       $join_conds['pagelinks'] = array( 'LEFT JOIN', 'pl_from=rc_cur_id' );
+                                       $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
                                }
+                               $subjoin = "rc_cur_id = {$pfx}_from";
                        } else {
-                               $tables[] = 'pagelinks';
-                               $conds['pl_from'] = $id;
-                               $join_conds['pagelinks'] = array( 'LEFT JOIN', 'pl_namespace = rc_namespace AND pl_title = rc_title' );
+                               // find changes to pages linked from this page
+                               $subconds = array( "{$pfx}_from" => $id );
+                               if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
+                                       $subconds["rc_namespace"] = $link_ns;
+                                       $subjoin = "rc_title = {$pfx}_to";
+                               } else {
+                                       $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
+                               }
                        }
+
+                       $subsql[] = $dbr->selectSQLText( array_merge( $tables, array( $link_table ) ), $select, $conds + $subconds,
+                                                        __METHOD__, array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ),
+                                                        $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) ) );
                }
 
-               if( $uid = $wgUser->getId() ) {
-                       $tables[] = 'watchlist';
-                       $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
-                       $select[] = 'wl_user';
+               if( count($subsql) == 0 )
+                       return false; // should never happen
+               if( count($subsql) == 1 )
+                       $sql = $subsql[0];
+               else {
+                       // need to resort and relimit after union
+                       $sql = "(" . implode( ") UNION (", $subsql ) . ") ORDER BY rc_timestamp DESC LIMIT {$limit}";
                }
 
-               $res = $dbr->select( $tables, $select, $conds, __METHOD__,
-                       array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ), $join_conds );
+               $res = $dbr->query( $sql, __METHOD__ );
 
                if( $dbr->numRows( $res ) == 0 )
                        $this->mResultEmpty = true;