Added LinkCache::addGoodLinkObjFromRow, since addGoodLinkObj is not going to work...
[lhc/web/wiklou.git] / includes / cache / 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 * @ingroup Cache
8 */
9 class LinkBatch {
10 /**
11 * 2-d array, first index namespace, second index dbkey, value arbitrary
12 */
13 var $data = array();
14
15 /**
16 * For debugging which method is using this class.
17 */
18 protected $caller;
19
20 function __construct( $arr = array() ) {
21 foreach( $arr as $item ) {
22 $this->addObj( $item );
23 }
24 }
25
26 /**
27 * Use ->setCaller( __METHOD__ ) to indicate which code is using this
28 * class. Only used in debugging output.
29 * @since 1.17
30 *
31 * @param $caller
32 */
33 public function setCaller( $caller ) {
34 $this->caller = $caller;
35 }
36
37 /**
38 * @param $title Title
39 */
40 public function addObj( $title ) {
41 if ( is_object( $title ) ) {
42 $this->add( $title->getNamespace(), $title->getDBkey() );
43 } else {
44 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
45 }
46 }
47
48 public function add( $ns, $dbkey ) {
49 if ( $ns < 0 ) {
50 return;
51 }
52 if ( !array_key_exists( $ns, $this->data ) ) {
53 $this->data[$ns] = array();
54 }
55
56 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
57 }
58
59 /**
60 * Set the link list to a given 2-d array
61 * First key is the namespace, second is the DB key, value arbitrary
62 *
63 * @param $array array
64 */
65 public function setArray( $array ) {
66 $this->data = $array;
67 }
68
69 /**
70 * Returns true if no pages have been added, false otherwise.
71 *
72 * @return bool
73 */
74 public function isEmpty() {
75 return ($this->getSize() == 0);
76 }
77
78 /**
79 * Returns the size of the batch.
80 *
81 * @return int
82 */
83 public function getSize() {
84 return count( $this->data );
85 }
86
87 /**
88 * Do the query and add the results to the LinkCache object
89 * Return an array mapping PDBK to ID
90 */
91 public function execute() {
92 $linkCache = LinkCache::singleton();
93 return $this->executeInto( $linkCache );
94 }
95
96 /**
97 * Do the query and add the results to a given LinkCache object
98 * Return an array mapping PDBK to ID
99 */
100 protected function executeInto( &$cache ) {
101 wfProfileIn( __METHOD__ );
102 $res = $this->doQuery();
103 $ids = $this->addResultToCache( $cache, $res );
104 $this->doGenderQuery();
105 wfProfileOut( __METHOD__ );
106 return $ids;
107 }
108
109 /**
110 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
111 * As normal, titles will go into the static Title cache field.
112 * This function *also* stores extra fields of the title used for link
113 * parsing to avoid extra DB queries.
114 *
115 * @param $cache
116 * @param $res
117 */
118 public function addResultToCache( $cache, $res ) {
119 if ( !$res ) {
120 return array();
121 }
122
123 // For each returned entry, add it to the list of good links, and remove it from $remaining
124
125 $ids = array();
126 $remaining = $this->data;
127 foreach ( $res as $row ) {
128 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
129 $cache->addGoodLinkObjFromRow( $title, $row );
130 $ids[$title->getPrefixedDBkey()] = $row->page_id;
131 unset( $remaining[$row->page_namespace][$row->page_title] );
132 }
133
134 // The remaining links in $data are bad links, register them as such
135 foreach ( $remaining as $ns => $dbkeys ) {
136 foreach ( $dbkeys as $dbkey => $unused ) {
137 $title = Title::makeTitle( $ns, $dbkey );
138 $cache->addBadLinkObj( $title );
139 $ids[$title->getPrefixedDBkey()] = 0;
140 }
141 }
142 return $ids;
143 }
144
145 /**
146 * Perform the existence test query, return a ResultWrapper with page_id fields
147 */
148 public function doQuery() {
149 if ( $this->isEmpty() ) {
150 return false;
151 }
152 wfProfileIn( __METHOD__ );
153
154 // This is similar to LinkHolderArray::replaceInternal
155 $dbr = wfGetDB( DB_SLAVE );
156 $table = 'page';
157 $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
158 'page_is_redirect', 'page_latest' );
159 $conds = $this->constructSet( 'page', $dbr );
160
161 // Do query
162 $caller = __METHOD__;
163 if ( strval( $this->caller ) !== '' ) {
164 $caller .= " (for {$this->caller})";
165 }
166 $res = $dbr->select( $table, $fields, $conds, $caller );
167 wfProfileOut( __METHOD__ );
168 return $res;
169 }
170
171 public function doGenderQuery() {
172 if ( $this->isEmpty() ) {
173 return false;
174 }
175
176 global $wgContLang;
177 if ( !$wgContLang->needsGenderDistinction() ) {
178 return false;
179 }
180
181 $genderCache = GenderCache::singleton();
182 $genderCache->dolinkBatch( $this->data, $this->caller );
183 }
184
185 /**
186 * Construct a WHERE clause which will match all the given titles.
187 *
188 * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
189 * @param $db DatabaseBase object to use
190 * @return mixed string with SQL where clause fragment, or false if no items.
191 */
192 public function constructSet( $prefix, $db ) {
193 return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
194 }
195 }