[ Index ]

PHP Cross Reference of MediaWiki-1.24.0

title

Body

[close]

/includes/cache/ -> LinkBatch.php (source)

   1  <?php
   2  /**
   3   * Batch query to determine page existence.
   4   *
   5   * This program is free software; you can redistribute it and/or modify
   6   * it under the terms of the GNU General Public License as published by
   7   * the Free Software Foundation; either version 2 of the License, or
   8   * (at your option) any later version.
   9   *
  10   * This program is distributed in the hope that it will be useful,
  11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13   * GNU General Public License for more details.
  14   *
  15   * You should have received a copy of the GNU General Public License along
  16   * with this program; if not, write to the Free Software Foundation, Inc.,
  17   * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18   * http://www.gnu.org/copyleft/gpl.html
  19   *
  20   * @file
  21   * @ingroup Cache
  22   */
  23  
  24  /**
  25   * Class representing a list of titles
  26   * The execute() method checks them all for existence and adds them to a LinkCache object
  27   *
  28   * @ingroup Cache
  29   */
  30  class LinkBatch {
  31      /**
  32       * 2-d array, first index namespace, second index dbkey, value arbitrary
  33       */
  34      public $data = array();
  35  
  36      /**
  37       * For debugging which method is using this class.
  38       */
  39      protected $caller;
  40  
  41  	function __construct( $arr = array() ) {
  42          foreach ( $arr as $item ) {
  43              $this->addObj( $item );
  44          }
  45      }
  46  
  47      /**
  48       * Use ->setCaller( __METHOD__ ) to indicate which code is using this
  49       * class. Only used in debugging output.
  50       * @since 1.17
  51       *
  52       * @param string $caller
  53       */
  54  	public function setCaller( $caller ) {
  55          $this->caller = $caller;
  56      }
  57  
  58      /**
  59       * @param Title $title
  60       */
  61  	public function addObj( $title ) {
  62          if ( is_object( $title ) ) {
  63              $this->add( $title->getNamespace(), $title->getDBkey() );
  64          } else {
  65              wfDebug( "Warning: LinkBatch::addObj got invalid title object\n" );
  66          }
  67      }
  68  
  69      /**
  70       * @param int $ns
  71       * @param string $dbkey
  72       */
  73  	public function add( $ns, $dbkey ) {
  74          if ( $ns < 0 ) {
  75              return;
  76          }
  77          if ( !array_key_exists( $ns, $this->data ) ) {
  78              $this->data[$ns] = array();
  79          }
  80  
  81          $this->data[$ns][str_replace( ' ', '_', $dbkey )] = 1;
  82      }
  83  
  84      /**
  85       * Set the link list to a given 2-d array
  86       * First key is the namespace, second is the DB key, value arbitrary
  87       *
  88       * @param array $array
  89       */
  90  	public function setArray( $array ) {
  91          $this->data = $array;
  92      }
  93  
  94      /**
  95       * Returns true if no pages have been added, false otherwise.
  96       *
  97       * @return bool
  98       */
  99  	public function isEmpty() {
 100          return $this->getSize() == 0;
 101      }
 102  
 103      /**
 104       * Returns the size of the batch.
 105       *
 106       * @return int
 107       */
 108  	public function getSize() {
 109          return count( $this->data );
 110      }
 111  
 112      /**
 113       * Do the query and add the results to the LinkCache object
 114       *
 115       * @return array Mapping PDBK to ID
 116       */
 117  	public function execute() {
 118          $linkCache = LinkCache::singleton();
 119  
 120          return $this->executeInto( $linkCache );
 121      }
 122  
 123      /**
 124       * Do the query and add the results to a given LinkCache object
 125       * Return an array mapping PDBK to ID
 126       *
 127       * @param LinkCache $cache
 128       * @return array Remaining IDs
 129       */
 130  	protected function executeInto( &$cache ) {
 131          wfProfileIn( __METHOD__ );
 132          $res = $this->doQuery();
 133          $this->doGenderQuery();
 134          $ids = $this->addResultToCache( $cache, $res );
 135          wfProfileOut( __METHOD__ );
 136  
 137          return $ids;
 138      }
 139  
 140      /**
 141       * Add a ResultWrapper containing IDs and titles to a LinkCache object.
 142       * As normal, titles will go into the static Title cache field.
 143       * This function *also* stores extra fields of the title used for link
 144       * parsing to avoid extra DB queries.
 145       *
 146       * @param LinkCache $cache
 147       * @param ResultWrapper $res
 148       * @return array Array of remaining titles
 149       */
 150  	public function addResultToCache( $cache, $res ) {
 151          if ( !$res ) {
 152              return array();
 153          }
 154  
 155          // For each returned entry, add it to the list of good links, and remove it from $remaining
 156  
 157          $ids = array();
 158          $remaining = $this->data;
 159          foreach ( $res as $row ) {
 160              $title = Title::makeTitle( $row->page_namespace, $row->page_title );
 161              $cache->addGoodLinkObjFromRow( $title, $row );
 162              $ids[$title->getPrefixedDBkey()] = $row->page_id;
 163              unset( $remaining[$row->page_namespace][$row->page_title] );
 164          }
 165  
 166          // The remaining links in $data are bad links, register them as such
 167          foreach ( $remaining as $ns => $dbkeys ) {
 168              foreach ( $dbkeys as $dbkey => $unused ) {
 169                  $title = Title::makeTitle( $ns, $dbkey );
 170                  $cache->addBadLinkObj( $title );
 171                  $ids[$title->getPrefixedDBkey()] = 0;
 172              }
 173          }
 174  
 175          return $ids;
 176      }
 177  
 178      /**
 179       * Perform the existence test query, return a ResultWrapper with page_id fields
 180       * @return bool|ResultWrapper
 181       */
 182  	public function doQuery() {
 183          global $wgContentHandlerUseDB;
 184  
 185          if ( $this->isEmpty() ) {
 186              return false;
 187          }
 188          wfProfileIn( __METHOD__ );
 189  
 190          // This is similar to LinkHolderArray::replaceInternal
 191          $dbr = wfGetDB( DB_SLAVE );
 192          $table = 'page';
 193          $fields = array( 'page_id', 'page_namespace', 'page_title', 'page_len',
 194              'page_is_redirect', 'page_latest' );
 195  
 196          if ( $wgContentHandlerUseDB ) {
 197              $fields[] = 'page_content_model';
 198          }
 199  
 200          $conds = $this->constructSet( 'page', $dbr );
 201  
 202          // Do query
 203          $caller = __METHOD__;
 204          if ( strval( $this->caller ) !== '' ) {
 205              $caller .= " (for {$this->caller})";
 206          }
 207          $res = $dbr->select( $table, $fields, $conds, $caller );
 208          wfProfileOut( __METHOD__ );
 209  
 210          return $res;
 211      }
 212  
 213      /**
 214       * Do (and cache) {{GENDER:...}} information for userpages in this LinkBatch
 215       *
 216       * @return bool Whether the query was successful
 217       */
 218  	public function doGenderQuery() {
 219          if ( $this->isEmpty() ) {
 220              return false;
 221          }
 222  
 223          global $wgContLang;
 224          if ( !$wgContLang->needsGenderDistinction() ) {
 225              return false;
 226          }
 227  
 228          $genderCache = GenderCache::singleton();
 229          $genderCache->doLinkBatch( $this->data, $this->caller );
 230  
 231          return true;
 232      }
 233  
 234      /**
 235       * Construct a WHERE clause which will match all the given titles.
 236       *
 237       * @param string $prefix The appropriate table's field name prefix ('page', 'pl', etc)
 238       * @param DatabaseBase $db DatabaseBase object to use
 239       * @return string|bool String with SQL where clause fragment, or false if no items.
 240       */
 241  	public function constructSet( $prefix, $db ) {
 242          return $db->makeWhereFrom2d( $this->data, "{$prefix}_namespace", "{$prefix}_title" );
 243      }
 244  }


Generated: Fri Nov 28 14:03:12 2014 Cross-referenced by PHPXref 0.7.1