MediaWiki  REL1_21
SpecialRecentchangeslinked.php
Go to the documentation of this file.
00001 <?php
00029 class SpecialRecentchangeslinked extends SpecialRecentChanges {
00030         var $rclTargetTitle;
00031 
00032         function __construct() {
00033                 parent::__construct( 'Recentchangeslinked' );
00034         }
00035 
00036         public function getDefaultOptions() {
00037                 $opts = parent::getDefaultOptions();
00038                 $opts->add( 'target', '' );
00039                 $opts->add( 'showlinkedto', false );
00040                 return $opts;
00041         }
00042 
00043         public function parseParameters( $par, FormOptions $opts ) {
00044                 $opts['target'] = $par;
00045         }
00046 
00047         public function feedSetup() {
00048                 $opts = parent::feedSetup();
00049                 $opts['target'] = $this->getRequest()->getVal( 'target' );
00050                 return $opts;
00051         }
00052 
00053         public function getFeedObject( $feedFormat ) {
00054                 $feed = new ChangesFeed( $feedFormat, false );
00055                 $feedObj = $feed->getFeedObject(
00056                         $this->msg( 'recentchangeslinked-title', $this->getTargetTitle()->getPrefixedText() )
00057                                 ->inContentLanguage()->text(),
00058                         $this->msg( 'recentchangeslinked-feed' )->inContentLanguage()->text(),
00059                         $this->getTitle()->getFullUrl()
00060                 );
00061                 return array( $feed, $feedObj );
00062         }
00063 
00064         public function doMainQuery( $conds, $opts ) {
00065                 $target = $opts['target'];
00066                 $showlinkedto = $opts['showlinkedto'];
00067                 $limit = $opts['limit'];
00068 
00069                 if ( $target === '' ) {
00070                         return false;
00071                 }
00072                 $outputPage = $this->getOutput();
00073                 $title = Title::newFromURL( $target );
00074                 if( !$title || $title->getInterwiki() != '' ) {
00075                         $outputPage->wrapWikiMsg( "<div class=\"errorbox\">\n$1\n</div><br style=\"clear: both\" />", 'allpagesbadtitle' );
00076                         return false;
00077                 }
00078 
00079                 $outputPage->setPageTitle( $this->msg( 'recentchangeslinked-title', $title->getPrefixedText() ) );
00080 
00081                 /*
00082                  * Ordinary links are in the pagelinks table, while transclusions are
00083                  * in the templatelinks table, categorizations in categorylinks and
00084                  * image use in imagelinks.  We need to somehow combine all these.
00085                  * Special:Whatlinkshere does this by firing multiple queries and
00086                  * merging the results, but the code we inherit from our parent class
00087                  * expects only one result set so we use UNION instead.
00088                  */
00089 
00090                 $dbr = wfGetDB( DB_SLAVE, 'recentchangeslinked' );
00091                 $id = $title->getArticleID();
00092                 $ns = $title->getNamespace();
00093                 $dbkey = $title->getDBkey();
00094 
00095                 $tables = array( 'recentchanges' );
00096                 $select = RecentChange::selectFields();
00097                 $join_conds = array();
00098                 $query_options = array();
00099 
00100                 // left join with watchlist table to highlight watched rows
00101                 $uid = $this->getUser()->getId();
00102                 if( $uid ) {
00103                         $tables[] = 'watchlist';
00104                         $select[] = 'wl_user';
00105                         $join_conds['watchlist'] = array( 'LEFT JOIN', array(
00106                                 'wl_user' => $uid,
00107                                 'wl_title=rc_title',
00108                                 'wl_namespace=rc_namespace'
00109                         ));
00110                 }
00111                 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
00112                         $tables[] = 'page';
00113                         $join_conds['page'] = array( 'LEFT JOIN', 'rc_cur_id=page_id' );
00114                         $select[] = 'page_latest';
00115                 }
00116                 ChangeTags::modifyDisplayQuery(
00117                         $tables,
00118                         $select,
00119                         $conds,
00120                         $join_conds,
00121                         $query_options,
00122                         $opts['tagfilter']
00123                 );
00124 
00125                 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
00126                         return false;
00127                 }
00128 
00129                 if( $ns == NS_CATEGORY && !$showlinkedto ) {
00130                         // special handling for categories
00131                         // XXX: should try to make this less kludgy
00132                         $link_tables = array( 'categorylinks' );
00133                         $showlinkedto = true;
00134                 } else {
00135                         // for now, always join on these tables; really should be configurable as in whatlinkshere
00136                         $link_tables = array( 'pagelinks', 'templatelinks' );
00137                         // imagelinks only contains links to pages in NS_FILE
00138                         if( $ns == NS_FILE || !$showlinkedto ) {
00139                                 $link_tables[] = 'imagelinks';
00140                         }
00141                 }
00142 
00143                 if( $id == 0 && !$showlinkedto ) {
00144                         return false; // nonexistent pages can't link to any pages
00145                 }
00146 
00147                 // field name prefixes for all the various tables we might want to join with
00148                 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
00149 
00150                 $subsql = array(); // SELECT statements to combine with UNION
00151 
00152                 foreach( $link_tables as $link_table ) {
00153                         $pfx = $prefix[$link_table];
00154 
00155                         // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
00156                         if( $link_table == 'imagelinks' ) {
00157                                 $link_ns = NS_FILE;
00158                         } elseif( $link_table == 'categorylinks' ) {
00159                                 $link_ns = NS_CATEGORY;
00160                         } else {
00161                                 $link_ns = 0;
00162                         }
00163 
00164                         if( $showlinkedto ) {
00165                                 // find changes to pages linking to this page
00166                                 if( $link_ns ) {
00167                                         if( $ns != $link_ns ) {
00168                                                 continue;
00169                                         } // should never happen, but check anyway
00170                                         $subconds = array( "{$pfx}_to" => $dbkey );
00171                                 } else {
00172                                         $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
00173                                 }
00174                                 $subjoin = "rc_cur_id = {$pfx}_from";
00175                         } else {
00176                                 // find changes to pages linked from this page
00177                                 $subconds = array( "{$pfx}_from" => $id );
00178                                 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
00179                                         $subconds["rc_namespace"] = $link_ns;
00180                                         $subjoin = "rc_title = {$pfx}_to";
00181                                 } else {
00182                                         $subjoin = array( "rc_namespace = {$pfx}_namespace", "rc_title = {$pfx}_title" );
00183                                 }
00184                         }
00185 
00186                         if( $dbr->unionSupportsOrderAndLimit()) {
00187                                 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
00188                         } else {
00189                                 $order = array();
00190                         }
00191 
00192                         $query = $dbr->selectSQLText(
00193                                 array_merge( $tables, array( $link_table ) ),
00194                                 $select,
00195                                 $conds + $subconds,
00196                                 __METHOD__,
00197                                 $order + $query_options,
00198                                 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
00199                         );
00200 
00201                         if( $dbr->unionSupportsOrderAndLimit())
00202                                 $query = $dbr->limitResult( $query, $limit );
00203 
00204                         $subsql[] = $query;
00205                 }
00206 
00207                 if( count( $subsql ) == 0 ) {
00208                         return false; // should never happen
00209                 }
00210                 if( count( $subsql ) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
00211                         $sql = $subsql[0];
00212                 } else {
00213                         // need to resort and relimit after union
00214                         $sql = $dbr->unionQueries( $subsql, false ) . ' ORDER BY rc_timestamp DESC';
00215                         $sql = $dbr->limitResult( $sql, $limit, false );
00216                 }
00217 
00218                 $res = $dbr->query( $sql, __METHOD__ );
00219 
00220                 if( $res->numRows() == 0 ) {
00221                         $this->mResultEmpty = true;
00222                 }
00223 
00224                 return $res;
00225         }
00226 
00231         function getExtraOptions( $opts ) {
00232                 $opts->consumeValues( array( 'showlinkedto', 'target', 'tagfilter' ) );
00233                 $extraOpts = array();
00234                 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
00235                 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
00236                         Xml::input( 'target', 40, str_replace( '_', ' ', $opts['target'] ) ) .
00237                         Xml::check( 'showlinkedto', $opts['showlinkedto'], array( 'id' => 'showlinkedto' ) ) . ' ' .
00238                         Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
00239                 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
00240                 if ( $tagFilter ) {
00241                         $extraOpts['tagfilter'] = $tagFilter;
00242                 }
00243                 return $extraOpts;
00244         }
00245 
00249         function getTargetTitle() {
00250                 if ( $this->rclTargetTitle === null ) {
00251                         $opts = $this->getOptions();
00252                         if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
00253                                 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
00254                         } else {
00255                                 $this->rclTargetTitle = false;
00256                         }
00257                 }
00258                 return $this->rclTargetTitle;
00259         }
00260 
00261         function setTopText( FormOptions $opts ) {
00262                 $target = $this->getTargetTitle();
00263                 if( $target ) {
00264                         $this->getOutput()->addBacklinkSubtitle( $target );
00265                 }
00266         }
00267 
00268         function setBottomText( FormOptions $opts ) {
00269                 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ) {
00270                         $this->getOutput()->addWikiMsg( 'recentchangeslinked-noresult' );
00271                 }
00272         }
00273 }