dbobjectset.class.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. // Copyright (C) 2010 Combodo SARL
  3. //
  4. // This program is free software; you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation; version 3 of the License.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  16. /**
  17. * Object set management
  18. *
  19. * @author Erwan Taloc <erwan.taloc@combodo.com>
  20. * @author Romain Quetiez <romain.quetiez@combodo.com>
  21. * @author Denis Flaven <denis.flaven@combodo.com>
  22. * @license http://www.opensource.org/licenses/gpl-3.0.html LGPL
  23. */
  24. /**
  25. * A set of persistent objects, could be heterogeneous
  26. *
  27. * @package iTopORM
  28. */
  29. class DBObjectSet
  30. {
  31. private $m_oFilter;
  32. private $m_aOrderBy;
  33. public $m_bLoaded;
  34. private $m_aData;
  35. private $m_aId2Row;
  36. private $m_iCurrRow;
  37. public function __construct(DBObjectSearch $oFilter, $aOrderBy = array(), $aArgs = array(), $aExtendedDataSpec = null, $iLimitCount = 0, $iLimitStart = 0)
  38. {
  39. $this->m_oFilter = $oFilter;
  40. $this->m_aOrderBy = $aOrderBy;
  41. $this->m_aArgs = $aArgs;
  42. $this->m_aAttToLoad = null;
  43. $this->m_aExtendedDataSpec = $aExtendedDataSpec;
  44. $this->m_iLimitCount = $iLimitCount;
  45. $this->m_iLimitStart = $iLimitStart;
  46. $this->m_iCount = null; // null if unknown yet
  47. $this->m_bLoaded = false; // true when the filter has been used OR the set is built step by step (AddObject...)
  48. $this->m_aData = array(); // array of (row => array of (classalias) => object/null)
  49. $this->m_aId2Row = array(); // array of (pkey => index in m_aData)
  50. $this->m_iCurrRow = 0;
  51. }
  52. public function __destruct()
  53. {
  54. }
  55. public function __toString()
  56. {
  57. $sRet = '';
  58. $this->Rewind();
  59. $sRet .= "Set (".$this->m_oFilter->ToOQL().")<br/>\n";
  60. $sRet .= "Query: <pre style=\"font-size: smaller; display:inline;\">".MetaModel::MakeSelectQuery($this->m_oFilter, array()).")</pre>\n";
  61. $sRet .= $this->Count()." records<br/>\n";
  62. if ($this->Count() > 0)
  63. {
  64. $sRet .= "<ul class=\"treeview\">\n";
  65. while ($oObj = $this->Fetch())
  66. {
  67. $sRet .= "<li>".$oObj->__toString()."</li>\n";
  68. }
  69. $sRet .= "</ul>\n";
  70. }
  71. return $sRet;
  72. }
  73. public function OptimizeColumnLoad($aAttToLoad)
  74. {
  75. if (is_null($aAttToLoad))
  76. {
  77. $this->m_aAttToLoad = null;
  78. }
  79. else
  80. {
  81. // Complete the attribute list with the attribute codes
  82. $aAttToLoadWithAttDef = array();
  83. foreach($aAttToLoad as $sClassAlias => $aAttList)
  84. {
  85. $aSelectedClasses = $this->m_oFilter->GetSelectedClasses();
  86. $sClass = $aSelectedClasses[$sClassAlias];
  87. foreach($aAttList as $sAttToLoad)
  88. {
  89. $oAttDef = MetaModel::GetAttributeDef($sClass, $sAttToLoad);
  90. $aAttToLoadWithAttDef[$sClassAlias][$sAttToLoad] = $oAttDef;
  91. if ($oAttDef->IsExternalKey())
  92. {
  93. // Add the external key friendly name anytime
  94. $oFriendlyNameAttDef = MetaModel::GetAttributeDef($sClass, $sAttToLoad.'_friendlyname');
  95. $aAttToLoadWithAttDef[$sClassAlias][$sAttToLoad.'_friendlyname'] = $oFriendlyNameAttDef;
  96. }
  97. }
  98. // Add the friendly name anytime
  99. $oFriendlyNameAttDef = MetaModel::GetAttributeDef($sClass, 'friendlyname');
  100. $aAttToLoadWithAttDef[$sClassAlias]['friendlyname'] = $oFriendlyNameAttDef;
  101. }
  102. $this->m_aAttToLoad = $aAttToLoadWithAttDef;
  103. }
  104. }
  105. static public function FromObject($oObject)
  106. {
  107. $oRetSet = self::FromScratch(get_class($oObject));
  108. $oRetSet->AddObject($oObject);
  109. return $oRetSet;
  110. }
  111. static public function FromScratch($sClass)
  112. {
  113. $oFilter = new CMDBSearchFilter($sClass);
  114. $oRetSet = new self($oFilter);
  115. $oRetSet->m_bLoaded = true; // no DB load
  116. return $oRetSet;
  117. }
  118. // create an object set ex nihilo
  119. // input = array of objects
  120. static public function FromArray($sClass, $aObjects)
  121. {
  122. $oFilter = new CMDBSearchFilter($sClass);
  123. $oRetSet = new self($oFilter);
  124. $oRetSet->m_bLoaded = true; // no DB load
  125. $oRetSet->AddObjectArray($aObjects, $sClass);
  126. return $oRetSet;
  127. }
  128. // create an object set ex nihilo
  129. // aClasses = array of (alias => class)
  130. // input = array of (array of (classalias => object))
  131. static public function FromArrayAssoc($aClasses, $aObjects)
  132. {
  133. // In a perfect world, we should create a complete tree of DBObjectSearch,
  134. // but as we lack most of the information related to the objects,
  135. // let's create one search definition
  136. $sClass = reset($aClasses);
  137. $sAlias = key($aClasses);
  138. $oFilter = new CMDBSearchFilter($sClass, $sAlias);
  139. $oRetSet = new self($oFilter);
  140. $oRetSet->m_bLoaded = true; // no DB load
  141. foreach($aObjects as $rowIndex => $aObjectsByClassAlias)
  142. {
  143. $oRetSet->AddObjectExtended($aObjectsByClassAlias);
  144. }
  145. return $oRetSet;
  146. }
  147. static public function FromLinkSet($oObject, $sLinkSetAttCode, $sExtKeyToRemote)
  148. {
  149. $oLinkAttCode = MetaModel::GetAttributeDef(get_class($oObject), $sLinkSetAttCode);
  150. $oExtKeyAttDef = MetaModel::GetAttributeDef($oLinkAttCode->GetLinkedClass(), $sExtKeyToRemote);
  151. $sTargetClass = $oExtKeyAttDef->GetTargetClass();
  152. $oLinkSet = $oObject->Get($sLinkSetAttCode);
  153. $aTargets = array();
  154. while ($oLink = $oLinkSet->Fetch())
  155. {
  156. $aTargets[] = MetaModel::GetObject($sTargetClass, $oLink->Get($sExtKeyToRemote));
  157. }
  158. return self::FromArray($sTargetClass, $aTargets);
  159. }
  160. public function ToArray($bWithId = true)
  161. {
  162. $aRet = array();
  163. $this->Rewind();
  164. while ($oObject = $this->Fetch())
  165. {
  166. if ($bWithId)
  167. {
  168. $aRet[$oObject->GetKey()] = $oObject;
  169. }
  170. else
  171. {
  172. $aRet[] = $oObject;
  173. }
  174. }
  175. return $aRet;
  176. }
  177. public function ToArrayOfValues()
  178. {
  179. if (!$this->m_bLoaded) $this->Load();
  180. $aSelectedClasses = $this->m_oFilter->GetSelectedClasses();
  181. $aRet = array();
  182. foreach($this->m_aData as $iRow => $aObjects)
  183. {
  184. foreach($aObjects as $sClassAlias => $oObject)
  185. {
  186. if (is_null($oObject))
  187. {
  188. $aRet[$iRow][$sClassAlias.'.'.'id'] = null;
  189. }
  190. else
  191. {
  192. $aRet[$iRow][$sClassAlias.'.'.'id'] = $oObject->GetKey();
  193. }
  194. if (is_null($oObject))
  195. {
  196. $sClass = $aSelectedClasses[$sClassAlias];
  197. }
  198. else
  199. {
  200. $sClass = get_class($oObject);
  201. }
  202. foreach(MetaModel::ListAttributeDefs($sClass) as $sAttCode => $oAttDef)
  203. {
  204. if ($oAttDef->IsScalar())
  205. {
  206. $sAttName = $sClassAlias.'.'.$sAttCode;
  207. if (is_null($oObject))
  208. {
  209. $aRet[$iRow][$sAttName] = null;
  210. }
  211. else
  212. {
  213. $aRet[$iRow][$sAttName] = $oObject->Get($sAttCode);
  214. }
  215. }
  216. }
  217. }
  218. }
  219. return $aRet;
  220. }
  221. public function GetColumnAsArray($sAttCode, $bWithId = true)
  222. {
  223. $aRet = array();
  224. $this->Rewind();
  225. while ($oObject = $this->Fetch())
  226. {
  227. if ($bWithId)
  228. {
  229. $aRet[$oObject->GetKey()] = $oObject->Get($sAttCode);
  230. }
  231. else
  232. {
  233. $aRet[] = $oObject->Get($sAttCode);
  234. }
  235. }
  236. return $aRet;
  237. }
  238. public function GetFilter()
  239. {
  240. // #@# This is false as soon as the set has been manipulated (AddObject...)
  241. return $this->m_oFilter;
  242. }
  243. public function GetClass()
  244. {
  245. return $this->m_oFilter->GetClass();
  246. }
  247. public function GetSelectedClasses()
  248. {
  249. return $this->m_oFilter->GetSelectedClasses();
  250. }
  251. public function GetRootClass()
  252. {
  253. return MetaModel::GetRootClass($this->GetClass());
  254. }
  255. public function SetLimit($iLimitCount, $iLimitStart = 0)
  256. {
  257. $this->m_iLimitCount = $iLimitCount;
  258. $this->m_iLimitStart = $iLimitStart;
  259. }
  260. public function GetLimitCount()
  261. {
  262. return $this->m_iLimitCount;
  263. }
  264. public function GetLimitStart()
  265. {
  266. return $this->m_iLimitStart;
  267. }
  268. public function Load()
  269. {
  270. if ($this->m_bLoaded) return;
  271. // Note: it is mandatory to set this value now, to protect against reentrance
  272. $this->m_bLoaded = true;
  273. if ($this->m_iLimitCount > 0)
  274. {
  275. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aAttToLoad, $this->m_aExtendedDataSpec, $this->m_iLimitCount, $this->m_iLimitStart);
  276. }
  277. else
  278. {
  279. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, $this->m_aAttToLoad, $this->m_aExtendedDataSpec);
  280. }
  281. $resQuery = CMDBSource::Query($sSQL);
  282. if (!$resQuery) return;
  283. $sClass = $this->m_oFilter->GetClass();
  284. while ($aRow = CMDBSource::FetchArray($resQuery))
  285. {
  286. $aObjects = array();
  287. foreach ($this->m_oFilter->GetSelectedClasses() as $sClassAlias => $sClass)
  288. {
  289. if (is_null($aRow[$sClassAlias.'id']))
  290. {
  291. $oObject = null;
  292. }
  293. else
  294. {
  295. $oObject = MetaModel::GetObjectByRow($sClass, $aRow, $sClassAlias, $this->m_aAttToLoad, $this->m_aExtendedDataSpec);
  296. }
  297. $aObjects[$sClassAlias] = $oObject;
  298. }
  299. $this->AddObjectExtended($aObjects);
  300. }
  301. CMDBSource::FreeResult($resQuery);
  302. }
  303. public function Count()
  304. {
  305. if ($this->m_bLoaded && ($this->m_iLimitCount == 0) && ($this->m_iLimitStart == 0))
  306. {
  307. return count($this->m_aData);
  308. }
  309. else
  310. {
  311. if (is_null($this->m_iCount))
  312. {
  313. $sSQL = MetaModel::MakeSelectQuery($this->m_oFilter, $this->m_aOrderBy, $this->m_aArgs, null, null, 0, 0, true);
  314. $resQuery = CMDBSource::Query($sSQL);
  315. if (!$resQuery) return 0;
  316. $aRow = CMDBSource::FetchArray($resQuery);
  317. CMDBSource::FreeResult($resQuery);
  318. $this->m_iCount = $aRow['COUNT'];
  319. }
  320. return $this->m_iCount;
  321. }
  322. }
  323. public function Fetch($sClassAlias = '')
  324. {
  325. if (!$this->m_bLoaded) $this->Load();
  326. if ($this->m_iCurrRow >= count($this->m_aData))
  327. {
  328. return null;
  329. }
  330. if (strlen($sClassAlias) == 0)
  331. {
  332. $sClassAlias = $this->m_oFilter->GetClassAlias();
  333. }
  334. $oRetObj = $this->m_aData[$this->m_iCurrRow][$sClassAlias];
  335. $this->m_iCurrRow++;
  336. return $oRetObj;
  337. }
  338. // Return the whole line if several classes have been specified in the query
  339. //
  340. public function FetchAssoc()
  341. {
  342. if (!$this->m_bLoaded) $this->Load();
  343. if ($this->m_iCurrRow >= count($this->m_aData))
  344. {
  345. return null;
  346. }
  347. $aRetObjects = $this->m_aData[$this->m_iCurrRow];
  348. $this->m_iCurrRow++;
  349. return $aRetObjects;
  350. }
  351. public function Rewind()
  352. {
  353. if ($this->m_bLoaded)
  354. {
  355. $this->Seek(0);
  356. }
  357. }
  358. public function Seek($iRow)
  359. {
  360. if (!$this->m_bLoaded) $this->Load();
  361. $this->m_iCurrRow = min($iRow, count($this->m_aData));
  362. return $this->m_iCurrRow;
  363. }
  364. public function AddObject($oObject, $sClassAlias = '')
  365. {
  366. if (!$this->m_bLoaded) $this->Load();
  367. if (strlen($sClassAlias) == 0)
  368. {
  369. $sClassAlias = $this->m_oFilter->GetClassAlias();
  370. }
  371. $iNextPos = count($this->m_aData);
  372. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  373. if (!is_null($oObject))
  374. {
  375. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  376. }
  377. }
  378. protected function AddObjectExtended($aObjectArray)
  379. {
  380. if (!$this->m_bLoaded) $this->Load();
  381. $iNextPos = count($this->m_aData);
  382. foreach ($aObjectArray as $sClassAlias => $oObject)
  383. {
  384. $this->m_aData[$iNextPos][$sClassAlias] = $oObject;
  385. if (!is_null($oObject))
  386. {
  387. $this->m_aId2Row[$sClassAlias][$oObject->GetKey()] = $iNextPos;
  388. }
  389. }
  390. }
  391. public function AddObjectArray($aObjects, $sClassAlias = '')
  392. {
  393. if (!$this->m_bLoaded) $this->Load();
  394. // #@# todo - add a check on the object class ?
  395. foreach ($aObjects as $oObj)
  396. {
  397. $this->AddObject($oObj, $sClassAlias);
  398. }
  399. }
  400. public function Merge($oObjectSet)
  401. {
  402. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  403. {
  404. throw new CoreException("Could not merge two objects sets if they don't have the same root class");
  405. }
  406. if (!$this->m_bLoaded) $this->Load();
  407. $oObjectSet->Seek(0);
  408. while ($oObject = $oObjectSet->Fetch())
  409. {
  410. $this->AddObject($oObject);
  411. }
  412. }
  413. public function CreateIntersect($oObjectSet)
  414. {
  415. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  416. {
  417. throw new CoreException("Could not 'intersect' two objects sets if they don't have the same root class");
  418. }
  419. if (!$this->m_bLoaded) $this->Load();
  420. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  421. $sClassAlias = $this->m_oFilter->GetClassAlias();
  422. $oObjectSet->Seek(0);
  423. while ($oObject = $oObjectSet->Fetch())
  424. {
  425. if (array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  426. {
  427. $oNewSet->AddObject($oObject);
  428. }
  429. }
  430. return $oNewSet;
  431. }
  432. // Note: This verb works only with objects existing in the database
  433. //
  434. public function HasSameContents($oObjectSet)
  435. {
  436. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  437. {
  438. return false;
  439. }
  440. if (!$this->m_bLoaded) $this->Load();
  441. if ($this->Count() != $oObjectSet->Count())
  442. {
  443. return false;
  444. }
  445. $sClassAlias = $this->m_oFilter->GetClassAlias();
  446. $oObjectSet->Rewind();
  447. while ($oObject = $oObjectSet->Fetch())
  448. {
  449. $iObjectKey = $oObject->GetKey();
  450. if ($iObjectKey < 0)
  451. {
  452. return false;
  453. }
  454. if (!array_key_exists($iObjectKey, $this->m_aId2Row[$sClassAlias]))
  455. {
  456. return false;
  457. }
  458. $iRow = $this->m_aId2Row[$sClassAlias][$iObjectKey];
  459. $oSibling = $this->m_aData[$iRow][$sClassAlias];
  460. if (!$oObject->Equals($oSibling))
  461. {
  462. return false;
  463. }
  464. }
  465. return true;
  466. }
  467. public function CreateDelta($oObjectSet)
  468. {
  469. if ($this->GetRootClass() != $oObjectSet->GetRootClass())
  470. {
  471. throw new CoreException("Could not 'delta' two objects sets if they don't have the same root class");
  472. }
  473. if (!$this->m_bLoaded) $this->Load();
  474. $oNewSet = DBObjectSet::FromScratch($this->GetClass());
  475. $sClassAlias = $this->m_oFilter->GetClassAlias();
  476. $oObjectSet->Seek(0);
  477. while ($oObject = $oObjectSet->Fetch())
  478. {
  479. if (!array_key_exists($oObject->GetKey(), $this->m_aId2Row[$sClassAlias]))
  480. {
  481. $oNewSet->AddObject($oObject);
  482. }
  483. }
  484. return $oNewSet;
  485. }
  486. public function GetRelatedObjects($sRelCode, $iMaxDepth = 99)
  487. {
  488. $aRelatedObjs = array();
  489. $aVisited = array(); // optimization for consecutive calls of MetaModel::GetRelatedObjects
  490. $this->Seek(0);
  491. while ($oObject = $this->Fetch())
  492. {
  493. $aMore = $oObject->GetRelatedObjects($sRelCode, $iMaxDepth, $aVisited);
  494. foreach ($aMore as $sClass => $aRelated)
  495. {
  496. foreach ($aRelated as $iObj => $oObj)
  497. {
  498. if (!isset($aRelatedObjs[$sClass][$iObj]))
  499. {
  500. $aRelatedObjs[$sClass][$iObj] = $oObj;
  501. }
  502. }
  503. }
  504. }
  505. return $aRelatedObjs;
  506. }
  507. /**
  508. * Builds an object that contains the values that are common to all the objects
  509. * in the set. If for a given attribute, objects in the set have various values
  510. * then the resulting object will contain null for this value.
  511. * @param $aValues Hash Output: the distribution of the values, in the set, for each attribute
  512. * @return Object
  513. */
  514. public function ComputeCommonObject(&$aValues)
  515. {
  516. $sClass = $this->GetClass();
  517. $aList = MetaModel::ListAttributeDefs($sClass);
  518. $aValues = array();
  519. foreach($aList as $sAttCode => $oAttDef)
  520. {
  521. if ($oAttDef->IsScalar())
  522. {
  523. $aValues[$sAttCode] = array();
  524. }
  525. }
  526. $this->Rewind();
  527. while($oObj = $this->Fetch())
  528. {
  529. foreach($aList as $sAttCode => $oAttDef)
  530. {
  531. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  532. {
  533. $currValue = $oObj->Get($sAttCode);
  534. if (is_object($currValue)) continue; // Skip non scalar values...
  535. if(!array_key_exists($currValue, $aValues[$sAttCode]))
  536. {
  537. $aValues[$sAttCode][$currValue] = array('count' => 1, 'display' => $oObj->GetAsHTML($sAttCode));
  538. }
  539. else
  540. {
  541. $aValues[$sAttCode][$currValue]['count']++;
  542. }
  543. }
  544. }
  545. }
  546. foreach($aValues as $sAttCode => $aMultiValues)
  547. {
  548. if (count($aMultiValues) > 1)
  549. {
  550. uasort($aValues[$sAttCode], 'HashCountComparison');
  551. }
  552. }
  553. // Now create an object that has values for the homogenous values only
  554. $oCommonObj = new $sClass(); // @@ What if the class is abstract ?
  555. $aComments = array();
  556. $iFormId = cmdbAbstractObject::GetNextFormId(); // Identifier that prefixes all the form fields
  557. $sReadyScript = '';
  558. $aDependsOn = array();
  559. $sFormPrefix = '2_';
  560. foreach($aList as $sAttCode => $oAttDef)
  561. {
  562. if ($oAttDef->IsScalar() && $oAttDef->IsWritable())
  563. {
  564. if ($oAttDef->GetEditClass() == 'One Way Password')
  565. {
  566. $oCommonObj->Set($sAttCode, null);
  567. }
  568. else
  569. {
  570. $iCount = count($aValues[$sAttCode]);
  571. if ($iCount == 1)
  572. {
  573. // Homogenous value
  574. reset($aValues[$sAttCode]);
  575. $aKeys = array_keys($aValues[$sAttCode]);
  576. $currValue = $aKeys[0]; // The only value is the first key
  577. $oCommonObj->Set($sAttCode, $currValue);
  578. }
  579. else
  580. {
  581. // Non-homogenous value
  582. $oCommonObj->Set($sAttCode, null);
  583. }
  584. }
  585. }
  586. }
  587. $this->Rewind();
  588. return $oCommonObj;
  589. }
  590. }
  591. /**
  592. * Helper function to perform a custom sort of a hash array
  593. */
  594. function HashCountComparison($a, $b) // Sort descending on 'count'
  595. {
  596. if ($a['count'] == $b['count'])
  597. {
  598. return 0;
  599. }
  600. return ($a['count'] > $b['count']) ? -1 : 1;
  601. }
  602. ?>