Initial revision
[lhc/web/wiklou.git] / includes / LinkCache.php
1 <?
2 # Cache for article titles and ids linked from one source
3
4 class LinkCache {
5
6 /* private */ var $mGoodLinks, $mBadLinks, $mActive;
7 /* private */ var $mImageLinks;
8
9 function LinkCache()
10 {
11 $this->mActive = true;
12 $this->mGoodLinks = array();
13 $this->mBadLinks = array();
14 $this->mImageLinks = array();
15 }
16
17 function getGoodLinkID( $title )
18 {
19 if ( array_key_exists( $title, $this->mGoodLinks ) ) {
20 return $this->mGoodLinks[$title];
21 } else {
22 return 0;
23 }
24 }
25
26 function isBadLink( $title )
27 {
28 return in_array( $title, $this->mBadLinks );
29 }
30
31 function addGoodLink( $id, $title )
32 {
33 if ( $this->mActive ) {
34 $this->mGoodLinks[$title] = $id;
35 }
36 }
37
38 function addBadLink( $title )
39 {
40 if ( $this->mActive && ( ! $this->isBadLink( $title ) ) ) {
41 array_push( $this->mBadLinks, $title );
42 }
43 }
44
45 function addImageLink( $title )
46 {
47 if ( $this->mActive ) { $this->mImageLinks[$title] = 1; }
48 }
49
50 function clearBadLink( $title )
51 {
52 $index = array_search( $title, $this->mBadLinks );
53 if ( isset( $index ) ) {
54 unset( $this->mBadLinks[$index] );
55 }
56 }
57
58 function suspend() { $this->mActive = false; }
59 function resume() { $this->mActive = true; }
60 function getGoodLinks() { return $this->mGoodLinks; }
61 function getBadLinks() { return $this->mBadLinks; }
62 function getImageLinks() { return $this->mImageLinks; }
63
64 function addLink( $title )
65 {
66 if ( $this->isBadLink( $title ) ) { return 0; }
67 $id = $this->getGoodLinkID( $title );
68 if ( 0 != $id ) { return $id; }
69
70 $nt = Title::newFromDBkey( $title );
71 $ns = $nt->getNamespace();
72 $t = $nt->getDBkey();
73
74 if ( "" == $t ) { return 0; }
75 $sql = "SELECT HIGH_PRIORITY cur_id FROM cur WHERE cur_namespace=" .
76 "{$ns} AND cur_title='" . wfStrencode( $t ) . "'";
77 $res = wfQuery( $sql, "LinkCache::addLink" );
78
79 if ( 0 == wfNumRows( $res ) ) {
80 $id = 0;
81 } else {
82 $s = wfFetchObject( $res );
83 $id = $s->cur_id;
84 }
85 if ( 0 == $id ) { $this->addBadLink( $title ); }
86 else { $this->addGoodLink( $id, $title ); }
87 return $id;
88 }
89
90 function preFill( $fromtitle )
91 {
92 wfProfileIn( "LinkCache::preFill" );
93 # Note -- $fromtitle is a Title *object*
94 $dbkeyfrom = wfStrencode( $fromtitle->getPrefixedDBKey() );
95 $sql = "SELECT HIGH_PRIORITY cur_id,cur_namespace,cur_title
96 FROM cur,links
97 WHERE cur_id=l_to AND l_from='{$dbkeyfrom}'";
98 $res = wfQuery( $sql, "LinkCache::preFill" );
99 while( $s = wfFetchObject( $res ) ) {
100 $this->addGoodLink( $s->cur_id,
101 Title::makeName( $s->cur_namespace, $s->cur_title )
102 );
103 }
104 wfProfileOut();
105 }
106
107 }
108
109 ?>