00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042 include_once( 'kernel/classes/ezpersistentobject.php' );
00043
00044 class eZURL extends eZPersistentObject
00045 {
00046
00047
00048 function eZURL( $row )
00049 {
00050 $this->eZPersistentObject( $row );
00051 }
00052
00053 function definition()
00054 {
00055 return array( 'fields' => array( 'id' => array( 'name' => 'ID',
00056 'datatype' => 'integer',
00057 'default' => 0,
00058 'required' => true ),
00059 'url' => array( 'name' => 'URL',
00060 'datatype' => 'string',
00061 'default' => '',
00062 'required' => true ),
00063 'original_url_md5' => array( 'name' => 'OriginalURLMD5',
00064 'datatype' => 'string',
00065 'default' => '',
00066 'required' => true ),
00067 'is_valid' => array( 'name' => 'IsValid',
00068 'datatype' => 'integer',
00069 'default' => 0,
00070 'required' => true ),
00071 'last_checked' => array( 'name' => 'LastChecked',
00072 'datatype' => 'integer',
00073 'default' => 0,
00074 'required' => true ),
00075 'created' => array( 'name' => 'Created',
00076 'datatype' => 'integer',
00077 'default' => 0,
00078 'required' => true ),
00079 'modified' => array( 'name' => 'Modified',
00080 'datatype' => 'integer',
00081 'default' => 0,
00082 'required' => true ) ),
00083 'keys' => array( 'id' ),
00084 'increment_key' => 'id',
00085 'class_name' => 'eZURL',
00086 'name' => 'ezurl' );
00087 }
00088
00089 function create( $url )
00090 {
00091 $dateTime = time();
00092 $row = array(
00093 'id' => null,
00094 'url' => $url,
00095 'original_url_md5' => md5( $url ),
00096 'is_valid' => true,
00097 'last_checked' => 0,
00098 'created' => $dateTime,
00099 'modified' => $dateTime );
00100 return new eZURL( $row );
00101 }
00102
00103
00104
00105
00106
00107 function removeByID( $urlID )
00108 {
00109 eZPersistentObject::removeObject( eZURL::definition(),
00110 array( 'id' => $urlID ) );
00111 }
00112
00113
00114
00115
00116
00117
00118 function registerURL( $url )
00119 {
00120 $urlID = false;
00121 $db =& eZDB::instance();
00122
00123
00124 $checkURLQuery = "SELECT id FROM ezurl WHERE url='" . $db->escapeString( $url ) . "'";
00125 $urlArray = $db->arrayQuery( $checkURLQuery );
00126
00127 if ( count( $urlArray ) == 0 )
00128 {
00129
00130 $url = eZURL::create( $url );
00131 $url->store();
00132 $urlID = $url->attribute( 'id' );
00133 }
00134 else
00135 {
00136 $urlID = $urlArray[0]['id'];
00137 }
00138 return $urlID;
00139 }
00140
00141
00142
00143
00144
00145
00146 function registerURLArray( $urlArray )
00147 {
00148 $db =& eZDB::instance();
00149
00150 foreach( $urlArray as $key => $url )
00151 {
00152 $urlArrayTmp[$key] = $db->escapeString( $url );
00153 }
00154
00155 $inURLSQL = implode( '\', \'', $urlArrayTmp );
00156 $checkURLQuery = "SELECT id, url FROM ezurl WHERE url IN ( '$inURLSQL' )";
00157 $urlRowArray = $db->arrayQuery( $checkURLQuery );
00158
00159 $registeredURLArray = array();
00160 foreach ( $urlRowArray as $urlRow )
00161 {
00162 $registeredURLArray[$urlRow['url']] = $urlRow['id'];
00163 }
00164
00165
00166 foreach ( $urlArray as $url )
00167 {
00168 if ( !isset( $registeredURLArray[$url] ) )
00169 {
00170 $url = eZURL::create( $url );
00171 $url->store();
00172 $urlID = $url->attribute( 'id' );
00173 $urlText = $url->attribute('url' );
00174 $registeredURLArray[$urlText] = $urlID;
00175 }
00176 }
00177
00178 return $registeredURLArray;
00179 }
00180
00181
00182
00183
00184
00185
00186 function setIsValid( $id, $isValid )
00187 {
00188 $dateTime = time();
00189 $isValid = (int) $isValid;
00190 eZPersistentObject::updateObjectList( array( 'definition' => eZURL::definition(),
00191 'update_fields' => array( 'is_valid' => $isValid,
00192 'modified' => $dateTime ),
00193 'conditions' => array( 'id' => $id ) ) );
00194 }
00195
00196
00197
00198
00199
00200 function setModified( $dateTime = false )
00201 {
00202 if ( $dateTime === false )
00203 {
00204 $dateTime = time();
00205 }
00206 $this->Modified = $dateTime;
00207 }
00208
00209
00210
00211
00212
00213 function setLastChecked( $id, $dateTime = false )
00214 {
00215 if ( $dateTime === false )
00216 {
00217 $dateTime = time();
00218 }
00219 eZPersistentObject::updateObjectList( array( 'definition' => eZURL::definition(),
00220 'update_fields' => array( 'last_checked' => $dateTime ),
00221 'conditions' => array( 'id' => $id ) ) );
00222 }
00223
00224
00225
00226
00227 function fetch( $id, $asObject = true )
00228 {
00229 return eZPersistentObject::fetchObject( eZURL::definition(),
00230 null, array( 'id' => $id ),
00231 $asObject );
00232 }
00233
00234
00235
00236
00237 function fetchListCount( $parameters = array() )
00238 {
00239 return eZURL::handleList( $parameters, true );
00240 }
00241
00242
00243
00244
00245 function fetchList( $parameters = array() )
00246 {
00247 return eZURL::handleList( $parameters, false );
00248 }
00249
00250
00251
00252
00253 function handleList( $parameters = array(), $asCount = false )
00254 {
00255 $parameters = array_merge( array( 'as_object' => true,
00256 'is_valid' => null,
00257 'offset' => false,
00258 'limit' => false,
00259 'only_published' => false ),
00260 $parameters );
00261 $asObject = $parameters['as_object'];
00262 $isValid = $parameters['is_valid'];
00263 $offset = $parameters['offset'];
00264 $limit = $parameters['limit'];
00265 $onlyPublished = $parameters['only_published'];
00266 $limitArray = null;
00267 if ( !$asCount and $offset !== false and $limit !== false )
00268 $limitArray = array( 'offset' => $offset,
00269 'length' => $limit );
00270 $conditions = array();
00271 if( $isValid === false ) $isValid = 0;
00272 if ( $isValid !== null )
00273 {
00274 $conditions['is_valid'] = $isValid;
00275 }
00276 if ( count( $conditions ) == 0 )
00277 $conditions = null;
00278
00279 if ( $onlyPublished )
00280 {
00281 $conditionQuery = "";
00282 if ( $isValid !== null )
00283 {
00284 $isValid = (int) $isValid;
00285 $conditionQuery = " AND ezurl.is_valid=$isValid ";
00286 }
00287 include_once( "lib/ezdb/classes/ezdb.php" );
00288 $db =& eZDB::instance();
00289 include_once( 'kernel/classes/datatypes/ezurl/ezurlobjectlink.php' );
00290 $cObjAttrVersionColumn = eZPersistentObject::getShortAttributeName( $db, eZURLObjectLink::definition(), 'contentobject_attribute_version' );
00291
00292 if ( $asCount )
00293 {
00294 $urls = $db->arrayQuery( "SELECT count( DISTINCT ezurl.id ) AS count
00295 FROM
00296 ezurl,
00297 ezurl_object_link,
00298 ezcontentobject_attribute,
00299 ezcontentobject_version
00300 WHERE
00301 ezurl.id = ezurl_object_link.url_id
00302 AND ezurl_object_link.contentobject_attribute_id = ezcontentobject_attribute.id
00303 AND ezurl_object_link.$cObjAttrVersionColumn = ezcontentobject_attribute.version
00304 AND ezcontentobject_attribute.contentobject_id = ezcontentobject_version.contentobject_id
00305 AND ezcontentobject_attribute.version = ezcontentobject_version.version
00306 AND ezcontentobject_version.status = 1
00307 $conditionQuery" );
00308 return $urls[0]['count'];
00309 }
00310 else
00311 {
00312 $query = "SELECT DISTINCT ezurl.*
00313 FROM
00314 ezurl,
00315 ezurl_object_link,
00316 ezcontentobject_attribute,
00317 ezcontentobject_version
00318 WHERE
00319 ezurl.id = ezurl_object_link.url_id
00320 AND ezurl_object_link.contentobject_attribute_id = ezcontentobject_attribute.id
00321 AND ezurl_object_link.$cObjAttrVersionColumn = ezcontentobject_attribute.version
00322 AND ezcontentobject_attribute.contentobject_id = ezcontentobject_version.contentobject_id
00323 AND ezcontentobject_attribute.version = ezcontentobject_version.version
00324 AND ezcontentobject_version.status = 1
00325 $conditionQuery";
00326
00327 if ( !$offset && !$limit )
00328 {
00329 $urlArray = $db->arrayQuery( $query );
00330 }
00331 else
00332 {
00333 $urlArray = $db->arrayQuery( $query, array( 'offset' => $offset,
00334 'limit' => $limit ) );
00335 }
00336 if ( $asObject )
00337 {
00338 $urls = array();
00339 foreach ( $urlArray as $url )
00340 {
00341 $urls[] = new eZURL( $url );
00342 }
00343 return $urls;
00344 }
00345 else
00346 $urls =& $urlArray;
00347 return $urls;
00348 }
00349 }
00350 else
00351 {
00352 if ( $asCount )
00353 {
00354 $urls = eZPersistentObject::fetchObjectList( eZURL::definition(),
00355 array(),
00356 $conditions,
00357 false,
00358 null,
00359 false,
00360 false,
00361 array( array( 'operation' => 'count( id )',
00362 'name' => 'count' ) ) );
00363 return $urls[0]['count'];
00364 }
00365 else
00366 {
00367 return eZPersistentObject::fetchObjectList( eZURL::definition(),
00368 null, $conditions, null, $limitArray,
00369 $asObject );
00370 }
00371 }
00372 }
00373
00374
00375
00376
00377
00378
00379 function url( $id, $onlyValid = false )
00380 {
00381 $url = false;
00382
00383 if ( !is_numeric( $id ) )
00384 {
00385 return $url;
00386 }
00387
00388 $id = (int) $id;
00389 $db =& eZDB::instance();
00390 $checkURLQuery = "SELECT url, is_valid FROM ezurl WHERE id='$id'";
00391 $urlArray = $db->arrayQuery( $checkURLQuery );
00392
00393 if ( count( $urlArray ) == 1 )
00394 {
00395 if ( $onlyValid and
00396 !$urlArray[0]['is_valid'] )
00397 {
00398 $url = "/url/view/" . $id;
00399 return $url;
00400 }
00401 $url = $urlArray[0]['url'];
00402 }
00403 return $url;
00404 }
00405
00406
00407
00408
00409
00410
00411 function urlByMD5( $urlMD5 )
00412 {
00413 $db =& eZDB::instance();
00414
00415 $url = false;
00416 $urlMD5 = $db->escapeString( $urlMD5 );
00417 $checkURLQuery = "SELECT url FROM ezurl WHERE original_url_md5='$urlMD5'";
00418 $urlArray = $db->arrayQuery( $checkURLQuery );
00419
00420 if ( count( $urlArray ) == 1 )
00421 {
00422 $url = $urlArray[0]['url'];
00423 }
00424 return $url;
00425 }
00426
00427
00428
00429
00430
00431 function urlByURL( $urlText )
00432 {
00433 $db =& eZDB::instance();
00434
00435 $url = false;
00436 $checkURLQuery = "SELECT * FROM ezurl WHERE url='" . $db->escapeString( $urlText ) . "'";
00437 $urlArray = $db->arrayQuery( $checkURLQuery );
00438
00439 if ( count( $urlArray ) == 1 )
00440 {
00441 $urlRow =& $urlArray[0];
00442 $url = new eZURL( $urlRow );
00443 }
00444 return $url;
00445 }
00446 }
00447
00448 ?>