LinkBatch.php 4.6 KB

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