MediaWiki  master
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', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
00106                 }
00107                 if ( $this->getUser()->isAllowed( 'rollback' ) ) {
00108                         $tables[] = 'page';
00109                         $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
00110                         $select[] = 'page_latest';
00111                 }
00112                 ChangeTags::modifyDisplayQuery(
00113                         $tables,
00114                         $select,
00115                         $conds,
00116                         $join_conds,
00117                         $query_options,
00118                         $opts['tagfilter']
00119                 );
00120 
00121                 if ( !wfRunHooks( 'SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts, &$query_options, &$select ) ) ) {
00122                         return false;
00123                 }
00124 
00125                 if( $ns == NS_CATEGORY && !$showlinkedto ) {
00126                         // special handling for categories
00127                         // XXX: should try to make this less klugy
00128                         $link_tables = array( 'categorylinks' );
00129                         $showlinkedto = true;
00130                 } else {
00131                         // for now, always join on these tables; really should be configurable as in whatlinkshere
00132                         $link_tables = array( 'pagelinks', 'templatelinks' );
00133                         // imagelinks only contains links to pages in NS_FILE
00134                         if( $ns == NS_FILE || !$showlinkedto ) {
00135                                 $link_tables[] = 'imagelinks';
00136                         }
00137                 }
00138 
00139                 if( $id == 0 && !$showlinkedto ) {
00140                         return false; // nonexistent pages can't link to any pages
00141                 }
00142 
00143                 // field name prefixes for all the various tables we might want to join with
00144                 $prefix = array( 'pagelinks' => 'pl', 'templatelinks' => 'tl', 'categorylinks' => 'cl', 'imagelinks' => 'il' );
00145 
00146                 $subsql = array(); // SELECT statements to combine with UNION
00147 
00148                 foreach( $link_tables as $link_table ) {
00149                         $pfx = $prefix[$link_table];
00150 
00151                         // imagelinks and categorylinks tables have no xx_namespace field, and have xx_to instead of xx_title
00152                         if( $link_table == 'imagelinks' ) {
00153                                 $link_ns = NS_FILE;
00154                         } elseif( $link_table == 'categorylinks' ) {
00155                                 $link_ns = NS_CATEGORY;
00156                         } else {
00157                                 $link_ns = 0;
00158                         }
00159 
00160                         if( $showlinkedto ) {
00161                                 // find changes to pages linking to this page
00162                                 if( $link_ns ) {
00163                                         if( $ns != $link_ns ) {
00164                                                 continue;
00165                                         } // should never happen, but check anyway
00166                                         $subconds = array( "{$pfx}_to" => $dbkey );
00167                                 } else {
00168                                         $subconds = array( "{$pfx}_namespace" => $ns, "{$pfx}_title" => $dbkey );
00169                                 }
00170                                 $subjoin = "rc_cur_id = {$pfx}_from";
00171                         } else {
00172                                 // find changes to pages linked from this page
00173                                 $subconds = array( "{$pfx}_from" => $id );
00174                                 if( $link_table == 'imagelinks' || $link_table == 'categorylinks' ) {
00175                                         $subconds["rc_namespace"] = $link_ns;
00176                                         $subjoin = "rc_title = {$pfx}_to";
00177                                 } else {
00178                                         $subjoin = "rc_namespace = {$pfx}_namespace AND rc_title = {$pfx}_title";
00179                                 }
00180                         }
00181 
00182                         if( $dbr->unionSupportsOrderAndLimit()) {
00183                                 $order = array( 'ORDER BY' => 'rc_timestamp DESC' );
00184                         } else {
00185                                 $order = array();
00186                         }
00187 
00188                         $query = $dbr->selectSQLText(
00189                                 array_merge( $tables, array( $link_table ) ),
00190                                 $select,
00191                                 $conds + $subconds,
00192                                 __METHOD__,
00193                                 $order + $query_options,
00194                                 $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
00195                         );
00196 
00197                         if( $dbr->unionSupportsOrderAndLimit())
00198                                 $query = $dbr->limitResult( $query, $limit );
00199 
00200                         $subsql[] = $query;
00201                 }
00202 
00203                 if( count($subsql) == 0 ) {
00204                         return false; // should never happen
00205                 }
00206                 if( count($subsql) == 1 && $dbr->unionSupportsOrderAndLimit() ) {
00207                         $sql = $subsql[0];
00208                 } else {
00209                         // need to resort and relimit after union
00210                         $sql = $dbr->unionQueries($subsql, false).' ORDER BY rc_timestamp DESC';
00211                         $sql = $dbr->limitResult($sql, $limit, false);
00212                 }
00213 
00214                 $res = $dbr->query( $sql, __METHOD__ );
00215 
00216                 if( $res->numRows() == 0 ) {
00217                         $this->mResultEmpty = true;
00218                 }
00219 
00220                 return $res;
00221         }
00222 
00227         function getExtraOptions( $opts ){
00228                 $opts->consumeValues( array( 'showlinkedto', 'target', 'tagfilter' ) );
00229                 $extraOpts = array();
00230                 $extraOpts['namespace'] = $this->namespaceFilterForm( $opts );
00231                 $extraOpts['target'] = array( $this->msg( 'recentchangeslinked-page' )->escaped(),
00232                         Xml::input( 'target', 40, str_replace('_',' ',$opts['target']) ) .
00233                         Xml::check( 'showlinkedto', $opts['showlinkedto'], array('id' => 'showlinkedto') ) . ' ' .
00234                         Xml::label( $this->msg( 'recentchangeslinked-to' )->text(), 'showlinkedto' ) );
00235                 $tagFilter = ChangeTags::buildTagFilterSelector( $opts['tagfilter'] );
00236                 if ($tagFilter) {
00237                         $extraOpts['tagfilter'] = $tagFilter;
00238                 }
00239                 return $extraOpts;
00240         }
00241 
00245         function getTargetTitle() {
00246                 if ( $this->rclTargetTitle === null ) {
00247                         $opts = $this->getOptions();
00248                         if ( isset( $opts['target'] ) && $opts['target'] !== '' ) {
00249                                 $this->rclTargetTitle = Title::newFromText( $opts['target'] );
00250                         } else {
00251                                 $this->rclTargetTitle = false;
00252                         }
00253                 }
00254                 return $this->rclTargetTitle;
00255         }
00256 
00257         function setTopText( FormOptions $opts ) {
00258                 $target = $this->getTargetTitle();
00259                 if( $target ) {
00260                         $this->getOutput()->addBacklinkSubtitle( $target );
00261                 }
00262         }
00263 
00264         public function getFeedQuery() {
00265                 $target = $this->getTargetTitle();
00266                 if( $target ) {
00267                         return "target=" . urlencode( $target->getPrefixedDBkey() );
00268                 } else {
00269                         return false;
00270                 }
00271         }
00272 
00273         function setBottomText( FormOptions $opts ) {
00274                 if( isset( $this->mResultEmpty ) && $this->mResultEmpty ) {
00275                         $this->getOutput()->addWikiMsg( 'recentchangeslinked-noresult' );
00276                 }
00277         }
00278 }