|
eZ Publish
[4.0]
|
00001 <?php 00002 // 00003 // Definition of eZURL class 00004 // 00005 // Created on: <08-Oct-2002 19:44:48 bf> 00006 // 00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00008 // SOFTWARE NAME: eZ Publish 00009 // SOFTWARE RELEASE: 4.0.x 00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS 00011 // SOFTWARE LICENSE: GNU General Public License v2.0 00012 // NOTICE: > 00013 // This program is free software; you can redistribute it and/or 00014 // modify it under the terms of version 2.0 of the GNU General 00015 // Public License as published by the Free Software Foundation. 00016 // 00017 // This program is distributed in the hope that it will be useful, 00018 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00020 // GNU General Public License for more details. 00021 // 00022 // You should have received a copy of version 2.0 of the GNU General 00023 // Public License along with this program; if not, write to the Free 00024 // Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 00025 // MA 02110-1301, USA. 00026 // 00027 // 00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ## 00029 // 00030 00031 /*! 00032 \class eZURL ezurl.php 00033 \ingroup eZDatatype 00034 \brief A class which handles central storage of urls 00035 00036 URLs can be stored using eZURL. When registering URL's 00037 to eZURL you will get a URL ID which is used to identify 00038 URLs. 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 static 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 static 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 \static 00105 Removes the URL with ID \a $urlID. 00106 */ 00107 static function removeByID( $urlID ) 00108 { 00109 eZPersistentObject::removeObject( eZURL::definition(), 00110 array( 'id' => $urlID ) ); 00111 } 00112 00113 /*! 00114 \static 00115 Registers a URL to the URL database. The URL id is 00116 returned if successful. False is returned if not. 00117 */ 00118 static function registerURL( $url ) 00119 { 00120 $urlID = false; 00121 $db = eZDB::instance(); 00122 00123 // check if URL already exists 00124 $checkURLQuery = "SELECT id FROM ezurl WHERE url='" . $db->escapeString( $url ) . "'"; 00125 $urlArray = $db->arrayQuery( $checkURLQuery ); 00126 00127 if ( count( $urlArray ) == 0 ) 00128 { 00129 // store URL 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 \static 00143 Registers an array of URLs to the URL database. A hash of array( url -> id ) 00144 is returned. 00145 */ 00146 static function registerURLArray( $urlArray ) 00147 { 00148 $db = eZDB::instance(); 00149 00150 foreach( $urlArray as $key => $url ) 00151 { 00152 $urlArrayTmp[$key] = $db->escapeString( $url ); 00153 } 00154 // Fetch the already existing URL's 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 // Check for URL's which are not registered, and register them 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 \static 00183 Updates the is_valid field of urls passed in \a $id. 00184 \param $id Can either be an array with ids or just one id value. 00185 */ 00186 static 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 Sets the modification date to \a $dateTime or the current 00198 date if it's \c false. 00199 */ 00200 function setModified( $dateTime = false ) 00201 { 00202 if ( $dateTime === false ) 00203 { 00204 $dateTime = time(); 00205 } 00206 $this->Modified = $dateTime; 00207 } 00208 00209 /*! 00210 Sets the last checked date to \a $dateTime or the current 00211 date if it's \c false. 00212 */ 00213 static 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 \return the url object for id \a $id. 00226 */ 00227 static function fetch( $id, $asObject = true ) 00228 { 00229 return eZPersistentObject::fetchObject( eZURL::definition(), 00230 null, array( 'id' => $id ), 00231 $asObject ); 00232 } 00233 00234 /*! 00235 \return the number of registered URLs. 00236 */ 00237 static function fetchListCount( $parameters = array() ) 00238 { 00239 return eZURL::handleList( $parameters, true ); 00240 } 00241 00242 /*! 00243 \return all registered URLs. 00244 */ 00245 static function fetchList( $parameters = array() ) 00246 { 00247 return eZURL::handleList( $parameters, false ); 00248 } 00249 00250 /*! 00251 \return all registered URLs. 00252 */ 00253 static 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 ) // Only fetch published urls 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 \static 00376 Returns the URL with the given ID. False is returned if the ID 00377 does not exits. 00378 */ 00379 static 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 \static 00408 Returns the URL with the given ID. False is returned if the ID 00409 does not exits. 00410 */ 00411 static 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 \static 00429 Returns the URL with the given URL. Returns false if the URL does not exists. 00430 */ 00431 static 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 $url = new eZURL( $urlArray[0] ); 00442 } 00443 return $url; 00444 } 00445 } 00446 00447 ?>