gave LinkBatch its own file
[lhc/web/wiklou.git] / includes / LinkBatch.php
1 <?php
2
3 /**
4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
6 +
7 * @package MediaWiki
8 * @subpackage Cache
9 */
10 class LinkBatch {
11 /**
12 * 2-d array, first index namespace, second index dbkey, value arbitrary
13 */
14 var $data = array();
15
16 function LinkBatch( $arr = array() ) {
17 foreach( $arr as $item ) {
18 $this->addObj( $item );
19 }
20 }
21
22 function addObj( $title ) {
23 if ( is_object( $title ) ) {
24 $this->add( $title->getNamespace(), $title->getDBkey() );
25 } else {
26 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
27 }
28 }
29
30 function add( $ns, $dbkey ) {
31 if ( $ns < 0 ) {
32 return;
33 }
34 if ( !array_key_exists( $ns, $this->data ) ) {
35 $this->data[$ns] = array();
36 }
37
38 $this->data[$ns][$dbkey] = 1;
39 }
40
41 /**
42 * Set the link list to a given 2-d array
43 * First key is the namespace, second is the DB key, value arbitrary
44 */
45 function setArray( $array ) {
46 $this->data = $array;
47 }
48
49 /**
50 * Do the query and add the results to a LinkCache object
51 * Return an array mapping PDBK to ID
52 */
53 function execute( &$cache ) {
54 $fname = 'LinkBatch::execute';
55 wfProfileIn( $fname );
56 // Do query
57 $res = $this->doQuery();
58 if ( !$res ) {
59 wfProfileOut( $fname );
60 return array();
61 }
62
63 // For each returned entry, add it to the list of good links, and remove it from $remaining
64
65 $ids = array();
66 $remaining = $this->data;
67 while ( $row = $res->fetchObject() ) {
68 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
69 $cache->addGoodLinkObj( $row->page_id, $title );
70 $ids[$title->getPrefixedDBkey()] = $row->page_id;
71 unset( $remaining[$row->page_namespace][$row->page_title] );
72 }
73 $res->free();
74
75 // The remaining links in $data are bad links, register them as such
76 foreach ( $remaining as $ns => $dbkeys ) {
77 foreach ( $dbkeys as $dbkey => $nothing ) {
78 $title = Title::makeTitle( $ns, $dbkey );
79 $cache->addBadLinkObj( $title );
80 $ids[$title->getPrefixedDBkey()] = 0;
81 }
82 }
83 wfProfileOut( $fname );
84 return $ids;
85 }
86
87 /**
88 * Perform the existence test query, return a ResultWrapper with page_id fields
89 */
90 function doQuery() {
91 $fname = 'LinkBatch::execute';
92 $namespaces = array();
93
94 if ( !count( $this->data ) ) {
95 return false;
96 }
97 wfProfileIn( $fname );
98
99 // Construct query
100 // This is very similar to Parser::replaceLinkHolders
101 $dbr =& wfGetDB( DB_SLAVE );
102 $page = $dbr->tableName( 'page' );
103 $set = $this->constructSet( 'page', $dbr );
104 if ( $set === false ) {
105 return false;
106 }
107 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
108
109 // Do query
110 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
111 wfProfileOut( $fname );
112 return $res;
113 }
114
115 /**
116 * Construct a WHERE clause which will match all the given titles.
117 * Give the appropriate table's field name prefix ('page', 'pl', etc).
118 *
119 * @param string $prefix
120 * @return string
121 * @access public
122 */
123 function constructSet( $prefix, &$db ) {
124 $first = true;
125 $firstTitle = true;
126 $sql = '';
127 foreach ( $this->data as $ns => $dbkeys ) {
128 if ( !count( $dbkeys ) ) {
129 continue;
130 }
131
132 if ( $first ) {
133 $first = false;
134 } else {
135 $sql .= ' OR ';
136 }
137 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
138
139 $firstTitle = true;
140 foreach( $dbkeys as $dbkey => $nothing ) {
141 if ( $firstTitle ) {
142 $firstTitle = false;
143 } else {
144 $sql .= ',';
145 }
146 $sql .= $db->addQuotes( $dbkey );
147 }
148
149 $sql .= '))';
150 }
151 if ( $first && $firstTitle ) {
152 # No titles added
153 return false;
154 } else {
155 return $sql;
156 }
157 }
158 }
159
160 ?>