/**
  * Implementation of SMWStore::getInProperties(). This function is meant to
  * be used for finding properties that link to wiki pages.
  *
  * @since 1.8
  * @see SMWStore::getInProperties
  * @param SMWDataItem $value
  * @param SMWRequestOptions $requestoptions
  *
  * @return array of SMWWikiPageValue
  */
 public function getInProperties(SMWDataItem $value, $requestoptions = null)
 {
     wfProfileIn("SMWSQLStore3::getInProperties (SMW)");
     $db = wfGetDB(DB_SLAVE);
     $result = array();
     // Potentially need to get more results, since options apply to union.
     if ($requestoptions !== null) {
         $suboptions = clone $requestoptions;
         $suboptions->limit = $requestoptions->limit + $requestoptions->offset;
         $suboptions->offset = 0;
     } else {
         $suboptions = null;
     }
     $diType = $value->getDIType();
     foreach (SMWSQLStore3::getPropertyTables() as $proptable) {
         if ($diType != $proptable->diType) {
             continue;
         }
         $where = $from = '';
         if ($proptable->fixedproperty == false) {
             // join smw_ids to get property titles
             $from = $db->tableName('smw_ids') . " INNER JOIN " . $db->tableName($proptable->name) . " AS t1 ON t1.p_id=smw_id";
             $this->prepareValueQuery($from, $where, $proptable, $value, 1);
             $res = $db->select($from, 'DISTINCT smw_title,smw_sortkey', $where . $this->store->getSQLConditions($suboptions, 'smw_sortkey', 'smw_sortkey', $where !== ''), 'SMW::getInProperties', $this->store->getSQLOptions($suboptions, 'smw_sortkey'));
             foreach ($res as $row) {
                 try {
                     $result[] = new SMWDIProperty($row->smw_title);
                 } catch (SMWDataItemException $e) {
                     // has been observed to happen (empty property title); cause unclear; ignore this data
                 }
             }
         } else {
             $from = $db->tableName($proptable->name) . " AS t1";
             $this->prepareValueQuery($from, $where, $proptable, $value, 1);
             $res = $db->select($from, '*', $where, 'SMW::getInProperties', array('LIMIT' => 1));
             if ($db->numRows($res) > 0) {
                 $result[] = new SMWDIProperty($proptable->fixedproperty);
             }
         }
         $db->freeResult($res);
     }
     $result = $this->store->applyRequestOptions($result, $requestoptions);
     // apply options to overall result
     wfProfileOut("SMWSQLStore3::getInProperties (SMW)");
     return $result;
 }
 /**
  * Implementation of SMWStore::getInProperties(). This function is meant to
  * be used for finding properties that link to wiki pages.
  *
  * @since 1.8
  * @see SMWStore::getInProperties
  *
  * @param SMWDataItem $value
  * @param SMWRequestOptions|null $requestOptions
  *
  * @return array of SMWWikiPageValue
  */
 public function getInProperties(SMWDataItem $value, SMWRequestOptions $requestOptions = null)
 {
     $db = $this->store->getConnection();
     $result = array();
     // Potentially need to get more results, since options apply to union.
     if ($requestOptions !== null) {
         $subOptions = clone $requestOptions;
         $subOptions->limit = $requestOptions->limit + $requestOptions->offset;
         $subOptions->offset = 0;
     } else {
         $subOptions = null;
     }
     $diType = $value->getDIType();
     foreach ($this->store->getPropertyTables() as $proptable) {
         if ($diType != $proptable->getDiType()) {
             continue;
         }
         $where = $from = '';
         if (!$proptable->isFixedPropertyTable()) {
             // join ID table to get property titles
             $from = $db->tableName(SMWSql3SmwIds::TABLE_NAME) . " INNER JOIN " . $db->tableName($proptable->getName()) . " AS t1 ON t1.p_id=smw_id";
             $this->prepareValueQuery($from, $where, $proptable, $value, 1);
             $where .= " AND smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWIW_OUTDATED) . " AND smw_iw!=" . $db->addQuotes(SMW_SQL3_SMWDELETEIW);
             $res = $db->select($from, 'DISTINCT smw_title,smw_sortkey,smw_iw', $where . $this->store->getSQLConditions($subOptions, 'smw_sortkey', 'smw_sortkey', $where !== ''), __METHOD__, $this->store->getSQLOptions($subOptions, 'smw_sortkey'));
             foreach ($res as $row) {
                 try {
                     $result[] = new SMW\DIProperty($row->smw_title);
                 } catch (SMWDataItemException $e) {
                     // has been observed to happen (empty property title); cause unclear; ignore this data
                 }
             }
         } else {
             $from = $db->tableName($proptable->getName()) . " AS t1";
             $this->prepareValueQuery($from, $where, $proptable, $value, 1);
             $res = $db->select($from, '*', $where, __METHOD__, array('LIMIT' => 1));
             if ($db->numRows($res) > 0) {
                 $result[] = new SMW\DIProperty($proptable->getFixedProperty());
             }
         }
         $db->freeResult($res);
     }
     $result = $this->store->applyRequestOptions($result, $requestOptions);
     // apply options to overall result
     return $result;
 }
 /**
  * Implementation of SMWStore::getWantedPropertiesSpecial(). Like all
  * WantedFoo specials, this function is very resource intensive and needs
  * to be cached on medium/large wikis.
  *
  * @param SMWRequestOptions $requestoptions
  *
  * @return array of array( SMWDIProperty, int )
  */
 public function getWantedPropertiesSpecial($requestoptions = null)
 {
     global $smwgPDefaultType;
     wfProfileIn("SMWSQLStore3::getWantedPropertiesSpecial (SMW)");
     // Note that Wanted Properties must have the default type.
     $proptables = SMWSQLStore3::getPropertyTables();
     $proptable = $proptables[SMWSQLStore3::findTypeTableId($smwgPDefaultType)];
     $result = array();
     if (!$proptable->isFixedPropertyTable()) {
         // anything else would be crazy, but let's fail gracefully even if the whole world is crazy
         // Wikia change - hack for smw+
         // $dbr = wfGetDB( DB_SLAVE );
         // $options = $this->store->getSQLOptions( $requestoptions, 'title' );
         // $options['ORDER BY'] = 'count DESC';
         // $res = $dbr->select( // TODO: this is not how JOINS should be specified in the select function
         // 	$dbr->tableName( $proptable->getName() ) . ' INNER JOIN ' .
         // 		$dbr->tableName( SMWSql3SmwIds::tableName ) . ' ON p_id=smw_id LEFT JOIN ' .
         // 		$dbr->tableName( 'page' ) . ' ON (page_namespace=' .
         // 		$dbr->addQuotes( SMW_NS_PROPERTY ) . ' AND page_title=smw_title)',
         // 	'smw_title, COUNT(*) as count',
         // 	'smw_id > 50 AND page_id IS NULL GROUP BY smw_title',
         // 	'SMW::getWantedPropertiesSpecial',
         // 	$options
         // );
         $dbr = wfGetDB(DB_SLAVE, 'smw');
         $dbl = wfGetDB(DB_SLAVE);
         // local connection
         $res = $dbl->select($dbl->tableName('page'), 'page_title', ['page_namespace' => SMW_NS_PROPERTY], __METHOD__);
         $titles = [];
         foreach ($res as $row) {
             $titles[] = $row->page_title;
         }
         $condition = !empty($titles) ? ' AND smw_title NOT IN (' . $dbl->makeList($titles) . ')' : '';
         $options = $this->store->getSQLOptions($requestoptions, 'title');
         $options['ORDER BY'] = 'count DESC';
         $res = $dbr->select($dbr->tableName($proptable->getName()) . ' INNER JOIN ' . $dbr->tableName(SMWSql3SmwIds::tableName) . ' ON p_id=smw_id', 'smw_title, COUNT(*) as count', "smw_id > 50 {$condition} GROUP BY smw_title", 'SMW::getWantedPropertiesSpecial', $options);
         foreach ($res as $row) {
             $result[] = array(new SMWDIProperty($row->smw_title), $row->count);
         }
     }
     wfProfileOut("SMWSQLStore3::getWantedPropertiesSpecial (SMW)");
     return $result;
 }
 /**
  * Implementation of SMWStore::getWantedPropertiesSpecial(). Like all
  * WantedFoo specials, this function is very resource intensive and needs
  * to be cached on medium/large wikis.
  *
  * @param SMWRequestOptions $requestoptions
  *
  * @return array of array( SMWDIProperty, int )
  */
 public function getWantedPropertiesSpecial($requestoptions = null)
 {
     global $smwgPDefaultType;
     wfProfileIn("SMWSQLStore3::getWantedPropertiesSpecial (SMW)");
     // Note that Wanted Properties must have the default type.
     $proptables = SMWSQLStore3::getPropertyTables();
     $proptable = $proptables[SMWSQLStore3::findTypeTableId($smwgPDefaultType)];
     $result = array();
     if ($proptable->fixedproperty == false) {
         // anything else would be crazy, but let's fail gracefully even if the whole world is crazy
         $dbr = wfGetDB(DB_SLAVE);
         $options = $this->store->getSQLOptions($requestoptions, 'title');
         $options['ORDER BY'] = 'count DESC';
         $res = $dbr->select($dbr->tableName($proptable->name) . ' INNER JOIN ' . $dbr->tableName('smw_ids') . ' ON p_id=smw_id LEFT JOIN ' . $dbr->tableName('page') . ' ON (page_namespace=' . $dbr->addQuotes(SMW_NS_PROPERTY) . ' AND page_title=smw_title)', 'smw_title, COUNT(*) as count', 'smw_id > 50 AND page_id IS NULL GROUP BY smw_title', 'SMW::getWantedPropertiesSpecial', $options);
         foreach ($res as $row) {
             $result[] = array(new SMWDIProperty($row->smw_title), $row->count);
         }
     }
     wfProfileOut("SMWSQLStore3::getWantedPropertiesSpecial (SMW)");
     return $result;
 }