ArticleController.php 26 KB

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