00001 <?php
00013 class SpecialWhatLinksHere extends SpecialPage {
00014
00015
00016 protected $opts, $target, $selfTitle;
00017
00018
00019 protected $skin;
00020
00021 protected $limits = array( 20, 50, 100, 250, 500 );
00022
00023 public function __construct() {
00024 parent::__construct( 'Whatlinkshere' );
00025 global $wgUser;
00026 $this->skin = $wgUser->getSkin();
00027 }
00028
00029 function execute( $par ) {
00030 global $wgOut, $wgRequest;
00031
00032 $this->setHeaders();
00033
00034 $opts = new FormOptions();
00035
00036 $opts->add( 'target', '' );
00037 $opts->add( 'namespace', '', FormOptions::INTNULL );
00038 $opts->add( 'limit', 50 );
00039 $opts->add( 'from', 0 );
00040 $opts->add( 'back', 0 );
00041 $opts->add( 'hideredirs', false );
00042 $opts->add( 'hidetrans', false );
00043 $opts->add( 'hidelinks', false );
00044 $opts->add( 'hideimages', false );
00045
00046 $opts->fetchValuesFromRequest( $wgRequest );
00047 $opts->validateIntBounds( 'limit', 0, 5000 );
00048
00049
00050 if ( isset($par) ) {
00051 $opts->setValue( 'target', $par );
00052 }
00053
00054
00055 $this->opts = $opts;
00056
00057 $this->target = Title::newFromURL( $opts->getValue( 'target' ) );
00058 if( !$this->target ) {
00059 $wgOut->addHTML( $this->whatlinkshereForm() );
00060 return;
00061 }
00062
00063 $this->selfTitle = SpecialPage::getTitleFor( 'Whatlinkshere', $this->target->getPrefixedDBkey() );
00064
00065 $wgOut->setPageTitle( wfMsg( 'whatlinkshere-title', $this->target->getPrefixedText() ) );
00066 $wgOut->setSubtitle( wfMsg( 'whatlinkshere-backlink', $this->skin->link( $this->target, $this->target->getPrefixedText(), array(), array( 'redirect' => 'no' ) ) ) );
00067
00068 $this->showIndirectLinks( 0, $this->target, $opts->getValue( 'limit' ),
00069 $opts->getValue( 'from' ), $opts->getValue( 'back' ) );
00070 }
00071
00080 function showIndirectLinks( $level, $target, $limit, $from = 0, $back = 0 ) {
00081 global $wgOut, $wgMaxRedirectLinksRetrieved;
00082 $dbr = wfGetDB( DB_SLAVE );
00083 $options = array();
00084
00085 $hidelinks = $this->opts->getValue( 'hidelinks' );
00086 $hideredirs = $this->opts->getValue( 'hideredirs' );
00087 $hidetrans = $this->opts->getValue( 'hidetrans' );
00088 $hideimages = $target->getNamespace() != NS_FILE || $this->opts->getValue( 'hideimages' );
00089
00090 $fetchlinks = (!$hidelinks || !$hideredirs);
00091
00092
00093 $plConds = array(
00094 'page_id=pl_from',
00095 'pl_namespace' => $target->getNamespace(),
00096 'pl_title' => $target->getDBkey(),
00097 );
00098 if( $hideredirs ) {
00099 $plConds['page_is_redirect'] = 0;
00100 } elseif( $hidelinks ) {
00101 $plConds['page_is_redirect'] = 1;
00102 }
00103
00104 $tlConds = array(
00105 'page_id=tl_from',
00106 'tl_namespace' => $target->getNamespace(),
00107 'tl_title' => $target->getDBkey(),
00108 );
00109
00110 $ilConds = array(
00111 'page_id=il_from',
00112 'il_to' => $target->getDBkey(),
00113 );
00114
00115 $namespace = $this->opts->getValue( 'namespace' );
00116 if ( is_int($namespace) ) {
00117 $plConds['page_namespace'] = $namespace;
00118 $tlConds['page_namespace'] = $namespace;
00119 $ilConds['page_namespace'] = $namespace;
00120 }
00121
00122 if ( $from ) {
00123 $tlConds[] = "tl_from >= $from";
00124 $plConds[] = "pl_from >= $from";
00125 $ilConds[] = "il_from >= $from";
00126 }
00127
00128
00129 $queryLimit = $limit + 1;
00130
00131
00132
00133 $options[] = 'STRAIGHT_JOIN';
00134
00135 $options['LIMIT'] = $queryLimit;
00136 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect' );
00137
00138 if( $fetchlinks ) {
00139 $options['ORDER BY'] = 'pl_from';
00140 $plRes = $dbr->select( array( 'pagelinks', 'page' ), $fields,
00141 $plConds, __METHOD__, $options );
00142 }
00143
00144 if( !$hidetrans ) {
00145 $options['ORDER BY'] = 'tl_from';
00146 $tlRes = $dbr->select( array( 'templatelinks', 'page' ), $fields,
00147 $tlConds, __METHOD__, $options );
00148 }
00149
00150 if( !$hideimages ) {
00151 $options['ORDER BY'] = 'il_from';
00152 $ilRes = $dbr->select( array( 'imagelinks', 'page' ), $fields,
00153 $ilConds, __METHOD__, $options );
00154 }
00155
00156 if( ( !$fetchlinks || !$dbr->numRows($plRes) ) && ( $hidetrans || !$dbr->numRows($tlRes) ) && ( $hideimages || !$dbr->numRows($ilRes) ) ) {
00157 if ( 0 == $level ) {
00158 $wgOut->addHTML( $this->whatlinkshereForm() );
00159
00160
00161 if( $hidelinks || $hidetrans || $hideredirs || $hideimages )
00162 $wgOut->addHTML( $this->getFilterPanel() );
00163
00164 $errMsg = is_int($namespace) ? 'nolinkshere-ns' : 'nolinkshere';
00165 $wgOut->addWikiMsg( $errMsg, $this->target->getPrefixedText() );
00166 }
00167 return;
00168 }
00169
00170
00171
00172
00173 if( $fetchlinks ) {
00174 while ( $row = $dbr->fetchObject( $plRes ) ) {
00175 $row->is_template = 0;
00176 $row->is_image = 0;
00177 $rows[$row->page_id] = $row;
00178 }
00179 $dbr->freeResult( $plRes );
00180
00181 }
00182 if( !$hidetrans ) {
00183 while ( $row = $dbr->fetchObject( $tlRes ) ) {
00184 $row->is_template = 1;
00185 $row->is_image = 0;
00186 $rows[$row->page_id] = $row;
00187 }
00188 $dbr->freeResult( $tlRes );
00189 }
00190 if( !$hideimages ) {
00191 while ( $row = $dbr->fetchObject( $ilRes ) ) {
00192 $row->is_template = 0;
00193 $row->is_image = 1;
00194 $rows[$row->page_id] = $row;
00195 }
00196 $dbr->freeResult( $ilRes );
00197 }
00198
00199
00200 ksort( $rows );
00201 $rows = array_values( $rows );
00202
00203 $numRows = count( $rows );
00204
00205
00206 if ( $numRows > $limit ) {
00207
00208
00209 $nextId = $rows[$limit]->page_id;
00210
00211 $rows = array_slice( $rows, 0, $limit );
00212 } else {
00213
00214 $nextId = false;
00215 }
00216 $prevId = $from;
00217
00218 if ( $level == 0 ) {
00219 $wgOut->addHTML( $this->whatlinkshereForm() );
00220 $wgOut->addHTML( $this->getFilterPanel() );
00221 $wgOut->addWikiMsg( 'linkshere', $this->target->getPrefixedText() );
00222
00223 $prevnext = $this->getPrevNext( $prevId, $nextId );
00224 $wgOut->addHTML( $prevnext );
00225 }
00226
00227 $wgOut->addHTML( $this->listStart() );
00228 foreach ( $rows as $row ) {
00229 $nt = Title::makeTitle( $row->page_namespace, $row->page_title );
00230
00231 if ( $row->page_is_redirect && $level < 2 ) {
00232 $wgOut->addHTML( $this->listItem( $row, $nt, true ) );
00233 $this->showIndirectLinks( $level + 1, $nt, $wgMaxRedirectLinksRetrieved );
00234 $wgOut->addHTML( Xml::closeElement( 'li' ) );
00235 } else {
00236 $wgOut->addHTML( $this->listItem( $row, $nt ) );
00237 }
00238 }
00239
00240 $wgOut->addHTML( $this->listEnd() );
00241
00242 if( $level == 0 ) {
00243 $wgOut->addHTML( $prevnext );
00244 }
00245 }
00246
00247 protected function listStart() {
00248 return Xml::openElement( 'ul', array ( 'id' => 'mw-whatlinkshere-list' ) );
00249 }
00250
00251 protected function listItem( $row, $nt, $notClose = false ) {
00252 # local message cache
00253 static $msgcache = null;
00254 if ( $msgcache === null ) {
00255 static $msgs = array( 'isredirect', 'istemplate', 'semicolon-separator',
00256 'whatlinkshere-links', 'isimage' );
00257 $msgcache = array();
00258 foreach ( $msgs as $msg ) {
00259 $msgcache[$msg] = wfMsgExt( $msg, array( 'escapenoentities' ) );
00260 }
00261 }
00262
00263 if( $row->page_is_redirect ) {
00264 $query = array( 'redirect' => 'no' );
00265 } else {
00266 $query = array();
00267 }
00268
00269 $link = $this->skin->linkKnown(
00270 $nt,
00271 null,
00272 array(),
00273 $query
00274 );
00275
00276
00277 $propsText = '';
00278 $props = array();
00279 if ( $row->page_is_redirect )
00280 $props[] = $msgcache['isredirect'];
00281 if ( $row->is_template )
00282 $props[] = $msgcache['istemplate'];
00283 if( $row->is_image )
00284 $props[] = $msgcache['isimage'];
00285
00286 if ( count( $props ) ) {
00287 $propsText = '(' . implode( $msgcache['semicolon-separator'], $props ) . ')';
00288 }
00289
00290 # Space for utilities links, with a what-links-here link provided
00291 $wlhLink = $this->wlhLink( $nt, $msgcache['whatlinkshere-links'] );
00292 $wlh = Xml::wrapClass( "($wlhLink)", 'mw-whatlinkshere-tools' );
00293
00294 return $notClose ?
00295 Xml::openElement( 'li' ) . "$link $propsText $wlh\n" :
00296 Xml::tags( 'li', null, "$link $propsText $wlh" ) . "\n";
00297 }
00298
00299 protected function listEnd() {
00300 return Xml::closeElement( 'ul' );
00301 }
00302
00303 protected function wlhLink( Title $target, $text ) {
00304 static $title = null;
00305 if ( $title === null )
00306 $title = SpecialPage::getTitleFor( 'Whatlinkshere' );
00307
00308 return $this->skin->linkKnown(
00309 $title,
00310 $text,
00311 array(),
00312 array( 'target' => $target->getPrefixedText() )
00313 );
00314 }
00315
00316 function makeSelfLink( $text, $query ) {
00317 return $this->skin->linkKnown(
00318 $this->selfTitle,
00319 $text,
00320 array(),
00321 $query
00322 );
00323 }
00324
00325 function getPrevNext( $prevId, $nextId ) {
00326 global $wgLang;
00327 $currentLimit = $this->opts->getValue( 'limit' );
00328 $fmtLimit = $wgLang->formatNum( $currentLimit );
00329 $prev = wfMsgExt( 'whatlinkshere-prev', array( 'parsemag', 'escape' ), $fmtLimit );
00330 $next = wfMsgExt( 'whatlinkshere-next', array( 'parsemag', 'escape' ), $fmtLimit );
00331
00332 $changed = $this->opts->getChangedValues();
00333 unset($changed['target']);
00334
00335 if ( 0 != $prevId ) {
00336 $overrides = array( 'from' => $this->opts->getValue( 'back' ) );
00337 $prev = $this->makeSelfLink( $prev, array_merge( $changed, $overrides ) );
00338 }
00339 if ( 0 != $nextId ) {
00340 $overrides = array( 'from' => $nextId, 'back' => $prevId );
00341 $next = $this->makeSelfLink( $next, array_merge( $changed, $overrides ) );
00342 }
00343
00344 $limitLinks = array();
00345 foreach ( $this->limits as $limit ) {
00346 $prettyLimit = $wgLang->formatNum( $limit );
00347 $overrides = array( 'limit' => $limit );
00348 $limitLinks[] = $this->makeSelfLink( $prettyLimit, array_merge( $changed, $overrides ) );
00349 }
00350
00351 $nums = $wgLang->pipeList( $limitLinks );
00352
00353 return wfMsgHtml( 'viewprevnext', $prev, $next, $nums );
00354 }
00355
00356 function whatlinkshereForm() {
00357 global $wgScript;
00358
00359
00360 $this->opts->consumeValue( 'target' );
00361
00362 $this->opts->consumeValues( array( 'back', 'from' ) );
00363
00364 $target = $this->target ? $this->target->getPrefixedText() : '';
00365 $namespace = $this->opts->consumeValue( 'namespace' );
00366
00367 # Build up the form
00368 $f = Xml::openElement( 'form', array( 'action' => $wgScript ) );
00369
00370 # Values that should not be forgotten
00371 $f .= Xml::hidden( 'title', SpecialPage::getTitleFor( 'Whatlinkshere' )->getPrefixedText() );
00372 foreach ( $this->opts->getUnconsumedValues() as $name => $value ) {
00373 $f .= Xml::hidden( $name, $value );
00374 }
00375
00376 $f .= Xml::fieldset( wfMsg( 'whatlinkshere' ) );
00377
00378 # Target input
00379 $f .= Xml::inputLabel( wfMsg( 'whatlinkshere-page' ), 'target',
00380 'mw-whatlinkshere-target', 40, $target );
00381
00382 $f .= ' ';
00383
00384 # Namespace selector
00385 $f .= Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
00386 Xml::namespaceSelector( $namespace, '' );
00387
00388 $f .= ' ';
00389
00390 # Submit
00391 $f .= Xml::submitButton( wfMsg( 'allpagessubmit' ) );
00392
00393 # Close
00394 $f .= Xml::closeElement( 'fieldset' ) . Xml::closeElement( 'form' ) . "\n";
00395
00396 return $f;
00397 }
00398
00404 function getFilterPanel() {
00405 global $wgLang;
00406 $show = wfMsgHtml( 'show' );
00407 $hide = wfMsgHtml( 'hide' );
00408
00409 $changed = $this->opts->getChangedValues();
00410 unset($changed['target']);
00411
00412 $links = array();
00413 $types = array( 'hidetrans', 'hidelinks', 'hideredirs' );
00414 if( $this->target->getNamespace() == NS_FILE )
00415 $types[] = 'hideimages';
00416
00417
00418
00419 foreach( $types as $type ) {
00420 $chosen = $this->opts->getValue( $type );
00421 $msg = $chosen ? $show : $hide;
00422 $overrides = array( $type => !$chosen );
00423 $links[] = wfMsgHtml( "whatlinkshere-{$type}", $this->makeSelfLink( $msg, array_merge( $changed, $overrides ) ) );
00424 }
00425 return Xml::fieldset( wfMsg( 'whatlinkshere-filters' ), $wgLang->pipeList( $links ) );
00426 }
00427 }