00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009 class GSiteMapModel extends PolicyHolder {
00010
00011
00012
00013
00014
00015 public $model;
00016
00017
00018
00019
00020
00021 public $action = 'view';
00022
00023 public $items_per_file = GsitemapController::ITEMS_PER_FILE;
00024
00025 public $priority = 0.0;
00026 public $changefreq = '';
00027
00028 protected $adapter;
00029
00030
00031
00032
00033 public function __construct($model, $action = 'view', $policy = GsitemapController::USE_TIMESTAMP) {
00034 $this->model = $model;
00035 $this->action = $action;
00036 parent::__construct($policy);
00037 }
00038
00039
00040
00041
00042
00043
00044 public function create_adapter() {
00045 if (empty($this->adapter)) {
00046 if ($this->model instanceof IDataObject) {
00047 $this->adapter = $this->model;
00048 }
00049 else {
00050 $this->adapter = DB::create(Cast::string($this->model));
00051 }
00052 }
00053 return $this->adapter;
00054 }
00055
00056 public function get_model_name() {
00057 if (is_string($this->model)) {
00058 return $this->model;
00059 } else {
00060 $adapter = $this->create_adapter();
00061 return $adapter->get_table_name();
00062 }
00063 }
00064
00065
00066
00067
00068 public function select_chunk(IDataObject $adapter, $chunk) {
00069 $adapter->limit($chunk * $this->items_per_file, $this->items_per_file);
00070 }
00071
00072 protected function get_index_elements() {
00073 return array(
00074 $this->get_model_name(),
00075 $this->action
00076 );
00077 }
00078
00079 public function build_index_name($chunk) {
00080 $index_elems = $this->get_index_elements();
00081 $index_elems[] = $chunk;
00082 return implode('.', $index_elems);
00083 }
00084
00085 public function extract_chunk($index) {
00086 $ret = false;
00087 $index_elems = $this->get_index_elements();
00088 $start = implode('.', $index_elems) . '.';
00089 $l_start = strlen($start);
00090 if (substr($index, 0, $l_start) == $start) {
00091 $si = substr($index, $l_start);
00092 $i = Cast::int($si);
00093 if ($si == (string)$i) {
00094 $ret = $i;
00095 }
00096 }
00097 return $ret;
00098 }
00099
00100 public function get_number_of_chunks() {
00101 $adapter = $this->create_adapter();
00102 $c = ceil($adapter->count() / $this->items_per_file);
00103 return $c;
00104 }
00105
00106
00107
00108
00109
00110
00111
00112 public function get_url(IDataObject $item) {
00113 return ActionMapper::get_url($this->action, $item);
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 public function get_lastmod(IDataObject $item) {
00123 $ret = 0;
00124 if ($this->has_policy(GsitemapController::USE_TIMESTAMP) && ($item instanceof ITimeStamped)) {
00125 $ret = $item->get_modification_date();
00126 }
00127 return $ret;
00128 }
00129
00130
00131
00132
00133
00134
00135 public function get_priority() {
00136 return $this->priority;
00137 }
00138
00139
00140
00141
00142
00143
00144 public function get_changefreq() {
00145 return $this->changefreq;
00146 }
00147
00148
00149
00150
00151
00152
00153
00154 public function create_formatter(IDataObject $dao) {
00155 return new GSiteMapItemFormatter($this->get_url($dao), array(
00156 'lastmod' => $this->get_lastmod($dao),
00157 'changefreq' => $this->get_changefreq(),
00158 'priority' => $this->get_priority()
00159 ));
00160 }
00161 }