ArticleController.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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\Http\Api\AuthApi;
  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 = AuthApi::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 = AuthApi::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 = AuthApi::current($request);
  269. if (!$user) {
  270. Log::error('未登录');
  271. return $this->error(__('auth.failed'), [], 401);
  272. } else {
  273. $user_uid = $user['user_uid'];
  274. }
  275. $canManage = ArticleController::userCanManage($user_uid, $request->input('studio'));
  276. if (!$canManage) {
  277. Log::error('userCanManage 失败');
  278. //判断是否有文集权限
  279. if ($request->has('anthologyId')) {
  280. $currPower = ShareApi::getResPower($user_uid, $request->input('anthologyId'));
  281. if ($currPower <= 10) {
  282. Log::error('没有文集编辑权限');
  283. return $this->error(__('auth.failed'), [], 403);
  284. }
  285. } else {
  286. Log::error('没有文集id');
  287. return $this->error(__('auth.failed'), [], 403);
  288. }
  289. }
  290. //权限判断结束
  291. //查询标题是否重复
  292. /*
  293. if(Article::where('title',$request->input('title'))->where('owner',$studioUuid)->exists()){
  294. return $this->error(__('validation.exists'));
  295. }*/
  296. Log::debug('开始新建' . $request->input('title'));
  297. $newArticle = new Article;
  298. DB::transaction(function () use ($user, $request, $newArticle) {
  299. $studioUuid = StudioApi::getIdByName($request->input('studio'));
  300. //新建文章,加入文集必须都成功。否则回滚
  301. $newArticle->id = app('snowflake')->id();
  302. $newArticle->uid = Str::uuid();
  303. $newArticle->title = mb_substr($request->input('title'), 0, 128, 'UTF-8');
  304. $newArticle->lang = $request->input('lang');
  305. if (!empty($request->input('status'))) {
  306. $newArticle->status = $request->input('status');
  307. }
  308. $newArticle->owner = $studioUuid;
  309. $newArticle->owner_id = $user['user_id'];
  310. $newArticle->editor_id = $user['user_id'];
  311. $newArticle->parent = $request->input('parentId');
  312. $newArticle->create_time = time() * 1000;
  313. $newArticle->modify_time = time() * 1000;
  314. $newArticle->save();
  315. OpsLog::debug($user['user_uid'], $newArticle);
  316. Log::debug('开始挂接 id=' . $newArticle->uid);
  317. $anthologyId = $request->input('anthologyId');
  318. if (Str::isUuid($anthologyId)) {
  319. $parentNode = $request->input('parentNode');
  320. if (Str::isUuid($parentNode)) {
  321. Log::debug('有挂接点' . $parentNode);
  322. $map = ArticleCollection::where('collect_id', $anthologyId)
  323. ->orderBy('id')->get();
  324. Log::debug('查询到原map数据' . count($map));
  325. $newMap = array();
  326. $parentNodeLevel = -1;
  327. $appended = false;
  328. foreach ($map as $key => $row) {
  329. $orgNode = $row;
  330. if (!$appended) {
  331. if ($parentNodeLevel > 0) {
  332. if ($row->level <= $parentNodeLevel) {
  333. //parent node 末尾
  334. $newNode = array();
  335. $newNode['collect_id'] = $anthologyId;
  336. $newNode['article_id'] = $newArticle->uid;
  337. $newNode['level'] = $parentNodeLevel + 1;
  338. $newNode['title'] = $newArticle->title;
  339. $newNode['children'] = 0;
  340. $newMap[] = $newNode;
  341. Log::debug('新增节点', ['node' => $newNode]);
  342. $appended = true;
  343. }
  344. } else {
  345. if ($row->article_id === $parentNode) {
  346. $parentNodeLevel = $row->level;
  347. $orgNode['children'] = $orgNode['children'] + 1;
  348. }
  349. }
  350. }
  351. $newMap[] = $orgNode;
  352. }
  353. if ($parentNodeLevel > 0) {
  354. if ($appended === false) {
  355. //
  356. Log::debug('没挂上 挂到结尾');
  357. $newNode = array();
  358. $newNode['collect_id'] = $anthologyId;
  359. $newNode['article_id'] = $newArticle->uid;
  360. $newNode['level'] = $parentNodeLevel + 1;
  361. $newNode['title'] = $newArticle->title;
  362. $newNode['children'] = 0;
  363. $newMap[] = $newNode;
  364. }
  365. } else {
  366. Log::error('没找到挂接点' . $parentNode);
  367. }
  368. Log::debug('新map数据' . count($newMap));
  369. $delete = ArticleCollection::where('collect_id', $anthologyId)->delete();
  370. Log::debug('删除旧map数据' . $delete);
  371. $count = 0;
  372. foreach ($newMap as $key => $row) {
  373. $new = new ArticleCollection;
  374. $new->id = app('snowflake')->id();
  375. $new->article_id = $row["article_id"];
  376. $new->collect_id = $row["collect_id"];
  377. $new->title = $row["title"];
  378. $new->level = $row["level"];
  379. $new->children = $row["children"];
  380. $new->editor_id = $user["user_id"];
  381. if (isset($row["deleted_at"])) {
  382. $new->deleted_at = $row["deleted_at"];
  383. }
  384. $new->save();
  385. $count++;
  386. }
  387. Log::debug('新map数据' . $count);
  388. ArticleMapController::updateCollection($anthologyId);
  389. } else {
  390. $articleMap = new ArticleCollection();
  391. $articleMap->id = app('snowflake')->id();
  392. $articleMap->article_id = $newArticle->uid;
  393. $articleMap->collect_id = $request->input('anthologyId');
  394. $articleMap->title = Article::find($newArticle->uid)->title;
  395. $articleMap->level = 1;
  396. $articleMap->save();
  397. }
  398. }
  399. });
  400. if (Str::isUuid($newArticle->uid)) {
  401. return $this->ok(new ArticleResource($newArticle));
  402. } else {
  403. return $this->error('fail');
  404. }
  405. }
  406. /**
  407. * Display the specified resource.
  408. * @param \Illuminate\Http\Request $request
  409. * @param \App\Models\Article $article
  410. * @return \Illuminate\Http\Response
  411. */
  412. public function show(Request $request, Article $article)
  413. {
  414. //
  415. if (!$article) {
  416. return $this->error("no recorder");
  417. }
  418. //判断权限
  419. $user = AuthApi::current($request);
  420. if (!$user) {
  421. $user_uid = "";
  422. } else {
  423. $user_uid = $user['user_uid'];
  424. }
  425. $canRead = ArticleController::userCanRead($user_uid, $article);
  426. if (!$canRead) {
  427. return $this->error(__('auth.failed'), 403, 403);
  428. }
  429. return $this->ok(new ArticleResource($article));
  430. }
  431. /**
  432. * Display the specified resource.
  433. * @param \Illuminate\Http\Request $request
  434. * @param string $article
  435. * @return \Illuminate\Http\Response
  436. */
  437. public function preview(Request $request, string $articleId)
  438. {
  439. //
  440. $article = Article::find($articleId);
  441. if (!$article) {
  442. return $this->error("no recorder");
  443. }
  444. //判断权限
  445. $user = AuthApi::current($request);
  446. if (!$user) {
  447. $user_uid = "";
  448. } else {
  449. $user_uid = $user['user_uid'];
  450. }
  451. $canRead = ArticleController::userCanRead($user_uid, $article);
  452. if (!$canRead) {
  453. return $this->error(__('auth.failed'), [], 401);
  454. }
  455. if ($request->has('content')) {
  456. $article->content = $request->input('content');
  457. return $this->ok(new ArticleResource($article));
  458. } else {
  459. return $this->error('no content', [], 200);
  460. }
  461. }
  462. /**
  463. * Update the specified resource in storage.
  464. *
  465. * @param \Illuminate\Http\Request $request
  466. * @param \App\Models\Article $article
  467. * @return \Illuminate\Http\Response
  468. */
  469. public function update(Request $request, Article $article)
  470. {
  471. //
  472. if (!$article) {
  473. return $this->error("no recorder");
  474. }
  475. //鉴权
  476. $user = AuthApi::current($request);
  477. if (!$user) {
  478. return $this->error(__('auth.failed'), 401, 401);
  479. } else {
  480. $user_uid = $user['user_uid'];
  481. }
  482. $canEdit = ArticleController::userCanEdit($user_uid, $article);
  483. if (!$canEdit) {
  484. return $this->error(__('auth.failed'), 401, 401);
  485. }
  486. /*
  487. //查询标题是否重复
  488. if(Article::where('title',$request->input('title'))
  489. ->where('owner',$article->owner)
  490. ->where('uid',"<>",$article->uid)
  491. ->exists()){
  492. return $this->error(__('validation.exists'));
  493. }*/
  494. $content = $request->input('content');
  495. if ($request->input('to_tpl') === true) {
  496. /**
  497. * 转化为模版
  498. */
  499. $tplContent = $this->toTpl(
  500. $content,
  501. $request->input('anthology_id'),
  502. $user
  503. );
  504. $content = $tplContent;
  505. }
  506. $article->title = mb_substr($request->input('title'), 0, 128, 'UTF-8');
  507. $article->subtitle = mb_substr($request->input('subtitle'), 0, 128, 'UTF-8');
  508. $article->summary = mb_substr($request->input('summary'), 0, 1024, 'UTF-8');
  509. $article->content = $content;
  510. $article->lang = $request->input('lang');
  511. $article->status = $request->input('status', 10);
  512. $article->editor_id = $user['user_id'];
  513. $article->modify_time = time() * 1000;
  514. $article->save();
  515. OpsLog::debug($user_uid, $article);
  516. return $this->ok(new ArticleResource($article));
  517. }
  518. /**
  519. * Remove the specified resource from storage.
  520. * @param \Illuminate\Http\Request $request
  521. * @param \App\Models\Article $article
  522. * @return \Illuminate\Http\Response
  523. */
  524. public function destroy(Request $request, Article $article)
  525. {
  526. //
  527. $user = AuthApi::current($request);
  528. if (!$user) {
  529. return $this->error(__('auth.failed'));
  530. }
  531. //判断当前用户是否有指定的studio的权限
  532. if ($user['user_uid'] !== $article->owner) {
  533. return $this->error(__('auth.failed'));
  534. }
  535. $delete = 0;
  536. DB::transaction(function () use ($article, $delete) {
  537. //TODO 删除文集中的文章
  538. $delete = $article->delete();
  539. ArticleMapController::deleteArticle($article->uid);
  540. });
  541. return $this->ok($delete);
  542. }
  543. public function toTpl($content, $anthologyId, $user)
  544. {
  545. //查询书号
  546. if (!Str::isUuid($anthologyId)) {
  547. throw new \Exception('anthology Id not uuid');
  548. }
  549. $bookId = $this->getBookId($anthologyId, $user);
  550. $tpl = $this->convertToTpl($content, $bookId['book'], $bookId['paragraph']);
  551. //保存原文到句子表
  552. $customBook = $this->getCustomBookByBookId($bookId['book']);
  553. $sentenceSave = new SentenceApi;
  554. $auth = $sentenceSave->auth($customBook->channel_id, $user['user_uid']);
  555. if (!$auth) {
  556. throw new \Exception('auth fail');
  557. }
  558. foreach ($tpl['sentences'] as $key => $sentence) {
  559. $sentenceSave->store($sentence, $user);
  560. }
  561. return $tpl['content'];
  562. }
  563. private function getCustomBookByBookId($bookId)
  564. {
  565. return CustomBook::where('book_id', $bookId)->first();
  566. }
  567. private function getBookId($anthologyId, $user)
  568. {
  569. $anthology = Collection::where('uid', $anthologyId)->first();
  570. if (!$anthology) {
  571. throw new \Exception('anthology not exists id=' . $anthologyId);
  572. }
  573. $bookId = $anthology->book_id;
  574. if (empty($bookId)) {
  575. //生成 book id
  576. $newBookId = CustomBook::max('book_id') + 1;
  577. $newBook = new CustomBook;
  578. $newBook->id = app('snowflake')->id();
  579. $newBook->book_id = $newBookId;
  580. $newBook->title = $anthology->title;
  581. $newBook->owner = $anthology->owner;
  582. $newBook->editor_id = $user['user_id'];
  583. $newBook->lang = $anthology->lang;
  584. $newBook->status = $anthology->status;
  585. //查询anthology所在的studio有没有符合要求的channel 没有的话,建立
  586. $channelId = ChannelApi::userBookGetOrCreate($anthology->owner, $anthology->lang, $anthology->status);
  587. if ($channelId === false) {
  588. throw new \Exception('user book get fail studio=' . $anthology->owner . ' language=' . $anthology->lang);
  589. }
  590. $newBook->channel_id = $channelId;
  591. $ok = $newBook->save();
  592. if (!$ok) {
  593. throw new \Exception('user book create fail studio=' . $anthology->owner . ' language=' . $anthology->lang);
  594. }
  595. CustomBookId::where('key', 'max_book_number')->update(['value' => $newBookId]);
  596. $bookId = $newBookId;
  597. $anthology->book_id = $newBookId;
  598. $anthology->save();
  599. } else {
  600. $channelId = CustomBook::where('book_id', $bookId)->value('channel_id');
  601. }
  602. $maxPara = Sentence::where('channel_uid', $channelId)
  603. ->where('book_id', $bookId)->max('paragraph');
  604. if (!$maxPara) {
  605. $maxPara = 0;
  606. }
  607. return ['book' => $bookId, 'paragraph' => $maxPara + 1];
  608. }
  609. public function convertToTpl($content, $bookId, $paraStart)
  610. {
  611. $newSentence = array();
  612. $para = $paraStart;
  613. $sentNum = 1;
  614. $newText = "";
  615. $isTable = false;
  616. $isList = false;
  617. $newSent = "";
  618. $sentences = explode("\n", $content);
  619. foreach ($sentences as $row) {
  620. //$data 为一行文本
  621. $listHead = "";
  622. $isList = false;
  623. $heading = false;
  624. $title = false;
  625. $trimData = trim($row);
  626. # 判断是否为list
  627. $listLeft = strstr($row, "- ", true);
  628. if ($listLeft !== FALSE) {
  629. if (ctype_space($listLeft) || empty($listLeft)) {
  630. # - 左侧是空,判定为list
  631. $isList = true;
  632. $iListPos = mb_strpos($row, '- ', 0, "UTF-8");
  633. $listHead = mb_substr($row, 0, $iListPos + 2, "UTF-8");
  634. $listBody = mb_substr($row, $iListPos + 2, mb_strlen($row, "UTF-8") - $iListPos + 2, "UTF-8");
  635. }
  636. }
  637. # TODO 判断是否为标题
  638. $headingStart = mb_strpos($row, "# ", 0, 'UTF-8');
  639. if ($headingStart !== false) {
  640. $headingLeft = mb_substr($row, 0, $headingStart + 2, 'UTF-8');
  641. $title = mb_substr($row, $headingStart + 2, null, 'UTF-8');
  642. if (str_replace('#', '', trim($headingLeft)) === '') {
  643. # 除了#没有其他东西,那么是标题
  644. $heading = $headingLeft;
  645. $newText .= $headingLeft;
  646. $newText .= '{{' . "{$bookId}-{$para}-{$sentNum}-{$sentNum}" . "}}\n";
  647. $newSentence[] = $this->newSent($bookId, $para, $sentNum, $sentNum, $title);
  648. $newSent = "";
  649. $para++;
  650. $sentNum = 1;
  651. continue;
  652. }
  653. }
  654. //判断是否为表格开始
  655. if (mb_substr($trimData, 0, 1, "UTF-8") == "|") {
  656. $isTable = true;
  657. }
  658. if ($trimData != "" && $isTable == true) {
  659. //如果是表格 不新增句子
  660. $newSent .= "{$row}\n";
  661. continue;
  662. }
  663. if ($isList == true) {
  664. $newSent .= $listBody;
  665. } else {
  666. $newSent .= $trimData;
  667. }
  668. #生成句子编号
  669. if ($trimData == "") {
  670. #空行
  671. if (strlen($newSent) > 0) {
  672. //之前有内容
  673. $newText .= '{{' . "{$bookId}-{$para}-{$sentNum}-{$sentNum}" . "}}\n";
  674. $newSentence[] = $this->newSent($bookId, $para, $sentNum, $sentNum, $newSent);
  675. $newSent = "";
  676. }
  677. #新的段落 不插入数据库
  678. $para++;
  679. $sentNum = 1;
  680. $newText .= "\n";
  681. $isTable = false; //表格开始标记
  682. $isList = false;
  683. continue;
  684. } else {
  685. $sentNum = $sentNum + 10;
  686. }
  687. if (mb_substr($trimData, 0, 2, "UTF-8") == "{{") {
  688. #已经有的句子链接不处理
  689. $newText .= $trimData . "\n";
  690. } else {
  691. $newText .= $listHead;
  692. $newText .= '{{' . "{$bookId}-{$para}-{$sentNum}-{$sentNum}" . "}}\n";
  693. $newSentence[] = $this->newSent($bookId, $para, $sentNum, $sentNum, $newSent);
  694. $newSent = "";
  695. }
  696. }
  697. return [
  698. 'content' => $newText,
  699. 'sentences' => $newSentence,
  700. ];
  701. }
  702. private function newSent($book, $para, $start, $end, $content)
  703. {
  704. return array(
  705. 'book_id' => $book,
  706. 'paragraph' => $para,
  707. 'word_start' => $start,
  708. 'word_end' => $end,
  709. 'content' => $content,
  710. );
  711. }
  712. }