[ Index ]

PHP Cross Reference of PivotX trunk SVN

title

Body

[close]

/pivotx/modules/ -> module_db.php (source)

   1  <?php
   2  
   3  // ---------------------------------------------------------------------------
   4  //
   5  // PIVOTX - LICENSE:
   6  //
   7  // This file is part of PivotX. PivotX and all its parts are licensed under
   8  // the GPL version 2. see: http://docs.pivotx.net/doku.php?id=help_about_gpl
   9  // for more information.
  10  //
  11  // $Id: module_db.php 4164 2012-05-08 10:13:37Z hansfn $
  12  //
  13  // ---------------------------------------------------------------------------
  14  
  15  
  16  // don't access directly..
  17  if(!defined('INPIVOTX')){ exit('not in pivotx'); }
  18  
  19  // Lamer protection
  20  $currentfile = basename(__FILE__);
  21  require dirname(dirname(__FILE__))."/lamer_protection.php";
  22  
  23  
  24  
  25  /**
  26   * class db
  27   * The API for accessing the database.
  28   *
  29   * @package pivotx
  30   */
  31  class db {
  32  
  33      var $db_type;
  34  
  35      /**
  36       * Initialises the db.
  37       *
  38       * @param boolean $loadindex Whether the index should be loaded.
  39       */
  40      function db($loadindex=TRUE) {
  41          global $PIVOTX, $pivotx_path;
  42  
  43          if ($PIVOTX['config']->get('db_model')=="flat") {
  44              $this->db_type = "flat";
  45              include_once( realpath($pivotx_path). '/modules/entries_flat.php');
  46              $this->db_lowlevel = new EntriesFlat($loadindex);
  47          } else if ( ($PIVOTX['config']->get('db_model')=="mysql") ||
  48                  ($PIVOTX['config']->get('db_model')=="sqlite") ||
  49                  ($PIVOTX['config']->get('db_model')=="postgresql") ) {
  50              $this->db_type = "sql";
  51              include_once( realpath($pivotx_path). '/modules/entries_sql.php');
  52              $this->db_lowlevel = new EntriesSql($loadindex);
  53          } else {
  54              // TODO: In case of a fatal error, we should give the user the chance to reset the
  55              // Config to the default state, and try again.
  56              echo("Unknown DB Model! It will be reset to 'flat files'. Please refresh this page, go to configuration and set it as you prefer.");
  57              $PIVOTX['config']->set('db_model', 'flat');
  58              die();
  59          }
  60  
  61      }
  62  
  63      /**
  64       * Gets a list of entries by date.  This function is really deprecated and
  65       * acts like a wrapper around read_entries.
  66       *
  67       * @param int $amount
  68       * @param int $offset
  69       * @param mixed $filteronuser
  70       * @param mixed $filteroncat
  71       * @param boolean $order Defines whether the results are in chronological
  72       *    order (false means reverse order).
  73       * @param string $field The field to order by.
  74       * @param string $status Return only entries with this status.
  75       * @return array
  76       */
  77      function getlist($amount, $offset=0, $filteronuser="", $filteroncat="", $order=TRUE, $field="", $status="") {
  78  
  79          if ($order == TRUE) {
  80              $order = 'asc';
  81          } else {
  82              $order = 'desc';
  83          }
  84  
  85          return $this->read_entries(array(
  86              'full'=>false, 'show'=>$amount, 'offset'=>$offset, 
  87              'cats'=>$filteroncat, 'user'=>$filteronuser,
  88              'status'=>$status, 'order'=>$order, 'orderby'=>$field)
  89          );
  90  
  91      }
  92  
  93      /**
  94       * Gets an array of archives.
  95       *
  96       * @param boolean $force tells if the cache (if any) should be updated.
  97       * @param string $unit the unit of the archives.
  98       * @return array
  99       */
 100      function getArchiveArray($force=FALSE, $unit) {
 101  
 102          return $this->db_lowlevel->getArchiveArray($force, $unit);
 103  
 104      }
 105  
 106      /**
 107       * Gets the number of entries. 
 108       *
 109       * The $params array can take the same keys as in the read_entries 
 110       * function, but only the following keys give meaning when counting:
 111       * 
 112       * - 'offset': The offset from the beginning of the filtered and sorted/ordered array. 
 113       * - 'cats': Filter entries by category/ies.
 114       * - 'extrafields': Filter entries by extrafields.
 115       * - 'user': Filter entries by user(s). 
 116       * - 'status': Filter entries by status. 
 117       * - 'date': A date range - day, month or year. 
 118       * - 'start'/'end': A start/end date. 
 119       *
 120       * 'cats', 'extrafields' and 'user' can either be (comma separated) strings or arrays.
 121       *
 122       * @param array $params
 123       * @return int
 124       */
 125      function get_entries_count($params=false) {
 126  
 127          return $this->db_lowlevel->get_entries_count($params);
 128  
 129      }
 130  
 131  
 132      /**
 133       * Gets the code of the next entry.
 134       *
 135       * @param int $num
 136       * @return int
 137       */
 138      function get_next_code($num=1, $category="") {
 139          return $this->db_lowlevel->get_next_code($num, $category);
 140      }
 141  
 142  
 143      /**
 144       * Gets the code of the previous entry.
 145       *
 146       * @param int $num
 147       * @return int
 148       */
 149      function get_previous_code($num=1, $category="") {
 150          return $this->db_lowlevel->get_previous_code($num, $category);
 151      }
 152  
 153  
 154  
 155      /**
 156       * Rebuilds the index, if necessary.
 157       */
 158      function generate_index() {
 159  
 160          if ($this->db_lowlevel->need_index()) {
 161  
 162              $this->db_lowlevel->generate_index();
 163  
 164          } else {
 165  
 166              echo "this database does not need an index.<br />";
 167  
 168          }
 169  
 170      }
 171  
 172      /**
 173       * Tells if the entry exists.
 174       *
 175       * @param int $code The code/id of the entry.
 176       * @return boolean
 177       */
 178      function entry_exists($code) {
 179          return $this->db_lowlevel->entry_exists($code);
 180      }
 181  
 182  
 183      /**
 184       * Retrieves a full entry as an associative array, and returns it. The $code
 185       * parameter can be a code/uid or an URI. The optional $date parameter helps
 186       * to narrow it down, if there's more than one option.
 187       *
 188       * @param mixed $code
 189       * @param string $date
 190       * @return array
 191       */
 192      function read_entry($code, $date="", $language=false) {
 193          global $loadcount, $PIVOTX;
 194  
 195          $entry = $this->db_lowlevel->read_entry($code, $date, $language);
 196  
 197          if (!empty($PIVOTX['extensions'])) {
 198              $PIVOTX['extensions']->executeHook('entry_afterload', $entry);
 199          }
 200  
 201          $this->entry = $entry;
 202  
 203          return $this->entry;
 204  
 205      }
 206  
 207      /**
 208       * As ready entry but retrieves all the languages at once
 209       *
 210       * @param integer    $uid
 211       * @return array     entry with all languages
 212       */
 213      function getEntryInAllLanguages($uid) {
 214          $entry = $this->db_lowlevel->getEntryInAllLanguages($uid);
 215  
 216          if (!empty($PIVOTX['extensions'])) {
 217              $PIVOTX['extensions']->executeHook('entry_afterload', $entry);
 218          }
 219  
 220          $this->entry = $entry;
 221  
 222          return $this->entry;
 223      }
 224  
 225      /**
 226       * Save an entry in all languages
 227       *
 228       * @param boolean $update_index
 229       */
 230      function saveEntryInAllLanguages($update_index=TRUE) {
 231          global $PIVOTX;
 232          
 233          $PIVOTX['events']->add('save_entry', intval($this->entry['uid']), $this->entry['title']);
 234  
 235          if (method_exists($this->db_lowlevel,'saveEntryInAllLanguages')) {
 236              $ret = $this->db_lowlevel->saveEntryInAllLanguages($update_index);
 237          }
 238          else {
 239              $ret = true;
 240              $this->db_lowlevel->save_entry($update_index);
 241          }
 242  
 243          $this->entry = $this->db_lowlevel->entry;
 244  
 245          return $ret;
 246      }
 247  
 248  
 249      /**
 250       * Read a bunch of entries.
 251       *
 252       * The $params array can have the following keys:
 253       *
 254       * - 'full': Determines if the returned entries should be full (contain all fields), the default, or be reduced. (true/false)
 255       * - 'show': Amount of entries to read. 
 256       * - 'offset': The offset from the beginning of the filtered and sorted/ordered array.
 257       * - 'cats': Filter entries by category/ies. 
 258       * - 'extrafields': Filter entries by extrafields. 
 259       * - 'user': Filter entries by user(s). 
 260       * - 'status': Filter entries by status. 
 261       * - 'order': Select random, asc(ending) or des(cending). 
 262       * - 'orderby': Default is date, but any entry field (e.g. code/uid) can be used. 
 263       * - 'date': A date range - day, month or year. 
 264       * - 'start'/'end': A start/end date. 
 265       *
 266       * 'cats', 'extrafields' and 'user' can either be (comma separated) strings or arrays.
 267       *
 268       * @param array $params
 269       * @param boolean $admin     (true if admin is loading entries)
 270       * @return array
 271       */
 272      function __read_entries($params, $admin=false) {
 273          global $PIVOTX;
 274  
 275          // Sanctifying the params:
 276          if (!isset($params['full'])) { $params['full'] = true; } 
 277  
 278          // Negative amounts are not supported.
 279          if (!empty($params['show']) && ($params['show'] < 0)) {
 280              $params['show'] = -$params['show'];
 281              debug("Negative amount of entries ('show') is not supported - using abs value.");
 282          }
 283          // Do not limit number of entries if an interval or a date range is given
 284          if ((!empty($params['start']) && !empty($params['end'])) || !empty($params['date'])) {
 285              if (!empty($params['show'])) {
 286                  unset($params['show']);
 287                  debug("Both an interval or a date range and 'show' given - ignoring 'show'");
 288              }
 289          } else {
 290              $params['show'] = intval(getDefault($params['show'], 20)); // 20 seems like a sane default..
 291          }
 292          // Fix reversed 'start' and 'end'.
 293          if (!empty($params['start']) && !empty($params['end'])) {
 294              if ($params['start'] > $params['end']) {
 295                  $temp = $params['start'];
 296                  $params['start'] = $params['end'];
 297                  $params['end'] = $temp;
 298              }
 299          }
 300          // Do not use a date range or start if an offset is given
 301          if (!empty($params['offset'])) {
 302              $params['offset'] = abs(intval($params['offset']));
 303              if (!empty($params['start'])) {
 304                  $params['start'] = '';
 305                  debug("Both 'offset' and a start value given - ignoring 'start'");
 306              }
 307              if (!empty($params['date'])) {
 308                  $params['date'] = '';
 309                  debug("Both 'offset' and a date (range) given - ignoring 'date'");
 310              }
 311          }
 312          // Do not use offset, a date range or start/end if random order is chosen.
 313          if ($params['order'] == "random") {
 314              if (!empty($params['offset'])) {
 315                  $params['offset'] = '';
 316                  debug("Both 'random' and a start value given - ignoring 'start'");
 317              }
 318              if (!empty($params['date'])) {
 319                  $params['date'] = '';
 320                  debug("Both 'random' and a date (range) given - ignoring 'date'");
 321              }
 322              if (!empty($params['start'])) {
 323                  $params['start'] = '';
 324                  debug("Both 'random' and a start value given - ignoring 'start'");
 325              }
 326              if (!empty($params['end'])) {
 327                  $params['end'] = '';
 328                  debug("Both 'random' and a end value given - ignoring 'end'");
 329              }
 330          }
 331  
 332          // add entry afterload hooks for all entries
 333          // @FIXME might be a big performance hit when evil extensions clog the pipes
 334          if (!empty($PIVOTX['extensions'])) {
 335              if ($admin) {
 336                  $this->entries = $this->db_lowlevel->read_admin_entries($params);
 337              }
 338              else {
 339                  $this->entries = $this->db_lowlevel->read_entries($params);
 340              }
 341              foreach($this->entries as $key => $entry) {
 342                  $PIVOTX['extensions']->executeHook('entry_afterload', $entry);
 343                  $this->entries[$key] = $entry;
 344              }
 345              return $this->entries;
 346          }
 347          // fallback if no extensions are available
 348          if ($admin) {
 349              return $this->db_lowlevel->read_admin_entries($params);
 350          }
 351          return $this->db_lowlevel->read_entries($params);
 352      }
 353  
 354      function read_entries($params) {
 355          return $this->__read_entries($params,false);
 356      }
 357  
 358      function read_admin_entries($params) {
 359          return $this->__read_entries($params,true);
 360      }
 361  
 362  
 363      /**
 364       * Tries to guess an entry by it's (incomplete) URI and date (if 
 365       * available). The entry is returned as an associative array.
 366       *
 367       * @param string $uri
 368       * @param string $date
 369       * @return array
 370       */
 371      function guess_entry($uri, $date = '') {
 372          global $loadcount;
 373  
 374          $this->entry = $this->db_lowlevel->guess_entry($uri, $date);
 375  
 376          return $this->entry;
 377  
 378      }
 379  
 380  
 381      /**
 382       * Get an entry by its specific URI.
 383       *
 384       * @param string $uri
 385       * @param string $date
 386       * @return array
 387       */
 388      function get_entry_by_uri($uri, $language=false) {
 389          
 390          return $this->db_lowlevel->get_entry_by_uri($uri,$language);
 391          
 392      }
 393      
 394  
 395  
 396      /**
 397       * Read the latest comments
 398       *
 399       * @param array $params
 400       * @return array
 401       */
 402      function read_latestcomments($params) {
 403          
 404          return $this->db_lowlevel->read_latestcomments($params);
 405          
 406      }
 407  
 408  
 409      /**
 410       * Read the last trackbacks
 411       *
 412       * @param array $params
 413       * @return array
 414       */
 415      function read_lasttrackbacks($params) {
 416          
 417          return $this->db_lowlevel->read_lasttrackbacks($params);
 418          
 419      }
 420  
 421  
 422      /**
 423       * Sets the current entry to the contents of $entry.
 424       *
 425       * Returns the inserted entry as it got stored in the database with
 426       * correct code/id and Word HTML stripped off.
 427       *
 428       * @param array $entry The entry to be inserted
 429       * @return array
 430       */
 431      function set_entry($entry) {
 432  
 433          $iswordhtml = false;
 434          if (isWordHtml($entry['introduction'])) {
 435              $entry['introduction'] = stripWordHtml($entry['introduction']);
 436              $iswordhtml = true;
 437          }
 438          if (isWordHtml($entry['body'])) {
 439              $entry['body'] = stripWordHtml($entry['body']);
 440              $iswordhtml = true;
 441          }
 442          if ($iswordhtml) {
 443              debug(__('Text pasted directly from Microsoft Word. Some of the markup might be lost.'));
 444          }
 445  
 446          $this->entry = $this->db_lowlevel->set_entry($entry);
 447  
 448          return $this->entry;
 449  
 450      }
 451  
 452      /**
 453       * Deletes the current entry
 454       */
 455      function delete_entry() {
 456          global $PIVOTX;
 457  
 458          $PIVOTX['events']->add('delete_entry', intval($this->entry['uid']), $this->entry['title']);
 459  
 460          $this->db_lowlevel->delete_entry();
 461  
 462      }
 463  
 464      /**
 465       * Delete one or more entries.
 466       *
 467       * @param array $ids
 468       */
 469      function delete_entries($ids) {
 470          global $PIVOTX;
 471  
 472          $PIVOTX['events']->add('delete_entries', $ids);
 473  
 474          return $this->db_lowlevel->delete_entries($ids);
 475  
 476      }
 477  
 478  
 479      /**
 480       * Deletes a comment from the current entry.
 481       *
 482       * @param integer uid
 483       */
 484      function delete_comment($uid) {
 485          global $PIVOTX;
 486  
 487          $PIVOTX['events']->add('delete_comment', $uid);
 488  
 489          $this->db_lowlevel->delete_comment($uid);
 490  
 491  
 492      }
 493  
 494      /**
 495       * Returns a comment from the current entry.
 496       *
 497       * @param integer uid
 498       */
 499      function get_comment($uid) {
 500          global $PIVOTX;
 501  
 502          return $this->db_lowlevel->get_comment($uid);
 503  
 504      }
 505  
 506       
 507      /**
 508       * Deletes a trackback from the current entry.
 509       *
 510       * @param integer uid
 511       */
 512      function delete_trackback($uid) {
 513          global $PIVOTX;
 514  
 515          $PIVOTX['events']->add('delete_trackback', intval($this->entry['uid']), $this->entry['title']);
 516  
 517          $this->db_lowlevel->delete_trackback($uid);
 518  
 519      }
 520  
 521      /**
 522       * Returns a trackback from the current entry.
 523       *
 524       * @param integer uid
 525       */
 526      function get_trackback($uid) {
 527          global $PIVOTX;
 528  
 529          return $this->db_lowlevel->get_trackback($uid);
 530  
 531      }
 532  
 533      /**
 534       * Saves the current entry.
 535       *
 536       * Returns true if successfully saved. Current implementation
 537       * (in module_db_xml.php) seems to return true no matter what.
 538       *
 539       * @param boolean $update_index Whether to update the date index.
 540       * @return boolean
 541       */
 542      function save_entry($update_index=TRUE) {
 543          global $PIVOTX;
 544  
 545          $PIVOTX['events']->add('save_entry', intval($this->entry['uid']), $this->entry['title']);
 546  
 547          $this->db_lowlevel->save_entry($update_index);
 548  
 549          $this->entry = $this->db_lowlevel->entry;
 550  
 551          return true;
 552  
 553  
 554  
 555      }
 556  
 557      /**
 558      * Gets the date for an entry
 559      *
 560      * @param int $code
 561      * @return string
 562      */
 563      function get_date($code) {
 564  
 565          return $this->db_lowlevel->get_date($code);
 566  
 567      }
 568  
 569      /**
 570      * Switches to writing-disallowed mode.
 571      */
 572      function disallow_write() {
 573          $this->db_lowlevel->disallow_write();
 574      }
 575  
 576  
 577      /**
 578      * Switches to writing-allowed mode.
 579      */
 580      function allow_write() {
 581          $this->db_lowlevel->allow_write();
 582      }
 583  
 584  
 585      /**
 586       * Set one or more entries to 'publish'
 587       *
 588       * @param array $ids
 589       */
 590      function publish_entries($ids) {
 591          global $PIVOTX;
 592  
 593          $PIVOTX['events']->add('publish_entries', $ids);        
 594          
 595          return $this->db_lowlevel->publish_entries($ids);
 596      }
 597  
 598  
 599      /**
 600       * Set one or more entries to 'hold'
 601       *
 602       * @param array $ids
 603       */
 604      function depublish_entries($ids) {
 605          global $PIVOTX;
 606  
 607          $PIVOTX['events']->add('depublish_entries', $ids);
 608          
 609          return $this->db_lowlevel->depublish_entries($ids);
 610      }
 611  
 612      /**
 613       * Checks if any entries set to 'timed publish' should be published.
 614       *
 615       */
 616      function checkTimedPublish() {
 617          return $this->db_lowlevel->checkTimedPublish();
 618      }
 619  
 620      /**
 621       * Clears the index for searching or tags.
 622       *
 623       * @return void
 624       * @param string $type 
 625       */
 626      function clearIndex($type) {
 627          if ($this->db_lowlevel->need_index()) {
 628              $this->db_lowlevel->clearIndex($type);
 629          } else {
 630              echo "this database does not use an index.<br />";
 631          }
 632      }
 633  
 634      // end of class
 635  }
 636  
 637  
 638  
 639  ?>


Generated: Mon May 21 01:08:32 2012 Cross-referenced by PHPXref 0.6