| 
									
										
										
										
											2010-07-30 17:12:35 -07:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2010-07-31 10:47:58 -07:00
										 |  |  |  * Table Definition for msn_waiting_message | 
					
						
							| 
									
										
										
										
											2010-07-30 17:12:35 -07:00
										 |  |  |  */ | 
					
						
							|  |  |  | require_once INSTALLDIR.'/classes/Memcached_DataObject.php'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-18 12:10:44 +02:00
										 |  |  | class Msn_waiting_message extends Managed_DataObject { | 
					
						
							| 
									
										
										
										
											2010-07-30 17:12:35 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public $__table = 'msn_waiting_message'; // table name
 | 
					
						
							|  |  |  |     public $id;                              // int primary_key not_null auto_increment
 | 
					
						
							|  |  |  |     public $screenname;                      // varchar(255) not_null
 | 
					
						
							|  |  |  |     public $message;                         // text not_null
 | 
					
						
							|  |  |  |     public $claimed;                         // datetime()
 | 
					
						
							| 
									
										
										
										
											2013-08-19 17:08:18 +02:00
										 |  |  |     public $created;                         // datetime()   not_null
 | 
					
						
							|  |  |  |     public $modified;                        // timestamp()   not_null default_CURRENT_TIMESTAMP
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public static function schemaDef() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         return array( | 
					
						
							|  |  |  |             'fields' => array( | 
					
						
							|  |  |  |                 'id' => array('type' => 'int', 'not null' => true, 'description' => 'Unique ID for entry'), | 
					
						
							|  |  |  |                 'screenname' => array('type' => 'varchar', 'length' => 255, 'description' => 'from screenname'), | 
					
						
							|  |  |  |                 'message' => array('type' => 'text', 'not null' => true, 'description' => 'MSN message text'), | 
					
						
							|  |  |  |                 'claimed' => array('type' => 'datetime', 'description' => 'date this irc message was claimed'), | 
					
						
							|  |  |  |                 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'), | 
					
						
							|  |  |  |                 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |             'primary key' => array('id'), | 
					
						
							|  |  |  |             'indexes' => array( | 
					
						
							|  |  |  |                 'msn_waiting_message_prioritise_idx' => array('screenname'), | 
					
						
							|  |  |  |             ), | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2010-07-31 10:47:58 -07:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $screenname screenname or array of screennames to pull from | 
					
						
							| 
									
										
										
										
											2010-07-30 17:12:35 -07:00
										 |  |  |      *                          If not specified, checks all queues in the system. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public static function top($screenname = null) { | 
					
						
							|  |  |  |         $wm = new Msn_waiting_message(); | 
					
						
							|  |  |  |         if ($screenname) { | 
					
						
							|  |  |  |             if (is_array($screenname)) { | 
					
						
							|  |  |  |                 // @fixme use safer escaping
 | 
					
						
							| 
									
										
										
										
											2010-08-10 17:42:18 -07:00
										 |  |  |                 $list = implode("','", array_map('addslashes', $screenname)); | 
					
						
							|  |  |  |                 $wm->whereAdd("screenname in ('$list')"); | 
					
						
							| 
									
										
										
										
											2010-07-30 17:12:35 -07:00
										 |  |  |             } else { | 
					
						
							|  |  |  |                 $wm->screenname = $screenname; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $wm->orderBy('created'); | 
					
						
							|  |  |  |         $wm->whereAdd('claimed is null'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $wm->limit(1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $cnt = $wm->find(true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($cnt) { | 
					
						
							| 
									
										
										
										
											2011-03-22 11:54:23 -04:00
										 |  |  |             // XXX: potential race condition
 | 
					
						
							|  |  |  |             // can we force it to only update if claimed is still null
 | 
					
						
							|  |  |  |             // (or old)?
 | 
					
						
							| 
									
										
										
										
											2010-07-30 17:12:35 -07:00
										 |  |  |             common_log(LOG_INFO, 'claiming msn waiting message id = ' . $wm->id); | 
					
						
							|  |  |  |             $orig = clone($wm); | 
					
						
							|  |  |  |             $wm->claimed = common_sql_now(); | 
					
						
							|  |  |  |             $result = $wm->update($orig); | 
					
						
							|  |  |  |             if ($result) { | 
					
						
							|  |  |  |                 common_log(LOG_INFO, 'claim succeeded.'); | 
					
						
							|  |  |  |                 return $wm; | 
					
						
							|  |  |  |             } else { | 
					
						
							|  |  |  |                 common_log(LOG_INFO, 'claim failed.'); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $wm = null; | 
					
						
							|  |  |  |         return null; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Release a claimed item. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function releaseClaim() { | 
					
						
							|  |  |  |         // DB_DataObject doesn't let us save nulls right now
 | 
					
						
							|  |  |  |         $sql = sprintf("UPDATE msn_waiting_message SET claimed=NULL WHERE id=%d", $this->id); | 
					
						
							|  |  |  |         $this->query($sql); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         $this->claimed = null; | 
					
						
							|  |  |  |         $this->encache(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |