Changeset 283
- Timestamp:
- 12/29/06 17:30:53 (2 years ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/php-checkpad-api/Services_CheckPad/CheckPad.php
r282 r283 37 37 $this->http->setDefaultHeader( 38 38 array( 39 'User-Agent' => 'Services_CheckPad _ToDo0.1.0',39 'User-Agent' => 'Services_CheckPad 0.1.0', 40 40 ) 41 41 ); 42 42 43 $this->login_url = 'http://' . $this->checkpad_domain . '/'; 44 $this->listoftodo_url = 'http://' . $this->checkpad_domain . '/'; 43 $this->login_url = 'http://' . $this->checkpad_domain . '/'; 44 $this->listoftodo_url = 'http://' . $this->checkpad_domain . '/'; 45 $this->addlistoftodo_url = 'http://' . $this->checkpad_domain . '/?mode=pjt&act=add'; 46 $this->editlistoftodotitle_url = 'http://' . $this->checkpad_domain . '/?mode=pjt&act=edit&id=%d'; 47 $this->editlistoftodomemo_url = 'http://' . $this->checkpad_domain . '/?mode=pjt&act=memo_edit&id=%d'; 48 $this->dellistoftodo_url = 'http://' . $this->checkpad_domain . '/?mode=pjt&act=del&id=%d'; 49 $this->todo_url = 'http://' . $this->checkpad_domain . '/?mode=pjt&act=detail&id=%d'; 50 $this->addtodo_url = 'http://' . $this->checkpad_domain . '/index.php'; 51 $this->edittodo_url = 'http://' . $this->checkpad_domain . '/index.php'; 52 $this->finishtodo_url = 'http://' . $this->checkpad_domain . '/index.php'; 53 $this->unfinishtodo_url = 'http://' . $this->checkpad_domain . '/index.php'; 54 $this->delnotyettodo_url = 'http://' . $this->checkpad_domain . '/index.php'; 55 $this->deldonetodo_url = 'http://' . $this->checkpad_domain . '/index.php'; 45 56 } 46 57 … … 48 59 * To login 49 60 * 50 * @param string $user email 51 * @param string $pass password 61 * @param string $user email 62 * @param string $pass password 63 * @return boolean 52 64 */ 53 65 function login($email, $pass) … … 59 71 'act' => 'login' 60 72 ); 61 $this->http->post($this->login_url, $param); 73 $res = $this->http->post($this->login_url, $param); 74 if (PEAR::isError($res)) { 75 return $res; 76 } 77 if (PEAR::isError($response = $this->http->currentResponse())) { 78 return $response; 79 } 80 return (boolean)preg_match('/ログアウト/is', mb_convert_encoding($response['body'], 'utf-8', 'euc-jp')); 62 81 } 63 82 … … 68 87 * @param void 69 88 * @return array list of todo 89 * array( 90 * array( 91 * 'title' => string title 92 * 'url' => string detail url 93 * 'id' => int detail id 94 * 'left' => int left 95 * 'shared' => boolean shared list flag 96 * ), 97 * ... 98 * ) 70 99 */ 71 100 function getListOfToDo() 72 101 { 73 $this->http->get($this->listoftodo_url); 74 $response = $this->http->currentResponse(); 75 return $this->_parseListOfToDo($response['body']); 102 $res = $this->http->get($this->listoftodo_url); 103 if (PEAR::isError($res)) { 104 return $res; 105 } 106 if (PEAR::isError($response = $this->http->currentResponse())) { 107 return $response; 108 } 109 return $this->_parseListOfToDo(mb_convert_encoding($response['body'], 'utf-8', 'euc-jp')); 76 110 } 77 111 … … 84 118 function _parseListOfToDo($html) 85 119 { 86 $html = mb_convert_encoding($html, 'utf-8', 'euc-jp'); 120 $result = array(); 121 foreach ($this->_parseListOfToDoSection($this->_choiseMyListHTML($html)) as $value) { 122 $value['shared'] = false; 123 $result[] = $value; 124 } 125 foreach ($this->_parseListOfToDoSection($this->_choiseSharedListHTML($html)) as $value) { 126 $value['shared'] = true; 127 $result[] = $value; 128 } 129 return $result; 130 } 131 132 /** 133 * choise my list section 134 * 135 * @param string $html html 136 * @return string my list html 137 */ 138 function _choiseMyListHTML($html) 139 { 140 $res = preg_match('/あなたのリスト(.+)<li class="add">/is', $html, $match); 141 return $res ? $match[1] : ''; 142 } 143 144 /** 145 * choise shared list section 146 * 147 * @param string $html html 148 * @return string my list html 149 */ 150 function _choiseSharedListHTML($html) 151 { 152 $res = preg_match('/他の人のリスト(.+)id="rightside"/is', $html, $match); 153 return $res ? $match[1] : ''; 154 } 155 156 /** 157 * parse list of todo section 158 * 159 * @param string $html html 160 * @return array list of todo 161 */ 162 function _parseListOfToDoSection($html) 163 { 87 164 preg_match_all('!<a[^>]*?href="(/\?mode=pjt&act=detail&id=([0-9]+))"[^>]*?>(.+?)</a>.*?<span style="color:#BFBFBF">- ([0-9]+)個!is', $html, $match, PREG_SET_ORDER); 88 165 $result = array(); 89 166 foreach ($match as $match_value) { 90 167 $result[] = array( 91 'title' => trim($match_value[3]),92 'url' => 'http://' . $this->checkpad_domain . trim($match_value[1]),93 'id' =>trim($match_value[2]),94 'left' =>trim($match_value[4])168 'title' => trim($match_value[3]), 169 'url' => 'http://' . $this->checkpad_domain . trim($match_value[1]), 170 'id' => (int)trim($match_value[2]), 171 'left' => (int)trim($match_value[4]) 95 172 ); 96 173 } 97 174 return $result; 175 } 176 177 /** 178 * add list of todo 179 * 180 * @param string $title 181 * @return int id 182 */ 183 function addListOfToDo($title) 184 { 185 $param = array( 186 'ttl' => mb_convert_encoding($title, 'euc-jp', 'utf-8'), 187 ); 188 $res = $this->http->post($this->addlistoftodo_url, $param); 189 if (PEAR::isError($res)) { 190 return $res; 191 } 192 if (PEAR::isError($response = $this->http->currentResponse())) { 193 return $response; 194 } 195 $listid = $this->_getListIdInToDo(mb_convert_encoding($response['body'], 'utf-8', 'euc-jp')); 196 return $listid; 197 } 198 199 /** 200 * getListIdInToDo 201 * 202 * @param string $html html 203 * @return int list id 204 */ 205 function _getListIdInToDo($html) 206 { 207 $res = preg_match('!/\?mode=pjt&act=memo_edit&id=([0-9]+)!is', $html, $match); 208 if (!$res) { 209 return PEAR::raiseError('failed find list id'); 210 } 211 return (int)$match[1]; 212 } 213 214 /** 215 * del list of todo 216 * 217 * @param int $list_id 218 * @return void 219 */ 220 function delListOfToDo($list_id) 221 { 222 $url = sprintf( 223 $this->dellistoftodo_url 224 , $list_id 225 ); 226 $res = $this->http->get($url); 227 if (PEAR::isError($res)) { 228 return $res; 229 } 230 if (PEAR::isError($response = $this->http->currentResponse())) { 231 return $response; 232 } 233 } 234 235 /** 236 * edit list of todo title 237 * 238 * @param int $list_id list id 239 * @param string $title 240 * @return void 241 */ 242 function editListOfToDoTitle($list_id, $title) 243 { 244 $url = sprintf( 245 $this->editlistoftodotitle_url 246 , $list_id 247 ); 248 $param = array( 249 'ttl' => mb_convert_encoding($title, 'euc-jp', 'utf-8') 250 ); 251 $res = $this->http->post($url, $param); 252 if (PEAR::isError($res)) { 253 return $res; 254 } 255 if (PEAR::isError($response = $this->http->currentResponse())) { 256 return $response; 257 } 258 } 259 260 /** 261 /** 262 * edit list of todo memo 263 * 264 * @param int $list_id list id 265 * @param string $memo 266 * @return void 267 */ 268 function editListOfToDoMemo($list_id, $memo) 269 { 270 $url = sprintf( 271 $this->editlistoftodomemo_url 272 , $list_id 273 ); 274 $param = array( 275 'memo' => mb_convert_encoding($memo, 'euc-jp', 'utf-8'), 276 ); 277 $res = $this->http->post($url, $param); 278 if (PEAR::isError($res)) { 279 return $res; 280 } 281 if (PEAR::isError($response = $this->http->currentResponse())) { 282 return $response; 283 } 284 } 285 286 /** 287 * get todo list 288 * 289 * @param string $list_id list id 290 * @return array list of todo 291 * array( 292 * array( 293 * 'title' =>string title, 294 * 'id' => int detail id, 295 * 'done' => boolean done, 296 * ), 297 * ... 298 * ) 299 */ 300 function getToDoList($list_id) 301 { 302 $res = $this->http->get($this->getToDoListURL($list_id)); 303 if (PEAR::isError($res)) { 304 return $res; 305 } 306 if (PEAR::isError($response = $this->http->currentResponse())) { 307 return $response; 308 } 309 $html = mb_convert_encoding($response['body'], 'utf-8', 'euc-jp'); 310 $result = array(); 311 foreach ($this->_parseToDoListNotYet($html) as $value) { 312 $value['done'] = false; 313 $result[] = $value; 314 } 315 foreach ($this->_parseToDoListDone($html) as $value) { 316 $value['done'] = true; 317 $result[] = $value; 318 } 319 return $result; 320 } 321 322 /** 323 * getToDoListURL 324 * 325 * @param int $list_id list id 326 * @return string url 327 */ 328 function getToDoListURL($list_id) 329 { 330 return sprintf( 331 $this->todo_url 332 , $list_id 333 ); 334 } 335 336 /** 337 * get todo list notyet 338 * 339 * @param string $list_id list id 340 * @return array list of todo 341 * array( 342 * array( 343 * 'title' => string title, 344 * 'id' => int detail id, 345 * ), 346 * ... 347 * ) 348 */ 349 function getToDoListNotYet($list_id) 350 { 351 $res = $this->http->get($this->getToDoListURL($list_id)); 352 if (PEAR::isError($res)) { 353 return $res; 354 } 355 if (PEAR::isError($response = $this->http->currentResponse())) { 356 return $response; 357 } 358 $html = mb_convert_encoding($response['body'], 'utf-8', 'euc-jp'); 359 return $this->_parseToDoListNotYet($html); 360 } 361 362 /** 363 * get todo list done 364 * 365 * @param string $list_id list id 366 * @return array list of todo 367 * array( 368 * array( 369 * 'title' => string title, 370 * 'id' => int detail id, 371 * ), 372 * ... 373 * ) 374 */ 375 function getToDoListDone($list_id) 376 { 377 $res = $this->http->get($this->getToDoListURL($list_id)); 378 if (PEAR::isError($res)) { 379 return $res; 380 } 381 if (PEAR::isError($response = $this->http->currentResponse())) { 382 return $response; 383 } 384 $html = mb_convert_encoding($response['body'], 'utf-8', 'euc-jp'); 385 return $this->_parseToDoListDone($html); 386 } 387 388 /** 389 * parse todo list notyet 390 * 391 * @param string $html html 392 * @return array 393 */ 394 function _parseToDoListNotYet($html) 395 { 396 $result = array(); 397 preg_match_all('!<input[^>]*?(id="ms_([0-9]+)_edit"[^>]*?|name="ttl"[^>]*?|value="([^"]*?)"[^>]*?){3}"!is', $html, $match, PREG_SET_ORDER); 398 $result = array(); 399 foreach ($match as $match_value) { 400 $result[] = array( 401 'title' => trim($match_value[3]), 402 'id' => (int)trim($match_value[2]) 403 ); 404 } 405 return $result; 406 } 407 408 /** 409 * parse todo list done 410 * 411 * @param string $html html 412 * @return array 413 */ 414 function _parseToDoListDone($html) 415 { 416 $result = array(); 417 preg_match_all('!<div id="ms_done_([0-9]+)">.+?<div id="[0-9]+" style="display:inline">(.+?)</div>!is', $html, $match, PREG_SET_ORDER); 418 $result = array(); 419 foreach ($match as $match_value) { 420 $result[] = array( 421 'title' => trim($match_value[2]), 422 'id' => (int)trim($match_value[1]) 423 ); 424 } 425 return $result; 426 } 427 428 /** 429 * add todo 430 * 431 * @param int $list_id list_id 432 * @param string $title title 433 * @return int id new todo id 434 */ 435 function addToDo($list_id, $title) 436 { 437 $param = array( 438 'mode' => 'ms', 439 'act' => 'add', 440 'ajax' => '1', 441 'pjt_id' => $list_id, 442 'ttl' => $title 443 ); 444 $res = $this->http->post($this->addtodo_url, $param); 445 if (PEAR::isError($res)) { 446 return $res; 447 } 448 if (PEAR::isError($response = $this->http->currentResponse())) { 449 return $response; 450 } 451 $todolist = $this->_parseToDoListNotYet(mb_convert_encoding($response['body'], 'utf-8', 'euc-jp')); 452 return $todolist[count($todolist) -1]['id']; 453 } 454 455 /** 456 * edit todo 457 * 458 * @param int $todo_id todo id 459 * @param string $title title 460 * @return void 461 */ 462 function editToDo($todo_id, $title) 463 { 464 $param = array( 465 'mode' => 'ms', 466 'act' => 'edit', 467 'id' => $todo_id, 468 'ttl' => $title 469 ); 470 $res = $this->http->post($this->edittodo_url, $param); 471 if (PEAR::isError($res)) { 472 return $res; 473 } 474 if (PEAR::isError($response = $this->http->currentResponse())) { 475 return $response; 476 } 477 } 478 479 /** 480 * finish todo 481 * 482 * @param int $todo_id todo id 483 * @return void 484 */ 485 function finishToDo($todo_id) 486 { 487 $param = array( 488 'mode' => 'ms', 489 'act' => 'finish', 490 'id' => $todo_id 491 ); 492 $res = $this->http->post($this->finishtodo_url, $param); 493 if (PEAR::isError($res)) { 494 return $res; 495 } 496 if (PEAR::isError($response = $this->http->currentResponse())) { 497 return $response; 498 } 499 } 500 501 /** 502 * unfinish todo 503 * 504 * @param int $todo_id todo id 505 * @return void 506 */ 507 function unfinishToDo($todo_id) 508 { 509 $param = array( 510 'mode' => 'ms', 511 'act' => 'unfinish', 512 'id' => $todo_id 513 ); 514 $res = $this->http->post($this->unfinishtodo_url, $param); 515 if (PEAR::isError($res)) { 516 return $res; 517 } 518 if (PEAR::isError($response = $this->http->currentResponse())) { 519 return $response; 520 } 521 } 522 523 /** 524 * del notyet todo 525 * 526 * @param int $todo_id todo id 527 * @return void 528 */ 529 function delNotYetToDo($todo_id) 530 { 531 $param = array( 532 'mode' => 'ms', 533 'act' => 'del_notyet', 534 'id' => $todo_id 535 ); 536 $res = $this->http->post($this->delnotyettodo_url, $param); 537 if (PEAR::isError($res)) { 538 return $res; 539 } 540 if (PEAR::isError($response = $this->http->currentResponse())) { 541 return $response; 542 } 543 } 544 545 /** 546 * del done todo 547 * 548 * @param int $todo_id todo id 549 * @return void 550 */ 551 function delDoneToDo($todo_id) 552 { 553 $param = array( 554 'mode' => 'ms', 555 'act' => 'del_done', 556 'id' => $todo_id 557 ); 558 $res = $this->http->post($this->deldonetodo_url, $param); 559 if (PEAR::isError($res)) { 560 return $res; 561 } 562 if (PEAR::isError($response = $this->http->currentResponse())) { 563 return $response; 564 } 98 565 } 99 566 } trunk/php-checkpad-api/Services_CheckPad/examples/Services_CheckPad_Example_01.php
r280 r283 1 1 <?php 2 /** 3 * Services_CheckPad Example Code 4 * 5 * usage: php Services_CheckPad_Example_01.php {email...} {password..} 6 */ 2 7 error_reporting(E_ALL); 3 8 require_once 'Services/CheckPad.php'; 4 9 10 if (isset($_SERVER['argv'][1]) == false || isset($_SERVER['argv'][2]) == false) { 11 die("usage: php Services_CheckPad_Example_01.php {email...} {password...}\n\n"); 12 } 13 5 14 $checkpad = new Services_CheckPad(); 6 $checkpad->login($_SERVER['argv'][1], $_SERVER['argv'][2]); 7 $res = $checkpad->getListOfToDo(); 8 var_dump($res); 15 echo format_test_start_message("login"); 16 $res = $checkpad->login($_SERVER['argv'][1], $_SERVER['argv'][2]); 17 if ($res !== true) { 18 echo "Failed\n"; 19 exit; 20 } 21 echo "Success\n"; 22 23 echo format_test_start_message("getListOfToDo", "1 of 3"); 24 $listoftodo_before = $res = $checkpad->getListOfToDo(); 25 if (!is_array($res)) { 26 echo "Failed\n"; 27 exit; 28 } 29 echo "Success\n"; 30 31 echo format_test_start_message("addListOfToDo"); 32 $new_listoftodo_id = $res = $checkpad->addListOfToDo('テスト用リスト作成'); 33 if (!is_int($res)) { 34 echo "Failed\n"; 35 exit; 36 } 37 echo "Success: new list of todo id is " . $new_listoftodo_id . "\n"; 38 39 echo format_test_start_message("getListOfToDo", "2 of 3"); 40 $listoftodo_after = $res = $checkpad->getListOfToDo(); 41 if (!is_array($res)) { 42 echo "Failed\n"; 43 exit; 44 } 45 if (count($listoftodo_after) - count($listoftodo_before) != 1) { 46 echo "Failed: no added\n"; 47 exit; 48 } 49 echo "Success\n"; 50 51 echo format_test_start_message("editListOfToDoTitle"); 52 $res = $checkpad->editListOfToDoTitle($new_listoftodo_id, 'テスト用リストタイトル変更'); 53 if (PEAR::isError($res)) { 54 echo "Failed\n"; 55 exit; 56 } 57 echo "Success\n"; 58 59 echo format_test_start_message("editListOfToDoMemo"); 60 $res = $checkpad->editListOfToDoMemo($new_listoftodo_id, 'メモ設定'); 61 if (PEAR::isError($res)) { 62 echo "Failed\n"; 63 exit; 64 } 65 echo "Success\n"; 66 67 echo format_test_start_message("addToDo", "1 of 2"); 68 $new_todo_id_1 = $res = $checkpad->addToDo($new_listoftodo_id, 'ほげ1'); 69 if (!is_int($res)) { 70 echo "Failed\n"; 71 exit; 72 } 73 echo "Success: new todo id is " . $new_todo_id_1 . "\n"; 74 75 echo format_test_start_message("addToDo" ,"2 of 2"); 76 $new_todo_id_2 = $res = $checkpad->addToDo($new_listoftodo_id, 'ほげ2'); 77 if (!is_int($res)) { 78 echo "Failed\n"; 79 exit; 80 } 81 echo "Success: new todo id is " . $new_todo_id_2 . "\n"; 82 83 echo format_test_start_message("getToDoListNotYet", "1 of 5"); 84 $res = $checkpad->getToDoListNotYet($new_listoftodo_id); 85 if (!is_array($res)) { 86 echo "Failed\n"; 87 exit; 88 } 89 if (count($res) != 2) { 90 echo "Failed: amount is not 2\n"; 91 } 92 echo "Success\n"; 93 94 echo format_test_start_message("finishToDo", "1 of 2"); 95 $res = $checkpad->finishToDo($new_todo_id_1); 96 if (PEAR::isError($res)) { 97 echo "Failed\n"; 98 exit; 99 } 100 echo "Success\n"; 101 102 echo format_test_start_message("getToDoListNotYet", "2 of 5"); 103 $res = $checkpad->getToDoListNotYet($new_listoftodo_id); 104 if (!is_array($res)) { 105 echo "Failed\n"; 106 exit; 107 } 108 if (count($res) != 1) { 109 echo "Failed: amount is not 1\n"; 110 } 111 echo "Success\n"; 112 113 echo format_test_start_message("finishToDo", "2 of 2"); 114 $res = $checkpad->finishToDo($new_todo_id_2); 115 if (PEAR::isError($res)) { 116 echo "Failed\n"; 117 exit; 118 } 119 echo "Success\n"; 120 121 echo format_test_start_message("getToDoListNotYet" ,"3 of 5"); 122 $res = $checkpad->getToDoListNotYet($new_listoftodo_id); 123 if (!is_array($res)) { 124 echo "Failed\n"; 125 exit; 126 } 127 if (count($res) != 0) { 128 echo "Faild: amount is not 0\n"; 129 } 130 echo "Success\n"; 131 132 echo format_test_start_message("getToDoListDone", "1 of 2"); 133 $res = $checkpad->getToDoListDone($new_listoftodo_id); 134 if (!is_array($res)) { 135 echo "Failed\n"; 136 exit; 137 } 138 if (count($res) != 2) { 139 echo "Faild: amount is not 0\n"; 140 } 141 echo "Success\n"; 142 143 echo format_test_start_message("unfinishToDo"); 144 $res = $checkpad->unfinishToDo($new_todo_id_1); 145 if (PEAR::isError($res)) { 146 echo "Failed\n"; 147 exit; 148 } 149 echo "Success\n"; 150 151 echo format_test_start_message("getToDoListDone", "1 of 2"); 152 $res = $checkpad->getToDoListDone($new_listoftodo_id); 153 if (!is_array($res)) { 154 echo "Failed\n"; 155 exit; 156 } 157 if (count($res) != 1) { 158 echo "Faild: amount is not 1\n"; 159 exit; 160 } 161 echo "Success\n"; 162 163 echo format_test_start_message("getToDoListNotYet", "4 of 5"); 164 $res = $checkpad->getToDoListNotYet($new_listoftodo_id); 165 if (!is_array($res)) { 166 echo "Failed\n"; 167 exit; 168 } 169 if (count($res) != 1) { 170 echo "Notice: amount is not 1\n"; 171 } else { 172 echo "Success\n"; 173 } 174 175 echo format_test_start_message("getToDoList"); 176 $res = $checkpad->getToDoList($new_listoftodo_id); 177 if (!is_array($res)) { 178 echo "Failed\n"; 179 exit; 180 } 181 if (count($res) == 0) { 182 echo "Failed: amount is zero\n"; 183 exit; 184 } 185 echo "Success\n"; 186 187 echo format_test_start_message("editToDo"); 188 $res = $checkpad->editToDo($new_todo_id_1, 'ふが変更'); 189 if (PEAR::isError($res)) { 190 echo "Failed\n"; 191 exit; 192 } 193 echo "Success\n"; 194 195 echo format_test_start_message("delNotYetToDo"); 196 $res = $checkpad->delNotYetToDo($new_todo_id_1); 197 if (PEAR::isError($res)) { 198 echo "Failed\n"; 199 exit; 200 } 201 echo "Success\n"; 202 203 echo format_test_start_message("getToDoListNotYet", "5 of 5"); 204 $res = $checkpad->getToDoListNotYet($new_listoftodo_id); 205 if (!is_array($res)) { 206 echo "Failed\n"; 207 exit; 208 } 209 if (count($res) != 0) { 210 echo "Failed: amount is not 0\n"; 211 } 212 echo "Success\n"; 213 214 echo format_test_start_message("delDoneToDo"); 215 $res = $checkpad->delDoneToDo($new_todo_id_2); 216 if (PEAR::isError($res)) { 217 echo "Failed\n"; 218 exit; 219 } 220 echo "Success\n"; 221 222 echo format_test_start_message("getToDoListDone", "2 of 2"); 223 $res = $checkpad->getToDoListDone($new_listoftodo_id); 224 if (!is_array($res)) { 225 echo "Failed\n"; 226 exit; 227 } 228 if (count($res) != 0) { 229 echo "Faild: amount is not 0\n"; 230 exit; 231 } 232 echo "Success\n"; 233 234 echo format_test_start_message("delListOfToDo"); 235 $res = $checkpad->delListOfToDo($new_listoftodo_id); 236 if (PEAR::isError($res)) { 237 echo "Failed\n"; 238 exit; 239 } 240 if (count($listoftodo_before) == $res) { 241 echo "Faild: no deleted\n"; 242 exit; 243 } 244 echo "Success\n"; 245 246 function format_test_start_message($title, $numofnum = '') 247 { 248 echo sprintf( 249 '%-20s %-6s ... ' 250 , $title 251 , $numofnum 252 ); 253 } 9 254 ?>