r190 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r189‎ | r190 | r191 >
Date:22:16, 23 February 2002
Author:hidders
Status:old
Tags:
Comment:
SQL optimized
Modified paths:
  • /trunk/phpwiki/fpw/special_whatlinkshere.php (modified) (history)

Diff [purge]

Index: trunk/phpwiki/fpw/special_whatlinkshere.php
@@ -1,63 +1,81 @@
22 <?
33 function whatLinksHere () {
4 - global $THESCRIPT , $target , $user , $protecting , $newrestrictions ;
5 - global $vpage , $target , $wikiLinkhereTitle ;
6 - global $wikiLinkhereBacklink , $wikiLinkhereNoBacklink , $wikiBacklinkNolink , $wikiBacklinkFollowing ;
7 - $vpage = new WikiPage ;
8 - $vpage->title = $title ;
9 - $vpage->makeSecureTitle () ;
10 - $ti = $vpage->secureTitle ;
11 - $niceTarget = $vpage->getNiceTitle ( $target ) ;
12 - $vpage->special ( str_replace ( "$1" , $niceTarget , $wikiLinkhereTitle ) ) ;
13 - $vpage->makeSecureTitle () ;
 4+ global $THESCRIPT , $target , $user , $protecting , $newrestrictions ;
 5+ global $vpage , $target , $wikiLinkhereTitle ;
 6+ global $wikiLinkhereBacklink , $wikiLinkhereNoBacklink , $wikiBacklinkNolink , $wikiBacklinkFollowing ;
 7+
 8+ $vpage = new WikiPage ;
 9+ $vpage->title = $title ;
 10+ $vpage->makeSecureTitle () ;
 11+ $ti = $vpage->secureTitle ;
 12+ $niceTarget = $vpage->getNiceTitle ( $target ) ;
 13+ $vpage->special ( str_replace ( "$1" , $niceTarget , $wikiLinkhereTitle ) ) ;
 14+ $vpage->makeSecureTitle () ;
1415
15 - $connection = getDBconnection () ;
 16+ $connection = getDBconnection () ;
 17+ # first we get all pages linked to $target
 18+ $sql1 = "SELECT DISTINCT linked_from AS lfrom
 19+ FROM linked
 20+ WHERE linked_to = \"$target\"
 21+ ORDER BY lfrom " ;
 22+ $result1 = mysql_query ( $sql1 , $connection ) ;
1623
17 - # The question is kinda long, but I don't want to sort everything out manually, so...
18 - $question = "SELECT cur_title FROM cur WHERE" ;
19 - $question .= " cur_linked_links LIKE \"$target\" OR " ;
20 - $question .= " cur_linked_links LIKE \"$target\n%\" OR" ;
21 - $question .= " cur_linked_links LIKE \"%\n$target\n%\" OR" ;
22 - $question .= " cur_linked_links LIKE \"%\n$target\" OR " ;
23 - $question .= " cur_unlinked_links LIKE \"$target\" OR " ;
24 - $question .= " cur_unlinked_links LIKE \"$target\n%\" OR" ;
25 - $question .= " cur_unlinked_links LIKE \"%\n$target\n%\" OR" ;
26 - $question .= " cur_unlinked_links LIKE \"%\n$target\"" ;
 24+ # we also get all pages that have an unlinked link to $target
 25+ # Note that at least one of these two queries is empty
 26+ $sql2 = "SELECT DISTINCT unlinked_from AS lfrom
 27+ FROM unlinked
 28+ WHERE unlinked_to = \"$target\"
 29+ ORDER BY lfrom " ;
 30+ $result2 = mysql_query ( $sql2 , $connection ) ;
 31+
 32+ # then we get all pages linked from $target
 33+ $sql3 = "SELECT DISTINCT linked_to
 34+ FROM linked
 35+ WHERE linked_from = \"$target\"
 36+ ORDER BY linked_to " ;
 37+ $result3 = mysql_query ( $sql3 , $connection ) ;
 38+
 39+ $dlb = "" ; # the pages that $target links back to
 40+ $dnlb = "" ; # the pages that $target does not link back to
 41+
 42+ # we find out if $target has links
 43+ $s3 = mysql_fetch_object ( $result3 ) ;
 44+ if ( $s3 ) { # yes, so we take $result1 and $result2 and split it into $dlb and $ndlb
 45+ foreach ( array ( $result1, $result2 ) as $aresult ) {
 46+ while ( $s1 = mysql_fetch_object ( $aresult ) ) {
 47+ $nt = $vpage->getNiceTitle ( $s1->lfrom ) ;
 48+ while ( $s3 and $s3->linked_to < $s1->lfrom )
 49+ $s3 = mysql_fetch_object ( $result3 ) ;
 50+ if ( $s3 )
 51+ if ( $s3->linked_to > $s1->lfrom )
 52+ $dnlb .= "\n* [[$nt]]" ;
 53+ else
 54+ $dlb .= "\n* [[$nt]]" ;
 55+ else
 56+ $dnlb .= "\n* [[$nt]]" ;
 57+ }
 58+ }
 59+ } else { # no, so we take $result1 and $result2 and put it in $dnlb
 60+ foreach ( array ( $result1, $result2 ) as $aresult ) {
 61+ while ( $s1 = mysql_fetch_object ( $aresult ) ) {
 62+ $nt = $vpage->getNiceTitle ( $s1->lfrom ) ;
 63+ $dnlb .= "\n* [[$nt]]" ;
 64+ }
 65+
 66+ }
 67+ }
 68+ mysql_free_result ( $result1 ) ;
 69+ mysql_free_result ( $result2 ) ;
 70+ mysql_free_result ( $result3 ) ;
 71+
 72+ if ( $dlb ) $dlb = "<h3>".str_replace("$1",$niceTarget,$wikiLinkhereBacklink)."</h3>$dlb\n" ;
 73+ if ( $dnlb ) $dnlb = "<h3>".str_replace("$1",$niceTarget,$wikiLinkhereNoBacklink)."</h3>$dnlb\n" ;
2774
28 - $result = mysql_query ( $question , $connection ) ;
29 - $p = array () ;
30 - if ( $result != "" ) {
31 - while ( $s = mysql_fetch_object ( $result ) ) array_push ( $p , $s->cur_title ) ;
32 - mysql_free_result ( $result ) ;
33 - }
 75+ $ret = $dnlb.$dlb ;
 76+ if ( $ret == "" ) $ret = "<h1>".str_replace("$1",$niceTarget,$wikiBacklinkNolink)."</h1>" ;
 77+ else $ret = "<h1>".str_replace("$1",$niceTarget,$wikiBacklinkFollowing)."</h1>\n$ret" ;
3478
35 - $question = "SELECT cur_linked_links,cur_unlinked_links FROM cur WHERE cur_title=\"$target\"" ;
36 - $result = mysql_query ( $question , $connection ) ;
37 - $s = mysql_fetch_object ( $result ) ;
38 - mysql_free_result ( $result ) ;
 79+ return $ret ;
 80+}
3981
40 - $out = explode ( "\n" , $s->cur_linked_links."\n".$s->cur_unlinked_links ) ;
41 - $dlb = array () ;
42 - $dnlb = array () ;
43 -
44 - foreach ( $p as $x ) {
45 - $y = $vpage->getNiceTitle ( $x ) ;
46 - if ( in_array ( $x , $out ) ) array_push ( $dlb , $y ) ;
47 - else array_push ( $dnlb , $y ) ;
48 - }
49 -
50 - asort ( $dlb ) ;
51 - $dlb = implode ( "]]\n*[[" , $dlb ) ;
52 - if ( $dlb != "" ) $dlb = "<h3>".str_replace("$1",$niceTarget,$wikiLinkhereBacklink)."</h3>\n*[[$dlb]]\n" ;
53 -
54 - asort ( $dnlb ) ;
55 - $dnlb = implode ( "]]\n*[[" , $dnlb ) ;
56 - if ( $dnlb != "" ) $dnlb = "<h3>".str_replace("$1",$niceTarget,$wikiLinkhereNoBacklink)."</h3>\n*[[$dnlb]]\n" ;
57 -
58 - $ret = $dnlb.$dlb ;
59 - if ( $ret == "" ) $ret = "<h1>".str_replace("$1",$niceTarget,$wikiBacklinkNolink)."</h1>" ;
60 - else $ret = "<h1>".str_replace("$1",$niceTarget,$wikiBacklinkFollowing)."</h1>\n$ret" ;
61 -
62 - return $ret ;
63 - }
6482 ?>

Status & tagging log