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