Restore hooks UploadForm:initial and UploadForm:BeforeProcessing removed in r57868.
[mediawiki.git] / includes / LinkBatch.php
blobd9a9666dc2e8e8dcc7535fea68050cf16caf4971
1 <?php
3 /**
4 * Class representing a list of titles
5 * The execute() method checks them all for existence and adds them to a LinkCache object
7 * @ingroup Cache
8 */
9 class LinkBatch {
10 /**
11 * 2-d array, first index namespace, second index dbkey, value arbitrary
13 var $data = array();
15 function __construct( $arr = array() ) {
16 foreach( $arr as $item ) {
17 $this->addObj( $item );
21 public function addObj( $title ) {
22 if ( is_object( $title ) ) {
23 $this->add( $title->getNamespace(), $title->getDBkey() );
24 } else {
25 wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
29 public function add( $ns, $dbkey ) {
30 if ( $ns < 0 ) {
31 return;
33 if ( !array_key_exists( $ns, $this->data ) ) {
34 $this->data[$ns] = array();
37 $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
40 /**
41 * Set the link list to a given 2-d array
42 * First key is the namespace, second is the DB key, value arbitrary
44 public function setArray( $array ) {
45 $this->data = $array;
48 /**
49 * Returns true if no pages have been added, false otherwise.
51 public function isEmpty() {
52 return ($this->getSize() == 0);
55 /**
56 * Returns the size of the batch.
58 public function getSize() {
59 return count( $this->data );
62 /**
63 * Do the query and add the results to the LinkCache object
64 * Return an array mapping PDBK to ID
66 public function execute() {
67 $linkCache = LinkCache::singleton();
68 return $this->executeInto( $linkCache );
71 /**
72 * Do the query and add the results to a given LinkCache object
73 * Return an array mapping PDBK to ID
75 protected function executeInto( &$cache ) {
76 wfProfileIn( __METHOD__ );
77 $res = $this->doQuery();
78 $ids = $this->addResultToCache( $cache, $res );
79 wfProfileOut( __METHOD__ );
80 return $ids;
83 /**
84 * Add a ResultWrapper containing IDs and titles to a LinkCache object.
85 * As normal, titles will go into the static Title cache field.
86 * This function *also* stores extra fields of the title used for link
87 * parsing to avoid extra DB queries.
89 public function addResultToCache( $cache, $res ) {
90 if ( !$res ) {
91 return array();
94 // For each returned entry, add it to the list of good links, and remove it from $remaining
96 $ids = array();
97 $remaining = $this->data;
98 while ( $row = $res->fetchObject() ) {
99 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
100 $cache->addGoodLinkObj( $row->page_id, $title, $row->page_len, $row->page_is_redirect );
101 $ids[$title->getPrefixedDBkey()] = $row->page_id;
102 unset( $remaining[$row->page_namespace][$row->page_title] );
105 // The remaining links in $data are bad links, register them as such
106 foreach ( $remaining as $ns => $dbkeys ) {
107 foreach ( $dbkeys as $dbkey => $unused ) {
108 $title = Title::makeTitle( $ns, $dbkey );
109 $cache->addBadLinkObj( $title );
110 $ids[$title->getPrefixedDBkey()] = 0;
113 return $ids;
117 * Perform the existence test query, return a ResultWrapper with page_id fields
119 public function doQuery() {
120 if ( $this->isEmpty() ) {
121 return false;
123 wfProfileIn( __METHOD__ );
125 // Construct query
126 // This is very similar to Parser::replaceLinkHolders
127 $dbr = wfGetDB( DB_SLAVE );
128 $page = $dbr->tableName( 'page' );
129 $set = $this->constructSet( 'page', $dbr );
130 if ( $set === false ) {
131 wfProfileOut( __METHOD__ );
132 return false;
134 $sql = "SELECT page_id, page_namespace, page_title, page_len, page_is_redirect FROM $page WHERE $set";
136 // Do query
137 $res = $dbr->query( $sql, __METHOD__ );
138 wfProfileOut( __METHOD__ );
139 return $res;
143 * Construct a WHERE clause which will match all the given titles.
145 * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
146 * @return string
147 * @public
149 public function constructSet( $prefix, &$db ) {
150 $first = true;
151 $firstTitle = true;
152 $sql = '';
153 foreach ( $this->data as $ns => $dbkeys ) {
154 if ( !count( $dbkeys ) ) {
155 continue;
158 if ( $first ) {
159 $first = false;
160 } else {
161 $sql .= ' OR ';
164 if (count($dbkeys)==1) { // avoid multiple-reference syntax if simple equality can be used
165 $singleKey = array_keys($dbkeys);
166 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title=".
167 $db->addQuotes($singleKey[0]).
168 ")";
169 } else {
170 $sql .= "({$prefix}_namespace=$ns AND {$prefix}_title IN (";
172 $firstTitle = true;
173 foreach( $dbkeys as $dbkey => $unused ) {
174 if ( $firstTitle ) {
175 $firstTitle = false;
176 } else {
177 $sql .= ',';
179 $sql .= $db->addQuotes( $dbkey );
181 $sql .= '))';
184 if ( $first && $firstTitle ) {
185 # No titles added
186 return false;
187 } else {
188 return $sql;