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