Move replaceLinkHolders() from OutputPage to Parser, because
[lhc/web/wiklou.git] / includes / SpecialWhatlinkshere.php
index 8d1c4ba..9c4b7c8 100644 (file)
@@ -1,19 +1,27 @@
-<?
-
-function wfSpecialWhatlinkshere($par = NULL)
-{
-       global $wgUser, $wgOut, $target;
+<?php
+/**
+ *
+ * @package MediaWiki
+ * @subpackage SpecialPage
+ */
+
+/**
+ *
+ */
+function wfSpecialWhatlinkshere($par = NULL) {
+       global $wgUser, $wgOut, $wgRequest;
        $fname = "wfSpecialWhatlinkshere";
 
-       if($par) {
+       $target = $wgRequest->getVal( 'target' );
+       $limit = $wgRequest->getInt( 'limit', 500 );
+       
+       if(!empty($par)) {
                $target = $par;
-       } else {
-               $target = wfCleanQueryVar( $_REQUEST['target'] );
-       }
-       if ( "" == $target ) {
+       } else if ( is_null( $target ) ) {
                $wgOut->errorpage( "notargettitle", "notargettext" );
                return;
        }
+
        $nt = Title::newFromURL( $target );
        if( !$nt ) {
                $wgOut->errorpage( "notargettitle", "notargettext" );
@@ -26,51 +34,57 @@ function wfSpecialWhatlinkshere($par = NULL)
        $sk = $wgUser->getSkin();
        $isredir = " (" . wfMsg( "isredirect" ) . ")\n";
 
+       $wgOut->addHTML("&lt; ".$sk->makeKnownLinkObj($nt, "", "redirect=no" )."<br />\n");
+       $dbr =& wfGetDB( DB_SLAVE );
+       extract( $dbr->tableNames( 'cur', 'brokenlinks', 'links' ) );
+
        if ( 0 == $id ) {
-               $sql = "SELECT DISTINCT bl_from FROM brokenlinks WHERE bl_to='" .
-                 wfStrencode( $nt->getPrefixedDBkey() ) . "' LIMIT 500";
-               $res = wfQuery( $sql, DB_READ, $fname );
+               $sql = "SELECT cur_id,cur_namespace,cur_title,cur_is_redirect FROM $brokenlinks,$cur WHERE bl_to='" .
+                 $dbr->strencode( $nt->getPrefixedDBkey() ) . "' AND bl_from=cur_id LIMIT $limit";
+               $res = $dbr->query( $sql, $fname );
 
-               if ( 0 == wfNumRows( $res ) ) {
+               if ( 0 == $dbr->numRows( $res ) ) {
                        $wgOut->addHTML( wfMsg( "nolinkshere" ) );
                } else {
                        $wgOut->addHTML( wfMsg( "linkshere" ) );
                        $wgOut->addHTML( "\n<ul>" );
 
-                       while ( $row = wfFetchObject( $res ) ) {
-                               $lid = $row->bl_from;
-                               $sql = "SELECT cur_namespace,cur_title,cur_is_redirect " .
-                                 "FROM cur WHERE cur_id={$lid}";
-                               $res2 = wfQuery( $sql, DB_READ, $fname );
-                               $s = wfFetchObject( $res2 );
-
-                               $n = Title::makeName( $s->cur_namespace, $s->cur_title );
-                               $link = $sk->makeKnownLink( $n, "", "redirect=no" );
+                       while ( $row = $dbr->fetchObject( $res ) ) {
+                               $nt = Title::makeTitle( $row->cur_namespace, $row->cur_title );
+                               if( !$nt ) {
+                                       continue;
+                               }
+                               $link = $sk->makeKnownLinkObj( $nt, "", "redirect=no" );
                                $wgOut->addHTML( "<li>{$link}" );
 
-                               if ( 1 == $s->cur_is_redirect ) {
+                               if ( $row->cur_is_redirect ) {
                                        $wgOut->addHTML( $isredir );
-                                       wfShowIndirectLinks( 1, $lid );
+                                       wfShowIndirectLinks( 1, $row->cur_id, $limit );
                                }
                                $wgOut->addHTML( "</li>\n" );
                        }
                        $wgOut->addHTML( "</ul>\n" );
-                       wfFreeResult( $res );
+                       $dbr->freeResult( $res );
                }
        } else {
-               wfShowIndirectLinks( 0, $id );
+               wfShowIndirectLinks( 0, $id, $limit );
        }
 }
 
-function wfShowIndirectLinks( $level, $lid )
-{
+/**
+ *
+ */
+function wfShowIndirectLinks( $level, $lid, $limit ) {
        global $wgOut, $wgUser;
        $fname = "wfShowIndirectLinks";
 
-       $sql = "SELECT DISTINCT l_from FROM links WHERE l_to={$lid} LIMIT 500";
-       $res = wfQuery( $sql, DB_READ, $fname );
+       $dbr =& wfGetDB( DB_READ );
+       extract( $dbr->tableNames( 'links','cur' ) );
+
+       $sql = "SELECT cur_id,cur_namespace,cur_title,cur_is_redirect FROM $links,$cur WHERE l_to={$lid} AND l_from=cur_id LIMIT $limit";
+       $res = $dbr->query( $sql, $fname );
 
-       if ( 0 == wfNumRows( $res ) ) {
+       if ( 0 == $dbr->numRows( $res ) ) {
                if ( 0 == $level ) {
                        $wgOut->addHTML( wfMsg( "nolinkshere" ) );
                }
@@ -83,26 +97,26 @@ function wfShowIndirectLinks( $level, $lid )
        $isredir = " (" . wfMsg( "isredirect" ) . ")\n";
 
        $wgOut->addHTML( "<ul>" );
-       while ( $row = wfFetchObject( $res ) ) {
-               $nt = Title::newFromDBkey( $row->l_from );
+       while ( $row = $dbr->fetchObject( $res ) ) {
+               $nt = Title::makeTitle( $row->cur_namespace, $row->cur_title );
                if( !$nt ) {
                        $wgOut->addHTML( "<!-- bad backlink: " . htmlspecialchars( $row->l_from ) . " -->\n" );
+                       continue;
                }
-               $ns = $nt->getNamespace();
-               $t = wfStrencode( $nt->getDBkey() );
 
-               $link = $sk->makeKnownLink( $row->l_from, "", "redirect=no" );
-               $wgOut->addHTML( "<li>{$link}" );
+               if ( $row->cur_is_redirect ) {
+                       $extra = "redirect=no";
+               } else {
+                       $extra = "";
+               }
 
-               $sql = "SELECT cur_id,cur_is_redirect FROM cur " .
-                 "WHERE cur_namespace={$ns} AND cur_title='{$t}'";
-               $res2 = wfQuery( $sql, DB_READ, $fname );
-               $s = wfFetchObject( $res2 );
+               $link = $sk->makeKnownLinkObj( $nt, "", $extra );
+               $wgOut->addHTML( "<li>{$link}" );
 
-               if ( 1 == $s->cur_is_redirect ) {
+               if ( $row->cur_is_redirect ) {
                        $wgOut->addHTML( $isredir );
                        if ( $level < 2 ) {
-                               wfShowIndirectLinks( $level + 1, $s->cur_id );
+                               wfShowIndirectLinks( $level + 1, $row->cur_id, $limit );
                        }
                }
                $wgOut->addHTML( "</li>\n" );