sitemapsFolder = osc_base_path() . 'sitemaps/'; $this->sitemapUrls = array(); $this->categories = Category::newInstance()->toTree(false); // Return categories with items $this->countries = CountryStats::newInstance()->listCountries('>'); $this->minimumItem = 1; $this->totalItem = mdh_seo_field("seo_sitemap", "i_number_listings"); $this->seo = $seo; try { $this->rules = Madhouse_Seo_Models_Rule::newInstance()->findAll(array("custom" => false)); } catch (Madhouse_NoResultsException $e) { $this->rules = array(); } } public function generate() { $this->testFolder(); $this->deleteSitemaps(); $this->staticPages(); $this->locations(); $this->categories(); $this->items(); $this->users(); $this->index(); $this->pingEngines(); return true; } public function index() { // Create sitemap index file $index = new Index(osc_base_path() . 'sitemap.xml'); // Add URLs foreach ($this->sitemapUrls as $sitemapUrl) { $index->addSitemap($sitemapUrl); } // write it $index->write(); } public function addSitemapUrls($sitemap) { // Get URLs of sitemaps written foreach ($sitemap->getSitemapUrls(osc_base_url() . 'sitemaps/') as $sitemapUrl) { array_push($this->sitemapUrls, $sitemapUrl); } } public function staticPages() { // create sitemap $sitemap = new Sitemap($this->sitemapsFolder . 'sitemap-static-pages.xml'); foreach ($this->rules as $rule) { if ($rule->isIndexed()) { $url = ""; try { $route = Madhouse_Seo_Services_RouteRepository::newInstance()->findRoute($rule->getRoute()); $refresh = Sitemap::WEEKLY; // If base url refresh = always if ($route['s_url'] == osc_base_url()) { $refresh = Sitemap::ALWAYS; } if ($route['s_url'] != "") { $sitemap->addItem($route['s_url'], time(), Sitemap::WEEKLY); } } catch (Exception $e) { } } } // Add static pages if ($this->getRule("page")->isIndexed()) { osc_reset_static_pages(); while (osc_has_static_pages()) : $sitemap->addItem(osc_static_page_url(), time(), Sitemap::ALWAYS); endwhile; View::newInstance()->_erase("pages"); } // Write it $sitemap->write(); $this->addSitemapUrls($sitemap); } public function items() { if (!$this->getRule("item")->isIndexed()) { return; } $aItems = $this->listItems(); View::newInstance()->_exportVariableToView('items', $aItems); if($aItems > 0) { // create sitemap $sitemap = new Sitemap($this->sitemapsFolder . 'sitemap-items.xml'); foreach ($aItems as $item) { View::newInstance()->_exportVariableToView('item', $item); $date = new DateTime((osc_item_mod_date() != '') ? osc_item_mod_date() : osc_item_pub_date()); $sitemap->addItem(osc_item_url(), $date->getTimeStamp(), Sitemap::DAILY); } // Write it $sitemap->write(); $this->addSitemapUrls($sitemap); } } public function users() { $userProfileRobot = $this->getRule("user_pub_profile")->isIndexed(); $userCompanyProfileRobot = $this->getRule("user_pub_profile+company")->isIndexed(); $userSearchRobot = $this->getRule("search/user")->isIndexed(); $userCompanySearchRobot = $this->getRule("search/user+company")->isIndexed(); if (!$userProfileRobot && !$userCompanyProfileRobot && !$userSearchRobot && !$userCompanySearchRobot) { return; } $model = User::newInstance(); $model->dao->select("u.pk_i_id, u.s_username, u.dt_mod_date, u.dt_reg_date, u.i_items, u.dt_access_date, u.b_company"); $model->dao->from(DB_TABLE_PREFIX."t_user u"); $model->dao->join(DB_TABLE_PREFIX.'t_seo_rule as r', 'u.pk_i_id = r.fk_i_user_id', 'LEFT OUTER'); $model->dao->where('COALESCE(r.b_indexed,1) = 1'); $result = $model->dao->get(); if( $result == false ) { $users = array(); } else { $users = $result->result(); } if(count($users) > 0) { // create sitemap $sitemap = new Sitemap($this->sitemapsFolder . 'sitemap-users.xml'); foreach ($users as $user) { if ( ($userProfileRobot && !$user['b_company']) || ($userCompanyProfileRobot && $user['b_company']) ) { $date = new DateTime(($user["dt_mod_date"] != '') ? $user["dt_mod_date"] : $user["dt_reg_date"]); $sitemap->addItem($this->publicProfileUrl($user), $date->getTimeStamp(), Sitemap::WEEKLY); } if ( ($user['i_items'] > 0) && ( ($userSearchRobot && !$user['b_company']) || ($userCompanySearchRobot && $user['b_company']) ) ) { $date = new DateTime(($user["dt_access_date"] != '' && $user["dt_access_date"] != "0000-00-00 00:00:00") ? $user["dt_access_date"] : ''); $url = osc_search_url(array('sUser' => $user['s_username'])); $sitemap->addItem($url, $date->getTimeStamp(), Sitemap::WEEKLY); } } // Write it $sitemap->write(); $this->addSitemapUrls($sitemap); } } public function categories() { // create sitemap $sitemap = new Sitemap($this->sitemapsFolder . 'sitemap-categories.xml'); foreach($this->categories as $category) { if ($this->getRule("search/category")->isIndexed()) { $url = osc_search_url(array('sCategory' => $category['s_slug'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } } if ($this->getRule("search/category+location")->isIndexed()) { $sitemap = $this->categoryAndCountry($sitemap); $sitemap = $this->categoryAndRegion($sitemap); $sitemap = $this->categoryAndCity($sitemap); } // Write it $sitemap->write(); $this->addSitemapUrls($sitemap); } public function categoryAndCountry($sitemap) { $countries = $this->listCountriesWithCategories($sitemap); foreach ($countries as $key => $country) { if($country['num_items'] >= $this->minimumItem) { $category = Category::newInstance()->findByPrimaryKey($country['category_id']); $url = osc_search_url(array('sCategory' => $category['s_slug'], 'sCountry' => $country['country_code'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } } return $sitemap; } public function categoryAndRegion($sitemap) { $regions = $this->listRegionsWithCategories(); foreach ($regions as $region) { if($region['num_items'] >= $this->minimumItem) { $url = osc_search_url(array('sCategory' => $region['category_id'], 'sRegion' => $region['region_id'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } } return $sitemap; } public function categoryAndCity($sitemap) { $cities = $this->listCitiesWithCategories(); foreach ($cities as $city) { if($city['num_items'] >= $this->minimumItem) { $url = osc_search_url(array('sCategory' => $city['category_id'], 'sCity' => $city['city_id'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } } return $sitemap; } public function locations() { // create sitemap $sitemap = new Sitemap($this->sitemapsFolder . 'sitemap-locations.xml'); if (!$this->getRule("search/location")->isIndexed()) { return; } foreach ($this->countries as $country) { $url = osc_search_url(array('sCountry' => $country['country_code'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } $regions = RegionStats::newInstance()->listRegions(); foreach ($regions as $region) { $url = osc_search_url(array('sRegion' => $region['region_id'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } $cities = CityStats::newInstance()->listCities(); foreach ($cities as $city) { $url = osc_search_url(array('sCity' => $city['city_id'])); $sitemap->addItem($url, time(), Sitemap::HOURLY); } // Write it $sitemap->write(); // Get URLs of sitemaps written $this->addSitemapUrls($sitemap); } /** * Ping search engines * @return void */ public function pingEngines() { osc_doRequest('http://www.google.com/webmasters/sitemaps/ping?sitemap=' . urlencode(osc_base_url() . 'sitemap.xml'), array()); osc_doRequest('http://www.bing.com/webmaster/ping.aspx?siteMap=' . urlencode(osc_base_url() . 'sitemap.xml'), array()); osc_doRequest('http://search.yahooapis.com/SiteExplorerService/V1/updateNotification?appid=' . osc_page_title() . '&url=' . urlencode(osc_base_url() . 'sitemap.xml'), array()); } public function publicProfileUrl($user) { if (osc_rewrite_enabled()) { return osc_base_url() . osc_get_preference('rewrite_user_profile') . '/' . rawurlencode($user['s_username']); } return sprintf(osc_base_url(true) . '?page=user&action=pub_profile&id=%d', $user['pk_i_id']); } public function deleteSitemaps() { if (file_exists(osc_base_path() . 'sitemap.xml')) { unlink(osc_base_path() . 'sitemap.xml'); } if (file_exists($this->sitemapsFolder)) { $fsIterator = new FilesystemIterator($this->sitemapsFolder); foreach ($fsIterator as $fileInfo) { if($fileInfo->isFile()) { unlink($fileInfo->getRealPath()); } } } } public function deleteFolder() { if (file_exists($this->sitemapsFolder)) { return rmdir($this->sitemapsFolder); } return true; } public function testFolder() { if (!file_exists($this->sitemapsFolder)) { // Create the directory, if necessary. if (!@mkdir($this->sitemapsFolder, 0755, true)) { $error = error_get_last(); throw new Exception($error["message"]); } } } public function getRule($route) { $rule = array_filter( $this->rules, function ($v) use ($route) { return $v->getRoute() == $route; } ); return array_shift($rule); } /** * Return a list of countries and counter items. * * @access public * @since 2.0.0 * @return array */ public function listCountriesWithCategories() { $model = Item::newInstance(); $model->dao->select( 'fk_c_country_code as country_code, c.pk_i_id as category_id, count(*) as num_items' ); $model->dao->from( $model->getTableName() . " i"); $model->dao->join(DB_TABLE_PREFIX.'t_item_location il', 'i.pk_i_id = il.fk_i_item_id'); $model->dao->join(DB_TABLE_PREFIX.'t_category c', 'i.fk_i_category_id = c.pk_i_id'); $model->dao->where( 'il.fk_c_country_code IS NOT NULL'); $model->dao->where( 'i.b_enabled', 1 ); $model->dao->where( 'b_active', 1 ); $model->dao->where( 'b_spam', 0 ); $model->dao->where( '( b_premium = 1 || dt_expiration >= \'' . date('Y-m-d H:i:s') .'\' )' ); $model->dao->groupBy('il.fk_c_country_code, i.fk_i_category_id'); $rs = $model->dao->get(); if($rs === false) { return array(); } $return = $rs->result(); return $return; } /** * Return a list of regions and counter items. * * @access public * @since 2.0.0 * @return array */ public function listRegionsWithCategories() { $model = Item::newInstance(); $model->dao->select( 'fk_i_region_id as region_id, c.pk_i_id as category_id, count(*) as num_items' ); $model->dao->from( $model->getTableName() . " i"); $model->dao->join(DB_TABLE_PREFIX.'t_item_location il', 'i.pk_i_id = il.fk_i_item_id'); $model->dao->join(DB_TABLE_PREFIX.'t_category c', 'i.fk_i_category_id = c.pk_i_id'); $model->dao->where( 'il.fk_i_region_id IS NOT NULL'); $model->dao->where( 'i.b_enabled', 1 ); $model->dao->where( 'b_active', 1 ); $model->dao->where( 'b_spam', 0 ); $model->dao->where( '( b_premium = 1 || dt_expiration >= \'' . date('Y-m-d H:i:s') .'\' )' ); $model->dao->groupBy('il.fk_i_region_id, i.fk_i_category_id'); $rs = $model->dao->get(); if($rs === false) { return array(); } $return = $rs->result(); return $return; } /** * Return a list of cities and counter items. * * @access public * @since 2.0.0 * @return array */ public function listCitiesWithCategories() { $model = Item::newInstance(); $model->dao->select( 'fk_i_city_id as city_id, c.pk_i_id as category_id, count(*) as num_items' ); $model->dao->from( $model->getTableName() . " i"); $model->dao->join(DB_TABLE_PREFIX.'t_item_location il', 'i.pk_i_id = il.fk_i_item_id'); $model->dao->join(DB_TABLE_PREFIX.'t_category c', 'i.fk_i_category_id = c.pk_i_id'); $model->dao->where( 'il.fk_i_city_id IS NOT NULL'); $model->dao->where( 'i.b_enabled', 1 ); $model->dao->where( 'b_active', 1 ); $model->dao->where( 'b_spam', 0 ); $model->dao->where( '( b_premium = 1 || dt_expiration >= \'' . date('Y-m-d H:i:s') .'\' )' ); $model->dao->groupBy('il.fk_i_city_id, i.fk_i_category_id'); $rs = $model->dao->get(); if($rs === false) { return array(); } $return = $rs->result(); return $return; } /** * Return a list of items. * * @access public * @since 2.0.0 * @return array */ public function listItems() { if( OC_ADMIN ) { $prefLocale = osc_current_admin_locale(); } else { $prefLocale = osc_current_user_locale(); } $model = Item::newInstance(); $model->dao->select( 'i.*, il.*' ); $model->dao->from( $model->getTableName() . " i"); $model->dao->join(DB_TABLE_PREFIX.'t_item_location il', 'i.pk_i_id = il.fk_i_item_id'); $model->dao->where( 'i.b_enabled', 1 ); $model->dao->where( 'b_active', 1 ); $model->dao->where( 'b_spam', 0 ); $model->dao->orderBy('dt_pub_date', 'DESC'); // Add limit if ($this->totalItem > 0) { $model->dao->limit(0, $this->totalItem); } $model->dao->where( '( b_premium = 1 || dt_expiration >= \'' . date('Y-m-d H:i:s') .'\' )' ); $rs = $model->dao->get(); if($rs === false) { return array(); } $return = $rs->result(); return $return; } }