Switching from phpdoc to doxygen (use less than 32MB of memory).
[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 the LinkCache object
51 * Return an array mapping PDBK to ID
52 */
53 function execute() {
54 $linkCache =& LinkCache::singleton();
55 $this->executeInto( $linkCache );
56 }
57
58 /**
59 * Do the query and add the results to a given LinkCache object
60 * Return an array mapping PDBK to ID
61 */
62 function executeInto( &$cache ) {
63 $fname = 'LinkBatch::executeInto';
64 wfProfileIn( $fname );
65 // Do query
66 $res = $this->doQuery();
67 if ( !$res ) {
68 wfProfileOut( $fname );
69 return array();
70 }
71
72 // For each returned entry, add it to the list of good links, and remove it from $remaining
73
74 $ids = array();
75 $remaining = $this->data;
76 while ( $row = $res->fetchObject() ) {
77 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
78 $cache->addGoodLinkObj( $row->page_id, $title );
79 $ids[$title->getPrefixedDBkey()] = $row->page_id;
80 unset( $remaining[$row->page_namespace][$row->page_title] );
81 }
82 $res->free();
83
84 // The remaining links in $data are bad links, register them as such
85 foreach ( $remaining as $ns => $dbkeys ) {
86 foreach ( $dbkeys as $dbkey => $nothing ) {
87 $title = Title::makeTitle( $ns, $dbkey );
88 $cache->addBadLinkObj( $title );
89 $ids[$title->getPrefixedDBkey()] = 0;
90 }
91 }
92 wfProfileOut( $fname );
93 return $ids;
94 }
95
96 /**
97 * Perform the existence test query, return a ResultWrapper with page_id fields
98 */
99 function doQuery() {
100 $fname = 'LinkBatch::doQuery';
101 $namespaces = array();
102
103 if ( !count( $this->data ) ) {
104 return false;
105 }
106 wfProfileIn( $fname );
107
108 // Construct query
109 // This is very similar to Parser::replaceLinkHolders
110 $dbr =& wfGetDB( DB_SLAVE );
111 $page = $dbr->tableName( 'page' );
112 $set = $this->constructSet( 'page', $dbr );
113 if ( $set === false ) {
114 wfProfileOut( $fname );
115 return false;
116 }
117 $sql = "SELECT page_id, page_namespace, page_title FROM $page WHERE $set";
118
119 // Do query
120 $res = new ResultWrapper( $dbr, $dbr->query( $sql, $fname ) );
121 wfProfileOut( $fname );
122 return $res;
123 }
124
125 /**
126 * Construct a WHERE clause which will match all the given titles.
127 * Give the appropriate table's field name prefix ('page', 'pl', etc).
128 *
129 * @param $prefix String: ??
130 * @return string
131 * @public
132 */
133 function constructSet( $prefix, &$db ) {
134 $first = true;
135 $firstTitle = true;
136 $sql = '';
137 foreach ( $this->data as $ns => $dbkeys ) {
138 if ( !count( $dbkeys ) ) {
139 continue;
140 }
141
142 if ( $first ) {
143 $first = false;
144 } else {
145 $sql .= ' OR ';
146 }
147 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
148
149 $firstTitle = true;
150 foreach( $dbkeys as $dbkey => $nothing ) {
151 if ( $firstTitle ) {
152 $firstTitle = false;
153 } else {
154 $sql .= ',';
155 }
156 $sql .= $db->addQuotes( $dbkey );
157 }
158
159 $sql .= '))';
160 }
161 if ( $first && $firstTitle ) {
162 # No titles added
163 return false;
164 } else {
165 return $sql;
166 }
167 }
168 }
169
170 ?>