ArticleController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. use App\Models\Article;
  8. use App\Models\ArticleCollection;
  9. use App\Models\Collection;
  10. use App\Models\CustomBook;
  11. use App\Models\CustomBookId;
  12. use App\Models\Sentence;
  13. use App\Http\Resources\ArticleResource;
  14. use App\Services\AuthService;
  15. use App\Http\Api\ShareApi;
  16. use App\Http\Api\StudioApi;
  17. use App\Http\Api\ChannelApi;
  18. use App\Http\Api\SentenceApi;
  19. use App\Tools\OpsLog;
  20. class ArticleController extends Controller
  21. {
  22. public static function userCanRead($user_uid, Article $article)
  23. {
  24. if ($article->status === 30) {
  25. return true;
  26. }
  27. if (empty($user_uid)) {
  28. return false;
  29. }
  30. //私有文章,判断是否为所有者
  31. if ($user_uid === $article->owner) {
  32. return true;
  33. }
  34. //非所有者
  35. //判断是否为文章协作者
  36. $power = ShareApi::getResPower($user_uid, $article->uid);
  37. if ($power >= 10) {
  38. return true;
  39. }
  40. //无读取权限
  41. //判断文集是否有读取权限
  42. $inCollection = ArticleCollection::where('article_id', $article->uid)
  43. ->select('collect_id')
  44. ->groupBy('collect_id')->get();
  45. if (!$inCollection) {
  46. return false;
  47. }
  48. //查找与文章同主人的文集
  49. $collections = Collection::whereIn('uid', $inCollection)
  50. ->where('owner', $article->owner)
  51. ->select('uid')
  52. ->get();
  53. if (!$collections) {
  54. return false;
  55. }
  56. //查找与文章同主人的文集是否是共享的
  57. $power = 0;
  58. foreach ($collections as $collection) {
  59. # code...
  60. $currPower = ShareApi::getResPower($user_uid, $collection->uid);
  61. if ($currPower >= 10) {
  62. return true;
  63. }
  64. }
  65. return false;
  66. }
  67. public static function userCanEditId($user_uid, $articleId)
  68. {
  69. $article = Article::find($articleId);
  70. if ($article) {
  71. return ArticleController::userCanEdit($user_uid, $article);
  72. } else {
  73. return false;
  74. }
  75. }
  76. public static function userCanEdit($user_uid, $article)
  77. {
  78. if (empty($user_uid)) {
  79. return false;
  80. }
  81. //私有文章,判断是否为所有者
  82. if ($user_uid === $article->owner) {
  83. return true;
  84. }
  85. //非所有者
  86. //判断是否为文章协作者
  87. $power = ShareApi::getResPower($user_uid, $article->uid);
  88. if ($power >= 20) {
  89. return true;
  90. }
  91. //无读取权限
  92. //判断文集是否有读取权限
  93. $inCollection = ArticleCollection::where('article_id', $article->uid)
  94. ->select('collect_id')
  95. ->groupBy('collect_id')->get();
  96. if (!$inCollection) {
  97. return false;
  98. }
  99. //查找与文章同主人的文集
  100. $collections = Collection::whereIn('uid', $inCollection)
  101. ->where('owner', $article->owner)
  102. ->select('uid')
  103. ->get();
  104. if (!$collections) {
  105. return false;
  106. }
  107. //查找与文章同主人的文集是否是共享的
  108. $power = 0;
  109. foreach ($collections as $collection) {
  110. # code...
  111. $currPower = ShareApi::getResPower($user_uid, $collection->uid);
  112. if ($currPower >= 20) {
  113. return true;
  114. }
  115. }
  116. return false;
  117. }
  118. public static function userCanManage($user_uid, $studioName)
  119. {
  120. if (empty($user_uid)) {
  121. return false;
  122. }
  123. //判断是否为所有者
  124. if ($user_uid === StudioApi::getIdByName($studioName)) {
  125. return true;
  126. } else {
  127. return false;
  128. }
  129. }
  130. /**
  131. * Display a listing of the resource.
  132. *
  133. * @return \Illuminate\Http\Response
  134. */
  135. public function index(Request $request)
  136. {
  137. //
  138. $field = [
  139. 'uid',
  140. 'title',
  141. 'subtitle',
  142. 'summary',
  143. 'owner',
  144. 'lang',
  145. 'status',
  146. 'editor_id',
  147. 'updated_at',
  148. 'created_at'
  149. ];
  150. if ($request->input('content') === "true") {
  151. $field[] = 'content';
  152. $field[] = 'content_type';
  153. }
  154. $table = Article::select($field);
  155. switch ($request->input('view')) {
  156. case 'template':
  157. $studioId = StudioApi::getIdByName($request->input('studio_name'));
  158. $table = $table->where('owner', $studioId);
  159. break;
  160. case 'studio':
  161. # 获取studio内所有 article
  162. $user = AuthService::current($request);
  163. if (!$user) {
  164. return $this->error(__('auth.failed'), [], 401);
  165. }
  166. //判断当前用户是否有指定的studio的权限
  167. $studioId = StudioApi::getIdByName($request->input('name'));
  168. if ($user['user_uid'] !== $studioId) {
  169. return $this->error(__('auth.failed'), [], 403);
  170. }
  171. if ($request->input('view2', 'my') === 'my') {
  172. $table = $table->where('owner', $studioId);
  173. } else {
  174. //协作
  175. $resList = ShareApi::getResList($studioId, 3);
  176. $resId = [];
  177. foreach ($resList as $res) {
  178. $resId[] = $res['res_id'];
  179. }
  180. $table = $table->whereIn('uid', $resId)->where('owner', '<>', $studioId);
  181. }
  182. //根据anthology过滤
  183. if ($request->has('anthology')) {
  184. switch ($request->input('anthology')) {
  185. case 'all':
  186. break;
  187. case 'none':
  188. # 我的文集
  189. $myCollection = Collection::where('owner', $studioId)->select('uid')->get();
  190. //收录在我的文集里面的文章
  191. $articles = ArticleCollection::whereIn('collect_id', $myCollection)
  192. ->select('article_id')->groupBy('article_id')->get();
  193. //不在这些范围之内的文章
  194. $table = $table->whereNotIn('uid', $articles);
  195. break;
  196. default:
  197. $articles = ArticleCollection::where('collect_id', $request->input('anthology'))
  198. ->select('article_id')->get();
  199. $table = $table->whereIn('uid', $articles);
  200. break;
  201. }
  202. }
  203. break;
  204. case 'public':
  205. $table = $table->where('status', 30);
  206. break;
  207. default:
  208. $this->error("view error");
  209. break;
  210. }
  211. //处理搜索
  212. if ($request->has("search") && !empty($request->input("search"))) {
  213. $table = $table->where('title', 'like', "%" . $request->input("search") . "%");
  214. }
  215. if ($request->has("subtitle") && !empty($request->input("subtitle"))) {
  216. $table = $table->where('subtitle', 'like', $request->input("subtitle"));
  217. }
  218. //获取记录总条数
  219. $count = $table->count();
  220. //处理排序
  221. $table = $table->orderBy(
  222. $request->input("order", 'updated_at'),
  223. $request->input("dir", 'desc')
  224. );
  225. //处理分页
  226. $table = $table->skip($request->input("offset", 0))
  227. ->take($request->input("limit", 1000));
  228. //获取数据
  229. $result = $table->get();
  230. return $this->ok(["rows" => ArticleResource::collection($result), "count" => $count]);
  231. }
  232. /**
  233. * Display a listing of the resource.
  234. *
  235. * @return \Illuminate\Http\Response
  236. */
  237. public function showMyNumber(Request $request)
  238. {
  239. $user = AuthService::current($request);
  240. if (!$user) {
  241. return $this->error(__('auth.failed'));
  242. }
  243. //判断当前用户是否有指定的studio的权限
  244. $studioId = StudioApi::getIdByName($request->input('studio'));
  245. if ($user['user_uid'] !== $studioId) {
  246. return $this->error(__('auth.failed'));
  247. }
  248. //我的
  249. $my = Article::where('owner', $studioId)->count();
  250. //协作
  251. $resList = ShareApi::getResList($studioId, 3);
  252. $resId = [];
  253. foreach ($resList as $res) {
  254. $resId[] = $res['res_id'];
  255. }
  256. $collaboration = Article::whereIn('uid', $resId)->where('owner', '<>', $studioId)->count();
  257. return $this->ok(['my' => $my, 'collaboration' => $collaboration]);
  258. }
  259. /**
  260. * Store a newly created resource in storage.
  261. *
  262. * @param \Illuminate\Http\Request $request
  263. * @return \Illuminate\Http\Response
  264. */
  265. public function store(Request $request)
  266. {
  267. //判断权限
  268. $user = AuthService::current($request);
  269. if (!$user) {
  270. return $this->error(__('auth.failed'), [], 401);
  271. } else {
  272. $user_uid = $user['user_uid'];
  273. }
  274. $canManage = ArticleController::userCanManage($user_uid, $request->input('studio'));
  275. if (!$canManage) {
  276. //判断是否有文集权限
  277. if ($request->has('anthologyId')) {
  278. $currPower = ShareApi::getResPower($user_uid, $request->input('anthologyId'));
  279. if ($currPower <= 10) {
  280. return $this->error(__('auth.failed'), [], 403);
  281. }
  282. } else {
  283. return $this->error(__('auth.failed'), [], 403);
  284. }
  285. }
  286. //权限判断结束
  287. //查询标题是否重复
  288. /*
  289. if(Article::where('title',$request->input('title'))->where('owner',$studioUuid)->exists()){
  290. return $this->error(__('validation.exists'));
  291. }*/
  292. $newArticle = new Article;
  293. DB::transaction(function () use ($user, $request, $newArticle) {
  294. $studioUuid = StudioApi::getIdByName($request->input('studio'));
  295. //新建文章,加入文集必须都成功。否则回滚
  296. $newArticle->id = app('snowflake')->id();
  297. $newArticle->uid = Str::uuid();
  298. $newArticle->title = mb_substr($request->input('title'), 0, 128, 'UTF-8');
  299. $newArticle->lang = $request->input('lang');
  300. if (!empty($request->input('status'))) {
  301. $newArticle->status = $request->input('status');
  302. }
  303. $newArticle->owner = $studioUuid;
  304. $newArticle->owner_id = $user['user_id'];
  305. $newArticle->editor_id = $user['user_id'];
  306. $newArticle->parent = $request->input('parentId');
  307. $newArticle->create_time = time() * 1000;
  308. $newArticle->modify_time = time() * 1000;
  309. $newArticle->save();
  310. OpsLog::debug($user['user_uid'], $newArticle);
  311. $anthologyId = $request->input('anthologyId');
  312. if (Str::isUuid($anthologyId)) {
  313. $parentNode = $request->input('parentNode');
  314. if (Str::isUuid($parentNode)) {
  315. $map = ArticleCollection::where('collect_id', $anthologyId)
  316. ->orderBy('id')->get();
  317. $newMap = array();
  318. $parentNodeLevel = -1;
  319. $appended = false;
  320. foreach ($map as $key => $row) {
  321. $orgNode = $row;
  322. if (!$appended) {
  323. if ($parentNodeLevel > 0) {
  324. if ($row->level <= $parentNodeLevel) {
  325. //parent node 末尾
  326. $newNode = array();
  327. $newNode['collect_id'] = $anthologyId;
  328. $newNode['article_id'] = $newArticle->uid;
  329. $newNode['level'] = $parentNodeLevel + 1;
  330. $newNode['title'] = $newArticle->title;
  331. $newNode['children'] = 0;
  332. $newMap[] = $newNode;
  333. $appended = true;
  334. }
  335. } else {
  336. if ($row->article_id === $parentNode) {
  337. $parentNodeLevel = $row->level;
  338. $orgNode['children'] = $orgNode['children'] + 1;
  339. }
  340. }
  341. }
  342. $newMap[] = $orgNode;
  343. }
  344. if ($parentNodeLevel > 0) {
  345. if ($appended === false) {
  346. $newNode = array();
  347. $newNode['collect_id'] = $anthologyId;
  348. $newNode['article_id'] = $newArticle->uid;
  349. $newNode['level'] = $parentNodeLevel + 1;
  350. $newNode['title'] = $newArticle->title;
  351. $newNode['children'] = 0;
  352. $newMap[] = $newNode;
  353. }
  354. } else {
  355. Log::warning('没找到挂接点' . $parentNode);
  356. }
  357. $delete = ArticleCollection::where('collect_id', $anthologyId)->delete();
  358. $count = 0;
  359. foreach ($newMap as $key => $row) {
  360. $new = new ArticleCollection;
  361. $new->id = app('snowflake')->id();
  362. $new->article_id = $row["article_id"];
  363. $new->collect_id = $row["collect_id"];
  364. $new->title = $row["title"];
  365. $new->level = $row["level"];
  366. $new->children = $row["children"];
  367. $new->editor_id = $user["user_id"];
  368. if (isset($row["deleted_at"])) {
  369. $new->deleted_at = $row["deleted_at"];
  370. }
  371. $new->save();
  372. $count++;
  373. }
  374. ArticleMapController::updateCollection($anthologyId);
  375. } else {
  376. $articleMap = new ArticleCollection();
  377. $articleMap->id = app('snowflake')->id();
  378. $articleMap->article_id = $newArticle->uid;
  379. $articleMap->collect_id = $request->input('anthologyId');
  380. $articleMap->title = Article::find($newArticle->uid)->title;
  381. $articleMap->level = 1;
  382. $articleMap->save();
  383. }
  384. }
  385. });
  386. if (Str::isUuid($newArticle->uid)) {
  387. return $this->ok(new ArticleResource($newArticle));
  388. } else {
  389. return $this->error('fail');
  390. }
  391. }
  392. /**
  393. * Display the specified resource.
  394. * @param \Illuminate\Http\Request $request
  395. * @param \App\Models\Article $article
  396. * @return \Illuminate\Http\Response
  397. */
  398. public function show(Request $request, Article $article)
  399. {
  400. //
  401. if (!$article) {
  402. return $this->error("no recorder");
  403. }
  404. //判断权限
  405. $user = AuthService::current($request);
  406. if (!$user) {
  407. $user_uid = "";
  408. } else {
  409. $user_uid = $user['user_uid'];
  410. }
  411. $canRead = ArticleController::userCanRead($user_uid, $article);
  412. if (!$canRead) {
  413. return $this->error(__('auth.failed'), 403, 403);
  414. }
  415. return $this->ok(new ArticleResource($article));
  416. }
  417. /**
  418. * Display the specified resource.
  419. * @param \Illuminate\Http\Request $request
  420. * @param string $article
  421. * @return \Illuminate\Http\Response
  422. */
  423. public function preview(Request $request, string $articleId)
  424. {
  425. //
  426. $article = Article::find($articleId);
  427. if (!$article) {
  428. return $this->error("no recorder");
  429. }
  430. //判断权限
  431. $user = AuthService::current($request);
  432. if (!$user) {
  433. $user_uid = "";
  434. } else {
  435. $user_uid = $user['user_uid'];
  436. }
  437. $canRead = ArticleController::userCanRead($user_uid, $article);
  438. if (!$canRead) {
  439. return $this->error(__('auth.failed'), [], 401);
  440. }
  441. if ($request->has('content')) {
  442. $article->content = $request->input('content');
  443. return $this->ok(new ArticleResource($article));
  444. } else {
  445. return $this->error('no content', [], 200);
  446. }
  447. }
  448. /**
  449. * Update the specified resource in storage.
  450. *
  451. * @param \Illuminate\Http\Request $request
  452. * @param \App\Models\Article $article
  453. * @return \Illuminate\Http\Response
  454. */
  455. public function update(Request $request, Article $article)
  456. {
  457. //
  458. if (!$article) {
  459. return $this->error("no recorder");
  460. }
  461. //鉴权
  462. $user = AuthService::current($request);
  463. if (!$user) {
  464. return $this->error(__('auth.failed'), 401, 401);
  465. } else {
  466. $user_uid = $user['user_uid'];
  467. }
  468. $canEdit = ArticleController::userCanEdit($user_uid, $article);
  469. if (!$canEdit) {
  470. return $this->error(__('auth.failed'), 401, 401);
  471. }
  472. /*
  473. //查询标题是否重复
  474. if(Article::where('title',$request->input('title'))
  475. ->where('owner',$article->owner)
  476. ->where('uid',"<>",$article->uid)
  477. ->exists()){
  478. return $this->error(__('validation.exists'));
  479. }*/
  480. $content = $request->input('content');
  481. if ($request->input('to_tpl') === true) {
  482. /**
  483. * 转化为模版
  484. */
  485. $tplContent = $this->toTpl(
  486. $content,
  487. $request->input('anthology_id'),
  488. $user
  489. );
  490. $content = $tplContent;
  491. }
  492. $article->title = mb_substr($request->input('title'), 0, 128, 'UTF-8');
  493. $article->subtitle = mb_substr($request->input('subtitle'), 0, 128, 'UTF-8');
  494. $article->summary = mb_substr($request->input('summary'), 0, 1024, 'UTF-8');
  495. $article->content = $content;
  496. $article->lang = $request->input('lang');
  497. $article->status = $request->input('status', 10);
  498. $article->editor_id = $user['user_id'];
  499. $article->modify_time = time() * 1000;
  500. $article->save();
  501. OpsLog::debug($user_uid, $article);
  502. return $this->ok(new ArticleResource($article));
  503. }
  504. /**
  505. * Remove the specified resource from storage.
  506. * @param \Illuminate\Http\Request $request
  507. * @param \App\Models\Article $article
  508. * @return \Illuminate\Http\Response
  509. */
  510. public function destroy(Request $request, Article $article)
  511. {
  512. //
  513. $user = AuthService::current($request);
  514. if (!$user) {
  515. return $this->error(__('auth.failed'));
  516. }
  517. //判断当前用户是否有指定的studio的权限
  518. if ($user['user_uid'] !== $article->owner) {
  519. return $this->error(__('auth.failed'));
  520. }
  521. $delete = 0;
  522. DB::transaction(function () use ($article, $delete) {
  523. //TODO 删除文集中的文章
  524. $delete = $article->delete();
  525. ArticleMapController::deleteArticle($article->uid);
  526. });
  527. return $this->ok($delete);
  528. }
  529. public function toTpl($content, $anthologyId, $user)
  530. {
  531. //查询书号
  532. if (!Str::isUuid($anthologyId)) {
  533. throw new \Exception('anthology Id not uuid');
  534. }
  535. $bookId = $this->getBookId($anthologyId, $user);
  536. $tpl = $this->convertToTpl($content, $bookId['book'], $bookId['paragraph']);
  537. //保存原文到句子表
  538. $customBook = $this->getCustomBookByBookId($bookId['book']);
  539. $sentenceSave = new SentenceApi;
  540. $auth = $sentenceSave->auth($customBook->channel_id, $user['user_uid']);
  541. if (!$auth) {
  542. throw new \Exception('auth fail');
  543. }
  544. foreach ($tpl['sentences'] as $key => $sentence) {
  545. $sentenceSave->store($sentence, $user);
  546. }
  547. return $tpl['content'];
  548. }
  549. private function getCustomBookByBookId($bookId)
  550. {
  551. return CustomBook::where('book_id', $bookId)->first();
  552. }
  553. private function getBookId($anthologyId, $user)
  554. {
  555. $anthology = Collection::where('uid', $anthologyId)->first();
  556. if (!$anthology) {
  557. throw new \Exception('anthology not exists id=' . $anthologyId);
  558. }
  559. $bookId = $anthology->book_id;
  560. if (empty($bookId)) {
  561. //生成 book id
  562. $newBookId = CustomBook::max('book_id') + 1;
  563. $newBook = new CustomBook;
  564. $newBook->id = app('snowflake')->id();
  565. $newBook->book_id = $newBookId;
  566. $newBook->title = $anthology->title;
  567. $newBook->owner = $anthology->owner;
  568. $newBook->editor_id = $user['user_id'];
  569. $newBook->lang = $anthology->lang;
  570. $newBook->status = $anthology->status;
  571. //查询anthology所在的studio有没有符合要求的channel 没有的话,建立
  572. $channelId = ChannelApi::userBookGetOrCreate($anthology->owner, $anthology->lang, $anthology->status);
  573. if ($channelId === false) {
  574. throw new \Exception('user book get fail studio=' . $anthology->owner . ' language=' . $anthology->lang);
  575. }
  576. $newBook->channel_id = $channelId;
  577. $ok = $newBook->save();
  578. if (!$ok) {
  579. throw new \Exception('user book create fail studio=' . $anthology->owner . ' language=' . $anthology->lang);
  580. }
  581. CustomBookId::where('key', 'max_book_number')->update(['value' => $newBookId]);
  582. $bookId = $newBookId;
  583. $anthology->book_id = $newBookId;
  584. $anthology->save();
  585. } else {
  586. $channelId = CustomBook::where('book_id', $bookId)->value('channel_id');
  587. }
  588. $maxPara = Sentence::where('channel_uid', $channelId)
  589. ->where('book_id', $bookId)->max('paragraph');
  590. if (!$maxPara) {
  591. $maxPara = 0;
  592. }
  593. return ['book' => $bookId, 'paragraph' => $maxPara + 1];
  594. }
  595. public function convertToTpl($content, $bookId, $paraStart)
  596. {
  597. $newSentence = array();
  598. $para = $paraStart;
  599. $sentNum = 1;
  600. $newText = "";
  601. $isTable = false;
  602. $isList = false;
  603. $newSent = "";
  604. $sentences = explode("\n", $content);
  605. foreach ($sentences as $row) {
  606. //$data 为一行文本
  607. $listHead = "";
  608. $isList = false;
  609. $heading = false;
  610. $title = false;
  611. $trimData = trim($row);
  612. # 判断是否为list
  613. $listLeft = strstr($row, "- ", true);
  614. if ($listLeft !== FALSE) {
  615. if (ctype_space($listLeft) || empty($listLeft)) {
  616. # - 左侧是空,判定为list
  617. $isList = true;
  618. $iListPos = mb_strpos($row, '- ', 0, "UTF-8");
  619. $listHead = mb_substr($row, 0, $iListPos + 2, "UTF-8");
  620. $listBody = mb_substr($row, $iListPos + 2, mb_strlen($row, "UTF-8") - $iListPos + 2, "UTF-8");
  621. }
  622. }
  623. # TODO 判断是否为标题
  624. $headingStart = mb_strpos($row, "# ", 0, 'UTF-8');
  625. if ($headingStart !== false) {
  626. $headingLeft = mb_substr($row, 0, $headingStart + 2, 'UTF-8');
  627. $title = mb_substr($row, $headingStart + 2, null, 'UTF-8');
  628. if (str_replace('#', '', trim($headingLeft)) === '') {
  629. # 除了#没有其他东西,那么是标题
  630. $heading = $headingLeft;
  631. $newText .= $headingLeft;
  632. $newText .= '{{' . "{$bookId}-{$para}-{$sentNum}-{$sentNum}" . "}}\n";
  633. $newSentence[] = $this->newSent($bookId, $para, $sentNum, $sentNum, $title);
  634. $newSent = "";
  635. $para++;
  636. $sentNum = 1;
  637. continue;
  638. }
  639. }
  640. //判断是否为表格开始
  641. if (mb_substr($trimData, 0, 1, "UTF-8") == "|") {
  642. $isTable = true;
  643. }
  644. if ($trimData != "" && $isTable == true) {
  645. //如果是表格 不新增句子
  646. $newSent .= "{$row}\n";
  647. continue;
  648. }
  649. if ($isList == true) {
  650. $newSent .= $listBody;
  651. } else {
  652. $newSent .= $trimData;
  653. }
  654. #生成句子编号
  655. if ($trimData == "") {
  656. #空行
  657. if (strlen($newSent) > 0) {
  658. //之前有内容
  659. $newText .= '{{' . "{$bookId}-{$para}-{$sentNum}-{$sentNum}" . "}}\n";
  660. $newSentence[] = $this->newSent($bookId, $para, $sentNum, $sentNum, $newSent);
  661. $newSent = "";
  662. }
  663. #新的段落 不插入数据库
  664. $para++;
  665. $sentNum = 1;
  666. $newText .= "\n";
  667. $isTable = false; //表格开始标记
  668. $isList = false;
  669. continue;
  670. } else {
  671. $sentNum = $sentNum + 10;
  672. }
  673. if (mb_substr($trimData, 0, 2, "UTF-8") == "{{") {
  674. #已经有的句子链接不处理
  675. $newText .= $trimData . "\n";
  676. } else {
  677. $newText .= $listHead;
  678. $newText .= '{{' . "{$bookId}-{$para}-{$sentNum}-{$sentNum}" . "}}\n";
  679. $newSentence[] = $this->newSent($bookId, $para, $sentNum, $sentNum, $newSent);
  680. $newSent = "";
  681. }
  682. }
  683. return [
  684. 'content' => $newText,
  685. 'sentences' => $newSentence,
  686. ];
  687. }
  688. private function newSent($book, $para, $start, $end, $content)
  689. {
  690. return array(
  691. 'book_id' => $book,
  692. 'paragraph' => $para,
  693. 'word_start' => $start,
  694. 'word_end' => $end,
  695. 'content' => $content,
  696. );
  697. }
  698. }